Skip to content

Commit 131c133

Browse files
alexnaskandrewrk
authored andcommittedMay 3, 2018
Fixed inlining determination test (#972)
When deciding wether we should inline a scope, look up the parents until we get to a function definition scope
1 parent 02c1b9d commit 131c133

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎src/ir.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ static bool ir_should_inline(IrExecutable *exec, Scope *scope) {
145145
while (scope != nullptr) {
146146
if (scope->id == ScopeIdCompTime)
147147
return true;
148+
if (scope->id == ScopeIdFnDef)
149+
break;
148150
scope = scope->parent;
149151
}
150152
return false;

‎test/behavior.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ comptime {
5252
_ = @import("cases/var_args.zig");
5353
_ = @import("cases/void.zig");
5454
_ = @import("cases/while.zig");
55+
_ = @import("cases/fn_in_struct_in_comptime.zig");
5556
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.