checker, cgen: fix if define comptime checking (fix #22906) (#22946)

This commit is contained in:
Felipe Pena 2024-11-24 08:32:28 -03:00 committed by GitHub
parent 1345eac570
commit c1df71abcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 6 deletions

View File

@ -742,7 +742,15 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, pos token.Pos) ComptimeBr
should_record_ident = true should_record_ident = true
is_user_ident = true is_user_ident = true
ident_name = cond.expr.name ident_name = cond.expr.name
return if cond.expr.name in c.pref.compile_defines_all { .eval } else { .skip } return if cond.expr.name in c.pref.compile_defines {
.eval
} else {
if cond.expr.name in c.pref.compile_defines_all {
ComptimeBranchSkipState.unknown
} else {
ComptimeBranchSkipState.skip
}
}
} else { } else {
c.error('invalid `\$if` condition', cond.pos) c.error('invalid `\$if` condition', cond.pos)
} }

View File

@ -511,13 +511,18 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
return is_cond_true, false return is_cond_true, false
} }
ast.PostfixExpr { ast.PostfixExpr {
ifdef := g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true) or { dname := (cond.expr as ast.Ident).name
ifdef := g.comptime_if_to_ifdef(dname, true) or {
verror(err.str()) verror(err.str())
return false, true return false, true
} }
g.write('defined(${ifdef})') g.write('defined(${ifdef})')
if dname in g.pref.compile_defines_all && dname !in g.pref.compile_defines {
return false, true
} else {
return true, false return true, false
} }
}
ast.InfixExpr { ast.InfixExpr {
match cond.op { match cond.op {
.and, .logical_or { .and, .logical_or {

View File

@ -0,0 +1,2 @@
some_define was not passed

View File

@ -0,0 +1,12 @@
module main
// vtest vflags: -d some_define=
fn main() {
$if some_define ? {
println('some_define was passed')
} $else {
println('some_define was not passed')
}
println($d('some_define', 'unknown'))
}

View File

@ -0,0 +1,2 @@
some_define was passed
true

View File

@ -0,0 +1,12 @@
module main
// vtest vflags: -d some_define
fn main() {
$if some_define ? {
println('some_define was passed')
} $else {
println('some_define was not passed')
}
println($d('some_define', 'unknown'))
}

View File

@ -438,6 +438,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
'-nofloat' { '-nofloat' {
res.nofloat = true res.nofloat = true
res.compile_defines_all << 'nofloat' // so that `$if nofloat? {` works res.compile_defines_all << 'nofloat' // so that `$if nofloat? {` works
res.compile_defines << 'nofloat'
} }
'-fast-math' { '-fast-math' {
res.fast_math = true res.fast_math = true
@ -1228,11 +1229,10 @@ fn (mut prefs Preferences) parse_define(define string) {
prefs.compile_values[dname] = dvalue prefs.compile_values[dname] = dvalue
prefs.compile_defines_all << dname prefs.compile_defines_all << dname
match dvalue { match dvalue {
'0' {} '' {}
'1' { else {
prefs.compile_defines << dname prefs.compile_defines << dname
} }
else {}
} }
} }