Compare commits

...

6 commits

Author SHA1 Message Date
Delyan Angelov
dbd5b5f56c
Revert "cgen: ensure variable names do not conflict with builtin methods (fix #25063) (#25178)"
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
This reverts commit f17e0fd52b.
2025-09-04 13:47:53 +03:00
kbkpbot
e15d8fcf49
checker: comptime match only eval true branch (fix #25223) (#25225) 2025-09-04 13:33:39 +03:00
CreeperFace
f17e0fd52b
cgen: ensure variable names do not conflict with builtin methods (fix #25063) (#25178) 2025-09-04 13:04:57 +03:00
Swastik Baranwal
dabc08b6ee
cgen: fix alias enum used in comptime $for (fix #25211) (#25212) 2025-09-04 12:57:25 +03:00
Delyan Angelov
8868696017
repl: fix handling of lines with comments like math.pi // comment (fix #25229) 2025-09-04 12:04:33 +03:00
Larsimusrex
682db66852
builtin, checker, cgen: expose is_embed in FieldData (#25232) 2025-09-04 11:39:24 +03:00
10 changed files with 72 additions and 13 deletions

View file

@ -441,7 +441,7 @@ fn run_repl(workdir string, vrepl_prefix string) int {
prompt = '... '
}
oline := r.get_one_line(prompt) or { break }
line := oline.trim_space()
line := oline.all_before('//').trim_space()
if line == '' {
continue
}

View file

@ -124,9 +124,10 @@ pub:
typ int // the internal TypeID of the field f,
unaliased_typ int // if f's type was an alias of int, this will be TypeID(int)
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
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

@ -221,10 +221,13 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
}
}
}
if node.is_expr {
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
} else {
c.stmts(mut branch.stmts)
if !node.is_comptime || (node.is_comptime && comptime_match_branch_result) {
if node.is_expr {
c.stmts_ending_with_expression(mut branch.stmts, c.expected_or_type)
} else {
c.stmts(mut branch.stmts)
}
}
c.smartcast_mut_pos = token.Pos{}
c.smartcast_cond_pos = token.Pos{}

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)};')
@ -780,7 +781,12 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
if g.pref.translated && node.typ.is_number() {
g.writeln('_const_main__${val};')
} else {
g.writeln('${g.styp(node.typ)}__${val};')
node_sym := g.table.sym(node.typ)
if node_sym.info is ast.Alias {
g.writeln('${g.styp(node_sym.info.parent_type)}__${val};')
} else {
g.writeln('${g.styp(node.typ)}__${val};')
}
}
enum_attrs := sym.info.attrs[val]
if enum_attrs.len == 0 {

View file

@ -0,0 +1,5 @@
math.pi
math.pi // some comment
===output===
3.141592653589793
3.141592653589793

View file

@ -5,6 +5,8 @@ enum CharacterGroup {
special
}
type AnotherCharGroup = CharacterGroup
fn (self CharacterGroup) value() string {
return match self {
.chars { 'first' }
@ -33,3 +35,21 @@ fn test_main() {
assert values == [CharacterGroup.chars, CharacterGroup.alphanumerics, CharacterGroup.numeric,
CharacterGroup.special]
}
fn test_alias_enum() {
mut values := []EnumData{}
$for entry in AnotherCharGroup.values {
values << entry
}
assert values[0].value == int(CharacterGroup.chars)
assert values[0].name == CharacterGroup.chars.str()
assert values[1].value == int(CharacterGroup.alphanumerics)
assert values[1].name == CharacterGroup.alphanumerics.str()
assert values[2].value == int(CharacterGroup.numeric)
assert values[2].name == CharacterGroup.numeric.str()
assert values[3].value == int(CharacterGroup.special)
assert values[3].name == CharacterGroup.special.str()
}

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

@ -0,0 +1,17 @@
module main
fn func[T]() bool {
$match T {
u8, u16 {
return true
}
$else {
// return false
$compile_error('fail')
}
}
}
fn test_comptime_match_eval_only_true_branch() {
assert func[u8]()
}

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) }