Skip to content

Adding an impl for type defined outside of crate causes every associated method call to be reported as an error #127798

Closed
@udoprog

Description

@udoprog

Code

/// This is not a valid implementation for the current crate.
impl dyn core::fmt::Display {}

fn foo(values: &[u32]) -> Option<&u32> {
    values.get(0)
}

Current output

As expected, we get an error indicating:


error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
 --> src/lib.rs:1:1
  |
1 | impl dyn core::fmt::Display {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
  |
  = note: define and implement a trait or new type instead

But we also get the following "bonus" error:

error[E0599]: no method named `get` found for reference `&[u32]` in the current scope
 --> src/lib.rs:5:12
  |
5 |     values.get(0)
  |            ^^^
  |
help: there is a method `ge` with a similar name
  |
5 |     values.ge(0)
  |            ~~

Some errors have detailed explanations: E0116, E0599.
For more information about an error, try `rustc --explain E0116`.
error: could not compile `playground` (lib) due to 2 previous errors

If for example we add the impl to the tokio crate, we get:

error: could not compile `tokio` (lib) due to 3370 previous errors; 9 warnings emitted

Desired output

Preferably, the impl for type defined outside of crate being an illegal implementation should not cause other seemingly unrelated errors.

Rust Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions