Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,20 @@ fn shrink_file(
) -> Option<(Span, String, usize)> {
let lo_byte = spans.iter().map(|s| s.lo()).min()?;
let lo_loc = sm.lookup_char_pos(lo_byte);
let lo = lo_loc.file.line_bounds(lo_loc.line.saturating_sub(1)).start;

let hi_byte = spans.iter().map(|s| s.hi()).max()?;
let hi_loc = sm.lookup_char_pos(hi_byte);
let hi = lo_loc.file.line_bounds(hi_loc.line.saturating_sub(1)).end;

if lo_loc.file.name != hi_loc.file.name {
// this may happen when spans cross file boundaries due to macro expansion.
return None;
}

let lo = lo_loc.file.line_bounds(lo_loc.line.saturating_sub(1)).start;
let hi = hi_loc.file.line_bounds(hi_loc.line.saturating_sub(1)).end;

let bounding_span = Span::with_root_ctxt(lo, hi);
let source = sm.span_to_snippet(bounding_span).unwrap_or_default();
let source = sm.span_to_snippet(bounding_span).ok()?;
let offset_line = sm.doctest_offset_line(file_name, lo_loc.line);

Some((bounding_span, source, offset_line))
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/delegation/ice-line-bounds-issue-148732.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
reuse a as b {
//~^ ERROR cannot find function `a` in this scope
//~| ERROR functions delegation is not yet fully implemented
dbg!(b);
//~^ ERROR missing lifetime specifier
}

fn main() {}
37 changes: 37 additions & 0 deletions tests/ui/delegation/ice-line-bounds-issue-148732.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
WARN rustc_errors::emitter Invalid span $SRC_DIR/std/src/macros.rs:LL:COL (#4), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/std/src/macros.rs" }) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have worries that this will not pass aarch64-msvc-1 the same way it was in my PR

cc: #general > Windows path normalization in tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, it do have the same issue.

Copy link
Member Author

@chenyukang chenyukang Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, I read the discussion on zulip, seems there is another standalone bug. so is it a temprary way we skip Windows for this testcase(and add it back after that bug is fixed)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These warnings are coming from this call to is_different. The reason this is happening with AnnotateSnippetEmitter and not HumanEmitter is because HumanEmitter checks for source code availability before it calls is_different. Changing lines 353 to 368 in annotate_snippet_emitter_writer.rs to the following fixes the issue:

let lo = subst.parts.iter().map(|part| part.span.lo()).min()?;
let lo_file = sm.lookup_source_file(lo);
let hi = subst.parts.iter().map(|part| part.span.hi()).max()?;
let hi_file = sm.lookup_source_file(hi);

// The different spans might belong to different contexts, if so ignore suggestion.
if lo_file.stable_id != hi_file.stable_id {
    return None;
}

// We can't splice anything if the source is unavailable.
if !sm.ensure_source_file_source_present(&lo_file) {
    return None;
}

// Account for cases where we are suggesting the same code that's already
// there. This shouldn't happen often, but in some cases for multipart
// suggestions it's much easier to handle it here than in the origin.
subst.parts.retain(|p| is_different(sm, &p.snippet, p.span));

if subst.parts.is_empty() { None } else { Some(subst) }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this can fix the issue.
I have a question, the old code invoke should_show_source_code to check with ignored_directories_in_source_blocks, we don't need this anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the code and tests.
@Kivooeo i'm not sure whether your PR could be unblocked by this commit, you can have a try.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll try later today

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this can fix the issue. I have a question, the old code invoke should_show_source_code to check with ignored_directories_in_source_blocks, we don't need this anymore?

I switched it so that it would match splice_lines, which is where I got the solution from. Going back to using should_show_source_code to maintain consistency with the old code would be a perfectly reasonable thing to do.

error[E0106]: missing lifetime specifier
--> $DIR/ice-line-bounds-issue-148732.rs:4:5
|
LL | dbg!(b);
| ^^^^^^^ expected named lifetime parameter
|
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
|

error[E0425]: cannot find function `a` in this scope
--> $DIR/ice-line-bounds-issue-148732.rs:1:7
|
LL | reuse a as b {
| ^ not found in this scope

error[E0658]: functions delegation is not yet fully implemented
--> $DIR/ice-line-bounds-issue-148732.rs:1:1
|
LL | / reuse a as b {
LL | |
LL | |
LL | | dbg!(b);
LL | |
LL | | }
| |_^
|
= note: see issue #118212 <https://github.com/rust-lang/rust/issues/118212> for more information
= help: add `#![feature(fn_delegation)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0106, E0425, E0658.
For more information about an error, try `rustc --explain E0106`.
Loading