Skip to content

Point E0117 at the self type to avoid overlapping spans - #164605

Open
VibhuGupta-dev wants to merge 1 commit into
rust-lang:mainfrom
VibhuGupta-dev:fix-e0117-overlapping-spans
Open

VibhuGupta-dev wants to merge 1 commit into
rust-lang:mainfrom
VibhuGupta-dev:fix-e0117-overlapping-spans

Conversation

@VibhuGupta-dev

@VibhuGupta-dev VibhuGupta-dev commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #164592

E0117 used the whole impl header (tcx.def_span(impl_def_id)) as its
primary span, while the labels point at the trait path and the self type,
both of which sit inside that header. These overlapping spans render as a
chopped-up underline in the terminal (^^^^^---------^^^^^--------) and
as fragmented ranges in LSP.

This PR uses the self type span (impl_.self_ty.span) as the primary span
in emit_orphan_check_error, so the trait label and the primary span no
longer overlap. Only diagnostic output changes; there are no logic changes.

Before:

LL | impl Into<i32> for Box<i32> {}
   | ^^^^^---------^^^^^--------
   |      |             |
   |      |             `i32` is not defined in the current crate
   |      this is not defined in the current crate because this is a foreign trait

After:

LL | impl Into<i32> for Box<i32> {}
   |      ---------     ^^^^^^^^ `i32` is not defined in the current crate
   |      |
   |      this is not defined in the current crate because this is a foreign trait

When the primary span and the label point at the same type (for example
impl Copy for i32 {}), they now coincide and are rendered as a single
underline with the label attached.

Notes for reviewers:

  • The existing .stderr files for E0117 under tests/ui/coherence/ (and a
    few others) are re-blessed. The diff there is only the changed spans.
  • The auto trait errors (E0321 etc.) use a different code path and are not
    touched.
  • Adds tests/ui/coherence/orphan-foreign-trait-box.rs as a regression test
    for the case from the issue.
  • The label wording (i32 on Box<i32>) is unchanged and out of scope here.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2026
