From 4b3008c23bf099b92ca678df4a0d173830bef8c4 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sat, 9 Aug 2025 14:09:43 +0000 Subject: [PATCH] Do not attempt to compute size of a type with escaping lifetimes (#15434) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A type with escaping bound vars cannot be wrapped in a dummy binder during size computation. Fixes rust-lang/rust-clippy#15429 changelog: [`zero_sized_hashmap_values`]: fix ICE in types with escaping lifetimes r? Jarcho ### Summary Notes - [Beta nomination](https://github.com/rust-lang/rust-clippy/pull/15434#issuecomment-3164866684) by [samueltardieu](https://github.com/samueltardieu) *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/note.html) for details* --- .../clippy/clippy_lints/src/zero_sized_map_values.rs | 3 +++ src/tools/clippy/tests/ui/zero_sized_hashmap_values.rs | 8 ++++++++ .../clippy/tests/ui/zero_sized_hashmap_values.stderr | 6 +++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs b/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs index 1550872bca2b9..f1572fd65bbff 100644 --- a/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs +++ b/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs @@ -56,6 +56,9 @@ impl LateLintPass<'_> for ZeroSizedMapValues { // 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() + // Ensure that no region escapes to avoid an assertion error when computing the layout. + // See an example in `issue15429()` of `tests/ui/zero_sized_hashmap_values.rs`. + && !ty.has_escaping_bound_vars() && let Ok(layout) = cx.layout_of(ty) && layout.is_zst() { diff --git a/src/tools/clippy/tests/ui/zero_sized_hashmap_values.rs b/src/tools/clippy/tests/ui/zero_sized_hashmap_values.rs index dcbfd16843de4..ee2fd19b5ee5a 100644 --- a/src/tools/clippy/tests/ui/zero_sized_hashmap_values.rs +++ b/src/tools/clippy/tests/ui/zero_sized_hashmap_values.rs @@ -92,6 +92,14 @@ fn issue14822() { //~^ zero_sized_map_values } +fn issue15429() { + struct E<'a>(&'a [E<'a>]); + + // The assertion error happens when the type being evaluated has escaping bound vars + // as it cannot be wrapped in a dummy binder during size computation. + type F = dyn for<'a> FnOnce(HashMap>) -> u32; +} + fn main() { let _: HashMap = HashMap::new(); //~^ zero_sized_map_values diff --git a/src/tools/clippy/tests/ui/zero_sized_hashmap_values.stderr b/src/tools/clippy/tests/ui/zero_sized_hashmap_values.stderr index d29491fa05c76..52ffef280c1df 100644 --- a/src/tools/clippy/tests/ui/zero_sized_hashmap_values.stderr +++ b/src/tools/clippy/tests/ui/zero_sized_hashmap_values.stderr @@ -89,7 +89,7 @@ LL | type D = HashMap>; = help: consider using a set instead error: map with zero-sized value type - --> tests/ui/zero_sized_hashmap_values.rs:96:34 + --> tests/ui/zero_sized_hashmap_values.rs:104:34 | LL | let _: HashMap = HashMap::new(); | ^^^^^^^ @@ -97,7 +97,7 @@ LL | let _: HashMap = HashMap::new(); = help: consider using a set instead error: map with zero-sized value type - --> tests/ui/zero_sized_hashmap_values.rs:96:12 + --> tests/ui/zero_sized_hashmap_values.rs:104:12 | LL | let _: HashMap = HashMap::new(); | ^^^^^^^^^^^^^^^^^^^ @@ -105,7 +105,7 @@ LL | let _: HashMap = HashMap::new(); = help: consider using a set instead error: map with zero-sized value type - --> tests/ui/zero_sized_hashmap_values.rs:102:12 + --> tests/ui/zero_sized_hashmap_values.rs:110:12 | LL | let _: HashMap<_, _> = std::iter::empty::<(String, ())>().collect(); | ^^^^^^^^^^^^^