-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
When a private member is accessed from another module an error is raised as expected, but it is wrongly hinted that you can import it into scope, as seen in this example:
mod other_module {
struct PrivateMember;
}
fn main() {
PrivateMember;
}
error[E0425]: cannot find value `PrivateMember` in this scope
--> src/main.rs:6:5
|
6 | PrivateMember;
| ^^^^^^^^^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
|
1 | use other_module::PrivateMember;
|
Following the hint is clearly wrong:
error[E0603]: struct `PrivateMember` is private
--> src/main.rs:5:5
|
5 | use other_module::PrivateMember;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Meta
$ rustc --version --verbose
rustc 1.26.0 (a77568041 2018-05-07)
binary: rustc
commit-hash: a7756804103447ea4e68a71ccf071e7ad8f7a03e
commit-date: 2018-05-07
host: x86_64-unknown-linux-gnu
release: 1.26.0
LLVM version: 6.0
kornelski
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
petrochenkov commentedon May 20, 2018
This was actually intentional (see the PR introducing this diagnostic #31674).
Items from the current crate are always suggested in case you forgot
pub
, because if you want to use some item outside of its module it's more often happens because the items is intended to be used, it just haspub
missing.Private items from other crates are indeed filtered away.
DomWilliams0 commentedon May 20, 2018
Ah, makes sense.
Perhaps a second hint could be added to suggest you had forgotten to make it
pub
?ExpHP commentedon May 21, 2018
I don't feel a second hint is necessary? Code bases of sufficient size may have many types with the same name, and sometimes it really is the case that you forgot to define it (or got the name wrong).
I think the current error says quite enough.
zackmdavis commentedon May 21, 2018
If the concern is about wasting vertical terminal real estate, we can take that into account. (Only output n lines worth of suggestions, even if more than n lines worth of candidates are found, with already-public candidates prioritized before those with the additional "add
pub
" suggestion.)(It's plausible that I'll find time to work on this this week.)
zackmdavis commentedon May 22, 2018
Never mind; I'm not going to work on this. (I don't see any insurmountable difficulties, but my enthusiasm has waned remembering the time I introduced a similar suggestion in resolve and it ended up invoking some gross snippet-munging that depended on knowing the type of item.)
pandark commentedon May 28, 2018
Hi,
I'm at Impl Days and I'm looking into this.
estk commentedon Jul 1, 2018
I've run into this personally. I would be interested in modifying the error message so that when a possible type is suggested it will include information about whether the type is
pub
or not. Additionally we could order the suggestions such that public members are first. I'm particularly interested in what people think about the messaging.Example:
Dushistov commentedon Jan 6, 2019
As example there is https://github.com/dtolnay/quote crate, by @dtolnay .
rustc
always suggests use wrong trait:use quote::to_tokens::ToTokens;
instead ofuse quote::ToTokens;
, usage of this suggestion lead to compile error.carols10cents commentedon Jan 9, 2019
I think what @Dushistov is getting at is that public re-exports aren't being suggested and private paths are; here's a minimal case (stable 1.31.0, edition 2018):
That is, the suggestion I get is:
But the suggestion I expect to get with the re-export is
use crate::outer::Thing;
.nicholastmosher commentedon Feb 3, 2019
So if I'm understanding this correctly, there are a few factors that should be considered when deciding what help message to print:
It seems like the order of operations should look something like this:
I'd be willing to take a stab at this with a little guidance (if nobody's started it). I want to get some more opinions on the exact approach to take before I start, though.
18 remaining items