Skip to content

Commit

Permalink
Fixed inlining determination test (#972)
Browse files Browse the repository at this point in the history
When deciding wether we should inline a scope, look up the parents until we get to a function definition scope
  • Loading branch information
alexnask authored and andrewrk committed May 3, 2018
1 parent 02c1b9d commit 131c133
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ir.cpp
Expand Up @@ -145,6 +145,8 @@ static bool ir_should_inline(IrExecutable *exec, Scope *scope) {
while (scope != nullptr) {
if (scope->id == ScopeIdCompTime)
return true;
if (scope->id == ScopeIdFnDef)
break;
scope = scope->parent;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions test/behavior.zig
Expand Up @@ -52,4 +52,5 @@ comptime {
_ = @import("cases/var_args.zig");
_ = @import("cases/void.zig");
_ = @import("cases/while.zig");
_ = @import("cases/fn_in_struct_in_comptime.zig");
}
17 changes: 17 additions & 0 deletions test/cases/fn_in_struct_in_comptime.zig
@@ -0,0 +1,17 @@
const assert = @import("std").debug.assert;

fn get_foo() fn(&u8)usize {
comptime {
return struct {
fn func(ptr: &u8) usize {
var u = @ptrToInt(ptr);
return u;
}
}.func;
}
}

test "define a function in an anonymous struct in comptime" {
const foo = get_foo();
assert(foo(@intToPtr(&u8, 12345)) == 12345);
}

0 comments on commit 131c133

Please sign in to comment.