Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker, cgen: fix if define comptime checking #22946

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,15 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, pos token.Pos) ComptimeBr
should_record_ident = true
is_user_ident = true
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
}
}
Comment on lines +745 to +753
Copy link
Member

@spytheman spytheman Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 separate ifs, and early returns would have been simpler

} else {
c.error('invalid `\$if` condition', cond.pos)
}
Expand Down
9 changes: 7 additions & 2 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,17 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
return is_cond_true, false
}
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 {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
verror(err.str())
return false, true
}
g.write('defined(${ifdef})')
return true, false
if dname in g.pref.compile_defines_all && dname !in g.pref.compile_defines {
return false, true
} else {
return true, false
}
Comment on lines +522 to +524
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no point in the else

}
ast.InfixExpr {
match cond.op {
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/gen/c/testdata/if_define_check_not_set.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some_define was not passed

12 changes: 12 additions & 0 deletions vlib/v/gen/c/testdata/if_define_check_not_set.vv
Original file line number Diff line number Diff line change
@@ -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'))
}
2 changes: 2 additions & 0 deletions vlib/v/gen/c/testdata/if_define_check_set.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some_define was passed
true
12 changes: 12 additions & 0 deletions vlib/v/gen/c/testdata/if_define_check_set.vv
Original file line number Diff line number Diff line change
@@ -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'))
}
6 changes: 3 additions & 3 deletions vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
'-nofloat' {
res.nofloat = true
res.compile_defines_all << 'nofloat' // so that `$if nofloat? {` works
res.compile_defines << 'nofloat'
}
'-fast-math' {
res.fast_math = true
Expand Down Expand Up @@ -1228,11 +1229,10 @@ fn (mut prefs Preferences) parse_define(define string) {
prefs.compile_values[dname] = dvalue
prefs.compile_defines_all << dname
match dvalue {
'0' {}
'1' {
'' {}
else {
prefs.compile_defines << dname
}
else {}
}
}

Expand Down
Loading