From 645fc2b9286793157e56ea9a7cabc2585b84caac Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Tue, 9 Dec 2025 16:59:15 -0500 Subject: [PATCH] Various cleanups --- src/canonicalize/CIR.zig | 7 ------- src/canonicalize/Can.zig | 9 +++++++-- src/canonicalize/ModuleEnv.zig | 22 +++++++++------------- src/canonicalize/NodeStore.zig | 14 -------------- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/canonicalize/CIR.zig b/src/canonicalize/CIR.zig index 507dba69f1..ed81985ff4 100644 --- a/src/canonicalize/CIR.zig +++ b/src/canonicalize/CIR.zig @@ -353,12 +353,6 @@ pub const ExposedItem = struct { } }; -/// Represents a field in a record pattern for pattern matching -pub const PatternRecordField = struct { - pub const Idx = enum(u32) { _ }; - pub const Span = extern struct { start: u32, len: u32 }; -}; - /// Represents an arbitrary precision smallish decimal value pub const SmallDecValue = struct { numerator: i16, @@ -1026,7 +1020,6 @@ pub fn isCastable(comptime T: type) bool { TypeAnno.RecordField.Idx, ExposedItem.Idx, Expr.Match.BranchPattern.Idx, - PatternRecordField.Idx, Node.Idx, TypeVar, => true, diff --git a/src/canonicalize/Can.zig b/src/canonicalize/Can.zig index d2e0b4c531..502c798988 100644 --- a/src/canonicalize/Can.zig +++ b/src/canonicalize/Can.zig @@ -95,6 +95,8 @@ scratch_free_vars: base.Scratch(Pattern.Idx), scratch_captures: base.Scratch(Pattern.Idx), /// Scratch bound variables (for filtering out locally-bound vars from captures) scratch_bound_vars: base.Scratch(Pattern.Idx), +/// Counter for generating unique malformed import placeholder names +malformed_import_count: u32 = 0, const Ident = base.Ident; const Region = base.Region; @@ -3157,8 +3159,11 @@ fn canonicalizeImportStatement( .region = region, } }); - // Use a placeholder identifier instead - const placeholder_text = "MALFORMED_IMPORT"; + // Use a unique placeholder identifier that starts with '#' to ensure it can't + // collide with user-defined identifiers (# starts a comment in Roc) + var buf: [32]u8 = undefined; + const placeholder_text = std.fmt.bufPrint(&buf, "#malformed_import_{d}", .{self.malformed_import_count}) catch unreachable; + self.malformed_import_count += 1; break :blk try self.env.insertIdent(base.Ident.for_text(placeholder_text)); } } else { diff --git a/src/canonicalize/ModuleEnv.zig b/src/canonicalize/ModuleEnv.zig index fb8ebe9619..fd964bfabb 100644 --- a/src/canonicalize/ModuleEnv.zig +++ b/src/canonicalize/ModuleEnv.zig @@ -1246,7 +1246,7 @@ pub fn diagnosticToReport(self: *Self, diagnostic: CIR.Diagnostic, allocator: st }, .f64_pattern_literal => |data| blk: { // Extract the literal text from the source - const literal_text = self.getSourceAll()[data.region.start.offset..data.region.end.offset]; + const literal_text = self.getSource(data.region); var report = Report.init(allocator, "F64 NOT ALLOWED IN PATTERN", .runtime_error); @@ -2224,14 +2224,6 @@ pub fn addMatchBranchPattern(self: *Self, expr: CIR.Expr.Match.BranchPattern, re return expr_idx; } -/// Add a new pattern record field to the node store. -/// This function asserts that the nodes and regions are in sync. -pub fn addPatternRecordField(self: *Self, expr: CIR.PatternRecordField) std.mem.Allocator.Error!CIR.PatternRecordField.Idx { - const expr_idx = try self.store.addPatternRecordField(expr); - self.debugAssertArraysInSync(); - return expr_idx; -} - /// Add a new type variable to the node store. /// This function asserts that the nodes and regions are in sync. pub fn addTypeSlot( @@ -2583,14 +2575,18 @@ pub fn getSource(self: *const Self, region: Region) []const u8 { return self.common.getSource(region); } -/// TODO this is a code smell... we should track down the places using this -/// and replace with something more sensible -- need to refactor diagnostics a little. +/// Get the entire source text. This is primarily needed for diagnostic output +/// where `addSourceRegion` requires access to the full source and line starts +/// to render error messages with context lines. +/// +/// For extracting source text for a specific region, prefer `getSource(region)` instead. pub fn getSourceAll(self: *const Self) []const u8 { return self.common.getSourceAll(); } -/// TODO this is a code smell... we should track down the places using this -/// and replace with something more sensible -- need to refactor diagnostics a little. +/// Get all line start offsets. This is primarily needed for diagnostic output +/// where `addSourceRegion` requires access to the full source and line starts +/// to render error messages with context lines. pub fn getLineStartsAll(self: *const Self) []const u32 { return self.common.getLineStartsAll(); } diff --git a/src/canonicalize/NodeStore.zig b/src/canonicalize/NodeStore.zig index 5ec8869c3e..c0264ee44b 100644 --- a/src/canonicalize/NodeStore.zig +++ b/src/canonicalize/NodeStore.zig @@ -46,7 +46,6 @@ const Scratch = struct { if_branches: base.Scratch(CIR.Expr.IfBranch.Idx), where_clauses: base.Scratch(CIR.WhereClause.Idx), patterns: base.Scratch(CIR.Pattern.Idx), - pattern_record_fields: base.Scratch(CIR.PatternRecordField.Idx), record_destructs: base.Scratch(CIR.Pattern.RecordDestruct.Idx), type_annos: base.Scratch(CIR.TypeAnno.Idx), anno_record_fields: base.Scratch(CIR.TypeAnno.RecordField.Idx), @@ -67,7 +66,6 @@ const Scratch = struct { .if_branches = try base.Scratch(CIR.Expr.IfBranch.Idx).init(gpa), .where_clauses = try base.Scratch(CIR.WhereClause.Idx).init(gpa), .patterns = try base.Scratch(CIR.Pattern.Idx).init(gpa), - .pattern_record_fields = try base.Scratch(CIR.PatternRecordField.Idx).init(gpa), .record_destructs = try base.Scratch(CIR.Pattern.RecordDestruct.Idx).init(gpa), .type_annos = try base.Scratch(CIR.TypeAnno.Idx).init(gpa), .anno_record_fields = try base.Scratch(CIR.TypeAnno.RecordField.Idx).init(gpa), @@ -89,7 +87,6 @@ const Scratch = struct { self.if_branches.deinit(); self.where_clauses.deinit(); self.patterns.deinit(); - self.pattern_record_fields.deinit(); self.record_destructs.deinit(); self.type_annos.deinit(); self.anno_record_fields.deinit(); @@ -1127,12 +1124,6 @@ pub fn getPattern(store: *const NodeStore, pattern_idx: CIR.Pattern.Idx) CIR.Pat } } -/// Retrieves a pattern record field from the store. -pub fn getPatternRecordField(_: *NodeStore, _: CIR.PatternRecordField.Idx) CIR.PatternRecordField { - // Return empty placeholder since PatternRecordField has no fields yet - return CIR.PatternRecordField{}; -} - /// Retrieves a type annotation from the store. pub fn getTypeAnno(store: *const NodeStore, typeAnno: CIR.TypeAnno.Idx) CIR.TypeAnno { const node_idx: Node.Idx = @enumFromInt(@intFromEnum(typeAnno)); @@ -2139,11 +2130,6 @@ pub fn addPattern(store: *NodeStore, pattern: CIR.Pattern, region: base.Region) return @enumFromInt(@intFromEnum(node_idx)); } -/// Adds a pattern record field to the store. -pub fn addPatternRecordField(_: *NodeStore, _: CIR.PatternRecordField) Allocator.Error!CIR.PatternRecordField.Idx { - @panic("TODO: addPatternRecordField not implemented"); -} - /// Adds a type annotation to the store. /// /// IMPORTANT: You should not use this function directly! Instead, use it's