Skip to content

Commit e0d331f

Browse files
committed
Auto merge of #6582 - rail-rain:ice_6539, r=flip1995
Fix the ICE 6539 Fixes #6539 It happened because `zero_sized_map_values` used `layout_of` with types from type aliases, which is essentially the same as the ICE 4968. --- changelog: Fix an ICE in `zero_sized_map_values`
2 parents 40ce9f8 + feee45c commit e0d331f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clippy_lints/src/zero_sized_map_values.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_target::abi::LayoutOf as _;
77
use rustc_typeck::hir_ty_to_ty;
88

9-
use crate::utils::{is_type_diagnostic_item, match_type, paths, span_lint_and_help};
9+
use crate::utils::{is_normalizable, is_type_diagnostic_item, match_type, paths, span_lint_and_help};
1010

1111
declare_clippy_lint! {
1212
/// **What it does:** Checks for maps with zero-sized value types anywhere in the code.
@@ -50,6 +50,8 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
5050
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP);
5151
if let Adt(_, ref substs) = ty.kind();
5252
let ty = substs.type_at(1);
53+
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.
54+
if is_normalizable(cx, cx.param_env, ty);
5355
if let Ok(layout) = cx.layout_of(ty);
5456
if layout.is_zst();
5557
then {

tests/ui/crashes/ice-6539.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// The test for the ICE 6539: https://github.com/rust-lang/rust-clippy/issues/6539.
2+
// The cause is that `zero_sized_map_values` used `layout_of` with types from type aliases,
3+
// which is essentially the same as the ICE 4968.
4+
// Note that only type aliases with associated types caused the crash this time,
5+
// not others such as trait impls.
6+
7+
use std::collections::{BTreeMap, HashMap};
8+
9+
pub trait Trait {
10+
type Assoc;
11+
}
12+
13+
type TypeAlias<T> = HashMap<(), <T as Trait>::Assoc>;
14+
type TypeAlias2<T> = BTreeMap<(), <T as Trait>::Assoc>;
15+
16+
fn main() {}

0 commit comments

Comments
 (0)