Skip to content

Commit 26d49e2

Browse files
authored
Merge pull request #7978 from roc-lang/remove-redundant-regions
Remove redundant regions
2 parents c6aa2ee + 3969867 commit 26d49e2

25 files changed

+529
-827
lines changed

src/check/canonicalize.zig

Lines changed: 124 additions & 218 deletions
Large diffs are not rendered by default.

src/check/canonicalize/CIR.zig

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ pub const RecordField = struct {
605605
tree.pushStaticAtom("field");
606606
tree.pushStringPair("name", ir.env.idents.getText(self.name));
607607
const attrs = tree.beginNode();
608-
ir.store.getExpr(self.value).pushToSExprTree(ir, tree);
608+
ir.store.getExpr(self.value).pushToSExprTree(ir, tree, self.value);
609609
tree.endNode(begin, attrs);
610610
}
611611
};
@@ -633,7 +633,6 @@ pub const WhereClause = union(enum) {
633633
/// Contains diagnostic information about what went wrong.
634634
malformed: struct {
635635
diagnostic: Diagnostic.Idx,
636-
region: Region,
637636
},
638637

639638
pub const ModuleMethod = struct {
@@ -642,22 +641,21 @@ pub const WhereClause = union(enum) {
642641
args: TypeAnno.Span, // Method argument types
643642
ret_anno: TypeAnno.Idx, // Method return type
644643
external_decl: ExternalDecl.Idx, // External declaration for module lookup
645-
region: Region,
646644
};
647645

648646
pub const ModuleAlias = struct {
649647
var_name: Ident.Idx, // Type variable identifier (e.g., "elem")
650648
alias_name: Ident.Idx, // Alias name without leading dot (e.g., "Sort")
651649
external_decl: ExternalDecl.Idx, // External declaration for module lookup
652-
region: Region,
653650
};
654651

655-
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree) void {
652+
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree, where_idx: WhereClause.Idx) void {
656653
switch (self.*) {
657654
.mod_method => |mm| {
658655
const begin = tree.beginNode();
659656
tree.pushStaticAtom("method");
660-
ir.appendRegionInfoToSExprTreeFromRegion(tree, mm.region);
657+
const region = ir.store.getNodeRegion(@enumFromInt(@intFromEnum(where_idx)));
658+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
661659
tree.pushStringPair("module-of", ir.env.idents.getText(mm.var_name));
662660
tree.pushStringPair("ident", ir.env.idents.getText(mm.method_name));
663661
const attrs = tree.beginNode();
@@ -666,26 +664,28 @@ pub const WhereClause = union(enum) {
666664
tree.pushStaticAtom("args");
667665
const attrs2 = tree.beginNode();
668666
for (ir.store.sliceTypeAnnos(mm.args)) |arg_idx| {
669-
ir.store.getTypeAnno(arg_idx).pushToSExprTree(ir, tree);
667+
ir.store.getTypeAnno(arg_idx).pushToSExprTree(ir, tree, arg_idx);
670668
}
671669
tree.endNode(args_begin, attrs2);
672670

673-
ir.store.getTypeAnno(mm.ret_anno).pushToSExprTree(ir, tree);
671+
ir.store.getTypeAnno(mm.ret_anno).pushToSExprTree(ir, tree, mm.ret_anno);
674672
tree.endNode(begin, attrs);
675673
},
676674
.mod_alias => |ma| {
677675
const begin = tree.beginNode();
678676
tree.pushStaticAtom("alias");
679-
ir.appendRegionInfoToSExprTreeFromRegion(tree, ma.region);
677+
const region = ir.store.getNodeRegion(@enumFromInt(@intFromEnum(where_idx)));
678+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
680679
tree.pushStringPair("module-of", ir.env.idents.getText(ma.var_name));
681680
tree.pushStringPair("ident", ir.env.idents.getText(ma.alias_name));
682681
const attrs = tree.beginNode();
683682
tree.endNode(begin, attrs);
684683
},
685-
.malformed => |m| {
684+
.malformed => |_| {
686685
const begin = tree.beginNode();
687686
tree.pushStaticAtom("malformed");
688-
ir.appendRegionInfoToSExprTreeFromRegion(tree, m.region);
687+
const region = ir.store.getNodeRegion(@enumFromInt(@intFromEnum(where_idx)));
688+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
689689
const attrs = tree.beginNode();
690690
tree.endNode(begin, attrs);
691691
},
@@ -716,15 +716,15 @@ pub const PatternRecordField = struct {
716716
pub const TypeHeader = struct {
717717
name: Ident.Idx, // The type name (e.g., "Map", "List", "Dict")
718718
args: TypeAnno.Span, // Type parameters (e.g., [a, b] for generic types)
719-
region: Region, // Source location of the entire header
720719

721720
pub const Idx = enum(u32) { _ };
722721
pub const Span = struct { span: DataSpan };
723722

724-
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree) void {
723+
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree, header_idx: TypeHeader.Idx) void {
725724
const begin = tree.beginNode();
726725
tree.pushStaticAtom("ty-header");
727-
ir.appendRegionInfoToSExprTreeFromRegion(tree, self.region);
726+
const region = ir.store.getRegionAt(@enumFromInt(@intFromEnum(header_idx)));
727+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
728728
tree.pushStringPair("name", ir.env.idents.getText(self.name));
729729
const attrs = tree.beginNode();
730730

@@ -733,7 +733,7 @@ pub const TypeHeader = struct {
733733
tree.pushStaticAtom("ty-args");
734734
const attrs2 = tree.beginNode();
735735
for (ir.store.sliceTypeAnnos(self.args)) |arg_idx| {
736-
ir.store.getTypeAnno(arg_idx).pushToSExprTree(ir, tree);
736+
ir.store.getTypeAnno(arg_idx).pushToSExprTree(ir, tree, arg_idx);
737737
}
738738
tree.endNode(args_begin, attrs2);
739739
}
@@ -849,9 +849,7 @@ pub const IngestedFile = struct {
849849
/// takes its value from an expression.
850850
pub const Def = struct {
851851
pattern: Pattern.Idx,
852-
pattern_region: Region,
853852
expr: Expr.Idx,
854-
expr_region: Region,
855853
// TODO:
856854
// pattern_vars: SendMap<Symbol, Variable>,
857855
annotation: ?Annotation.Idx,
@@ -923,11 +921,11 @@ pub const Def = struct {
923921

924922
ir.store.getPattern(self.pattern).pushToSExprTree(ir, tree, self.pattern);
925923

926-
ir.store.getExpr(self.expr).pushToSExprTree(ir, tree);
924+
ir.store.getExpr(self.expr).pushToSExprTree(ir, tree, self.expr);
927925

928926
if (self.annotation) |anno_idx| {
929927
const anno = ir.store.getAnnotation(anno_idx);
930-
anno.pushToSExprTree(ir, tree);
928+
anno.pushToSExprTree(ir, tree, anno_idx);
931929
}
932930

933931
tree.endNode(begin, attrs);
@@ -942,22 +940,21 @@ pub const Annotation = struct {
942940
type_anno: TypeAnno.Idx,
943941
/// The canonical type signature as a type variable (for type inference)
944942
signature: TypeVar,
945-
/// Source region of the annotation
946-
region: Region,
947943

948944
pub const Idx = enum(u32) { _ };
949945

950-
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree) void {
946+
pub fn pushToSExprTree(self: *const @This(), ir: *const CIR, tree: *SExprTree, anno_idx: Annotation.Idx) void {
951947
const begin = tree.beginNode();
952948
tree.pushStaticAtom("annotation");
953-
ir.appendRegionInfoToSExprTreeFromRegion(tree, self.region);
949+
const region = ir.store.getRegionAt(@enumFromInt(@intFromEnum(anno_idx)));
950+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
954951
const attrs = tree.beginNode();
955952

956953
// Add the declared type annotation structure
957954
const type_begin = tree.beginNode();
958955
tree.pushStaticAtom("declared-type");
959956
const type_attrs = tree.beginNode();
960-
ir.store.getTypeAnno(self.type_anno).pushToSExprTree(ir, tree);
957+
ir.store.getTypeAnno(self.type_anno).pushToSExprTree(ir, tree, self.type_anno);
961958
tree.endNode(type_begin, type_attrs);
962959

963960
tree.endNode(begin, attrs);
@@ -986,7 +983,7 @@ pub const ExternalDecl = struct {
986983
/// Whether this is a value or type declaration
987984
kind: enum { value, type },
988985

989-
/// Region where this was referenced
986+
/// Region where this external declaration was referenced
990987
region: Region,
991988

992989
pub const Idx = enum(u32) { _ };
@@ -1009,6 +1006,24 @@ pub const ExternalDecl = struct {
10091006
const attrs = tree.beginNode();
10101007
tree.endNode(begin, attrs);
10111008
}
1009+
1010+
pub fn pushToSExprTreeWithRegion(self: *const @This(), ir: *const CIR, tree: *SExprTree, region: Region) void {
1011+
const begin = tree.beginNode();
1012+
tree.pushStaticAtom("ext-decl");
1013+
ir.appendRegionInfoToSExprTreeFromRegion(tree, region);
1014+
1015+
// Add fully qualified name
1016+
tree.pushStringPair("ident", ir.env.idents.getText(self.qualified_name));
1017+
1018+
// Add kind
1019+
switch (self.kind) {
1020+
.value => tree.pushStringPair("kind", "value"),
1021+
.type => tree.pushStringPair("kind", "type"),
1022+
}
1023+
1024+
const attrs = tree.beginNode();
1025+
tree.endNode(begin, attrs);
1026+
}
10121027
};
10131028

10141029
/// Tracks type variables introduced during annotation canonicalization
@@ -1139,7 +1154,7 @@ pub fn pushToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTree, so
11391154

11401155
if (maybe_expr_idx) |expr_idx| {
11411156
// Only output the given expression
1142-
ir.store.getExpr(expr_idx).pushToSExprTree(ir, tree);
1157+
ir.store.getExpr(expr_idx).pushToSExprTree(ir, tree, expr_idx);
11431158
} else {
11441159
const root_begin = tree.beginNode();
11451160
tree.pushStaticAtom("can-ir");
@@ -1158,7 +1173,7 @@ pub fn pushToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTree, so
11581173
}
11591174

11601175
for (statements_slice) |stmt_idx| {
1161-
ir.store.getStatement(stmt_idx).pushToSExprTree(ir, tree);
1176+
ir.store.getStatement(stmt_idx).pushToSExprTree(ir, tree, stmt_idx);
11621177
}
11631178

11641179
for (ir.external_decls.items) |*external_decl| {
@@ -1305,7 +1320,9 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
13051320
const patt_begin = tree.beginNode();
13061321
tree.pushStaticAtom("patt");
13071322

1308-
ir.appendRegionInfoToSExprTree(tree, def_idx);
1323+
// Get the pattern region instead of the whole def region
1324+
const pattern_region = ir.store.getPatternRegion(def.pattern);
1325+
ir.appendRegionInfoToSExprTreeFromRegion(tree, pattern_region);
13091326

13101327
// Get the type variable for this definition
13111328
const def_var = ir.idxToTypeVar(&ir.env.types, def_idx) catch |err| exitOnOom(err);
@@ -1367,7 +1384,8 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
13671384

13681385
const stmt_node_begin = tree.beginNode();
13691386
tree.pushStaticAtom("alias");
1370-
ir.appendRegionInfoToSExprTreeFromRegion(tree, alias.region);
1387+
const alias_region = ir.store.getStatementRegion(stmt_idx);
1388+
ir.appendRegionInfoToSExprTreeFromRegion(tree, alias_region);
13711389

13721390
// Clear the buffer and write the type
13731391
type_string_buf.clearRetainingCapacity();
@@ -1376,7 +1394,7 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
13761394
const stmt_node_attrs = tree.beginNode();
13771395

13781396
const header = ir.store.getTypeHeader(alias.header);
1379-
header.pushToSExprTree(ir, tree);
1397+
header.pushToSExprTree(ir, tree, alias.header);
13801398

13811399
tree.endNode(stmt_node_begin, stmt_node_attrs);
13821400
},
@@ -1385,7 +1403,8 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
13851403

13861404
const stmt_node_begin = tree.beginNode();
13871405
tree.pushStaticAtom("nominal");
1388-
ir.appendRegionInfoToSExprTreeFromRegion(tree, nominal.region);
1406+
const nominal_region = ir.store.getStatementRegion(stmt_idx);
1407+
ir.appendRegionInfoToSExprTreeFromRegion(tree, nominal_region);
13891408

13901409
// Clear the buffer and write the type
13911410
type_string_buf.clearRetainingCapacity();
@@ -1395,7 +1414,7 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
13951414
const stmt_node_attrs = tree.beginNode();
13961415

13971416
const header = ir.store.getTypeHeader(nominal.header);
1398-
header.pushToSExprTree(ir, tree);
1417+
header.pushToSExprTree(ir, tree, nominal.header);
13991418

14001419
tree.endNode(stmt_node_begin, stmt_node_attrs);
14011420
},
@@ -1423,7 +1442,10 @@ pub fn pushTypesToSExprTree(ir: *CIR, maybe_expr_idx: ?Expr.Idx, tree: *SExprTre
14231442

14241443
const expr_node_begin = tree.beginNode();
14251444
tree.pushStaticAtom("expr");
1426-
ir.appendRegionInfoToSExprTreeFromRegion(tree, def.expr_region);
1445+
1446+
// Add region info for the expression
1447+
const expr_region = ir.store.getExprRegion(def.expr);
1448+
ir.appendRegionInfoToSExprTreeFromRegion(tree, expr_region);
14271449

14281450
if (@intFromEnum(expr_var) > ir.env.types.slots.backing.items.len) {
14291451
const unknown_begin = tree.beginNode();

0 commit comments

Comments
 (0)