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
//! The `Dynamic` discipline: every value is `any`; the only errors are the
//! ones that are certain whatever the run-time values (profile toggle
//! `certain_errors`): calling a function of this unit with arguments that
//! cannot match its parameters, a constructor or constructor pattern of the
//! wrong length, and a record literal naming a field its record does not
//! declare.
//!
//! No annotation is lowered (annotations are hints for the runtime), so a
//! dynamic check never reports a type error.
use hir_lang::{
ArgKind, Def, Expr, ExprId, ItemKind, NodeRef, Pat, PathId, Res, Shape, Stmt, VariantId,
};
use super::{Ctx, call::CallTarget, index::FieldOwner};
use crate::{diag::DiagnosticKind, error::CheckError, typed::NONE, types::TypeId};
/// What a call's callee names, when that is certain.
enum Known {
Fn(hir_lang::ItemId),
/// A tuple-shaped record or variant constructor with this many fields.
Ctor(usize),
}
impl Ctx<'_> {
pub(crate) fn check_dynamic(&mut self) -> Result<(), CheckError> {
let any = TypeId::ANY.raw();
let hir = self.hir;
let exprs = core::mem::take(&mut self.idx.exprs);
let pats = core::mem::take(&mut self.idx.pats);
for &e in &exprs {
self.meter.tick(1)?;
if let Some(slot) = self.expr_ty.get_mut(e.index()) {
*slot = any;
}
// Binders that are not bound by patterns.
let mut bind = |b: hir_lang::BinderId| {
if let Some(slot) = self.binder_ty.get_mut(b.index()) {
*slot = any;
}
};
match *hir.expr(e) {
Expr::LetPlace { binder, .. } => bind(binder),
Expr::Closure(c) => {
hir.list(c.captures).iter().for_each(|cap| bind(cap.binder));
c.self_binder.into_iter().for_each(&mut bind);
}
Expr::Block(b) => {
for &s in hir.list(b.stmts) {
if let Stmt::Static { binder, .. } | Stmt::Global { binder, .. } =
*hir.stmt(s)
{
bind(binder);
}
}
}
_ => {}
}
}
for &p in &pats {
self.meter.tick(1)?;
if let Some(slot) = self.pat_ty.get_mut(p.index()) {
*slot = any;
}
if let Pat::Bind { binder, .. } | Pat::Ident { binder, .. } = *hir.pat(p) {
if let Some(slot) = self.binder_ty.get_mut(binder.index()) {
*slot = any;
}
}
}
// Tails of partial paths still resolve by type where the prefix is
// a declaration of this unit (`Shape::Circle`, `Point::new`).
for &e in &exprs {
if let Expr::Path(p) = *hir.expr(e) {
self.dynamic_tail(p);
}
}
for &p in &pats {
if let Pat::Ctor { path, .. }
| Pat::Path(path)
| Pat::Record {
path: Some(path), ..
} = *hir.pat(p)
{
self.dynamic_tail(path);
}
}
if self.profile.certain_errors {
for &e in &exprs {
self.meter.tick(1)?;
self.certain_expr(e)?;
}
for &p in &pats {
self.meter.tick(1)?;
self.certain_pat(p);
}
}
self.idx.exprs = exprs;
self.idx.pats = pats;
Ok(())
}
/// Resolves a partial path's tail silently, when its prefix is a sum,
/// record, or class of this unit.
fn dynamic_tail(&mut self, p: PathId) {
let path = *self.hir.path(p);
if path.unresolved != 1 || path.qself.is_some() {
return;
}
let Some(owner) = self.local_item(path.res) else {
return;
};
let Some(last) = self.hir.list(path.segments).last().copied() else {
return;
};
let name = last.name.sym;
if matches!(self.item_kind(owner), ItemKind::Sum(_)) {
if let Some(v) = self.idx.variant_named(owner, name) {
self.record_path_def(p, Def::Variant(v));
return;
}
}
let t = self.types.adt(owner);
if let [one] = self.idx.members_named(t, name) {
let item = one.item;
self.record_path_def(p, Def::Item(item));
}
}
/// The variant or tuple record a path certainly names.
fn certain_ctor(&self, p: PathId) -> Option<usize> {
let path = self.hir.path(p);
let variant = |v: VariantId| {
self.hir
.variant(v)
.filter(|x| x.shape == Shape::Tuple)
.map(|x| x.fields.len())
};
if path.unresolved != 0 {
return match self.path_def.get(p.index()).copied().flatten() {
Some(Def::Variant(v)) => variant(v),
_ => None,
};
}
match path.res {
Res::Def(d) if d.unit() == self.hir.unit() => match d.def() {
Def::Variant(v) => variant(v),
Def::Item(i) => match self.item_kind(i) {
ItemKind::Record(r) if r.shape == Shape::Tuple => Some(r.fields.len()),
_ => None,
},
},
_ => None,
}
}
fn certain_callee(&self, callee: ExprId) -> Option<Known> {
let Expr::Path(p) = *self.hir.expr(callee) else {
return None;
};
if let Some(n) = self.certain_ctor(p) {
return Some(Known::Ctor(n));
}
let path = self.hir.path(p);
let item = if path.unresolved == 0 {
self.local_item(path.res)?
} else {
match self.path_def.get(p.index()).copied().flatten()? {
Def::Item(i) => i,
Def::Variant(_) => return None,
}
};
(matches!(self.item_kind(item), ItemKind::Fn(_)) && !self.idx.is_generic(item))
.then_some(Known::Fn(item))
}
fn certain_expr(&mut self, e: ExprId) -> Result<(), CheckError> {
match *self.hir.expr(e) {
Expr::Call { callee, args } => {
let list = self.hir.list(args);
// A spread supplies an unknown number of arguments.
if list
.iter()
.any(|a| matches!(a.kind, ArgKind::Spread | ArgKind::SpreadNamed))
{
return Ok(());
}
match self.certain_callee(callee) {
Some(Known::Fn(item)) => {
if let Some(sig) = self.sig(item)? {
let target = CallTarget::Sig {
sig: u32::try_from(sig).unwrap_or(NONE),
skip: 0,
method: None,
deref: 0,
};
self.match_args(e, target, args, true)?;
}
}
Some(Known::Ctor(n)) => {
let mut positional = 0u32;
for a in list {
match a.kind {
ArgKind::Named(id) => self.report(
NodeRef::Expr(a.value),
DiagnosticKind::UnknownNamedArg { name: id.sym },
),
_ => positional += 1,
}
}
let n = u32::try_from(n).unwrap_or(NONE);
if positional != n {
self.report(
NodeRef::Expr(e),
DiagnosticKind::Arity {
min: n,
max: Some(n),
found: positional,
},
);
}
}
None => {}
}
}
Expr::Record {
path: Some(p),
fields,
..
} => {
let owner = match self.certain_record(p) {
Some(o) => o,
None => return Ok(()),
};
for fi in self.hir.list(fields) {
if self.idx.field_named(owner.0, fi.name.sym).is_none() {
self.report(
NodeRef::Expr(e),
DiagnosticKind::UnknownField {
ty: owner.1,
name: fi.name.sym,
},
);
}
}
}
_ => {}
}
Ok(())
}
/// A record (not a class: classes take dynamic properties) or variant
/// with named fields that a path certainly names.
fn certain_record(&mut self, p: PathId) -> Option<(FieldOwner, TypeId)> {
let path = *self.hir.path(p);
let def = if path.unresolved == 0 {
match path.res {
Res::Def(d) if d.unit() == self.hir.unit() => d.def(),
_ => return None,
}
} else {
self.path_def.get(p.index()).copied().flatten()?
};
match def {
Def::Item(i) => match self.item_kind(i) {
ItemKind::Record(r) if r.shape == Shape::Named && !self.idx.is_generic(i) => {
let raw = u32::try_from(i.index()).ok()?;
self.name_item(i);
Some((FieldOwner::Item(raw), self.types.adt(i)))
}
_ => None,
},
Def::Variant(v) => {
let named = self.hir.variant(v).is_some_and(|x| x.shape == Shape::Named);
let sum = self.hir.variant_owner(v)?;
let raw = u32::try_from(v.index()).ok()?;
self.name_item(sum);
named.then(|| (FieldOwner::Variant(raw), self.types.adt(sum)))
}
}
}
fn certain_pat(&mut self, p: hir_lang::PatId) {
let Pat::Ctor { path, elems, rest } = *self.hir.pat(p) else {
return;
};
let Some(n) = self.certain_ctor(path) else {
return;
};
let k = elems.len();
let fits = if rest.is_some() { k <= n } else { k == n };
if !fits {
let want = u32::try_from(n).unwrap_or(NONE);
self.report(
NodeRef::Pat(p),
DiagnosticKind::Arity {
min: want,
max: Some(want),
found: u32::try_from(k).unwrap_or(NONE),
},
);
}
}
}