diff --git a/src/c_import.zig b/src/c_import.zig index 98248ae7..b1ec6868 100644 --- a/src/c_import.zig +++ b/src/c_import.zig @@ -332,10 +332,20 @@ fn checkForeignRefs(valid: *const std.StringHashMap(void), decls: []const *Node, for (decls) |d| { switch (d.data) { .fn_decl => |fd| { - if (fd.body.data != .foreign_expr) continue; - const ref = fd.body.data.foreign_expr.library_ref orelse continue; + // A library reference rides on the legacy `#foreign` body + // (foreign_expr.library_ref) OR the new `extern LIB` keyword + // (extern_lib); both must name a declared #library / #import c + // unit. The diagnostic names the surface keyword the user wrote + // so the two spellings stay self-describing. + const kw: []const u8, const ref: []const u8 = switch (fd.body.data) { + .foreign_expr => |fe| .{ "#foreign", fe.library_ref orelse continue }, + else => if (fd.extern_export == .extern_) + .{ "extern", fd.extern_lib orelse continue } + else + continue, + }; if (!valid.contains(ref)) { - diags.addFmt(.err, d.span, "#foreign library '{s}' is not declared; expected a #library constant or a named '#import c' unit", .{ref}); + diags.addFmt(.err, d.span, "{s} library '{s}' is not declared; expected a #library constant or a named '#import c' unit", .{ kw, ref }); } }, .namespace_decl => |ns| checkForeignRefs(valid, ns.decls, diags),