Skip to content

Conversation

@joculatrix
Copy link
Contributor

Related to #138312.

Unsure about the second "help" message suggested by the issue writer, but for at least the main part of the issue, I've added a check to emit a suggestion replacing the current crate's name in a path with crate. Let me know if there's a more elegant way to do this.

Prioritizing the added check for the crate's name at the top of
`report_path_resolution_error()` seems to account for differences
in Rust 2015 and 2018's crate/module path systems, allowing
the same suggestion to show up for Rust 2015.
(The `help` annotation was missing due to an issue that, admittedly,
resulted from a typo I stared at and didn't catch.)
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2025

r? @estebank

rustbot has assigned @estebank.
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

@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 Mar 15, 2025
@apiraino
Copy link
Contributor

r? compiler

@rustbot rustbot assigned fee1-dead and unassigned estebank Apr 24, 2025
@fee1-dead

This comment was marked as duplicate.

@rustbot rustbot assigned estebank and unassigned fee1-dead Apr 25, 2025
@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned jieyouxu and unassigned estebank Apr 25, 2025
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. However, I'm hesitant about making this suggestion at all because of potential for a lot of incorrect/misleading suggestions, even if the suggestion is MaybeIncorrect (as in, I'm hesitant about the signal-to-noise ratio).

Some examples:

  • There might be modules in other places of various visibilities that shares the same name as the root crate name, and it's not clear to me that the root crate is necessarily frequently the right answer.
  • It's not clear to me that just because the current crate name happens to be the same as the unresolved module/linked crate, that it is the right suggestion. It is possible that even though the root crate name is foo, you can still have an extern crate of the same name! E.g. you can provide an extern crate candidate via rustc foo.rs --extern foo=libbar.rlib, and extern crate foo; and use foo::{...} in foo.rs root module would not be "itself".

Comment on lines +2006 to +2016
if ident.name == self.tcx().crate_name(0_usize.into()) {
(
format!("use of unresolved module or unlinked crate `{ident}`"),
Some((
vec![(ident.span, String::from("crate"))],
format!(
"the current crate name can not be used in a path, use the keyword `crate` instead"
),
Applicability::MaybeIncorrect,
)),
)
Copy link
Member

Choose a reason for hiding this comment

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

Problem: I don't think this is right. Counter-example:

// foo.rs
mod bar {
    pub fn baz() {}
}

use crate::foo::baz;

fn main() {}
error[E0432]: unresolved import `crate::foo`
 --> foo.rs:5:12
  |
5 | use crate::foo::baz;
  |            ^^^ use of unresolved module or unlinked crate `foo`
  |
help: the current crate name can not be used in a path, use the keyword `crate` instead
  |
5 - use crate::foo::baz;
5 + use crate::crate::baz;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0432`.

_ => None,
};
if module_res == self.graph_root.res() {
if ident.name == self.tcx().crate_name(0_usize.into()) {
Copy link
Member

Choose a reason for hiding this comment

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

Problem: I think this condition is too lax. Even if we constrain this suggestion to only be made if

  1. Unresolved module/crate is the first path segment of a non-1-length path
  2. Not a global path, e.g. ::foo

I think the possibility of false positives is still quite high, even if the applicability is MaybeIncorrect.

@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 Apr 25, 2025
@joculatrix
Copy link
Contributor Author

Thanks for the review! I definitely agree with your comments. In light of your hesitancy about trying to add the suggestion at all, should I go ahead and close the PR?

@joculatrix joculatrix requested a review from jieyouxu April 25, 2025 14:21
@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 Apr 25, 2025
@jieyouxu
Copy link
Member

Yeah, I think for now we should close this PR. I'd be open to revisiting some heuristic that'd provide a higher signal-to-noise ratio, but I can't immediately think of a combination right now. Thanks for the exploration anyway!

@joculatrix joculatrix closed this Apr 25, 2025
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.

6 participants