Skip to content

Fix invalid suggestions and misleading notes for imports - #162688

Open
chenyukang wants to merge 2 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-149418-private-import-alias-suggestion-2
Open

chenyukang wants to merge 2 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-149418-private-import-alias-suggestion-2

Conversation

@chenyukang

@chenyukang chenyukang commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #149418

It's better to review by commits.

The first commit is adding test.

The second commit is trying to fix the original issue in #149418.

The last commit fix a seperated accessibility issue by the way.

@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 Sep 12, 2026
@rustbot

rustbot commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
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 76 candidates
  • Random selection from 20 candidates

@petrochenkov

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rustbot rustbot assigned mu001999 and unassigned petrochenkov Sep 13, 2026
@rust-bors

This comment has been minimized.

@mu001999 mu001999 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr Outdated
Comment thread tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr Outdated
Comment thread tests/ui/imports/private-import-alias-public-parent-issue-149418.stderr Outdated
Comment thread tests/ui/imports/private-import-alias-public-parent-issue-149418.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 17, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-149418-private-import-alias-suggestion-2 branch from 58482ba to 73601a2 Compare September 20, 2026 23:14
@rustbot

rustbot commented Sep 20, 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.

@mu001999 mu001999 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let mut next_binding = Some((decl, false));
let mut next_ident = ident;
while let Some(binding) = next_binding {
while let Some((binding, path_to_binding_accessible)) = next_binding {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What I have in mind is like, to reduce the nesting depth and help reading code:

while let Some((binding, path_to_binding_accessible)) = next_binding.take() {
    ...

    if res != Res::Err
        && let DeclKind::Import { source_decl, import, .. } = binding.kind
        && !source_decl.span.is_dummy()
        && let Some(source) = match import.kind {
            ImportKind::Single { source, .. } => Some(source),
            ImportKind::Glob { .. }
            | ImportKind::MacroUse { .. }
            | ImportKind::MacroExport => Some(next_ident),
            ImportKind::ExternCrate { .. } => None,
        }
    {
        ...

        next_ident = source;
        next_binding = Some((source_decl, path_is_accessible));
    }

segments: import
.module_path
.iter()
.filter(|segment| segment.ident.name != kw::PathRoot)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
.filter(|segment| segment.ident.name != kw::PathRoot)
.filter(|segment| {
segment.ident.name != kw::PathRoot || segment.ident.span.at_least_rust_2018()
})

source_res,
&parent_scope,
) {
path_is_accessible = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

import_source_suggestion_path only checks the Res. So if the path points to the different source_decl in parent_scope, we shouldn't label this import with you could import this re-export, right?

Maybe we should also check that this path would point to the same source_decl.

let resolves_to_source =
match self.cm().maybe_resolve_path(&segments, source_res.ns(), parent_scope, None) {
PathResult::NonModule(partial_res) => partial_res.full_res() == Some(source_res),
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it is fine to include ModuleAndExternPrelude here

Suggested change
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
PathResult::Module(
ModuleOrUniformRoot::Module(module)
| ModuleOrUniformRoot::ModuleAndExternPrelude(module),
) => module.res() == Some(source_res),

@rust-bors

rust-bors Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #164804) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

E0603 : constant import ... is private gives incrorrect suggestions

4 participants