-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
non-local-definitions lint fires for impl using private types #125068
Copy link
Copy link
Closed
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-non_local_definitionsLint: non_local_definitionsLint: non_local_definitionsT-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.T-langRelevant to the language teamRelevant to the language team
Metadata
Metadata
Assignees
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.Category: This is a bug.L-non_local_definitionsLint: non_local_definitionsLint: non_local_definitionsT-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.T-langRelevant to the language teamRelevant to the language team
Type
Fields
Give feedbackNo fields configured for issues without a type.
To reproduce
https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=6c4a42e00403c3a76de54d5010d7df08
Compile with Beta ("Build using the Beta version: 1.79.0-beta.4 (2024-05-10 a269819)")
Expected results
I expected this to compile without warning.
Actual results
Analysis
This is the new lint from RFC3373 (tracking issue).
In this code, the trait implementation cannot be moved out of the function, because it relies on types that are local to the function. The impl is coherence-legal only because the trait is within the same crate (so, this function couldn't be written outside
Trait's crate), since by coherence rules it's an impl onOption. But the impl is reasonable where it is because its effects are invisible outside the function - no-one outside this function can seeLocal, so they can't seeOption<Local>either - so they can't be affected by the impl.In principle, the types could be moved too. But this will usually be undesirable. The compiler definitely ought not to be suggesting the programmer needlessly expose private items, just to satisfy this lint. (Also moving the types might involve renaming them.)
ISTM that this demonstrates that the rules for this lint need to be considerably more complicated than they are.
@rustbot label +regression-from-stable-to-beta