Skip to content

Commit 1862559

Browse files
committed
Fix the ICE 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.
1 parent 13ca5c8 commit 1862559

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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, not others such as trait impls.
5+
6+
use std::collections::{BTreeMap, HashMap};
7+
8+
pub trait Trait {
9+
type Assoc;
10+
}
11+
12+
type TypeAlias<T> = HashMap<(), <T as Trait>::Assoc>;
13+
type TypeAlias2<T> = BTreeMap<(), <T as Trait>::Assoc>;
14+
15+
fn main() {}

0 commit comments

Comments
 (0)