From 131c133bb74eab2012158c8ecfaa78944db197c7 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Thu, 3 May 2018 04:43:07 +0300 Subject: 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 --- src/ir.cpp | 2 ++ test/behavior.zig | 1 + test/cases/fn_in_struct_in_comptime.zig | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 test/cases/fn_in_struct_in_comptime.zig diff --git a/src/ir.cpp b/src/ir.cpp index 469900bf07..47f188fdf8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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; diff --git a/test/behavior.zig b/test/behavior.zig index 2c10c6d71b..3e540e0cf4 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -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"); } diff --git a/test/cases/fn_in_struct_in_comptime.zig b/test/cases/fn_in_struct_in_comptime.zig new file mode 100644 index 0000000000..4f181d7ffb --- /dev/null +++ b/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); +} -- cgit v1.2.3