Skip to content

[E0117]: non-local ADT/primitive label in trait position names the type instead of attributing it to the foreign trait #160648

Description

@Omar1H1

Code

impl PartialEq for u32 {
    fn eq(&self, _other: &Self) -> bool {
        todo!()
    }
}

Current output

error[E0117]: only traits defined in the current crate can be implemented for primitive types
	--> src/lib.rs:1:1
	 |
	1 | impl PartialEq for u32 {
	 | ^^^^^---------^^^^^---
	 |      |             |
	 |      |             `u32` is not defined in the current crate
	 |      `u32` is not defined in the current crate
	 |
	 = note: impl doesn't have any local type before any uncovered type parameters
	 = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
	 = note: define and implement a trait or new type instead

	For more information about this error, try `rustc --explain E0117`.

Desired output

| impl PartialEq for u32 {
  | ^^^^^---------^^^^^---
  |      |             |
  |      |             `u32` is not defined in the current crate
  |      this is not defined in the current crate because this is a foreign trait

Rationale and extra context

Original diagnosis in this issue was incorrect. see the comment below for the corrected root cause. Leaving the original text below for context.

In compiler/rustc_hir_analysis/src/coherence/orphan.rs, emit_orphan_check_error iterates through non-local input types. When processing the trait (is_target_ty == IsFirstInputType::No), it passes the target type's subdiagnostic message template (OnlyCurrentTraitsTy), causing the type name (u32) to be formatted for both the type and trait span labels.

Other cases

Rust Version

❯ rustc --version --verbose
rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: x86_64-unknown-linux-gnu
release: 1.97.1
LLVM version: 22.1.6

Anything else?

I would like to work on a PR to fix this issue !

Metadata

Metadata

Assignees

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