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: fix comptime if branch checking #16938

Merged
merged 15 commits into from
Jan 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
} else if skip_state == .skip {
c.skip_flags = true
skip_state = .unknown // Reset the value of `skip_state` for the next branch
} else if !is_comptime_type_is_expr && skip_state == .eval {
} else if skip_state == .eval {
found_branch = true // If a branch wasn't skipped, the rest must be
c.skip_flags = skip_state == .skip
}
if c.fn_level == 0 && c.pref.output_cross_c {
// do not skip any of the branches for top level `$if OS {`
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ fn has_top_return(stmts []ast.Stmt) bool {
}
ast.ExprStmt {
if stmt.expr is ast.CallExpr {
if stmt.expr.is_noreturn {
// do not ignore panic() calls on non checked stmts
if stmt.expr.is_noreturn
|| (stmt.expr.is_method == false && stmt.expr.name == 'panic') {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/comptime_branching_working_with_a_custom_compile_error.vv:8:3: error: 11
6 | return Value(val)
7 | } $else {
8 | $compile_error('11')
| ~~~~~~~~~~~~~~~~~~~~
9 | println('not bool ${val}')
10 | }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Value = bool | voidptr

pub fn create[T](val T) Value {
$if T is bool {
println('bool ${val}')
return Value(val)
} $else {
$compile_error('11')
println('not bool ${val}')
}
return Value(voidptr(123))
}

fn main() {
println(create(123))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Value = bool | voidptr

pub fn create[T](val T) Value {
$if T is bool {
println('bool ${val}')
return Value(val)
} $else {
$compile_error('11')
println('not bool ${val}')
}
return Value(voidptr(123))
}

fn test_calling_generic_function_that_has_inside_a_comptime_compile_error_directive() {
assert create(true) == Value(true)
}