-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
False positive and highly misleading suggestions for the non-local-definitions
lint
#124396
Comments
I think I have reduced the issue to this MCVE: #![allow(dead_code, non_camel_case_types)]
trait JoinTo {}
fn simple_belongs_to() {
mod posts {
pub struct table {}
}
impl JoinTo for posts::table {}
} |
A gap in the implementation of #122747? The check probably doesn't account for this case: rust/compiler/rustc_lint/src/non_local_def.rs Lines 167 to 169 in 6acb9e7
|
The RFC says that either the In the MCVE:
None of those are equal, so we emit the lint. However, this is were things get interesting, rust-analyzer actually also consider "all ancestor of the current block"; which means that the edit: to be ignored
trait JoinTo {
fn gg(&self) {}
}
fn simple_belongs_to() {
mod posts {
pub mod posts {
pub struct table;
}
fn hh() {
use crate::JoinTo; // EDIT: actually `rustc` doesn't compile without this import
// and adding it makes r-a also resolve this 🤔
let _a = posts::table.gg(); // --this is NOT resolved by r-a--
}
}
impl JoinTo for posts::posts::table {}
let _a = posts::posts::table.gg(); // but here it is resolved by r-a
}
|
Thanks to @Veykril, I now better understand r-a internals. I have confirmed with him that making modules transparent when checking if the Implementation wise making modules transparent would be very similar to the I tried finding examples where type inference would make the impl non-local but couldn't find any, that doesn't mean there aren't, just that if there are, I was not able to find any. Whenever we should do this is beyond me. cc @joshtriplett Footnotes
|
I don't see any obvious ways to get non-local impls out of this either. Assuming nobody finds any, this exception (treating a local module's contents as local) seems reasonable and consistent with what we're trying to do here. I also don't see any obvious ways in which this closes off future avenues for adding ways to reach inside/outside of a module-inside-function. We've talked about adding a way for a nested module to reference the containing fn's scope, but that wouldn't create non-local behavior. I'm nominating this for lang. If anyone in lang can see a way to get a non-local impl out of a module-inside-fn like this, please speak up. Otherwise, I think it's reasonable to fix this as proposed. |
There's another false positive:
The error shown is:
Should I file a new issue? |
Thanks you all for your swift response. It would be great to see that resolved before it hits stable. The lint seems now to be enabled on the beta branch as well. Also I want to highlight the other part of the issue (misleading message) again:
I feel that this should detect whether an update could even resolve this issue. This is not the case if the user put the macro call inside a function on it's own, which I feel is something that should be easy to detect while emitting the lint. (If someone points to the right location I even can have a look at that myself.) |
… r=lcnr Consider inner modules to be local in the `non_local_definitions` lint This PR implements the [proposed fix](rust-lang#124396 (comment)) for rust-lang#124396, that is to consider inner modules to be local in the `non_local_definitions` lint. This PR is voluntarily kept as minimal as possible so it can be backported easily. T-lang [nomination](rust-lang#124396 (comment)) will need to be removed before this can be merged. Fixes *(nearly, needs backport)* rust-lang#124396
Updating the labels so we track this issue as a regression. @rustbot labels -needs-triage +regression-from-stable-to-beta |
@rustbot labels -I-lang-nominated We discussed this in the lang meeting today. Our consensus was in favor of adopting the modification to the RFC 3373 rules discussed here, to make "modules transparent when checking if the I.e., this code is not a "sneaky inner impl" and should not lint: trait Trait {}
fn foo() {
mod m {
pub struct Ty {}
}
impl Trait for m::Ty {}
} This is good to go on the lang side, and we leave it as usual to the compiler team to decide on the appropriate follow-up actions. |
WG-prioritization assigning priority (Zulip discussion). Since this looks like a diag regression so maybe confusing but not critical. Fixed by #124539 (beta-nominated) @rustbot label -I-prioritize +P-medium |
Consider inner modules to be local in the `non_local_definitions` lint This PR implements the [proposed fix](rust-lang/rust#124396 (comment)) for #124396, that is to consider inner modules to be local in the `non_local_definitions` lint. This PR is voluntarily kept as minimal as possible so it can be backported easily. T-lang [nomination](rust-lang/rust#124396 (comment)) will need to be removed before this can be merged. Fixes *(nearly, needs backport)* rust-lang/rust#124396
I think since this lint is new in 1.79 this is a higher priority than a normal diagnostic regression. Retagging as P-high. @rustbot label -P-medium +P-high |
…=estebank Improve diagnostic output the `non_local_definitions` lint This PR improves (or at least tries to improve) the diagnostic output the `non_local_definitions` lint, by simplifying the wording, by adding a "sort of" explanation of bounds interaction that leak the impl... This PR is best reviewed commit by commit and is voluntarily made a bit vague as to have a starting point to improve on. Related to https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/non_local_defs.20wording.20improvements Fixes rust-lang#125068 Fixes rust-lang#124396 cc `@workingjubilee` r? `@estebank`
…=estebank Improve diagnostic output the `non_local_definitions` lint This PR improves (or at least tries to improve) the diagnostic output the `non_local_definitions` lint, by simplifying the wording, by adding a "sort of" explanation of bounds interaction that leak the impl... This PR is best reviewed commit by commit and is voluntarily made a bit vague as to have a starting point to improve on. Related to https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/non_local_defs.20wording.20improvements Fixes rust-lang#125068 Fixes rust-lang#124396 cc ``@workingjubilee`` r? ``@estebank``
Rollup merge of rust-lang#125089 - Urgau:non_local_def-suggestions, r=estebank Improve diagnostic output the `non_local_definitions` lint This PR improves (or at least tries to improve) the diagnostic output the `non_local_definitions` lint, by simplifying the wording, by adding a "sort of" explanation of bounds interaction that leak the impl... This PR is best reviewed commit by commit and is voluntarily made a bit vague as to have a starting point to improve on. Related to https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/non_local_defs.20wording.20improvements Fixes rust-lang#125068 Fixes rust-lang#124396 cc ```@workingjubilee``` r? ```@estebank```
I tried this code: https://github.com/diesel-rs/diesel/blob/974814c6792c534736498a0bd7abf95e21f8bf7b/diesel_derives/tests/associations.rs#L37
I expected to see this happen: I get an error message that explains that this macro expands to a trait impl and therefore it triggers the lint as I (the user) placed the macro in a function scope.
Instead, this happened: I get a rather generic error that points to the macro (ok so far) and claims that this might be an issue with the dependency version (diesel in this case) I'm using.
Example error message:
Especially the suggestion
= note: the macro allow_tables_to_appear_in_same_query
may come from an old version of the diesel crate, try updating your dependency with cargo update -p diesel` is highly problematic in this context as that never could resolve the error given above. The wording might result in cases where the user will believe that this is an upstream diesel issue, rather than an issue with their local code.Instead I propose that the lint detects weather the impl macro is placed in an local scope or not and adjusts the error message based on that.
The other interesting observation about that specific code example is that the lint is only triggered by the macro rules macro there and not by any of the proc macros above/below the linked line. In fact the macro also only generates impls that refer only to local types defined by the
table!
macro above, so arguably the lint is even wrong at all in this case as it does not add a non local impl.(code for joinable!)
Relevant for #120363
Meta
rustc --version
:The text was updated successfully, but these errors were encountered: