Skip to content

Commit 5cb0a2a

Browse files
committed
Tidy up name suggestions
Breaks up the long, vertical method chain into some separate definitions, and tweaks the field names in the related messages for clarity.
1 parent dab98d9 commit 5cb0a2a

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

fathom/src/surface/elaboration.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,20 +1330,16 @@ impl<'arena> Context<'arena> {
13301330
return (core::Term::Prim(file_range.into(), prim), r#type.clone());
13311331
}
13321332

1333-
let candidates = self
1334-
.local_env
1335-
.names
1336-
.iter()
1337-
.flatten()
1338-
.copied()
1339-
.chain(self.item_env.names.iter().copied());
1340-
let suggestion = suggest_name(*name, candidates);
1341-
13421333
self.push_message(Message::UnboundName {
13431334
range: file_range,
13441335
name: *name,
1345-
suggestion,
1336+
suggested_name: {
1337+
let item_names = self.item_env.names.iter().copied();
1338+
let local_names = self.local_env.names.iter().flatten().copied();
1339+
suggest_name(*name, item_names.chain(local_names))
1340+
},
13461341
});
1342+
13471343
self.synth_reported_error(*range)
13481344
}
13491345
Term::Hole(_, name) => {
@@ -1660,7 +1656,7 @@ impl<'arena> Context<'arena> {
16601656
head_type: self.pretty_value(&head_type),
16611657
label_range: self.file_range(*label_range),
16621658
label: *proj_label,
1663-
suggestion: suggest_name(*proj_label, labels.iter().map(|(_, l)| *l)),
1659+
suggested_label: suggest_name(*proj_label, labels.iter().map(|(_, l)| *l)),
16641660
});
16651661
return self.synth_reported_error(*range);
16661662
}

fathom/src/surface/elaboration/reporting.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum Message {
1515
UnboundName {
1616
range: FileRange,
1717
name: Symbol,
18-
suggestion: Option<Symbol>,
18+
suggested_name: Option<Symbol>,
1919
},
2020
RefutablePattern {
2121
pattern_range: FileRange,
@@ -47,7 +47,7 @@ pub enum Message {
4747
head_type: String,
4848
label_range: FileRange,
4949
label: Symbol,
50-
suggestion: Option<Symbol>,
50+
suggested_label: Option<Symbol>,
5151
},
5252
MismatchedFieldLabels {
5353
range: FileRange,
@@ -148,21 +148,16 @@ impl Message {
148148
Message::UnboundName {
149149
range,
150150
name,
151-
suggestion,
151+
suggested_name,
152152
} => {
153153
let name = name.resolve();
154154

155-
let mut diagnostic = Diagnostic::error()
155+
Diagnostic::error()
156156
.with_message(format!("cannot find `{name}` in scope"))
157-
.with_labels(vec![primary_label(range).with_message("unbound name")]);
158-
159-
if let Some(suggestion) = suggestion {
160-
diagnostic = diagnostic.with_notes(vec![format!(
161-
"help: did you mean `{}`?",
162-
suggestion.resolve()
163-
)])
164-
}
165-
diagnostic
157+
.with_labels(vec![primary_label(range).with_message("unbound name")])
158+
.with_notes(suggested_name.map_or(Vec::new(), |name| {
159+
vec![format!("help: did you mean `{}`?", name.resolve())]
160+
}))
166161
}
167162
Message::RefutablePattern { pattern_range } => Diagnostic::error()
168163
.with_message("refutable patterns found in binding")
@@ -219,24 +214,20 @@ impl Message {
219214
head_type,
220215
label_range,
221216
label,
222-
suggestion,
217+
suggested_label,
223218
} => {
224219
let label = label.resolve();
225220

226-
let mut diagnostic = Diagnostic::error()
221+
Diagnostic::error()
227222
.with_message(format!("cannot find `{label}` in expression"))
228223
.with_labels(vec![
229224
primary_label(label_range).with_message("unknown label"),
230225
secondary_label(head_range)
231226
.with_message(format!("expression of type {head_type}")),
232-
]);
233-
if let Some(suggestion) = suggestion {
234-
diagnostic = diagnostic.with_notes(vec![format!(
235-
"help: did you mean `{}`?",
236-
suggestion.resolve()
237-
)]);
238-
}
239-
diagnostic
227+
])
228+
.with_notes(suggested_label.map_or(Vec::new(), |label| {
229+
vec![format!("help: did you mean `{}`?", label.resolve())]
230+
}))
240231
}
241232
Message::MismatchedFieldLabels {
242233
range,

0 commit comments

Comments
 (0)