Skip to content

non_local_definitions lint triggers when implementing trait for reference in doc test under beta 1.80.0 #126339

Closed
@tspiteri

Description

@tspiteri

I tried this code:

#![doc(test(attr(deny(warnings))))]

/// ```rust
/// use l::T;
/// struct _S;
/// impl T for &'_ _S {}
/// ```
pub trait T {}

I expected to see this happen: No warning or error for cargo +beta test

Instead, this happened:

    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running unittests src/lib.rs (target/debug/deps/l-f86ba059936edb88)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests l

running 1 test
test src/lib.rs - T (line 3) ... FAILED

failures:

---- src/lib.rs - T (line 3) stdout ----
error: non-local `impl` definition, `impl` blocks should be written at the same level as their item
 --> src/lib.rs:7:1
  |
7 | impl T for &'_ _S {}
  | ^^^^^-^^^^^------
  |      |     |
  |      |     `&'_ _S` is not local
  |      |     help: remove `&` to make the `impl` local
  |      `T` 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 `_doctest_main_src_lib_rs_3_0` and up 2 bodies
 --> src/lib.rs:4:38
  |
4 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_lib_rs_3_0() {
  |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | use l::T;
6 | struct _S;
  | --------- 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: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(warnings)]
  |         ^^^^^^^^
  = note: `#[deny(non_local_definitions)]` implied by `#[deny(warnings)]`

error: aborting due to 1 previous error

Couldn't compile the test.

failures:
    src/lib.rs - T (line 3)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s

error: doctest failed, to rerun pass `--doc`

Meta

rustc +beta --version --verbose:

rustc 1.80.0-beta.1 (75ac3b633 2024-06-10)
binary: rustc
commit-hash: 75ac3b6331873133c4f7a10f2252afd6f3906c6a
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.80.0-beta.1
LLVM version: 18.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-discussionCategory: Discussion or questions that doesn't represent real issues.L-non_local_definitionsLint: non_local_definitionsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions