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:
parent
80cd9f820a
commit
71e8fc8b38
@ -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 {`
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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 | }
|
@ -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))
|
||||
}
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user