From 1befb0bac0310b8585a244eb009e7f795c293c53 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 13 Nov 2025 03:50:17 -0700 Subject: [PATCH 1/3] fix: Only add extra padding to the first group's last file --- compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs | 2 ++ tests/rustdoc-ui/doctest/main-alongside-macro-calls.fail.stdout | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index 5b1fffd21d184..f26bf31a1aead 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -213,6 +213,7 @@ impl AnnotateSnippetEmitter { file_ann.swap(0, pos); } + let file_ann_len = file_ann.len(); for (file_idx, (file, annotations)) in file_ann.into_iter().enumerate() { if should_show_source_code(&self.ignored_directories_in_source_blocks, sm, &file) { if let Some(snippet) = self.annotated_snippet(annotations, &file.name, sm) { @@ -240,6 +241,7 @@ impl AnnotateSnippetEmitter { // ╰ warning: this was previously accepted if let Some(c) = children.first() && (!c.span.has_primary_spans() && !c.span.has_span_labels()) + && file_idx == file_ann_len - 1 { group = group.element(Padding); } diff --git a/tests/rustdoc-ui/doctest/main-alongside-macro-calls.fail.stdout b/tests/rustdoc-ui/doctest/main-alongside-macro-calls.fail.stdout index 1048db07ae95a..65989a8ef47c7 100644 --- a/tests/rustdoc-ui/doctest/main-alongside-macro-calls.fail.stdout +++ b/tests/rustdoc-ui/doctest/main-alongside-macro-calls.fail.stdout @@ -19,7 +19,6 @@ LL | println!(); error: macro expansion ignores `{` and any tokens following --> $SRC_DIR/std/src/macros.rs:LL:COL | - | ::: $DIR/main-alongside-macro-calls.rs:30:1 | LL | println!(); @@ -42,7 +41,6 @@ LL | println!(); error: macro expansion ignores `{` and any tokens following --> $SRC_DIR/std/src/macros.rs:LL:COL | - | ::: $DIR/main-alongside-macro-calls.rs:34:1 | LL | println!(); From ec17d9ea7cfbe6c69c14f40bff651d0ba8fb5671 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 13 Nov 2025 03:50:17 -0700 Subject: [PATCH 2/3] fix: Sort annotations by line number in AnnotateSnippetEmitter --- .../src/annotate_snippet_emitter_writer.rs | 8 ++++++++ tests/ui/statics/check-values-constraints.stderr | 4 ++-- tests/ui/typeck/typeck_type_placeholder_item.stderr | 12 ++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index f26bf31a1aead..f7f78d2b28520 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -742,6 +742,14 @@ fn collect_annotations( } } } + + // Sort annotations within each file by line number + for (_, ann) in output.iter_mut() { + ann.sort_by_key(|a| { + let lo = sm.lookup_char_pos(a.span.lo()); + lo.line + }); + } output } diff --git a/tests/ui/statics/check-values-constraints.stderr b/tests/ui/statics/check-values-constraints.stderr index f6fa8df45e5e4..c54f4830533ae 100644 --- a/tests/ui/statics/check-values-constraints.stderr +++ b/tests/ui/statics/check-values-constraints.stderr @@ -38,10 +38,10 @@ LL | field2: SafeEnum::Variant4("str".to_string()), note: method `to_string` is not const because trait `ToString` is not const --> $SRC_DIR/alloc/src/string.rs:LL:COL | - = note: this method is not const + = note: this trait is not const ::: $SRC_DIR/alloc/src/string.rs:LL:COL | - = note: this trait is not const + = note: this method is not const = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 747d032dfd4be..5820078d8329b 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -686,12 +686,12 @@ LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); | note: method `filter` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL - ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | - = note: this method is not const + = note: this trait is not const + ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | - = note: this trait is not const + = note: this method is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method `, {closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}> as Iterator>::map::` in constants @@ -702,12 +702,12 @@ LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); | note: method `map` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL - ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | - = note: this method is not const + = note: this trait is not const + ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | - = note: this trait is not const + = note: this method is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 83 previous errors From 83485b94a03ae6cae525eaa00679b0d10cb80330 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Thu, 13 Nov 2025 03:50:17 -0700 Subject: [PATCH 3/3] fix: Only show one origin for multiline annotations with no source --- compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs | 2 +- tests/ui/typeck/typeck_type_placeholder_item.stderr | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index f7f78d2b28520..7f64dc3df23ed 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -633,7 +633,7 @@ impl AnnotateSnippetEmitter { report.push(std::mem::replace(&mut group, Group::with_level(level.clone()))); } - if !line_tracker.contains(&lo.line) { + if !line_tracker.contains(&lo.line) && (i == 0 || hi.line <= lo.line) { line_tracker.push(lo.line); // ╭▸ $SRC_DIR/core/src/option.rs:594:0 (<- It adds *this*) // ⸬ $SRC_DIR/core/src/option.rs:602:4 diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 5820078d8329b..0b70ac97fd435 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -688,7 +688,6 @@ note: method `filter` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this trait is not const - ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const @@ -704,7 +703,6 @@ note: method `map` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this trait is not const - ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const