Skip to content

Fix relative paths in private import suggestions#157524

Open
qaijuang wants to merge 4 commits into
rust-lang:mainfrom
qaijuang:fix-private-import-relative-path
Open

Fix relative paths in private import suggestions#157524
qaijuang wants to merge 4 commits into
rust-lang:mainfrom
qaijuang:fix-private-import-relative-path

Conversation

@qaijuang

@qaijuang qaijuang commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

View all comments

Resolves #157455.

The private import diagnostic was reusing import.module_path for the "import directly" help. That path is relative to the module where the import was written, so a relative import can be suggested from the wrong place.

For the issue repro, this suggested:

use super::public::hi;

from the crate root. The suggestion should be:

use public::hi;

For relative local imports, this PR rebuilds the help from the resolved module path and only emits it if the item and every module segment are accessible from the failing use site. Additionally, this covers the private-ancestor case from #157455 (comment), where suggesting a direct path would still be invalid. Finally, it maintains an external-alias case as a guardrail to ensure super::s::mem is not treated like a local module path.

@rustbot rustbot added 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. labels Jun 6, 2026
@qaijuang qaijuang marked this pull request as ready for review June 6, 2026 09:02
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 6, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 6, 2026
@rustbot

rustbot commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

r? @ShoyuVanilla

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

@fee1-dead fee1-dead self-assigned this Jun 8, 2026
@fee1-dead fee1-dead removed their assignment Jun 16, 2026
@rust-bors

This comment has been minimized.

@qaijuang

Copy link
Copy Markdown
Contributor Author

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

will do a rebase rq

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from c128242 to 97aa4c9 Compare June 17, 2026 14:44
@rustbot

This comment has been minimized.

@qaijuang

Copy link
Copy Markdown
Contributor Author

It seems like @ShoyuVanilla is tight on schedule, safe to @rustbot reroll

@rustbot rustbot assigned wesleywiser and unassigned ShoyuVanilla Jul 10, 2026

@ShoyuVanilla ShoyuVanilla 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.

Sorry, I've been distracted these days and this slipped my mind 😢

View changes since this review

None
};

if let Some(module_path) = module_path {

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.

Could this be simplified with module_path.map(..)?

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.

addressed!

Comment on lines +2542 to +2544
// Reuse the existing relative shortening policy. The fields above
// other than `did` and `path` are not used by this helper.
self.shorten_candidate_path(&mut suggestion, parent_scope.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.

This feels like a temporary workaround 😅 Could you extract the path shortening part from shorten_candidate_path into a new method?

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.

Done!

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.

Could you make this into a new test file with //@ run-rustfix? I think that would make the purpose of this test clearer.

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.

Done. Moved the root super case to tests/ui/imports/private-import-suggestion-relative-rustfix.rs

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 mean, I think leaving this test as-is before this PR and moving the added lines into a new one would be nice

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from 3e420b6 to 49a5aa3 Compare July 10, 2026 10:06

@ShoyuVanilla ShoyuVanilla 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.

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.

We have lots of tests that runs rustfix but don't name them with the postfix -rustfix 😄 Could you rename this file without it and make the name just explain what this test is for?

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.

Makes sense 🙂‍↔️

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 mean, I think leaving this test as-is before this PR and moving the added lines into a new one would be nice

@qaijuang

qaijuang commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

I mean, I think leaving this test as-is before this PR and moving the added lines into a new one would be nice

I tried for both cases, but neither is compatible with rustfix. The private ancestor intentionally still errors after rustfix runs and external alias produces use core::mem;, which fails in the default 2015 test context with an “unresolved import core” error

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from 49a5aa3 to 57821f8 Compare July 10, 2026 11:09
@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 10, 2026
@rust-log-analyzer

This comment has been minimized.

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from c0bee86 to a9bbba1 Compare July 10, 2026 23:48
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 10, 2026
@rust-log-analyzer

This comment has been minimized.

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from a9bbba1 to 9a0a4bf Compare July 11, 2026 00:34
@ShoyuVanilla

Copy link
Copy Markdown
Member

I mean, I think leaving this test as-is before this PR and moving the added lines into a new one would be nice

I tried for both cases, but neither is compatible with rustfix. The private ancestor intentionally still errors after rustfix runs and external alias produces use core::mem;, which fails in the default 2015 test context with an “unresolved import core” error

Ah, right. I got confused. Sorry 😅 Those cases aren't the cases that can be fixed with the suggested imports

@ShoyuVanilla ShoyuVanilla 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.

@rust-bors

rust-bors Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 9a0a4bf has been approved by ShoyuVanilla

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 13, 2026
@rust-bors

This comment has been minimized.

@qaijuang

Copy link
Copy Markdown
Contributor Author

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

This pull request was unapproved.

will rebase rq

@qaijuang qaijuang force-pushed the fix-private-import-relative-path branch from 9a0a4bf to db1ab0b Compare July 13, 2026 08:46
@rustbot

rustbot commented Jul 13, 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.

@qaijuang

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 13, 2026
@ShoyuVanilla

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

📌 Commit db1ab0b has been approved by ShoyuVanilla

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 13, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 13, 2026
…ve-path, r=ShoyuVanilla

Fix relative paths in private import suggestions

Resolves rust-lang#157455.

The private import diagnostic was reusing `import.module_path` for the "import directly" help. That path is relative to the module where the import was written, so a relative import can be suggested from the wrong place.

For the issue repro, this suggested:

```rust
use super::public::hi;
```

from the crate root. The suggestion should be:

```rust
use public::hi;
```

For relative local imports, this PR rebuilds the help from the resolved module path and only emits it if the item and every module segment are accessible from the failing use site. Additionally, this covers the private-ancestor case from rust-lang#157455 (comment), where suggesting a direct path would still be invalid. Finally, it maintains an external-alias case as a guardrail to ensure `super::s::mem` is not treated like a local module path.
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 13, 2026
…ve-path, r=ShoyuVanilla

Fix relative paths in private import suggestions

Resolves rust-lang#157455.

The private import diagnostic was reusing `import.module_path` for the "import directly" help. That path is relative to the module where the import was written, so a relative import can be suggested from the wrong place.

For the issue repro, this suggested:

```rust
use super::public::hi;
```

from the crate root. The suggestion should be:

```rust
use public::hi;
```

For relative local imports, this PR rebuilds the help from the resolved module path and only emits it if the item and every module segment are accessible from the failing use site. Additionally, this covers the private-ancestor case from rust-lang#157455 (comment), where suggesting a direct path would still be invalid. Finally, it maintains an external-alias case as a guardrail to ensure `super::s::mem` is not treated like a local module path.
rust-bors Bot pushed a commit that referenced this pull request Jul 13, 2026
Rollup of 11 pull requests

Successful merges:

 - #159210 (`rust-analyzer` subtree update)
 - #158655 (Fix coroutine MIR saved local remapping)
 - #159205 (bootstrap: Replace `ShouldRun::crates` with `crate_or_deps_filtered`)
 - #159208 (Fix `attr_on_non_tail_expr` typo)
 - #157524 (Fix relative paths in private import suggestions)
 - #158325 (Document NonNull layout guarantees)
 - #158882 (enable `do_not_recommend` attr for method call errors in current solver)
 - #158982 (Pretty-print MIR user types too.)
 - #159069 (Add codegen test for constant returns after local use)
 - #159163 (rustc_target: Add acquire-release to implied features of v8)
 - #159201 (borrowck: Represent 'best blame constraint' as index into `Vec<OutlivesConstraint>`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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: relative paths are not being correctly suggested for help: import directly

6 participants