Skip to content

Commit c267408

Browse files
committed
Fix remaining issues
1 parent b61151e commit c267408

File tree

5 files changed

+16
-32
lines changed

5 files changed

+16
-32
lines changed

src/build/builtin_compiler/main.zig

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -901,11 +901,7 @@ fn serializeModuleEnv(
901901
/// For builtin_compiler, types are always in all_statements (not builtin_statements)
902902
/// because we're compiling Builtin.roc itself, not importing from it.
903903
fn findTypeDeclaration(env: *const ModuleEnv, type_name: []const u8) !CIR.Statement.Idx {
904-
// Construct the qualified name (e.g., "Builtin.Bool")
905-
// Types in nested declarations are stored with their full qualified names
906-
var qualified_name_buf: [256]u8 = undefined;
907-
const qualified_name = try std.fmt.bufPrint(&qualified_name_buf, "{s}.{s}", .{ env.module_name, type_name });
908-
904+
// Type headers now store the unqualified name (e.g., "Bool" not "Builtin.Bool")
909905
// Search in all_statements (where Builtin.roc's own types are stored)
910906
const all_stmts = env.store.sliceStatements(env.all_statements);
911907
for (all_stmts) |stmt_idx| {
@@ -915,7 +911,7 @@ fn findTypeDeclaration(env: *const ModuleEnv, type_name: []const u8) !CIR.Statem
915911
const header = env.store.getTypeHeader(decl.header);
916912
const ident_idx = header.name;
917913
const ident_text = env.getIdentText(ident_idx);
918-
if (std.mem.eql(u8, ident_text, qualified_name)) {
914+
if (std.mem.eql(u8, ident_text, type_name)) {
919915
return stmt_idx;
920916
}
921917
},
@@ -927,13 +923,13 @@ fn findTypeDeclaration(env: *const ModuleEnv, type_name: []const u8) !CIR.Statem
927923
}
928924

929925
/// Find a nested type declaration by parent and type name in a compiled module
930-
/// For example, findNestedTypeDeclaration(env, "Num", "U8") finds "Builtin.Num.U8"
926+
/// For example, findNestedTypeDeclaration(env, "Num", "U8") finds type with header.name = "U8"
927+
/// The parent_name parameter is ignored since type headers store the unqualified name
931928
/// Returns the statement index of the type declaration
932929
fn findNestedTypeDeclaration(env: *const ModuleEnv, parent_name: []const u8, type_name: []const u8) !CIR.Statement.Idx {
933-
// Construct the qualified name (e.g., "Builtin.Num.U8")
934-
var qualified_name_buf: [256]u8 = undefined;
935-
const qualified_name = try std.fmt.bufPrint(&qualified_name_buf, "{s}.{s}.{s}", .{ env.module_name, parent_name, type_name });
930+
_ = parent_name; // Type headers store unqualified names
936931

932+
// Type headers now store the unqualified name (e.g., "U8" not "Builtin.Num.U8")
937933
// Search in all_statements (where Builtin.roc's own types are stored)
938934
const all_stmts = env.store.sliceStatements(env.all_statements);
939935
for (all_stmts) |stmt_idx| {
@@ -943,7 +939,7 @@ fn findNestedTypeDeclaration(env: *const ModuleEnv, parent_name: []const u8, typ
943939
const header = env.store.getTypeHeader(decl.header);
944940
const ident_idx = header.name;
945941
const ident_text = env.getIdentText(ident_idx);
946-
if (std.mem.eql(u8, ident_text, qualified_name)) {
942+
if (std.mem.eql(u8, ident_text, type_name)) {
947943
return stmt_idx;
948944
}
949945
},

src/canonicalize/Can.zig

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,12 @@ fn processTypeDeclFirstPass(
412412
break :blk try self.env.insertQualifiedIdent(parent_text, type_text);
413413
} else type_header.name;
414414

415-
// Create a new header with the qualified name if needed
416-
const final_header_idx = if (parent_name != null and qualified_name_idx.idx != type_header.name.idx) blk: {
417-
const qualified_header = CIR.TypeHeader{
418-
.name = qualified_name_idx,
419-
.args = type_header.args,
420-
};
421-
break :blk try self.env.addTypeHeader(qualified_header, region);
422-
} else header_idx;
415+
// Keep the original header with its unqualified name.
416+
// The qualified_name_idx is only used for scope registration, not stored in the type header.
417+
// This ensures that NominalType.ident contains just the type name (e.g., "Bool"),
418+
// while NominalType.origin_module contains the module name (e.g., "Builtin").
419+
// Method lookup then constructs the full path: origin_module + "." + ident + "." + method
420+
const final_header_idx = header_idx;
423421

424422
// Check if this type was already introduced in Phase 1.5.8 (for forward reference support)
425423
// If so, reuse the existing statement index instead of creating a new one

src/check/Check.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,8 @@ fn mkNumberTypeContent(self: *Self, type_name: []const u8, env: *Env) Allocator.
672672
else
673673
self.common_idents.module_name; // We're compiling Builtin module itself
674674

675-
// Number types are nested in Num module, so the qualified name is "Num.U8", "Num.I32", etc.
676-
const qualified_type_name = try std.fmt.allocPrint(self.gpa, "Num.{s}", .{type_name});
677-
defer self.gpa.free(qualified_type_name);
678-
const type_name_ident = try @constCast(self.cir).insertIdent(base.Ident.for_text(qualified_type_name));
675+
// Type headers now store the unqualified name (e.g., "U8" not "Num.U8")
676+
const type_name_ident = try @constCast(self.cir).insertIdent(base.Ident.for_text(type_name));
679677
const type_ident = types_mod.TypeIdent{
680678
.ident_idx = type_name_ident,
681679
};

src/check/test/type_checking_integration.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ test "associated item can reference another associated item from same type" {
20932093
\\
20942094
\\x = Test.MyBool.my_eq(Test.MyBool.MyTrue, Test.MyBool.MyFalse)
20952095
;
2096-
try checkTypesModule(source, .{ .pass = .{ .def = "x" } }, "Test.MyBool");
2096+
try checkTypesModule(source, .{ .pass = .{ .def = "x" } }, "MyBool");
20972097
}
20982098

20992099
test "Bool.not works as builtin associated item" {

src/eval/interpreter.zig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5663,14 +5663,6 @@ pub const Interpreter = struct {
56635663
const method_name_str = current_ident_store.getText(method_name);
56645664
// Construct: "OriginModule.TypeName.methodName"
56655665
// Note: TypeName may already contain dots for nested types
5666-
// If type_name already starts with origin_module_text (e.g., "Builtin.Bool" with origin "Builtin"),
5667-
// then just use "TypeName.methodName" to avoid duplication like "Builtin.Builtin.Bool.method"
5668-
if (std.mem.startsWith(u8, type_name, origin_module_text) and
5669-
type_name.len > origin_module_text.len and
5670-
type_name[origin_module_text.len] == '.')
5671-
{
5672-
return std.fmt.bufPrint(buf, "{s}.{s}", .{ type_name, method_name_str });
5673-
}
56745666
return std.fmt.bufPrint(buf, "{s}.{s}.{s}", .{ origin_module_text, type_name, method_name_str });
56755667
}
56765668

0 commit comments

Comments
 (0)