Skip to content

Commit d122155

Browse files
Explain HRTB + infer limitations of old solver
1 parent b0d4553 commit d122155

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
986986
}
987987
}
988988

989+
self.explain_hrtb_projection(&mut err, trait_predicate, obligation.param_env, &obligation.cause);
990+
989991
// Return early if the trait is Debug or Display and the invocation
990992
// originates within a standard library macro, because the output
991993
// is otherwise overwhelming and unhelpful (see #85844 for an

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

+73
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ pub trait TypeErrCtxtExt<'tcx> {
406406
candidate_impls: &[ImplCandidate<'tcx>],
407407
span: Span,
408408
);
409+
410+
fn explain_hrtb_projection(
411+
&self,
412+
diag: &mut Diagnostic,
413+
pred: ty::PolyTraitPredicate<'tcx>,
414+
param_env: ty::ParamEnv<'tcx>,
415+
cause: &ObligationCause<'tcx>,
416+
);
409417
}
410418

411419
fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -> (Span, String) {
@@ -4014,6 +4022,71 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
40144022
}
40154023
}
40164024
}
4025+
4026+
fn explain_hrtb_projection(
4027+
&self,
4028+
diag: &mut Diagnostic,
4029+
pred: ty::PolyTraitPredicate<'tcx>,
4030+
param_env: ty::ParamEnv<'tcx>,
4031+
cause: &ObligationCause<'tcx>,
4032+
) {
4033+
if pred.skip_binder().has_escaping_bound_vars() && pred.skip_binder().has_non_region_infer()
4034+
{
4035+
self.probe(|_| {
4036+
let ocx = ObligationCtxt::new(self);
4037+
let pred = self.instantiate_binder_with_placeholders(pred);
4038+
let pred = ocx.normalize(&ObligationCause::dummy(), param_env, pred);
4039+
ocx.register_obligation(Obligation::new(
4040+
self.tcx,
4041+
ObligationCause::dummy(),
4042+
param_env,
4043+
pred,
4044+
));
4045+
if !ocx.select_where_possible().is_empty() {
4046+
// encountered errors.
4047+
return;
4048+
}
4049+
4050+
if let ObligationCauseCode::FunctionArgumentObligation {
4051+
call_hir_id,
4052+
arg_hir_id,
4053+
parent_code: _,
4054+
} = cause.code()
4055+
{
4056+
let arg_span = self.tcx.hir().span(*arg_hir_id);
4057+
let mut sp: MultiSpan = arg_span.into();
4058+
4059+
sp.push_span_label(
4060+
arg_span,
4061+
"the trait solver is unable to infer the \
4062+
generic types that should be inferred from this argument",
4063+
);
4064+
sp.push_span_label(
4065+
self.tcx.hir().span(*call_hir_id),
4066+
"add turbofish arguments to this call to \
4067+
specify the types manually, even if it's redundant",
4068+
);
4069+
diag.span_note(
4070+
sp,
4071+
"this is a known limitation of the trait solver that \
4072+
will be lifted in the future",
4073+
);
4074+
} else {
4075+
let mut sp: MultiSpan = cause.span.into();
4076+
sp.push_span_label(
4077+
cause.span,
4078+
"try adding turbofish arguments to this expression to \
4079+
specify the types manually, even if it's redundant",
4080+
);
4081+
diag.span_note(
4082+
sp,
4083+
"this is a known limitation of the trait solver that \
4084+
will be lifted in the future",
4085+
);
4086+
}
4087+
});
4088+
}
4089+
}
40174090
}
40184091

40194092
/// Add a hint to add a missing borrow or remove an unnecessary one.

tests/ui/generic-associated-types/bugs/issue-88460.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ LL | test(Foo);
77
| required by a bound introduced by this call
88
|
99
= help: the trait `Marker` is implemented for `()`
10+
note: this is a known limitation of the trait solver that will be lifted in the future
11+
--> $DIR/issue-88460.rs:28:10
12+
|
13+
LL | test(Foo);
14+
| -----^^^-
15+
| | |
16+
| | the trait solver is unable to infer the generic types that should be inferred from this argument
17+
| add turbofish arguments to this call to specify the types manually, even if it's redundant
1018
note: required by a bound in `test`
1119
--> $DIR/issue-88460.rs:15:27
1220
|

tests/ui/higher-ranked/trait-bounds/normalize-under-binder/issue-62529-3.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ LL | call(f, ());
88
|
99
= note: expected a closure with arguments `((),)`
1010
found a closure with arguments `(<_ as ATC<'a>>::Type,)`
11+
note: this is a known limitation of the trait solver that will be lifted in the future
12+
--> $DIR/issue-62529-3.rs:25:14
13+
|
14+
LL | call(f, ());
15+
| -----^-----
16+
| | |
17+
| | the trait solver is unable to infer the generic types that should be inferred from this argument
18+
| add turbofish arguments to this call to specify the types manually, even if it's redundant
1119
note: required by a bound in `call`
1220
--> $DIR/issue-62529-3.rs:9:36
1321
|

tests/ui/higher-ranked/trait-bounds/normalize-under-binder/issue-90950.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ LL | upcast(y)
77
| required by a bound introduced by this call
88
|
99
= help: the trait `IsCovariant<'a>` is implemented for `std::borrow::Cow<'a, T>`
10+
note: this is a known limitation of the trait solver that will be lifted in the future
11+
--> $DIR/issue-90950.rs:50:12
12+
|
13+
LL | upcast(y)
14+
| -------^-
15+
| | |
16+
| | the trait solver is unable to infer the generic types that should be inferred from this argument
17+
| add turbofish arguments to this call to specify the types manually, even if it's redundant
1018
note: required by a bound in `upcast`
1119
--> $DIR/issue-90950.rs:27:42
1220
|

tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0277]: the trait bound `for<'a> <_ as Trait<'a>>::Out: Copy` is not satis
44
LL | let _: () = weird_bound();
55
| ^^^^^^^^^^^ the trait `for<'a> Copy` is not implemented for `<_ as Trait<'a>>::Out`
66
|
7+
note: this is a known limitation of the trait solver that will be lifted in the future
8+
--> $DIR/norm-before-method-resolution.rs:22:17
9+
|
10+
LL | let _: () = weird_bound();
11+
| ^^^^^^^^^^^ try adding turbofish arguments to this expression to specify the types manually, even if it's redundant
712
note: required by a bound in `weird_bound`
813
--> $DIR/norm-before-method-resolution.rs:18:40
914
|

0 commit comments

Comments
 (0)