-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
non_local_definitions
lint triggers when implementing trait for reference in doc test under beta 1.80.0
#126339
Comments
Looks like the lint doesn't account for fundamental types like |
That's because they don't make an impl def local, quite the opposite; here is an example where despite not naming pub trait Trait {}
pub fn doctest() {
struct Local;
impl Trait for &Local {} // with this impl definition
}
fn do_stuff<U: Trait>() {}
fn main() {
do_stuff::<&_>(); // this code will compile, as `_` will be resolved to `Local`
} @tspiteri There are several ways of fixing the issue:
It should also be noted that denying warnings is an anti-pattern and is not recommanded. @rustbot labels -C-bug +C-discussion |
Ah, I see! Thanks for the explainer, makes perfect sense |
This here is a reduced test case. I originally hit this on a doc example that was explicitly on implementing a trait for a reference.
This is good, though it looks like a workaround rather than a fix.
Usually it is an anti-pattern, but not for doc examples. For doc examples it makes sense to ensure that doc examples are not generating warnings. It is even given as an example in the official documentation. |
There is no fix. This is not a bug (at least from The Making the doc-test standalone (in the sense that it is guaranteed to be compiled and executed alone) removes most of the test harness and makes the test free of the warning. #126245 would introduce a dedicated However one thing that could be considered to be a bug is the diagnostic output which should probably suggest making the doctest standalone. |
…r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? `@fmease`
…r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? ``@fmease``
Rollup merge of rust-lang#126422 - Urgau:doctest-impl-non-local-def, r=fmease Suggest using a standalone doctest for non-local impl defs This PR tweaks the lint output of the `non_local_definitions` lint to suggest using a standalone doctest instead of a moving the `impl` def to an impossible place as was already done with `macro_rules!` case in rust-lang#124568. Fixes rust-lang#126339 r? ```@fmease```
I tried this code:
I expected to see this happen: No warning or error for
cargo +beta test
Instead, this happened:
Meta
rustc +beta --version --verbose
:The text was updated successfully, but these errors were encountered: