Skip to content

Commit 86b791a

Browse files
Rollup merge of #123618 - compiler-errors:overflow-ambig, r=spastorino
Discard overflow obligations in `impl_may_apply` Hacky fix for #123493. Throws away obligations that are overflowing in `impl_may_apply` when we recompute if an impl applies, since those will lead to fatal overflow if processed during fulfillment. Something about #114811 (I think it's the predicate reordering) caused us to evaluate predicates differently in error reporting leading to fatal overflow, though I believe the underlying overflow is possible to hit since this code was rewritten to use fulfillment. Fixes #123493
2 parents f3a68fb + 87a387a commit 86b791a

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation};
55
use rustc_middle::ty;
66
use rustc_span::{Span, DUMMY_SP};
77

8+
use crate::traits::query::evaluate_obligation::InferCtxtExt;
89
use crate::traits::ObligationCtxt;
910

1011
#[derive(Debug)]
@@ -52,10 +53,21 @@ pub fn compute_applicable_impls_for_diagnostics<'tcx>(
5253
_ => return false,
5354
}
5455

55-
let impl_predicates = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_args);
56-
ocx.register_obligations(impl_predicates.predicates.iter().map(|&predicate| {
57-
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
58-
}));
56+
let obligations = tcx
57+
.predicates_of(impl_def_id)
58+
.instantiate(tcx, impl_args)
59+
.into_iter()
60+
.map(|(predicate, _)| {
61+
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
62+
})
63+
// Kinda hacky, but let's just throw away obligations that overflow.
64+
// This may reduce the accuracy of this check (if the obligation guides
65+
// inference or it actually resulted in error after others are processed)
66+
// ... but this is diagnostics code.
67+
.filter(|obligation| {
68+
infcx.next_trait_solver() || infcx.evaluate_obligation(obligation).is_ok()
69+
});
70+
ocx.register_obligations(obligations);
5971

6072
ocx.select_where_possible().is_empty()
6173
})

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2399,12 +2399,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23992399
if ambiguities.len() > 5 {
24002400
let infcx = self.infcx;
24012401
if !ambiguities.iter().all(|option| match option {
2402-
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
2402+
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
24032403
ParamEnv(_) => true,
24042404
}) {
24052405
// If not all are blanket impls, we filter blanked impls out.
24062406
ambiguities.retain(|option| match option {
2407-
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
2407+
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
24082408
ParamEnv(_) => true,
24092409
});
24102410
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait Hello {}
2+
3+
struct Foo<'a, T: ?Sized>(&'a T);
4+
5+
impl<'a, T: ?Sized> Hello for Foo<'a, &'a T> where Foo<'a, T>: Hello {}
6+
7+
impl Hello for Foo<'static, i32> {}
8+
9+
fn hello<T: ?Sized + Hello>() {}
10+
11+
fn main() {
12+
hello();
13+
//~^ ERROR type annotations needed
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/overflow-computing-ambiguity.rs:12:5
3+
|
4+
LL | hello();
5+
| ^^^^^ cannot infer type of the type parameter `T` declared on the function `hello`
6+
|
7+
= note: cannot satisfy `_: Hello`
8+
= help: the following types implement trait `Hello`:
9+
Foo<'a, &'a T>
10+
Foo<'static, i32>
11+
note: required by a bound in `hello`
12+
--> $DIR/overflow-computing-ambiguity.rs:9:22
13+
|
14+
LL | fn hello<T: ?Sized + Hello>() {}
15+
| ^^^^^ required by this bound in `hello`
16+
help: consider specifying the generic argument
17+
|
18+
LL | hello::<T>();
19+
| +++++
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0283`.

0 commit comments

Comments
 (0)