Skip to content

False positive and highly misleading suggestions for the non-local-definitions lint #124396

Closed
@weiznich

Description

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:

error: non-local `impl` definition, they should be avoided as they go against expectation
  --> diesel_derives/tests/associations.rs:37:5
   |
37 |     joinable!(posts -> users(user_id));
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: move this `impl` block outside the of the current constant `_` and up 2 bodies
   = note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
   = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
   = 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 macro `$crate::joinable_inner` may come from an old version of the `diesel` crate, try updating your dependency with `cargo update -p diesel`
   = note: this error originates in the macro `$crate::joinable_inner` which comes from the expansion of the macro `joinable` (in Nightly builds, run with -Z macro-backtrace for more info)

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:

rustc 1.79.0-nightly (3a36386dc 2024-04-25)

Metadata

Assignees

Labels

C-bugCategory: This is a bug.L-non_local_definitionsLint: non_local_definitionsP-highHigh priorityT-langRelevant to the language team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions