Skip to content

Commit

Permalink
Tidy up name suggestions
Browse files Browse the repository at this point in the history
Breaks up the long, vertical method chain into some separate
definitions, and tweaks the field names in the related messages for
clarity.
  • Loading branch information
brendanzab committed Feb 9, 2023
1 parent dab98d9 commit 9ed1c4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
18 changes: 7 additions & 11 deletions fathom/src/surface/elaboration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,20 +1330,16 @@ impl<'arena> Context<'arena> {
return (core::Term::Prim(file_range.into(), prim), r#type.clone());
}

let candidates = self
.local_env
.names
.iter()
.flatten()
.copied()
.chain(self.item_env.names.iter().copied());
let suggestion = suggest_name(*name, candidates);

self.push_message(Message::UnboundName {
range: file_range,
name: *name,
suggestion,
suggested_name: {
let item_names = self.item_env.names.iter().copied();
let local_names = self.local_env.names.iter().flatten().copied();
suggest_name(*name, item_names.chain(local_names))
},
});

self.synth_reported_error(*range)
}
Term::Hole(_, name) => {
Expand Down Expand Up @@ -1660,7 +1656,7 @@ impl<'arena> Context<'arena> {
head_type: self.pretty_value(&head_type),
label_range: self.file_range(*label_range),
label: *proj_label,
suggestion: suggest_name(*proj_label, labels.iter().map(|(_, l)| *l)),
suggested_label: suggest_name(*proj_label, labels.iter().map(|(_, l)| *l)),
});
return self.synth_reported_error(*range);
}
Expand Down
26 changes: 12 additions & 14 deletions fathom/src/surface/elaboration/reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Message {
UnboundName {
range: FileRange,
name: Symbol,
suggestion: Option<Symbol>,
suggested_name: Option<Symbol>,
},
RefutablePattern {
pattern_range: FileRange,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub enum Message {
head_type: String,
label_range: FileRange,
label: Symbol,
suggestion: Option<Symbol>,
suggested_label: Option<Symbol>,
},
MismatchedFieldLabels {
range: FileRange,
Expand Down Expand Up @@ -148,19 +148,17 @@ impl Message {
Message::UnboundName {
range,
name,
suggestion,
suggested_name,
} => {
let name = name.resolve();

let mut diagnostic = Diagnostic::error()
.with_message(format!("cannot find `{name}` in scope"))
.with_labels(vec![primary_label(range).with_message("unbound name")]);

if let Some(suggestion) = suggestion {
diagnostic = diagnostic.with_notes(vec![format!(
"help: did you mean `{}`?",
suggestion.resolve()
)])
if let Some(name) = suggested_name {
diagnostic = diagnostic
.with_notes(vec![format!("help: did you mean `{}`?", name.resolve())])
}
diagnostic
}
Expand Down Expand Up @@ -219,7 +217,7 @@ impl Message {
head_type,
label_range,
label,
suggestion,
suggested_label,
} => {
let label = label.resolve();

Expand All @@ -230,12 +228,12 @@ impl Message {
secondary_label(head_range)
.with_message(format!("expression of type {head_type}")),
]);
if let Some(suggestion) = suggestion {
diagnostic = diagnostic.with_notes(vec![format!(
"help: did you mean `{}`?",
suggestion.resolve()
)]);

if let Some(label) = suggested_label {
diagnostic = diagnostic
.with_notes(vec![format!("help: did you mean `{}`?", label.resolve())]);
}

diagnostic
}
Message::MismatchedFieldLabels {
Expand Down

0 comments on commit 9ed1c4a

Please sign in to comment.