Skip to content

Fix ICE in pointer layout for DSTs with bogus Sized impls#155260

Open
GokhanKabar wants to merge 1 commit intorust-lang:mainfrom
GokhanKabar:fix-ice-bogus-sized-impl-layout
Open

Fix ICE in pointer layout for DSTs with bogus Sized impls#155260
GokhanKabar wants to merge 1 commit intorust-lang:mainfrom
GokhanKabar:fix-ice-bogus-sized-impl-layout

Conversation

@GokhanKabar
Copy link
Copy Markdown
Contributor

@GokhanKabar GokhanKabar commented Apr 13, 2026

Fixes #155252.

Summary

A bogus impl<T: ?Sized> Sized for T {} (rejected as E0322) was processed by the new trait solver (-Znext-solver=globally), making DSTs like [T] appear sized. This caused *const [T] to receive a thin-pointer (Scalar) layout instead of a fat-pointer (ScalarPair) layout in rustc_ty_utils/src/layout.rs.

When const evaluation then performed an array-to-slice unsizing coercion (unsize_into_ptr) and tried to write the resulting ScalarPair value to a Scalar-layout place, it triggered an ICE:

error: internal compiler error: invalid immediate for given destination place:
value ScalarPair(alloc<imm>, 0x2) does not match ABI Scalar(...)

Fix

In compiler/rustc_ty_utils/src/layout.rs, after is_sized returns true for the pointee, we cross-check using struct_tail_for_codegen — a purely structural traversal that is unaffected by bogus trait impls. If the tail is a known DST kind (Slice / Str / Dynamic), we skip the thin-pointer early return and fall through to compute the correct fat-pointer (ScalarPair) layout.

This approach is safe because struct_tail_for_codegen does not invoke the trait solver, so it cannot be misled by the bogus Sized impl.

Test

Added tests/ui/traits/next-solver/dont-ice-on-bogus-sized-impl-in-const-block.rs which previously ICE'd and now produces the expected E0322 + E0210 errors.

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 13, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

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

@rust-log-analyzer

This comment has been minimized.

@jieyouxu jieyouxu assigned jieyouxu and unassigned nnethercote Apr 14, 2026
@jieyouxu jieyouxu added the S-no-work-capacity-tracking Status: Exempted from triagebot work capacity tracking. label Apr 14, 2026
A bogus `impl<T: ?Sized> Sized for T {}` (rejected as E0322) was
processed by the new trait solver (`-Znext-solver=globally`), making
DSTs like `[T]` appear sized. This caused `*const [T]` to receive a
thin-pointer (Scalar) layout instead of a fat-pointer (ScalarPair)
layout. When const evaluation then performed an array-to-slice unsizing
coercion and tried to write a ScalarPair value to a Scalar-layout place,
it triggered an ICE:

  "invalid immediate for given destination place: value ScalarPair(...)
   does not match ABI Scalar(...)"

The fix adds a structural cross-check in the pointer layout computation:
after `is_sized` returns true, we verify via `struct_tail_for_codegen`
(which is purely structural and unaffected by bogus trait impls) that
the pointee tail is not a known DST kind (Slice/Str/Dynamic). If it is,
we skip the thin-pointer early return and fall through to compute the
correct fat-pointer layout.
@GokhanKabar GokhanKabar force-pushed the fix-ice-bogus-sized-impl-layout branch from 7f1b2c3 to 07c2864 Compare April 15, 2026 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-no-work-capacity-tracking Status: Exempted from triagebot work capacity tracking. 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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: Got a scalar pair where a scalar was expected

5 participants