1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix comptime if branch checking (#16938)

This commit is contained in:
Felipe Pena 2023-01-14 10:06:38 -03:00 committed by GitHub
parent 80cd9f820a
commit 71e8fc8b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 2 deletions

View File

@ -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 {`

View File

@ -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') {
return true
}
}

View File

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

View File

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

View File

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