Skip to content

Commit 928819a

Browse files
authored
Rollup merge of #82112 - BoxyUwU:tumbleweed, r=varkor
const_generics: Dont evaluate array length const when handling yet another error Same ICE as #82009 except triggered by a different error. cc ``@lcnr`` r? ``@varkor``
2 parents 66211f6 + 7e368e5 commit 928819a

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,16 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
7272
// We were unable to unify the abstract constant with
7373
// a constant found in the caller bounds, there are
7474
// now three possible cases here.
75-
//
76-
// - The substs are concrete enough that we can simply
77-
// try and evaluate the given constant.
78-
// - The abstract const still references an inference
79-
// variable, in this case we return `TooGeneric`.
80-
// - The abstract const references a generic parameter,
81-
// this means that we emit an error here.
8275
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
8376
enum FailureKind {
77+
/// The abstract const still references an inference
78+
/// variable, in this case we return `TooGeneric`.
8479
MentionsInfer,
80+
/// The abstract const references a generic parameter,
81+
/// this means that we emit an error here.
8582
MentionsParam,
83+
/// The substs are concrete enough that we can simply
84+
/// try and evaluate the given constant.
8685
Concrete,
8786
}
8887
let mut failure_kind = FailureKind::Concrete;

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
200200
if let Some(def) = aty.ty_adt_def() {
201201
// We also want to be able to select the array's type's original
202202
// signature with no type arguments resolved
203-
flags.push((
204-
sym::_Self,
205-
Some(format!("[{}]", self.tcx.type_of(def.did).to_string())),
206-
));
207-
let tcx = self.tcx;
208-
if let Some(len) = len.try_eval_usize(tcx, ty::ParamEnv::empty()) {
209-
flags.push((
210-
sym::_Self,
211-
Some(format!("[{}; {}]", self.tcx.type_of(def.did).to_string(), len)),
212-
));
213-
} else {
214-
flags.push((
215-
sym::_Self,
216-
Some(format!("[{}; _]", self.tcx.type_of(def.did).to_string())),
217-
));
218-
}
203+
let type_string = self.tcx.type_of(def.did).to_string();
204+
flags.push((sym::_Self, Some(format!("[{}]", type_string))));
205+
206+
let len = len.val.try_to_value().and_then(|v| v.try_to_machine_usize(self.tcx));
207+
let string = match len {
208+
Some(n) => format!("[{}; {}]", type_string, n),
209+
None => format!("[{}; _]", type_string),
210+
};
211+
flags.push((sym::_Self, Some(string)));
219212
}
220213
}
221214
if let ty::Dynamic(traits, _) = self_ty.kind() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
// This tests that during error handling for the "trait not implemented" error
5+
// we dont try to evaluate std::mem::size_of::<Self::Assoc> causing an ICE
6+
7+
struct Adt;
8+
9+
trait Foo {
10+
type Assoc;
11+
fn foo()
12+
where
13+
[Adt; std::mem::size_of::<Self::Assoc>()]: ,
14+
{
15+
<[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
16+
//~^ Error: the trait bound
17+
}
18+
19+
fn bar() {}
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the trait bound `[Adt; _]: Foo` is not satisfied
2+
--> $DIR/dont-evaluate-array-len-on-err-1.rs:15:9
3+
|
4+
LL | <[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; _]`
6+
...
7+
LL | fn bar() {}
8+
| -------- required by `Foo::bar`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)