@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @beetrees (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@Kivooeo

This comment was marked as resolved.

@maxdexh

This comment was marked as resolved.

@maxdexh

maxdexh commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

The code change looks good to me, thanks for writing this!
I'm not sure why there are still tests failing. Did you run x test --bless ui?

@VibhuGupta-dev
VibhuGupta-dev force-pushed the fix-e0117-overlapping-spans branch from b7bf455 to 5f70091 Compare October 22, 2026 21:37
@VibhuGupta-dev

Copy link
Copy Markdown
Author

Thanks for the review!

You were right, I had missed re-blessing some UI tests outside tests/ui/coherence that also emit E0117. I've now re-blessed them with ./x test --bless:

  • tests/ui/any/reject_manual_impl
  • tests/ui/field_representing_types/incoherent-impl (both old and next revisions)
  • tests/ui/pin-ergonomics/impl-unpin (tait revision)
  • tests/ui/traits/const-traits/const-and-non-const-impl
  • tests/ui/traits/trait-implementation-for-non-local-types-67535
  • tests/ui/type/pattern_types/range_patterns_trait_impls2

The diff in these files is only the changed primary span (it now points at the self type instead of the whole impl header). There are no other changes.

I also ran ./x test tests/ui/coherence locally on this branch and all tests pass.

I amended and force-pushed, so the PR is still a single commit.
@maxdexh

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-aarch64-gnu-llvm-22-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
To only update this specific test, also pass `--test-args dropck/drop-on-non-struct.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/dropck/drop-on-non-struct.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/dropck/drop-on-non-struct" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0425]: cannot find type `Nonexistent` in this scope
##[error]  --> /checkout/tests/ui/dropck/drop-on-non-struct.rs:9:15
   |
---
To only update this specific test, also pass `--test-args error-codes/E0117.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/error-codes/E0117.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/error-codes/E0117" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0117]: only traits defined in the current crate can be implemented for primitive types
##[error]  --> /checkout/tests/ui/error-codes/E0117.rs:1:15
   |
LL | impl Drop for u32 {} //~ ERROR E0117
   |               ^^^ `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

error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
##[error]  --> /checkout/tests/ui/error-codes/E0117.rs:1:15
   |
LL | impl Drop for u32 {} //~ ERROR E0117
   |               ^^^ must be a struct, enum, or union in the current crate

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0117, E0120.
For more information about an error, try `rustc --explain E0117`.
---
1 error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
-   --> $DIR/complex-impl.rs:9:1
+   --> $DIR/complex-impl.rs:9:22
3    |
4 LL | impl<R> External for (Q, R) {}
-    | ^^^^^^^^^^^^^^^^^^^^^------
-    |                      |
-    |                      this is not defined in the current crate because tuples are always foreign
+    |                      ^^^^^^ this is not defined in the current crate because tuples are always foreign
8    |
---
To only update this specific test, also pass `--test-args error-codes/e0119/complex-impl.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/error-codes/e0119/complex-impl.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/error-codes/e0119/complex-impl" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/error-codes/e0119/complex-impl/auxiliary" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
##[error]  --> /checkout/tests/ui/error-codes/e0119/complex-impl.rs:9:22
   |
LL | impl<R> External for (Q, R) {} //~ ERROR only traits defined
   |                      ^^^^^^ this is not defined in the current crate because tuples are always foreign
   |
   = 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

---
19 error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
-   --> $DIR/issue-99572-impl-trait-on-pointer.rs:19:1
+   --> $DIR/issue-99572-impl-trait-on-pointer.rs:19:26
21    |
22 LL | impl<T> marker::Copy for *mut T {
-    | ^^^^^^^^^^^^^^^^^^^^^^^^^------
-    |                          |
-    |                          `*mut T` is not defined in the current crate because raw pointers are always foreign
+    |                          ^^^^^^ `*mut T` is not defined in the current crate because raw pointers are always foreign
26    |
---
To only update this specific test, also pass `--test-args errors/issue-99572-impl-trait-on-pointer.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/errors/issue-99572-impl-trait-on-pointer.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/errors/issue-99572-impl-trait-on-pointer" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
##[error]  --> /checkout/tests/ui/errors/issue-99572-impl-trait-on-pointer.rs:7:23
   |
---

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
##[error]  --> /checkout/tests/ui/errors/issue-99572-impl-trait-on-pointer.rs:19:26
   |
LL | impl<T> marker::Copy for *mut T {
   |                          ^^^^^^ `*mut T` is not defined in the current crate because raw pointers are always foreign
   |
   = 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

---
1 error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
-   --> $DIR/unpin-for-future.rs:19:1
+   --> $DIR/unpin-for-future.rs:19:16
3    |
4 LL | impl Unpin for MyFut {}
-    | ^^^^^^^^^^^^^^^-----
-    |                |
-    |                `MyFut` is not defined in the current crate
+    |                ^^^^^ `MyFut` is not defined in the current crate
8    |
9    = note: impl doesn't have any local type before any uncovered type parameters
10    = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules

Note: some mismatched output was normalized before being compared
-   --> /checkout/tests/ui/impl-trait/unpin-for-future.rs:19:16
+   --> $DIR/unpin-for-future.rs:19:16
+    |                ^^^^^ `MyFut` is not defined in the current crate


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args impl-trait/unpin-for-future.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/impl-trait/unpin-for-future.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/impl-trait/unpin-for-future" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2024"
stdout: none
--- stderr -------------------------------
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
##[error]  --> /checkout/tests/ui/impl-trait/unpin-for-future.rs:19:16
   |
LL | impl Unpin for MyFut {}
   |                ^^^^^ `MyFut` 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

@oli-obk

oli-obk commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

For the future, please avoid generating PR descriptions and comments with LLMs. Even if you write part of them yourself, there is no benefit to us to include the verbose things like which commands you ran and which tests you modified. All of that is visible to us in the diff, so this should be just information the LLM gives to you, but you should only write messages to us including things actually relevant to us.

That said: please read our LLM policy on how to contribute to this project in general.

@oli-obk

oli-obk commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

I've now re-blessed them with ./x test --bless:

considering the new error that occurred in CI, did you actually run that command?

@oli-obk oli-obk added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 23, 2026
@oli-obk oli-obk assigned oli-obk and unassigned beetrees Oct 23, 2026
@oli-obk oli-obk added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overlapping spans when implementing generic foreign trait for foreign type (E0117)

7 participants