-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Spurious unused import warning with trait used transitively via glob #45268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just ran into this myself today. I'm surprised I haven't run into it earlier. My own findings are aligned with the OP's: // build with cargo test
mod tr {
pub trait Trait: Sized { fn frob(self); }
impl Trait for () { fn frob(self) {} }
}
#[cfg(test)]
mod c {
use tr::Trait;
mod c {
use super::*;
#[test] fn lol() { ().frob(); }
}
}
It has been around since the introduction of item-like imports. Or at least since 1.15, which is the first version in which the snippet compiles.
|
Because I am interested in beginning to learn more about the compiler internals, I tried tracing through the relevant logic in the source code. Here's what I think happens:
As a result, the glob import is marked as used, but not the trait import. Does it sound like this "amateur analysis" is on the right track? I wonder which component ought to be changed to fix this... (could the glob import be followed by method resolution to insert more entries into the table?) |
This seems to be similar to #43970. |
Status update: this bug is still present. I get hit by it increasingly often as I like to use miniature inline modules (with I still don't have the chops to fix it myself (at least, not without a fair investment of time learning compiler internals). |
^--- I took a trip around the tracker and rounded up these four juicy, delicious duplicate bug reports, for anybody with an appetite |
I just ran into this kind of issue myself. It looks like if you import a trait and use it only in submodules you will face this false warning. Here is another playground that shows the bug (importing an existing trait from another crate) => Playground |
Sorry I dropped the ball, didn't I? @ExpHP's analysis above is completely correct, and "follow glob" suggestion is also what I thought. Source: I wrote the most of relevant code. I probably don't have the time to do this myself in near future... |
pub(crate) use Trait
and another module
Is this a thing that can be fixed just by modifying one crate? Or is it much more complicated (due to e.g. incrementalization, query system, rainbow unicorn dust...) If it is something a beginner could do, do you have any hints on the proper way to do it? Any method names you can easily recall? I guess basically what I need is to be able to get the |
So, I've made a fix for this -- the meat of it is in I've verified that it actually fixes the problem, and I'm running the full test suite now. Only thing is that then I'd have to change pub struct TraitCandidate {
pub def_id: DefId,
pub import_id: Option<NodeId>,
} into pub struct TraitCandidate {
pub def_id: DefId,
pub import_ids: Vec<NodeId>,
} (and similar change to Please advice! |
That's marvelous! Once the test suite passes, I think you should go ahead and submit a PR. This will give it higher visibility to the people who are best equipped to answer your concern, and it will also enable them to queue up a rustc-perf comparison if deemed necessary. |
I'm looking into the test failures. |
Fix #45268 by saving all NodeId's for resolved traits. This fixes #45268 by saving all NodeId's for resolved traits. I've verifies this against master (but only on MacOS). However, with some recent changes in master, it appears that there are some failures on my machine. They are unrelated to my changes, anyway.
When does a bug like this get merged to stable? |
It takes 3 months unless explicit backport is done. |
Every three months, the current master becomes the new beta, and the current beta becomes the new stable. Therefore, any change takes 3-6 months to get into stable, depending on its timing. Since a new version of rust was just released, there are 3 months remaining for this change. |
Eh no. master->beta and beta->stable happens every 1.5 months. So 3 months is correct. |
It's been nearly a year since then but the bug still remains. |
The bug is closed, and all the examples given are fixes -- so why do you think the bug still exists? A trick: If you get unused import warnings when building, but you code fails to compile if you remove them, it could be because they are used in conditionally imported code such as tests. If that is the case, you should move the imports into the conditional section. That was the problem I had as a beginner to begin with, which lead me to this bug... |
You are completely right, that was why I was getting such an error! Thanks for telling me, and sorry for every participants of this issue to have sent such a silly notification... |
Thanks @jespersm! I've notice too that I've forgot the attribute |
Greetings, I've run into this issue on a small project, using version 1.55. You can reproduce it here https://github.com/goatfryed/curry-lang/tree/trouble/rust%2F45268 When I'm running the tests in The test config attribute is set in mod.rs. I'm rather new to rust, so sorry if I just misunderstand the previous setups and solutions, but it seems to me like a setup that should work without a warning. For my use case, re-exporting pest:Parser from language::parser seems more appropriate anyway and fixes this warning. |
You are most likely missing a #[cfg(test)] on your However, this closed issue is probably not the best place to discuss this. If you have further trouble, please ask on https://users.rust-lang.org , preferably with a direct link to the line of code that is giving you trouble. |
Under certain circumstances involving
pub(crate) use Trait
, the compiler issues an "unused import" warning even though the import is actually being used.This is an example (playground link):
Since the body of
bar::bar()
uses theAsciiExt
trait (AsciiExt
provides.is_ascii()
), there shouldn't be a warning.Instead, this happened:
Applying any of the following changes makes the warning go away:
pub(crate) use std::ascii::AsciiExt
topub use std::ascii::AsciiExt
use foo::*
touse foo::AsciiExt
"bar".is_ascii()
toAsciiExt::is_ascii("bar")
Meta
This occurs for all versions of the compiler on the playground:
Also, on my machine,
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: