From 1f416d9bb7259c000e946fef0e34ecd72c9474e4 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Tue, 28 Oct 2025 02:00:46 +0800 Subject: [PATCH] all: add comptime if is shared support (fix #25600) (#25602) --- vlib/v/ast/ast.v | 2 ++ vlib/v/fmt/fmt.v | 1 + vlib/v/gen/golang/golang.v | 1 + vlib/v/parser/comptime.v | 6 +++++- vlib/v/parser/expr.v | 2 +- .../comptime/comptime_shared_field_test.v | 18 ++++++++++++++++++ vlib/v/type_resolver/comptime_resolver.v | 3 +++ 7 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/comptime/comptime_shared_field_test.v diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 711bea46b..74fedfe6b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -163,6 +163,7 @@ pub enum ComptimeTypeKind { alias function option + shared string pointer voidptr @@ -190,6 +191,7 @@ pub fn (cty ComptimeType) str() string { .alias { '\$alias' } .function { '\$function' } .option { '\$option' } + .shared { '\$shared' } .string { '\$string' } .pointer { '\$pointer' } .voidptr { '\$voidptr' } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 25e76af49..7acc9c99e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -770,6 +770,7 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) { .alias { f.write('\$alias') } .function { f.write('\$function') } .option { f.write('\$option') } + .shared { f.write('\$shared') } .string { f.write('\$string') } .pointer { f.write('\$pointer') } .voidptr { f.write('\$voidptr') } diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index 2573b04ec..21558b1a1 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -676,6 +676,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) { .alias { f.write('\$alias') } .function { f.write('\$function') } .option { f.write('\$option') } + .shared { f.write('\$shared') } .string { f.write('\$string') } .pointer { f.write('\$pointer') } .voidptr { f.write('\$voidptr') } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 51f74bbc3..e8589520c 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -10,7 +10,8 @@ import v.token const supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error', 'compile_warn', 'd', 'res'] const comptime_types = ['map', 'array', 'array_dynamic', 'array_fixed', 'int', 'float', 'struct', - 'interface', 'enum', 'sumtype', 'alias', 'function', 'option', 'string', 'pointer', 'voidptr'] + 'interface', 'enum', 'sumtype', 'alias', 'function', 'option', 'shared', 'string', 'pointer', + 'voidptr'] fn (mut p Parser) parse_comptime_type() ast.ComptimeType { pos := p.tok.pos() @@ -60,6 +61,9 @@ fn (mut p Parser) parse_comptime_type() ast.ComptimeType { 'option' { .option } + 'shared' { + .shared + } 'string' { .string } diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index a82aacdd3..197403e52 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -117,7 +117,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { } .dollar { match p.peek_tok.kind { - .name, .key_struct, .key_enum, .key_interface { + .name, .key_struct, .key_enum, .key_interface, .key_shared { if p.peek_tok.lit in comptime_types { node = p.parse_comptime_type() } else { diff --git a/vlib/v/tests/comptime/comptime_shared_field_test.v b/vlib/v/tests/comptime/comptime_shared_field_test.v new file mode 100644 index 000000000..2cc85b297 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_shared_field_test.v @@ -0,0 +1,18 @@ +struct Test { + a shared int + b shared f64 + c shared string + d int + e f64 + f string +} + +fn test_shared_comptime() { + mut shares := []string{} + $for f in Test.fields { + $if f.typ is $shared { + shares << f.name + } + } + assert shares == ['a', 'b', 'c'] +} diff --git a/vlib/v/type_resolver/comptime_resolver.v b/vlib/v/type_resolver/comptime_resolver.v index c33cff536..2e1cd246f 100644 --- a/vlib/v/type_resolver/comptime_resolver.v +++ b/vlib/v/type_resolver/comptime_resolver.v @@ -294,6 +294,9 @@ pub fn (t &TypeResolver) is_comptime_type(x ast.Type, y ast.ComptimeType) bool { .option { return x.has_flag(.option) } + .shared { + return x.has_flag(.shared_f) + } } } -- 2.39.5