From e723fc302d424811d18b2ab8d65f598c12607863 Mon Sep 17 00:00:00 2001 From: Tim Fennis Date: Sun, 29 Nov 2026 14:12:21 +0100 Subject: [PATCH] =?UTF-8?q?feat(analyser):=20give=20a=20call=20one=20error?= =?UTF-8?q?=20per=20message,=20with=20a=20note=20at=20each=20place=20?= =?UTF-8?q?=F0=9F=93=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- ndc_analyser/src/analyser/mod.rs | 15 ++++++++++++--- ndc_analyser/src/analyser/tests.rs | 19 +++++++++++++++++++ ndc_analyser/src/error.rs | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/ndc_analyser/src/analyser/mod.rs b/ndc_analyser/src/analyser/mod.rs index c8e7ec75..c67aade8 100644 --- a/ndc_analyser/src/analyser/mod.rs +++ b/ndc_analyser/src/analyser/mod.rs @@ -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 @@ -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), } } diff --git a/ndc_analyser/src/analyser/tests.rs b/ndc_analyser/src/analyser/tests.rs index a882ed0c..a2e09dd2 100644 --- a/ndc_analyser/src/analyser/tests.rs +++ b/ndc_analyser/src/analyser/tests.rs @@ -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![("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] diff --git a/ndc_analyser/src/error.rs b/ndc_analyser/src/error.rs index 9624a336..e39bc720 100644 --- a/ndc_analyser/src/error.rs +++ b/ndc_analyser/src/error.rs @@ -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(¬e) { + 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";