Skip to content

Commit 8253040

Browse files
committed
Auto merge of rust-lang#12591 - y21:issue12585, r=Jarcho
type certainty: clear `DefId` when an expression's type changes to non-adt Fixes rust-lang#12585 The root cause of the ICE in the linked issue was in the expression `one.x`, in the array literal. The type of `one` is the `One` struct: an adt with a DefId, so its certainty is `Certain(def_id_of_one)`. However, the field access `.x` can then change the type (to `i32` here) and that should update that `DefId` accordingly. It does do that correctly when `one.x` would be another adt with a DefId: https://github.com/rust-lang/rust-clippy/blob/97ba291d5aa026353ad93e48cf00e06f08c73830/clippy_utils/src/ty/type_certainty/mod.rs#L90-L91 but when it *isn't* an adt and there is no def id (which is the case in the linked issue: `one.x` is an i32), it keeps the `DefId` of `One`, even though that's the wrong type (which would then lead to a contradiction later when joining `Certainty`s): https://github.com/rust-lang/rust-clippy/blob/97ba291d5aa026353ad93e48cf00e06f08c73830/clippy_utils/src/ty/type_certainty/mod.rs#L92-L93 In particular, in the linked issue, `from_array([one.x, two.x])` would try to join the `Certainty` of the two array elements, which *should* have been `[Certain(None), Certain(None)]`, because `i32`s have no `DefId`, but instead it was `[Certain(One), Certain(Two)]`, because the DefId wasn't cleared from when it was visiting `one` and `two`. This is the "contradiction" that could be seen in the ICE message ... so this changes it to clear the `DefId` when it isn't an adt. cc `@smoelius` you implemented this initially in rust-lang#11135, does this change make sense to you? changelog: none
2 parents a73e751 + 9f5d31e commit 8253040

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

clippy_utils/src/ty/type_certainty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn expr_type_certainty(cx: &LateContext<'_>, expr: &Expr<'_>) -> Certainty {
9090
if let Some(def_id) = adt_def_id(expr_ty) {
9191
certainty.with_def_id(def_id)
9292
} else {
93-
certainty
93+
certainty.clear_def_id()
9494
}
9595
}
9696

tests/ui/crashes/ice-12585.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![allow(clippy::unit_arg)]
2+
3+
struct One {
4+
x: i32,
5+
}
6+
struct Two {
7+
x: i32,
8+
}
9+
10+
struct Product {}
11+
12+
impl Product {
13+
pub fn a_method(self, _: ()) {}
14+
}
15+
16+
fn from_array(_: [i32; 2]) -> Product {
17+
todo!()
18+
}
19+
20+
pub fn main() {
21+
let one = One { x: 1 };
22+
let two = Two { x: 2 };
23+
24+
let product = from_array([one.x, two.x]);
25+
product.a_method(<()>::default());
26+
}

0 commit comments

Comments
 (0)