Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions ndc_analyser/src/analyser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ impl Analyser {
/// the rest of the program is still checked. An error in a generic
/// function's body, found by a copy of its constraints at a call or by
/// the instance a call needs, is the call's: it's reported there, with
/// notes pointing into the function. The same error is reported once.
/// notes pointing into the function. The same error is reported once,
/// and so is an error a call already got for another place in the
/// function, with a note at each: in `fn f(t) => (t[0], t[1]); f(1)`,
/// `f(1)` gets one error that `[]` can't index an Int, with notes at
/// `t[0]` and `t[1]`.
fn emit(&mut self, err: AnalysisError) {
let mut calls = self
.recording
Expand All @@ -384,9 +388,14 @@ impl Analyser {
.flat_map(|frames| frames.iter())
.map(|frame| (frame.name.clone(), frame.call)),
);
let through_calls = !calls.is_empty();
let err = err.through_calls(calls.iter().map(|(name, call)| (name.as_str(), *call)));
if !self.result.errors.contains(&err) {
self.result.errors.push(err);
let known = self.result.errors.iter_mut().find(|known| {
**known == err || (through_calls && known.says_the_same_at_the_same_place(&err))
});
match known {
Some(known) => known.add_notes_of(err),
None => self.result.errors.push(err),
}
}

Expand Down
19 changes: 19 additions & 0 deletions ndc_analyser/src/analyser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,25 @@ fn an_error_in_a_generic_body_is_the_outermost_calls() {
);
}

#[test]
fn one_call_gets_one_error_for_each_message_with_a_note_at_each_place() {
let source = "fn f(x) { x + 1; x + 2 } f(\"a\");";
let (_, result) = analyse_with_globals(source, ambiguous_plus());
let [error] = &result.errors[..] else {
panic!("one error expected: {:?}", result.errors);
};
let text = |span: Span| &source[span.range()];
assert_eq!(text(error.span()), "f(\"a\")");
assert_eq!(
error
.notes()
.iter()
.map(|(span, note)| (text(*span), note.as_str()))
.collect::<Vec<_>>(),
vec![("x + 1", "`f` needs this"), ("x + 2", "`f` needs this")]
);
}

// Each of two values that share no type is labelled with its type: here
// the returns of a generic function, found while checking a call of it.
#[test]
Expand Down
18 changes: 18 additions & 0 deletions ndc_analyser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ impl AnalysisError {
self
}

/// Whether `other` says what this error says, where it says it, but for
/// its notes.
pub(crate) fn says_the_same_at_the_same_place(&self, other: &Self) -> bool {
self.text == other.text
&& self.span == other.span
&& self.label == other.label
&& self.help_text == other.help_text
}

/// Adds the notes of `other` that this error doesn't have yet.
pub(crate) fn add_notes_of(&mut self, other: Self) {
for note in other.notes {
if !self.notes.contains(&note) {
self.notes.push(note);
}
}
}

/// What a function that returns a set, where a function returning any
/// kind of sequence is expected, is told.
pub(crate) const SET_RETURNED_AS_SEQUENCE: &str = "a function that returns a set doesn't give its elements here: return `.keys()`, a list of them";
Expand Down
Loading