File tree Expand file tree Collapse file tree 5 files changed +27
-0
lines changed Expand file tree Collapse file tree 5 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -7,9 +7,12 @@ import v.pref
7
7
8
8
// TODO 600 line function
9
9
fn (mut c Checker) assign_stmt (mut node ast.AssignStmt) {
10
+ prev_inside_assign := c.inside_assign
11
+ c.inside_assign = true
10
12
c.expected_type = ast.none_type // TODO a hack to make `x := if ... work`
11
13
defer {
12
14
c.expected_type = ast.void_type
15
+ c.inside_assign = prev_inside_assign
13
16
}
14
17
is_decl := node.op == .decl_assign
15
18
right_first := node.right[0 ]
Original file line number Diff line number Diff line change 113
113
inside_println_arg bool
114
114
inside_decl_rhs bool
115
115
inside_if_guard bool // true inside the guard condition of `if x := opt() {}`
116
+ inside_assign bool
116
117
is_index_assign bool
117
118
comptime_call_pos int // needed for correctly checking use before decl for templates
118
119
goto_labels map [string ]ast.GotoLabel // to check for unused goto labels
Original file line number Diff line number Diff line change @@ -319,6 +319,13 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
319
319
}
320
320
c.error ('mismatched types `${c.table.type_to_str(node.typ)} ` and `${c.table.type_to_str(stmt.typ)} `' ,
321
321
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
+ }
322
329
}
323
330
} else if ! node.is_comptime {
324
331
c.error ('`${if_kind} ` expression requires an expression as the last statement of every branch' ,
Original file line number Diff line number Diff line change
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 | }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments