Skip to content

Commit 87a387a

Browse files
Discard overflow obligations in impl_may_apply
1 parent f65f84f commit 87a387a

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
@@ -2398,12 +2398,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23982398
if ambiguities.len() > 5 {
23992399
let infcx = self.infcx;
24002400
if !ambiguities.iter().all(|option| match option {
2401-
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
2401+
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
24022402
ParamEnv(_) => true,
24032403
}) {
24042404
// If not all are blanket impls, we filter blanked impls out.
24052405
ambiguities.retain(|option| match option {
2406-
DefId(did) => infcx.fresh_args_for_item(DUMMY_SP, *did).is_empty(),
2406+
DefId(did) => infcx.tcx.generics_of(*did).count() == 0,
24072407
ParamEnv(_) => true,
24082408
});
24092409
}
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)