Skip to content

Avoid recording unnameable extern crate aliases in diagnostic metadata - #158997

Open
raushan728 wants to merge 2 commits into
rust-lang:mainfrom
raushan728:issues/153459
Open

Avoid recording unnameable extern crate aliases in diagnostic metadata#158997
raushan728 wants to merge 2 commits into
rust-lang:mainfrom
raushan728:issues/153459

Conversation

@raushan728

@raushan728 raushan728 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

the root cause was that block scoped extern crate aliases were still being recorded as ExternCrateSource::Extern(def_id). later, diagnostics reused that metadata and attempted to print paths through those aliases, producing unnameable paths such as crate::_::_my_crate.

this change detects extern crate items whose DefPath passes through a value namespace (for example inside functions or const blocks) and records them as ExternCrateSource::Path instead. diagnostics then fall back to the crate name rather than an unnameable alias.

Closes #153459

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 9, 2026
@rustbot

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 18 candidates

self.print_crate_name(cnum)?;
return Ok(true);
}

@cjgillot cjgillot Aug 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the wrong place to add this code.

What is the entry in visible_parent_map for MyTrait? Why isn't this entry printed directly? Why is the path using _ preferred?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is the entry in visible_parent_map for MyTrait?

form what i traced visible_parent_map already looks correct. For MyTrait it points back to the crate root, so i dont think the incorrect suggestion originates there.

Why isn't this entry printed directly?

while following the path logic , i found that after reaching the crate root try_print_visible_def_path_recur calls tcx.extern_crate(cnum) to decide how to refer to that crate in diagnostic. It doesn't stop at the visible_parent_map entry.

Why is the path using _ preferred?

that seems to come from the metadata recorded in process_extern_crate. for an extern crate inside a block, we currently still record it as ExternCrateSource::Extern(def_id), so tcx.extern_crate(cnum) later returns that block scoped alias. pretty.rs is then just printing the DefId it was given, which is why the suggestion ends up as crate::_::_my_crate.

based on that i moved the fix to process_extern_crate instead of pretty.rs so we avoid recording an unnameable extern crate as the diagnostic representation in the first place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@cjgillot please take a another look.

@cjgillot cjgillot self-assigned this Aug 4, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@raushan728
raushan728 requested a review from cjgillot August 6, 2026 07:58
@rust-log-analyzer

This comment has been minimized.

…Source::Path`

Use the `DefPath` to determine whether an `extern crate` item is
globally nameable.

`extern crate` items nested inside value namespaces cannot be referred
to by a stable path outside their enclosing scope. Record them as
`ExternCrateSource::Path` instead of `ExternCrateSource::Extern` so
later path resolution does not reconstruct unnameable paths.
@raushan728 raushan728 changed the title Avoid suggesting crate::_::_... paths for trait imports Avoid recording unnameable extern crate aliases in diagnostic metadata Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[derive(Deserialize)] makes rustc sugget suggests bizarre and non-working use crate::_::_serde::de::Error; instead of use serde::de::Error;

5 participants