Skip to content

Commit 273dc22

Browse files
Don't ICE when ambiguity is found when selecting Index implementation in typeck
1 parent 46ecc10 commit 273dc22

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30643064
return None;
30653065
};
30663066

3067-
self.commit_if_ok(|_| {
3067+
self.commit_if_ok(|snapshot| {
3068+
let outer_universe = self.universe();
3069+
30683070
let ocx = ObligationCtxt::new(self);
30693071
let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
30703072
let impl_trait_ref =
@@ -3074,7 +3076,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30743076
// Match the impl self type against the base ty. If this fails,
30753077
// we just skip this impl, since it's not particularly useful.
30763078
let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3077-
ocx.eq(&cause, self.param_env, impl_trait_ref.self_ty(), base_ty)?;
3079+
ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
30783080

30793081
// Register the impl's predicates. One of these predicates
30803082
// must be unsatisfied, or else we wouldn't have gotten here
@@ -3110,11 +3112,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31103112
Ty::new_projection(self.tcx, index_trait_output_def_id, impl_trait_ref.args),
31113113
);
31123114

3113-
let errors = ocx.select_where_possible();
3115+
let true_errors = ocx.select_where_possible();
3116+
3117+
// Do a leak check -- we can't really report report a useful error here,
3118+
// but it at least avoids an ICE when the error has to do with higher-ranked
3119+
// lifetimes.
3120+
self.leak_check(outer_universe, Some(snapshot))?;
3121+
3122+
// Bail if we have ambiguity errors, which we can't report in a useful way.
3123+
let ambiguity_errors = ocx.select_all_or_error();
3124+
if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3125+
return Err(NoSolution);
3126+
}
3127+
31143128
// There should be at least one error reported. If not, we
31153129
// will still delay a span bug in `report_fulfillment_errors`.
31163130
Ok::<_, NoSolution>((
3117-
self.err_ctxt().report_fulfillment_errors(errors),
3131+
self.err_ctxt().report_fulfillment_errors(true_errors),
31183132
impl_trait_ref.args.type_at(1),
31193133
element_ty,
31203134
))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test against ICE in #118111
2+
3+
use std::ops::Index;
4+
5+
struct Map<T, F> {
6+
f: F,
7+
inner: T,
8+
}
9+
10+
impl<T, F, Idx> Index<Idx> for Map<T, F>
11+
where
12+
T: Index<Idx>,
13+
F: FnOnce(&T, Idx) -> Idx,
14+
{
15+
type Output = T::Output;
16+
17+
fn index(&self, index: Idx) -> &Self::Output {
18+
todo!()
19+
}
20+
}
21+
22+
fn main() {
23+
Map { inner: [0_usize], f: |_, i: usize| 1_usize }[0];
24+
//~^ ERROR cannot index into a value of type
25+
// Problem here is that
26+
// `f: |_, i: usize| ...`
27+
// should be
28+
// `f: |_: &_, i: usize| ...`
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0608]: cannot index into a value of type `Map<[usize; 1], {closure@$DIR/bad-index-modulo-higher-ranked-regions.rs:23:32: 23:45}>`
2+
--> $DIR/bad-index-modulo-higher-ranked-regions.rs:23:55
3+
|
4+
LL | Map { inner: [0_usize], f: |_, i: usize| 1_usize }[0];
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0608`.

0 commit comments

Comments
 (0)