Fix ICE while computing type layout

If a type is incomplete, for example if generic parameters are not
available yet, although they are not escaping, its layout may not
be computable. Calling `TyCtxt::layout_of()` would create a delayed bug
in the compiler.
This commit is contained in:
Samuel Tardieu 2025-05-18 14:39:28 +02:00
parent da7b678992
commit 72a4e33d69
No known key found for this signature in database
GPG key ID: BDDC3208C6FEAFA8
4 changed files with 39 additions and 7 deletions

View file

@ -51,9 +51,11 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
&& (is_type_diagnostic_item(cx, ty, sym::HashMap) || is_type_diagnostic_item(cx, ty, sym::BTreeMap))
&& let ty::Adt(_, args) = ty.kind()
&& let ty = args.type_at(1)
// Fixes https://github.com/rust-lang/rust-clippy/issues/7447 because of
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_middle/src/ty/sty.rs#L968
&& !ty.has_escaping_bound_vars()
// Ensure that no type information is missing, to avoid a delayed bug in the compiler if this is not the case.
// This might happen when computing a reference/pointer metadata on a type for which we
// cannot check if it is `Sized` or not, such as an incomplete associated type in a
// type alias. See an example in `issue14822()` of `tests/ui/zero_sized_hashmap_values.rs`.
&& !ty.has_non_region_param()
&& let Ok(layout) = cx.layout_of(ty)
&& layout.is_zst()
{