From ad6aed3d7a8362fc61a352f93e66ecf3e29a3b63 Mon Sep 17 00:00:00 2001 From: agra Date: Mon, 15 Jun 2026 03:53:03 +0300 Subject: [PATCH] =?UTF-8?q?fix(ffi-linkage):=20Phase=205.0=20prereq=20?= =?UTF-8?q?=E2=80=94=20validate=20extern=20LIB=20refs=20like=20#foreign?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit checkForeignRefs now reads a library reference from either spelling — the legacy #foreign body (foreign_expr.library_ref) or the new extern keyword (extern_lib) — and validates both against the declared #library / #import c units. The diagnostic names the surface keyword the user wrote (#foreign vs extern), so example 1620 (#foreign) is byte-unchanged and example 1231 (extern) gets the parallel 'extern library ... not declared'. Greens 1231. 647 corpus / 444 unit, 0 failed. --- src/c_import.zig | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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),