Commit 373b3586a1

Andrew Kelley <superjoe30@gmail.com>
2018-04-12 22:26:23
inline functions must be stored in const or comptime var
closes #913
1 parent 0d8646d
Changed files (3)
src/ir.cpp
@@ -11395,7 +11395,19 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
                 }
                 break;
             case VarClassRequiredAny:
-                // OK
+                if (casted_init_value->value.special == ConstValSpecialStatic &&
+                    casted_init_value->value.type->id == TypeTableEntryIdFn &&
+                    casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways)
+                {
+                    var_class_requires_const = true;
+                    if (!var->src_is_const && !is_comptime_var) {
+                        ErrorMsg *msg = ir_add_error_node(ira, source_node,
+                            buf_sprintf("functions marked inline must be stored in const or comptime var"));
+                        AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node;
+                        add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here"));
+                        result_type = ira->codegen->builtin_types.entry_invalid;
+                    }
+                }
                 break;
         }
     }
test/cases/fn.zig
@@ -104,3 +104,10 @@ test "number literal as an argument" {
 fn numberLiteralArg(a: var) void {
     assert(a == 3);
 }
+
+test "assign inline fn to const variable" {
+    const a = inlineFn;
+    a();
+}
+
+inline fn inlineFn() void { }
test/compile_errors.zig
@@ -1,6 +1,15 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: &tests.CompileErrorContext) void {
+    cases.add("assign inline fn to non-comptime var",
+        \\export fn entry() void {
+        \\    var a = b;
+        \\}
+        \\inline fn b() void { }
+    ,
+        ".tmp_source.zig:2:5: error: functions marked inline must be stored in const or comptime var",
+        ".tmp_source.zig:4:8: note: declared here");
+
     cases.add("wrong type passed to @panic",
         \\export fn entry() void {
         \\    var e = error.Foo;