builtin, checker, cgen: expose is_embed in FieldData (#25232)

This commit is contained in:
Larsimusrex 2025-09-04 10:39:24 +02:00 committed by GitHub
parent f6b60e4d9f
commit 682db66852
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 16 additions and 7 deletions

View file

@ -127,6 +127,7 @@ pub:
attrs []string // the attributes of the field f
is_pub bool // f is in a `pub:` section
is_mut bool // f is in a `mut:` section
is_embed bool // f is a embedded struct
is_shared bool // `f shared Abc`
is_atomic bool // `f atomic int` , TODO

View file

@ -1398,7 +1398,7 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, mut sb strings.Builder) (
}
ast.SelectorExpr {
if c.comptime.comptime_for_field_var != '' && cond.expr is ast.Ident {
if (cond.expr as ast.Ident).name == c.comptime.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] {
if (cond.expr as ast.Ident).name == c.comptime.comptime_for_field_var && cond.field_name in ['is_mut', 'is_pub', 'is_embed', 'is_shared', 'is_atomic', 'is_option', 'is_array', 'is_map', 'is_chan', 'is_struct', 'is_alias', 'is_enum'] {
is_true = c.type_resolver.get_comptime_selector_bool_field(cond.field_name)
sb.write_string('${is_true}')
return is_true, true

View file

@ -740,6 +740,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
g.writeln('\t${node.val_var}.unaliased_typ = ${int(unaliased_styp.idx())};\t// ${g.table.type_to_str(unaliased_styp)}')
g.writeln('\t${node.val_var}.is_pub = ${field.is_pub};')
g.writeln('\t${node.val_var}.is_mut = ${field.is_mut};')
g.writeln('\t${node.val_var}.is_embed = ${field.is_embed};')
g.writeln('\t${node.val_var}.is_shared = ${field.typ.has_flag(.shared_f)};')
g.writeln('\t${node.val_var}.is_atomic = ${field.typ.has_flag(.atomic_f)};')

View file

@ -1,4 +1,5 @@
struct App {
Inner
a string
b string
mut:
@ -12,6 +13,8 @@ pub mut:
h u8
}
struct Inner {}
@['foo/bar/three']
fn (mut app App) run() {
}
@ -85,13 +88,16 @@ fn test_comptime_for_fields() {
assert field.name in ['d', 'e']
}
if field.is_mut {
assert field.name in ['c', 'd', 'g', 'h']
assert field.name in ['c', 'd', 'g', 'h', 'Inner']
}
if field.is_pub {
assert field.name in ['e', 'f', 'g', 'h']
assert field.name in ['e', 'f', 'g', 'h', 'Inner']
}
if field.is_pub && field.is_mut {
assert field.name in ['g', 'h']
assert field.name in ['g', 'h', 'Inner']
}
if field.is_embed {
assert field.name == 'Inner'
}
if field.name == 'f' {
assert sizeof(field) == 8

View file

@ -224,6 +224,7 @@ pub fn (mut t TypeResolver) get_comptime_selector_bool_field(field_name string)
match field_name {
'is_pub' { return field.is_pub }
'is_mut' { return field.is_mut }
'is_embed' { return field.is_embed }
'is_shared' { return field_typ.has_flag(.shared_f) }
'is_atomic' { return field_typ.has_flag(.atomic_f) }
'is_option' { return field.typ.has_flag(.option) }