From dc9997f58c211a9816b4a0bd1ff37d5853b993ef Mon Sep 17 00:00:00 2001 From: Taegon Kim Date: Tue, 8 Nov 2022 15:05:48 +0900 Subject: [PATCH] fmt: fix - `v fmt` transforms compile time options in some cases (#16351) --- vlib/v/checker/constants/constants.v | 10 ++++++++++ vlib/v/fmt/fmt.v | 5 +++++ .../conditional_compilation_keep_in_module.vv | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 vlib/v/checker/constants/constants.v create mode 100644 vlib/v/fmt/tests/conditional_compilation_keep_in_module.vv diff --git a/vlib/v/checker/constants/constants.v b/vlib/v/checker/constants/constants.v new file mode 100644 index 0000000000..ed01069fd3 --- /dev/null +++ b/vlib/v/checker/constants/constants.v @@ -0,0 +1,10 @@ +module constants + +// TODO: move all constants from `checker` to here, and replace the hardcoded one with the computed one +pub const valid_comptime_not_user_defined = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', + 'gnu', 'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'termux', + 'solaris', 'haiku', 'serenity', 'vinix', 'gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus', + 'amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32', 'x64', 'x32', 'little_endian', + 'big_endian', 'apk', 'js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking', + 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding', 'interpreter', 'es5', + 'profile', 'wasm32_emscripten'] diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 9666f91557..8f0867446b 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -7,6 +7,7 @@ import strings import v.ast import v.util import v.pref +import v.checker.constants const ( bs = '\\' @@ -1936,6 +1937,10 @@ pub fn (mut f Fmt) enum_val(node ast.EnumVal) { pub fn (mut f Fmt) ident(node ast.Ident) { if node.info is ast.IdentVar { + if node.comptime && node.name in constants.valid_comptime_not_user_defined { + f.write(node.name) + return + } if node.info.is_mut { f.write(node.info.share.str() + ' ') } diff --git a/vlib/v/fmt/tests/conditional_compilation_keep_in_module.vv b/vlib/v/fmt/tests/conditional_compilation_keep_in_module.vv new file mode 100644 index 0000000000..1431465bdf --- /dev/null +++ b/vlib/v/fmt/tests/conditional_compilation_keep_in_module.vv @@ -0,0 +1,15 @@ +module log + +const ( + debug = 'debug' + prod = 'prod' +) + +fn log() { + $if debug { + println('debug') + } + $if !prod { + println('prod') + } +}