File tree Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
204
204
}
205
205
}
206
206
if node.is_expr && c.table.sym (former_expected_type).kind == .sum_type {
207
+ node.typ = former_expected_type
207
208
continue
208
209
}
209
210
if is_noreturn_callexpr (last_expr.expr) {
Original file line number Diff line number Diff line change @@ -74,7 +74,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
74
74
g.expr (branch.cond)
75
75
g.write (' ? ' )
76
76
}
77
+ prev_expected_cast_type := g.expected_cast_type
78
+ if node.is_expr && g.table.sym (node.typ).kind == .sum_type {
79
+ g.expected_cast_type = node.typ
80
+ }
77
81
g.stmts (branch.stmts)
82
+ g.expected_cast_type = prev_expected_cast_type
78
83
}
79
84
if node.branches.len == 1 {
80
85
g.write (': 0' )
@@ -195,11 +200,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
195
200
}
196
201
}
197
202
if needs_tmp_var {
203
+ prev_expected_cast_type := g.expected_cast_type
198
204
if node.is_expr && g.table.sym (node.typ).kind == .sum_type {
199
205
g.expected_cast_type = node.typ
200
206
}
201
207
g.stmts_with_tmp_var (branch.stmts, tmp)
202
- g.expected_cast_type = 0
208
+ g.expected_cast_type = prev_expected_cast_type
203
209
} else {
204
210
// restore if_expr stmt header pos
205
211
stmt_pos := g.nth_stmt_pos (0 )
Original file line number Diff line number Diff line change
1
+ type Opt< T> = None< T> | Some< T>
2
+
3
+ struct None < T> {}
4
+
5
+ struct Some < T> {
6
+ mut :
7
+ value T
8
+ }
9
+
10
+ fn operation (r int ) Opt< int > {
11
+ return if r > 0 { Some< int > {r} } else { None< int > {} }
12
+ }
13
+
14
+ fn test_if_expr_with_generic_sumtype () {
15
+ op := operation (1 )
16
+ assert Opt <int >(Some< int > {1 }) == op
17
+ }
You can’t perform that action at this time.
0 commit comments