Skip to content

Commit 0afb41f

Browse files
authored
checker: fix missing type mismatch with ptr types (#17695)
1 parent a552a79 commit 0afb41f

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

vlib/v/checker/assign.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import v.pref
77

88
// TODO 600 line function
99
fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
10+
prev_inside_assign := c.inside_assign
11+
c.inside_assign = true
1012
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
1113
defer {
1214
c.expected_type = ast.void_type
15+
c.inside_assign = prev_inside_assign
1316
}
1417
is_decl := node.op == .decl_assign
1518
right_first := node.right[0]

vlib/v/checker/checker.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ mut:
113113
inside_println_arg bool
114114
inside_decl_rhs bool
115115
inside_if_guard bool // true inside the guard condition of `if x := opt() {}`
116+
inside_assign bool
116117
is_index_assign bool
117118
comptime_call_pos int // needed for correctly checking use before decl for templates
118119
goto_labels map[string]ast.GotoLabel // to check for unused goto labels

vlib/v/checker/if.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
319319
}
320320
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
321321
node.pos)
322+
} else {
323+
if c.inside_assign && node.is_expr && !node.typ.has_flag(.shared_f)
324+
&& stmt.typ.is_ptr() != node.typ.is_ptr()
325+
&& stmt.typ != ast.voidptr_type {
326+
c.error('mismatched types `${c.table.type_to_str(node.typ)}` and `${c.table.type_to_str(stmt.typ)}`',
327+
node.pos)
328+
}
322329
}
323330
} else if !node.is_comptime {
324331
c.error('`${if_kind}` expression requires an expression as the last statement of every branch',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/if_diff_expected_type_err.vv:7:11: error: mismatched types `&[]rune` and `[]rune`
2+
5 |
3+
6 | fn main() {
4+
7 | runes := if true { &some_runes } else { some_other_runes }
5+
| ~~
6+
8 | println(runes)
7+
9 | }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const (
2+
some_runes = [`a`, `b`, `c`]
3+
some_other_runes = [`c`, `b`, `a`]
4+
)
5+
6+
fn main() {
7+
runes := if true { &some_runes } else { some_other_runes }
8+
println(runes)
9+
}

0 commit comments

Comments
 (0)