Closed
Description
#120363 introduces some warnings that seem like bad advice. Nightly now warns if a function has an internal implementation of a trait on a non-internal (to that function) type. That seems like a reasonable warning in the general case, but it doesn't seem so reasonable for types like Box<Inner>
, where Inner
is defined within the function and cannot be named outside it.
It rather seems like something like the orphan rule should apply. You should be able to impl traits on Foo<T>
as long as T
is defined at the same level as the impl
.
I tried this code on nightly:
#![allow(dead_code)]
trait Tr {}
fn foo() {
struct Bar;
impl Tr for Box<Bar> {}
}
I expected to see this happen: no errors/warnings
Instead, this happened:
warning: non-local `impl` definition, `impl` blocks should be written at the same level as their item
--> src/lib.rs:5:5
|
5 | impl Tr for Box<Bar> {}
| ^^^^^--^^^^^---^^^^^
| | |
| | `Box` is not local
| `Tr` is not local
|
= note: `impl` may be usable in bounds, etc. from outside the expression, which might e.g. make something constructible that previously wasn't, because it's still on a publicly-visible type
= note: an `impl` is never scoped, even when it is nested inside an item, as it may impact type checking outside of that item, which can be the case if neither the trait or the self type are at the same nesting level as the `impl`
help: move the `impl` block outside of this function `foo`
--> src/lib.rs:3:1
|
3 | fn foo() {
| ^^^^^^^^
4 | struct Bar;
| ---------- may need to be moved as well
= note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
= note: `#[warn(non_local_definitions)]` on by default
Meta
rustc --version --verbose
(well, from the playground):
Build using the Nightly version: 1.81.0-nightly
(2024-06-19 d8a38b00024cd7156dea)
Backtrace
<backtrace>