Skip to content

Normalize type projections when computing pointer metadata - #4815

Merged
feliperodri merged 1 commit into
model-checking:mainfrom
feliperodri:fix-4812-normalize-ptr-metadata
Sep 25, 2026
Merged

feliperodri merged 1 commit into
model-checking:mainfrom
feliperodri:fix-4812-normalize-ptr-metadata

Conversation

@feliperodri

Copy link
Copy Markdown
Member

Kani crashed with Error in struct_expr; must be given a struct_tag when codegening an unsized coercion of a struct whose unsized tail is an associated type projection, e.g. &Wrap<Array<N>> to &Wrap<Slice> where the tail of Wrap<Slice> is <Slice as Storage>::Buffer = [()].

The normalize_type callback passed to ptr_metadata_ty_or_tail / ptr_metadata_ty skipped normalization (the TODO from #752), so the projection stayed unresolved, use_thin_pointer classified the reference as a thin pointer, and the unsized cast then tried to build a slice fat pointer with a thin pointer type, hitting the panic in slice_fat_ptr.

This PR normalizes the projection when computing pointer metadata, falling back to the unnormalized type when normalization fails (e.g., for types that still contain generic parameters), so that the thin/fat pointer decision agrees with the actual monomorphized type.

Testing

  • Added a regression test tests/kani/UnsizedCoercion/assoc_type_tail_coercion.rs (crashes without the fix, verifies successfully with it), covering both a non-ZST element type and the original ZST reproducer.
  • Confirmed the exact reproducer from the issue (cargo kani autoharness -Z autoharness) now verifies successfully.
  • kani and expected compiletest suites pass locally.

Resolves #4812

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@rajath-mk

Copy link
Copy Markdown
Contributor

Approved. just a note:

The normalization itself looks right, and it lines up use_*_pointer with extract_unsize_casting,
which already normalized via struct_lockstep_tails_for_codegen(.., fully_monomorphized()).

One asymmetry inside use_thin_pointer though: is_unsized routes through self.monomorphize()
(instantiate_mir_and_normalize_erasing_regions, so it instantiates the current instance's args
and normalizes), while the new normalize_type only normalizes. In the case the comment names as
the fallback trigger . i.e. "the type still contains generic parameters" ,is_unsized would resolve the
type and the metadata computation wouldn't, giving is_unsized == true + metadata.is_err() → thin
pointer, i.e. the same shape as #4812.

Should normalize_type become a method and go through self.monomorphize(..) so both halves agree?
Related: at two of the three call sites the fallback can't help anyway:
use_slice_fat_pointer/use_vtable_fat_pointer call ptr_metadata_ty, which bug!s
unconditionally on Err(tail), and Err is returned for exactly the Param/Alias tails
skip_normalization() produces. So the fallback is graceful only in use_thin_pointer (via
_or_tail). Non-blocking, but worth either making the two consistent or narrowing what the comment
claims.

Kani crashed with "Error in struct_expr; must be given a struct_tag"
when codegening an unsized coercion of a struct whose unsized tail is
an associated type projection, e.g. `Wrap<Array<N>>` to `Wrap<Slice>`
where the tail of `Wrap<Slice>` is `<Slice as Storage>::Buffer = [()]`.

The `normalize_type` callback passed to `ptr_metadata_ty_or_tail` and
`ptr_metadata_ty` skipped normalization, so the projection stayed
unresolved, `use_thin_pointer` treated the reference as thin, and the
unsized cast then tried to build a slice fat pointer with a thin
pointer type, causing the panic.

Normalize the projection (falling back to the unnormalized type when
normalization fails, e.g. for types with generic parameters) so that
pointer-width decisions agree with the actual monomorphized type.

Resolves model-checking#4812
@feliperodri
feliperodri force-pushed the fix-4812-normalize-ptr-metadata branch from f257905 to 15358ae Compare September 25, 2026 15:40
@feliperodri

Copy link
Copy Markdown
Member Author

Good catch! Fixed in 15358ae.

@feliperodri
feliperodri added this pull request to the merge queue Sep 25, 2026
Merged via the queue into model-checking:main with commit cff7c2c Sep 25, 2026
34 checks passed
@feliperodri
feliperodri deleted the fix-4812-normalize-ptr-metadata branch September 25, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: autoharness: Error in struct_expr; must be given a struct_tag.

3 participants