Commit f4b8850002

Andrew Kelley <andrew@ziglang.org>
2019-06-16 20:14:57
fix type info crash on extern lib name
1 parent aa60d2a
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -18613,10 +18613,11 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
                         true, false, PtrLenUnknown,
                         0, 0, 0, false);
                     fn_decl_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
-                    if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {
+                    if (fn_node->is_extern && fn_node->lib_name != nullptr && buf_len(fn_node->lib_name) > 0) {
                         fn_decl_fields[6].data.x_optional = create_const_vals(1);
                         ConstExprValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name);
-                        init_const_slice(ira->codegen, fn_decl_fields[6].data.x_optional, lib_name, 0, buf_len(fn_node->lib_name), true);
+                        init_const_slice(ira->codegen, fn_decl_fields[6].data.x_optional, lib_name, 0,
+                                buf_len(fn_node->lib_name), true);
                     } else {
                         fn_decl_fields[6].data.x_optional = nullptr;
                     }
test/stage1/behavior/type_info.zig
@@ -1,7 +1,9 @@
-const expect = @import("std").testing.expect;
-const mem = @import("std").mem;
-const TypeInfo = @import("builtin").TypeInfo;
-const TypeId = @import("builtin").TypeId;
+const std = @import("std");
+const expect = std.testing.expect;
+const mem = std.mem;
+const builtin = @import("builtin");
+const TypeInfo = builtin.TypeInfo;
+const TypeId = builtin.TypeId;
 
 test "type info: tag type, void info" {
     testBasic();
@@ -317,3 +319,20 @@ test "type info: TypeId -> TypeInfo impl cast" {
     _ = passTypeInfo(TypeId.Void);
     _ = comptime passTypeInfo(TypeId.Void);
 }
+
+test "type info: extern fns with and without lib names" {
+    const S = struct {
+        extern fn bar1() void;
+        extern "cool" fn bar2() void;
+    };
+    const info = @typeInfo(S);
+    comptime {
+        for (info.Struct.decls) |decl| {
+            if (std.mem.eql(u8, decl.name, "bar1")) {
+                expect(decl.data.Fn.lib_name == null);
+            } else {
+                std.testing.expectEqual(([]const u8)("cool"), decl.data.Fn.lib_name.?);
+            }
+        }
+    }
+}