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

checker: fix missing type mismatch with ptr types (#17695)

This commit is contained in:
Felipe Pena 2023-03-22 19:49:02 -03:00 committed by GitHub
parent a552a79ca8
commit 0afb41f7e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 0 deletions

View File

@ -7,9 +7,12 @@ import v.pref
// TODO 600 line function
fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
prev_inside_assign := c.inside_assign
c.inside_assign = true
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
defer {
c.expected_type = ast.void_type
c.inside_assign = prev_inside_assign
}
is_decl := node.op == .decl_assign
right_first := node.right[0]

View File

@ -113,6 +113,7 @@ mut:
inside_println_arg bool
inside_decl_rhs bool
inside_if_guard bool // true inside the guard condition of `if x := opt() {}`
inside_assign bool
is_index_assign bool
comptime_call_pos int // needed for correctly checking use before decl for templates
goto_labels map[string]ast.GotoLabel // to check for unused goto labels

View File

@ -319,6 +319,13 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
}
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
} else {
if c.inside_assign && node.is_expr && !node.typ.has_flag(.shared_f)
&& stmt.typ.is_ptr() != node.typ.is_ptr()
&& stmt.typ != ast.voidptr_type {
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
node.pos)
}
}
} else if !node.is_comptime {
c.error('`${if_kind}` expression requires an expression as the last statement of every branch',

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/if_diff_expected_type_err.vv:7:11: error: mismatched types `&[]rune` and `[]rune`
5 |
6 | fn main() {
7 | runes := if true { &some_runes } else { some_other_runes }
| ~~
8 | println(runes)
9 | }

View File

@ -0,0 +1,9 @@
const (
some_runes = [`a`, `b`, `c`]
some_other_runes = [`c`, `b`, `a`]
)
fn main() {
runes := if true { &some_runes } else { some_other_runes }
println(runes)
}