| author | |
| committer | |
| log | 131c133bb74eab2012158c8ecfaa78944db197c7 |
| tree | d02e9e5c9725ca18204d4e9658ad8069287e3c06 |
| parent | 02c1b9df3bcf2e0324adf02fd6c0ed56fe58c6d3 |
When deciding wether we should inline a scope, look up the parents until we get to a function definition scope
3 files changed, 20 insertions(+), 0 deletions(-)
src/ir.cpp+2| ... | @@ -145,6 +145,8 @@ static bool ir_should_inline(IrExecutable *exec, Scope *scope) { | ... | @@ -145,6 +145,8 @@ static bool ir_should_inline(IrExecutable *exec, Scope *scope) { |
| 145 | while (scope != nullptr) { | 145 | while (scope != nullptr) { |
| 146 | if (scope->id == ScopeIdCompTime) | 146 | if (scope->id == ScopeIdCompTime) |
| 147 | return true; | 147 | return true; |
| 148 | if (scope->id == ScopeIdFnDef) | ||
| 149 | break; | ||
| 148 | scope = scope->parent; | 150 | scope = scope->parent; |
| 149 | } | 151 | } |
| 150 | return false; | 152 | return false; |
test/behavior.zig+1| ... | @@ -52,4 +52,5 @@ comptime { | ... | @@ -52,4 +52,5 @@ comptime { |
| 52 | _ = @import("cases/var_args.zig"); | 52 | _ = @import("cases/var_args.zig"); |
| 53 | _ = @import("cases/void.zig"); | 53 | _ = @import("cases/void.zig"); |
| 54 | _ = @import("cases/while.zig"); | 54 | _ = @import("cases/while.zig"); |
| 55 | _ = @import("cases/fn_in_struct_in_comptime.zig"); | ||
| 55 | } | 56 | } |
test/cases/fn_in_struct_in_comptime.zig created+17| ... | @@ -0,0 +1,17 @@ | ||
| 1 | const assert = @import("std").debug.assert; | ||
| 2 | |||
| 3 | fn get_foo() fn(&u8)usize { | ||
| 4 | comptime { | ||
| 5 | return struct { | ||
| 6 | fn func(ptr: &u8) usize { | ||
| 7 | var u = @ptrToInt(ptr); | ||
| 8 | return u; | ||
| 9 | } | ||
| 10 | }.func; | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | test "define a function in an anonymous struct in comptime" { | ||
| 15 | const foo = get_foo(); | ||
| 16 | assert(foo(@intToPtr(&u8, 12345)) == 12345); | ||
| 17 | } | ||