1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! Reachability walkers used by the DCE pass.
//!
//! The walkers traverse [`crate::ir::ResolvedType`] and [`IrExpr`] values,
//! marking any [`StructId`], [`TraitId`], or [`EnumId`] they encounter as
//! live in the parent [`super::DeadCodeEliminator`].
use crate::ir::IrExpr;
use super::DeadCodeEliminator;
impl DeadCodeEliminator<'_> {
/// Mark structs used in a type.
pub(super) fn mark_used_in_type(&mut self, ty: &crate::ir::ResolvedType) {
use crate::ir::ResolvedType;
match ty {
ResolvedType::Struct(id) => {
self.used_structs.insert(*id);
}
ResolvedType::Trait(id) => {
self.used_traits.insert(*id);
}
ResolvedType::Generic { base, args } => {
match base {
crate::ir::GenericBase::Struct(id) => {
self.used_structs.insert(*id);
}
crate::ir::GenericBase::Enum(id) => {
self.used_enums.insert(*id);
}
crate::ir::GenericBase::Trait(id) => {
self.used_traits.insert(*id);
}
}
for arg in args {
self.mark_used_in_type(arg);
}
}
ResolvedType::Tuple(fields) => {
for (_, field_ty) in fields {
self.mark_used_in_type(field_ty);
}
}
ResolvedType::Closure {
param_tys,
return_ty,
} => {
for (_, pty) in param_tys {
self.mark_used_in_type(pty);
}
self.mark_used_in_type(return_ty);
}
ResolvedType::External { type_args, .. } => {
for arg in type_args {
self.mark_used_in_type(arg);
}
}
ResolvedType::Enum(id) => {
self.used_enums.insert(*id);
}
// Placeholder types do not reference any definition.
ResolvedType::Primitive(_) | ResolvedType::TypeParam(_) | ResolvedType::Error => {}
}
}
/// Mark structs used in an expression.
#[expect(
clippy::too_many_lines,
reason = "exhaustive match over every IrExpr variant; splitting would hide the walk"
)]
pub(super) fn mark_used_in_expr(&mut self, expr: &IrExpr) {
// Mark every expression's type so the carriers reachable only
// through type slots (e.g. `Array<T>`/`Dictionary<K,V>` from
// `[1, 2, 3]` / `["k": v]` literals, `Optional<T>` from a `nil`
// value) survive DCE. Without this, post-(b) prelude built-ins
// get dropped because their carrier `StructId` is only ever
// referenced through expression types, never through a
// dedicated marking branch.
self.mark_used_in_type(expr.ty());
match expr {
IrExpr::StructInst {
struct_id,
fields,
ty,
type_args,
..
} => {
if let Some(id) = struct_id {
self.used_structs.insert(*id);
}
// walk the resolved type and any explicit
// type-arguments so a local struct/enum used as a generic
// arg of an external receiver (e.g. `Box<LocalThing>` from
// an imported module) is marked used.
self.mark_used_in_type(ty);
for arg in type_args {
self.mark_used_in_type(arg);
}
for (_, _, e) in fields {
self.mark_used_in_expr(e);
}
}
IrExpr::BinaryOp { left, right, .. } => {
self.mark_used_in_expr(left);
self.mark_used_in_expr(right);
}
IrExpr::UnaryOp { operand, .. } => self.mark_used_in_expr(operand),
IrExpr::If {
condition,
then_branch,
else_branch,
..
} => {
self.mark_used_in_expr(condition);
self.mark_used_in_expr(then_branch);
if let Some(else_b) = else_branch {
self.mark_used_in_expr(else_b);
}
}
IrExpr::Array { elements, .. } => {
for e in elements {
self.mark_used_in_expr(e);
}
}
IrExpr::EnumInst {
enum_id,
fields,
ty,
..
} => {
if let Some(id) = enum_id {
self.used_enums.insert(*id);
}
// walk the resolved type so a local
// struct/enum used as a generic arg of an external
// enum (e.g. `Result<LocalErr, OK>`) is marked used.
self.mark_used_in_type(ty);
for (_, _, e) in fields {
self.mark_used_in_expr(e);
}
}
IrExpr::Tuple { fields, .. } => {
for (_, e) in fields {
self.mark_used_in_expr(e);
}
}
IrExpr::For {
collection, body, ..
} => {
self.mark_used_in_expr(collection);
self.mark_used_in_expr(body);
}
IrExpr::Match {
scrutinee, arms, ..
} => {
self.mark_used_in_expr(scrutinee);
for arm in arms {
self.mark_used_in_expr(&arm.body);
}
}
IrExpr::FunctionCall { args, .. } => {
for (_, arg) in args {
self.mark_used_in_expr(arg);
}
}
IrExpr::CallClosure {
closure, args, ty, ..
} => {
self.mark_used_in_type(ty);
self.mark_used_in_expr(closure);
for (_, arg) in args {
self.mark_used_in_expr(arg);
}
}
IrExpr::MethodCall {
receiver,
args,
dispatch,
..
} => {
self.mark_used_in_expr(receiver);
for (_, arg) in args {
self.mark_used_in_expr(arg);
}
// Virtual dispatch keeps its trait alive.
if let crate::ir::DispatchKind::Virtual { trait_id, .. } = dispatch {
self.used_traits.insert(*trait_id);
}
// Static dispatch points at an impl whose target struct/enum
// is already reached via the receiver's type.
}
IrExpr::DictLiteral { entries, .. } => {
for (k, v) in entries {
self.mark_used_in_expr(k);
self.mark_used_in_expr(v);
}
}
IrExpr::DictAccess { dict, key, .. } => {
self.mark_used_in_expr(dict);
self.mark_used_in_expr(key);
}
IrExpr::Block {
statements, result, ..
} => {
for stmt in statements {
self.mark_used_in_block_statement(stmt);
}
self.mark_used_in_expr(result);
}
IrExpr::Literal { .. }
| IrExpr::Reference { .. }
| IrExpr::SelfFieldRef { .. }
| IrExpr::LetRef { .. } => {}
IrExpr::FieldAccess { object, .. } => self.mark_used_in_expr(object),
IrExpr::Closure {
params,
captures,
body,
..
} => {
for (_, _, _, ty) in params {
self.mark_used_in_type(ty);
}
for (_, _, _, ty) in captures {
self.mark_used_in_type(ty);
}
self.mark_used_in_expr(body);
}
IrExpr::ClosureRef { env_struct, ty, .. } => {
self.mark_used_in_type(ty);
self.mark_used_in_expr(env_struct);
}
}
}
pub(super) fn mark_used_in_block_statement(&mut self, stmt: &crate::ir::IrBlockStatement) {
use crate::ir::IrBlockStatement;
match stmt {
IrBlockStatement::Let { value, ty, .. } => {
if let Some(annotated) = ty {
self.mark_used_in_type(annotated);
}
self.mark_used_in_expr(value);
}
IrBlockStatement::Assign { target, value, .. } => {
self.mark_used_in_expr(target);
self.mark_used_in_expr(value);
}
IrBlockStatement::Expr(expr) => {
self.mark_used_in_expr(expr);
}
}
}
}