Skip to content

Commit

Permalink
Rollup merge of rust-lang#123618 - compiler-errors:overflow-ambig, r=…
Browse files Browse the repository at this point in the history
…spastorino

Discard overflow obligations in `impl_may_apply`

Hacky fix for rust-lang#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 rust-lang#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 rust-lang#123493
  • Loading branch information
GuillaumeGomez authored Apr 14, 2024
2 parents f3a68fb + 87a387a commit 86b791a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_infer::traits::{Obligation, ObligationCause, PolyTraitObligation};
use rustc_middle::ty;
use rustc_span::{Span, DUMMY_SP};

use crate::traits::query::evaluate_obligation::InferCtxtExt;
use crate::traits::ObligationCtxt;

#[derive(Debug)]
Expand Down Expand Up @@ -52,10 +53,21 @@ pub fn compute_applicable_impls_for_diagnostics<'tcx>(
_ => return false,
}

let impl_predicates = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_args);
ocx.register_obligations(impl_predicates.predicates.iter().map(|&predicate| {
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
}));
let obligations = tcx
.predicates_of(impl_def_id)
.instantiate(tcx, impl_args)
.into_iter()
.map(|(predicate, _)| {
Obligation::new(tcx, ObligationCause::dummy(), param_env, predicate)
})
// Kinda hacky, but let's just throw away obligations that overflow.
// This may reduce the accuracy of this check (if the obligation guides
// inference or it actually resulted in error after others are processed)
// ... but this is diagnostics code.
.filter(|obligation| {
infcx.next_trait_solver() || infcx.evaluate_obligation(obligation).is_ok()
});
ocx.register_obligations(obligations);

ocx.select_where_possible().is_empty()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2399,12 +2399,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
if ambiguities.len() > 5 {
let infcx = self.infcx;
if !ambiguities.iter().all(|option| match option {
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
ParamEnv(_) => true,
}) {
// If not all are blanket impls, we filter blanked impls out.
ambiguities.retain(|option| match option {
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
ParamEnv(_) => true,
});
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/traits/overflow-computing-ambiguity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait Hello {}

struct Foo<'a, T: ?Sized>(&'a T);

impl<'a, T: ?Sized> Hello for Foo<'a, &'a T> where Foo<'a, T>: Hello {}

impl Hello for Foo<'static, i32> {}

fn hello<T: ?Sized + Hello>() {}

fn main() {
hello();
//~^ ERROR type annotations needed
}
23 changes: 23 additions & 0 deletions tests/ui/traits/overflow-computing-ambiguity.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0283]: type annotations needed
--> $DIR/overflow-computing-ambiguity.rs:12:5
|
LL | hello();
| ^^^^^ cannot infer type of the type parameter `T` declared on the function `hello`
|
= note: cannot satisfy `_: Hello`
= help: the following types implement trait `Hello`:
Foo<'a, &'a T>
Foo<'static, i32>
note: required by a bound in `hello`
--> $DIR/overflow-computing-ambiguity.rs:9:22
|
LL | fn hello<T: ?Sized + Hello>() {}
| ^^^^^ required by this bound in `hello`
help: consider specifying the generic argument
|
LL | hello::<T>();
| +++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0283`.

0 comments on commit 86b791a

Please sign in to comment.