Skip to content

Commit 0b441d5

Browse files
committed
Auto merge of #12877 - y21:const_host_ice, r=dswij
Handle const effects inherited from parent correctly in `type_certainty` This fixes a (debug) ICE in `type_certainty` that happened in the [k256 crate]. (I'm sure you can also specifically construct an edge test case that will run into type_certainty false positives visible outside of debug builds from this bug) <details> <summary>Minimal ICE repro</summary> ```rs use std::ops::Add; Add::add(1_i32, 1).add(i32::MIN); ``` </details> The subtraction here overflowed: https://github.com/rust-lang/rust-clippy/blob/436675b4772049a0ff535bd46d944689c90ed750/clippy_utils/src/ty/type_certainty/mod.rs#L209 ... when we have something like `Add::add` where `add` fn has 0 generic params but the `host_effect_index` is `Some(2)` (inherited from the parent generics, the const trait `Add`), and we end up executing `0 - 1`. (Even if the own generics weren't empty and we didn't overflow, this would still be wrong because it would assume that a trait method with 1 generic parameter didn't have any generics). So, *only* exclude the "host" generic parameter if it's actually bound by the own generics changelog: none [k256 crate]: https://github.com/RustCrypto/elliptic-curves/tree/master/k256
2 parents d9ae936 + 25d40c9 commit 0b441d5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

clippy_utils/src/ty/type_certainty/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ fn path_segment_certainty(
206206
// Checking `res_generics_def_id(..)` before calling `generics_of` avoids an ICE.
207207
if cx.tcx.res_generics_def_id(path_segment.res).is_some() {
208208
let generics = cx.tcx.generics_of(def_id);
209-
let count = generics.own_params.len() - usize::from(generics.host_effect_index.is_some());
210-
let lhs = if (parent_certainty.is_certain() || generics.parent_count == 0) && count == 0 {
209+
210+
let own_count = generics.own_params.len()
211+
- usize::from(generics.host_effect_index.is_some_and(|index| {
212+
// Check that the host index actually belongs to this resolution.
213+
// E.g. for `Add::add`, host_effect_index is `Some(2)`, but it's part of the parent `Add`
214+
// trait's generics.
215+
// Add params: [Self#0, Rhs#1, host#2] parent_count=0, count=3
216+
// Add::add params: [] parent_count=3, count=3
217+
// (3..3).contains(&host_effect_index) => false
218+
(generics.parent_count..generics.count()).contains(&index)
219+
}));
220+
let lhs = if (parent_certainty.is_certain() || generics.parent_count == 0) && own_count == 0 {
211221
Certainty::Certain(None)
212222
} else {
213223
Certainty::Uncertain

tests/ui/or_fun_call.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,11 @@ mod lazy {
311311
}
312312
}
313313

314+
fn host_effect() {
315+
// #12877 - make sure we don't ICE in type_certainty
316+
use std::ops::Add;
317+
318+
Add::<i32>::add(1, 1).add(i32::MIN);
319+
}
320+
314321
fn main() {}

tests/ui/or_fun_call.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,11 @@ mod lazy {
311311
}
312312
}
313313

314+
fn host_effect() {
315+
// #12877 - make sure we don't ICE in type_certainty
316+
use std::ops::Add;
317+
318+
Add::<i32>::add(1, 1).add(i32::MIN);
319+
}
320+
314321
fn main() {}

0 commit comments

Comments
 (0)