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 @@ -420,7 +420,11 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
420
420
if node.typ == ast.void_type {
421
421
// first branch of if expression
422
422
node.is_expr = true
423
- node.typ = stmt.typ
423
+ if stmt.expr.is_auto_deref_var () {
424
+ node.typ = stmt.typ.deref ()
425
+ } else {
426
+ node.typ = stmt.typ
427
+ }
424
428
continue
425
429
} else if node.typ in [ast.float_literal_type, ast.int_literal_type] {
426
430
if node.typ == ast.int_literal_type {
Original file line number Diff line number Diff line change @@ -2186,6 +2186,9 @@ fn (mut g Gen) stmt(node ast.Stmt) {
2186
2186
// }
2187
2187
old_is_void_expr_stmt := g.is_void_expr_stmt
2188
2188
g.is_void_expr_stmt = ! node.is_expr
2189
+ if node.expr.is_auto_deref_var () {
2190
+ g.write ('*' )
2191
+ }
2189
2192
if node.typ != ast.void_type && g.expected_cast_type != 0
2190
2193
&& node.expr ! in [ast.IfExpr, ast.MatchExpr] {
2191
2194
g.expr_with_cast (node.expr, node.typ, g.expected_cast_type)
Original file line number Diff line number Diff line change
1
+ struct Foo {
2
+ str string
3
+ }
4
+
5
+ fn (mut f Foo) foo (f2 Foo) string {
6
+ return (if f.str != '' {
7
+ f
8
+ } else {
9
+ f2
10
+ }).str
11
+ }
12
+
13
+ fn test_deref_mut_var_in_if_expr () {
14
+ mut foo := Foo{}
15
+ dump (foo.foo (Foo{ str: 'a' }))
16
+ assert foo.foo (Foo{ str: 'a' }) == 'a'
17
+ }
You can’t perform that action at this time.
0 commit comments