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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Top-level driver: declaration pre-passes and the program walk.
//!
//! `check_inner` is the canonical entry point — every public `check*`
//! method on `TypeChecker` funnels through here. The two `register_*`
//! helpers run before the main walk so forward references and
//! declaration order don't trip the strict cross-module undefined-name
//! check.
use crate::ast::*;
use super::super::scope::{
EnumDeclInfo, FnSignature, ImplMethodSig, InterfaceDeclInfo, StructDeclInfo, TypeAliasInfo,
TypeScope,
};
use super::super::{InlayHintInfo, TypeChecker, TypeDiagnostic};
impl TypeChecker {
pub(in crate::typechecker) fn check_inner(
mut self,
program: &[SNode],
) -> (Vec<TypeDiagnostic>, Vec<InlayHintInfo>) {
Self::register_declarations_into(&mut self.scope, &self.imported_type_decls);
// First pass: collect declarations (type/enum/struct/interface) into scope
// before type-checking bodies so forward references resolve.
Self::register_declarations_into(&mut self.scope, program);
for snode in program {
if let Node::Pipeline { body, .. } = &snode.node {
Self::register_declarations_into(&mut self.scope, body);
}
}
// Pre-register every top-level `fn`/`pipeline`/`tool` name so a
// caller earlier in the file can reference a callable defined
// later without the strict cross-module check falsely flagging
// it as undefined. Signatures populated here are overwritten
// when the body is actually walked; this pass only needs the
// name to exist so `check_call`'s resolvability check passes.
Self::register_callable_placeholders(&mut self.scope, program);
// Pre-pass: index `@deprecated` attributes on top-level fn decls so
// `check_call` (and the standalone deprecation visitor below) can
// flag callers anywhere in the program.
for snode in program {
if let Node::AttributedDecl { attributes, inner } = &snode.node {
if let Node::FnDecl { name, .. } = &inner.node {
for attr in attributes {
if attr.name == "deprecated" {
let since = attr.string_arg("since");
let use_hint = attr.string_arg("use");
self.deprecated_fns.insert(name.clone(), (since, use_hint));
}
}
}
}
}
// Walk every node looking for FunctionCalls of deprecated names.
// This catches calls in contexts (e.g. `let x = old_fn()`) where
// `check_node`'s FunctionCall arm doesn't fire because the value
// is inferred rather than checked.
if !self.deprecated_fns.is_empty() {
for snode in program {
self.visit_for_deprecation(snode);
}
}
for snode in program {
// Transparently process attributed wrappers around top-level
// declarations. Attribute-specific semantics (deprecation,
// unknown-attribute warnings) were already applied above and
// by `check_attributes`.
let inner_node = match &snode.node {
Node::AttributedDecl { inner, .. } => inner.as_ref(),
_ => snode,
};
match &inner_node.node {
Node::Pipeline {
params,
return_type,
body,
..
} => {
let mut child = self.scope.child();
for p in params {
child.define_var(p, None);
child.clear_nil_widenable(p);
}
self.fn_depth += 1;
let ret_scope_base = return_type.as_ref().map(|_| child.child());
self.check_block(body, &mut child);
if let (Some(ret_type), Some(mut ret_scope)) =
(return_type.as_ref(), ret_scope_base)
{
for stmt in body {
self.check_return_type(stmt, ret_type, &mut ret_scope);
}
}
self.fn_depth -= 1;
}
Node::FnDecl {
name,
type_params,
params,
return_type,
where_clauses,
body,
..
} => {
let required_params =
params.iter().filter(|p| p.default_value.is_none()).count();
let sig = FnSignature {
params: params
.iter()
.map(|p| (p.name.clone(), p.type_expr.clone()))
.collect(),
return_type: return_type.clone(),
type_param_names: type_params.iter().map(|tp| tp.name.clone()).collect(),
required_params,
where_clauses: where_clauses
.iter()
.map(|wc| (wc.type_name.clone(), wc.bound.clone()))
.collect(),
has_rest: params.last().is_some_and(|p| p.rest),
};
self.scope.define_fn(name, sig);
self.check_fn_body(type_params, params, return_type, body, where_clauses);
}
_ => {
let mut scope = self.scope.clone();
self.check_node(snode, &mut scope);
// Promote top-level definitions out of the temporary scope.
for (name, ty) in scope.vars {
self.scope.vars.entry(name).or_insert(ty);
}
for name in scope.mutable_vars {
self.scope.mutable_vars.insert(name);
}
for (name, enabled) in scope.nil_widenable_vars {
self.scope.nil_widenable_vars.insert(name, enabled);
}
}
}
}
(self.diagnostics, self.hints)
}
/// Pre-populate placeholder signatures for every
/// `fn`/`pipeline`/`tool`/`let`/`var` name reachable from the
/// program (including names defined inside pipeline or fn bodies)
/// so the strict cross-module undefined-call check can resolve
/// forward references and recursive calls whose own scope does not
/// inherit from the enclosing block.
///
/// Rust's lexical scoping guarantees the runtime lookup will still
/// respect shadowing at execution time; the placeholders only
/// satisfy the *static* "does this name exist somewhere" check.
fn register_callable_placeholders(scope: &mut TypeScope, nodes: &[SNode]) {
fn walk(scope: &mut TypeScope, node: &SNode) {
let inner = match &node.node {
Node::AttributedDecl { inner, .. } => inner.as_ref(),
_ => node,
};
match &inner.node {
Node::FnDecl {
name,
params,
return_type,
type_params,
where_clauses,
body,
..
} => {
let sig = FnSignature {
params: params
.iter()
.map(|p| (p.name.clone(), p.type_expr.clone()))
.collect(),
return_type: return_type.clone(),
type_param_names: type_params.iter().map(|tp| tp.name.clone()).collect(),
required_params: params
.iter()
.filter(|p| p.default_value.is_none())
.count(),
where_clauses: where_clauses
.iter()
.map(|wc| (wc.type_name.clone(), wc.bound.clone()))
.collect(),
has_rest: params.last().is_some_and(|p| p.rest),
};
scope.define_fn(name, sig);
walk_all(scope, body);
}
Node::Pipeline { name, body, .. } => {
let sig = FnSignature {
params: Vec::new(),
return_type: None,
type_param_names: Vec::new(),
required_params: 0,
where_clauses: Vec::new(),
has_rest: false,
};
scope.define_fn(name, sig);
walk_all(scope, body);
}
Node::ToolDecl { name, body, .. } => {
let sig = FnSignature {
params: Vec::new(),
return_type: None,
type_param_names: Vec::new(),
required_params: 0,
where_clauses: Vec::new(),
has_rest: false,
};
scope.define_fn(name, sig);
walk_all(scope, body);
}
Node::SkillDecl { name, .. } => {
scope.define_var(name, None);
scope.clear_nil_widenable(name);
}
Node::LetBinding { pattern, .. } | Node::VarBinding { pattern, .. } => {
// Only bare-identifier patterns at module scope
// need forward-ref placeholders; destructuring
// patterns are checked as statements and define
// their vars as they are walked.
if let BindingPattern::Identifier(name) = pattern {
if !crate::ast::is_discard_name(name) {
scope.define_var(name, None);
scope.clear_nil_widenable(name);
}
}
}
_ => {}
}
}
fn walk_all(scope: &mut TypeScope, nodes: &[SNode]) {
for node in nodes {
walk(scope, node);
}
}
walk_all(scope, nodes);
}
/// Register type, enum, interface, and struct declarations from AST nodes into a scope.
fn register_declarations_into(scope: &mut TypeScope, nodes: &[SNode]) {
for snode in nodes {
match &snode.node {
Node::TypeDecl {
name,
type_params,
type_expr,
} => {
scope.type_aliases.insert(
name.clone(),
TypeAliasInfo {
type_params: type_params.clone(),
body: type_expr.clone(),
},
);
}
Node::EnumDecl {
name,
type_params,
variants,
..
} => {
scope.enums.insert(
name.clone(),
EnumDeclInfo {
type_params: type_params.clone(),
variants: variants.clone(),
},
);
}
Node::InterfaceDecl {
name,
type_params,
associated_types,
methods,
} => {
scope.interfaces.insert(
name.clone(),
InterfaceDeclInfo {
type_params: type_params.clone(),
associated_types: associated_types.clone(),
methods: methods.clone(),
},
);
}
Node::StructDecl {
name,
type_params,
fields,
..
} => {
scope.structs.insert(
name.clone(),
StructDeclInfo {
type_params: type_params.clone(),
fields: fields.clone(),
},
);
}
Node::ImplBlock {
type_name, methods, ..
} => {
let sigs: Vec<ImplMethodSig> = methods
.iter()
.filter_map(|m| {
if let Node::FnDecl {
name,
params,
return_type,
..
} = &m.node
{
let non_self: Vec<_> =
params.iter().filter(|p| p.name != "self").collect();
let param_count = non_self.len();
let param_types: Vec<Option<TypeExpr>> =
non_self.iter().map(|p| p.type_expr.clone()).collect();
Some(ImplMethodSig {
name: name.clone(),
param_count,
param_types,
return_type: return_type.clone(),
})
} else {
None
}
})
.collect();
scope.impl_methods.insert(type_name.clone(), sigs);
}
_ => {}
}
}
}
}