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
255
256
257
258
259
260
261
262
263
//! Recursive `IrExpr` walker for `ResolveReferencesPass` — the giant
//! match. Pulled out of `mod.rs` to keep the pass file under the
//! 500-LOC ceiling.
use super::lookups::{lookup_method_idx, struct_field_idx};
use super::walkers::{self, resolve_match_arm};
use super::{BindingKind, FnResolver};
use crate::error::CompilerError;
use crate::ir::{FieldIdx, IrExpr, MethodIdx, ReferenceTarget, ResolvedType, VariantIdx};
#[expect(
clippy::too_many_lines,
reason = "exhaustive walk over IrExpr variants"
)]
pub(super) fn resolve_expr(expr: &mut IrExpr, r: &mut FnResolver<'_>) {
match expr {
IrExpr::Literal { .. } | IrExpr::SelfFieldRef { .. } => {}
IrExpr::Reference {
path, target, ty, ..
} => {
*target = walkers::resolve_path(path, r);
// Promote a remaining `Unresolved` to a typed
// `UndefinedReference` error — but only when the upstream
// didn't already mark the reference's type as `Error`,
// which is how lowering signals "I already pushed a
// CompilerError for this site". Without the gate we'd
// double-count any unbound name.
if matches!(target, ReferenceTarget::Unresolved) && !matches!(ty, ResolvedType::Error) {
r.errors.push(CompilerError::UndefinedReference {
name: path.join("::"),
span: crate::location::Span::default(),
});
}
}
IrExpr::LetRef {
name, binding_id, ..
} => {
if let Some((id, _)) = r.lookup(name) {
*binding_id = id;
}
}
IrExpr::FunctionCall {
path,
function_id,
args,
..
} => {
if function_id.is_none() {
*function_id = walkers::resolve_function_call_id(path, r);
}
// DP-8 default substitution for forward-ref / cross-module
// calls happens in a second walk in `mod.rs` after all
// function bodies have been put back on `module.functions`
// so the IrFunctionParam.default lookups succeed.
for (_, arg) in args {
resolve_expr(arg, r);
}
}
IrExpr::CallClosure { closure, args, .. } => {
resolve_expr(closure, r);
for (_, arg) in args {
resolve_expr(arg, r);
}
}
IrExpr::MethodCall {
receiver,
method,
method_idx,
dispatch,
args,
..
} => {
if let Some(idx) = lookup_method_idx(dispatch, method, r.module) {
*method_idx = MethodIdx(idx);
}
resolve_expr(receiver, r);
for (_, arg) in args {
resolve_expr(arg, r);
}
}
IrExpr::FieldAccess {
object,
field,
field_idx,
..
} => {
resolve_expr(object, r);
if let Some(idx) = struct_field_idx(object.ty(), field, r.module) {
*field_idx = FieldIdx(idx);
}
}
IrExpr::Tuple { fields, .. } => {
for (_, fexpr) in fields {
resolve_expr(fexpr, r);
}
}
IrExpr::StructInst {
struct_id, fields, ..
} => {
for (name, idx, fexpr) in fields.iter_mut() {
resolve_expr(fexpr, r);
if let Some(sid) = struct_id {
if let Some(found) = r
.module
.get_struct(*sid)
.and_then(|s| s.fields.iter().position(|f| f.name == *name))
{
#[expect(
clippy::cast_possible_truncation,
reason = "field count is bounded upstream"
)]
let new_idx = FieldIdx(found as u32);
*idx = new_idx;
}
}
}
}
IrExpr::EnumInst {
enum_id,
variant,
variant_idx,
fields,
..
} => {
if let Some(eid) = enum_id {
if let Some(found) = r
.module
.get_enum(*eid)
.and_then(|e| e.variants.iter().position(|v| v.name == *variant))
{
#[expect(
clippy::cast_possible_truncation,
reason = "variant count is bounded upstream"
)]
let new_idx = VariantIdx(found as u32);
*variant_idx = new_idx;
}
}
for (fname, fidx, fexpr) in fields.iter_mut() {
resolve_expr(fexpr, r);
if let Some(eid) = enum_id {
if let Some(found_field) = r
.module
.get_enum(*eid)
.and_then(|e| {
e.variants
.iter()
.find(|v| v.name == *variant)
.map(|v| v.fields.iter().position(|f| f.name == *fname))
})
.flatten()
{
#[expect(
clippy::cast_possible_truncation,
reason = "field count is bounded upstream"
)]
let new_field_idx = FieldIdx(found_field as u32);
*fidx = new_field_idx;
}
}
}
}
IrExpr::Array { elements, .. } => {
for e in elements {
resolve_expr(e, r);
}
}
IrExpr::BinaryOp { left, right, .. } => {
resolve_expr(left, r);
resolve_expr(right, r);
}
IrExpr::UnaryOp { operand, .. } => {
resolve_expr(operand, r);
}
IrExpr::If {
condition,
then_branch,
else_branch,
..
} => {
resolve_expr(condition, r);
r.push_scope();
resolve_expr(then_branch, r);
r.pop_scope();
if let Some(eb) = else_branch.as_mut() {
r.push_scope();
resolve_expr(eb, r);
r.pop_scope();
}
}
IrExpr::For {
var,
var_binding_id,
collection,
body,
..
} => {
resolve_expr(collection, r);
r.push_scope();
let id = r.fresh();
*var_binding_id = id;
r.bind(var.clone(), id, BindingKind::Local);
resolve_expr(body, r);
r.pop_scope();
}
IrExpr::Match {
scrutinee, arms, ..
} => {
resolve_expr(scrutinee, r);
let scrutinee_ty = scrutinee.ty().clone();
for arm in arms {
resolve_match_arm(arm, &scrutinee_ty, r);
}
}
IrExpr::Closure {
params,
captures,
body,
..
} => {
// Capture binding-id resolution: each capture's
// `outer_binding_id` must point at the introducing
// binding *in the enclosing scope*, which we look up
// BEFORE pushing the closure's own scope frame.
for (cap_bid, cap_name, _, _) in captures.iter_mut() {
if let Some((id, _)) = r.lookup(cap_name) {
*cap_bid = id;
}
}
r.push_scope();
for (_, param_bid, name, _) in params.iter_mut() {
let id = r.fresh();
*param_bid = id;
r.bind(name.clone(), id, BindingKind::Local);
}
resolve_expr(body, r);
r.pop_scope();
}
IrExpr::ClosureRef { env_struct, .. } => {
resolve_expr(env_struct, r);
}
IrExpr::DictLiteral { entries, .. } => {
for (k, v) in entries {
resolve_expr(k, r);
resolve_expr(v, r);
}
}
IrExpr::DictAccess { dict, key, .. } => {
resolve_expr(dict, r);
resolve_expr(key, r);
}
IrExpr::Block {
statements, result, ..
} => {
r.push_scope();
for stmt in statements {
walkers::resolve_block_stmt(stmt, r);
}
resolve_expr(result, r);
r.pop_scope();
}
}
}