File tree Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -1522,8 +1522,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
1522
1522
g.write ('static ' )
1523
1523
}
1524
1524
mut return_type := table.void_type
1525
- op := if assign_stmt.op == .decl_assign { token.Kind.assign } else { assign_stmt.op }
1526
1525
is_decl := assign_stmt.op == .decl_assign
1526
+ op := if is_decl { token.Kind.assign } else { assign_stmt.op }
1527
1527
right_expr := assign_stmt.right[0 ]
1528
1528
match right_expr {
1529
1529
ast.CallExpr { return_type = right_expr.return_type }
@@ -1756,12 +1756,18 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
1756
1756
if blank_assign {
1757
1757
g.write ('{' )
1758
1758
}
1759
- ret_styp := g.typ (val.decl.return_type)
1760
- g.write ('$ret_styp (*$ident.name ) (' )
1761
- def_pos := g.definitions.len
1762
- g.fn_args (val.decl.params, val.decl.is_variadic)
1763
- g.definitions.go_back (g.definitions.len - def_pos)
1764
- g.write (') = ' )
1759
+ // if it's a decl assign (`:=`) or a blank assignment `_ =`/`_ :=` then generate `void (*ident) (args) =`
1760
+ if (is_decl || blank_assign) && left is ast.Ident {
1761
+ ret_styp := g.typ (val.decl.return_type)
1762
+ g.write ('$ret_styp (*$ident.name ) (' )
1763
+ def_pos := g.definitions.len
1764
+ g.fn_args (val.decl.params, val.decl.is_variadic)
1765
+ g.definitions.go_back (g.definitions.len - def_pos)
1766
+ g.write (') = ' )
1767
+ } else {
1768
+ g.expr (left)
1769
+ g.write (' = ' )
1770
+ }
1765
1771
g.expr (val)
1766
1772
g.writeln (';' )
1767
1773
if blank_assign {
Original file line number Diff line number Diff line change @@ -8,3 +8,16 @@ fn test_go_anon_fn() {
8
8
}(mut wg)
9
9
wg.wait ()
10
10
}
11
+
12
+ struct AnonFnWrapper {
13
+ mut :
14
+ fn_ fn () bool
15
+ }
16
+
17
+ fn test_anon_assign_struct () {
18
+ mut w := AnonFnWrapper{}
19
+ w.fn_ = fn () bool {
20
+ return true
21
+ }
22
+ assert w.fn_ ()
23
+ }
You can’t perform that action at this time.
0 commit comments