Skip to content

Commit d6823ba

Browse files
committed
Auto merge of #71108 - estebank:suggest-proj-type-mismatch-constraint, r=oli-obk
On type mismatch involving associated type, suggest constraint When an associated type is found when a specific type was expected, if possible provide a structured suggestion constraining the associated type in a bound. ``` error[E0271]: type mismatch resolving `<T as Foo>::Y == i32` --> $DIR/associated-types-multiple-types-one-trait.rs:13:5 | LL | want_y(t); | ^^^^^^ expected `i32`, found associated type ... LL | fn want_y<T:Foo<Y=i32>>(t: &T) { } | ----- required by this bound in `want_y` | = note: expected type `i32` found associated type `<T as Foo>::Y` help: consider constraining the associated type `<T as Foo>::Y` to `i32` | LL | fn have_x_want_y<T:Foo<X=u32, Y = i32>>(t: &T) | ^^^^^^^^^ ``` ``` error[E0308]: mismatched types --> $DIR/trait-with-missing-associated-type-restriction.rs:12:9 | LL | qux(x.func()) | ^^^^^^^^ expected `usize`, found associated type | = note: expected type `usize` found associated type `<impl Trait as Trait>::A` help: consider constraining the associated type `<impl Trait as Trait>::A` to `usize` | LL | fn foo(x: impl Trait<A = usize>) { | ^^^^^^^^^^ ``` Fix #71035. Related to #70908.
2 parents ff4df04 + b368229 commit d6823ba

35 files changed

+859
-136
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13881388
terr: &TypeError<'tcx>,
13891389
) {
13901390
let span = cause.span(self.tcx);
1391+
debug!("note_type_err cause={:?} values={:?}, terr={:?}", cause, values, terr);
13911392

13921393
// For some types of errors, expected-found does not make
13931394
// sense, so just ignore the values we were given.
@@ -1599,11 +1600,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15991600
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
16001601
});
16011602
self.check_and_note_conflicting_crates(diag, terr);
1602-
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id.to_def_id());
1603+
self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id());
16031604

16041605
// It reads better to have the error origin as the final
16051606
// thing.
1606-
self.note_error_origin(diag, &cause, exp_found);
1607+
self.note_error_origin(diag, cause, exp_found);
16071608
}
16081609

16091610
/// When encountering a case where `.as_ref()` on a `Result` or `Option` would be appropriate,

src/librustc_middle/traits/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ pub enum ObligationCauseCode<'tcx> {
193193

194194
DerivedObligation(DerivedObligationCause<'tcx>),
195195

196+
/// Error derived when matching traits/impls; see ObligationCause for more details
197+
CompareImplConstObligation,
198+
196199
/// Error derived when matching traits/impls; see ObligationCause for more details
197200
CompareImplMethodObligation {
198201
item_name: ast::Name,

src/librustc_middle/traits/structural_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
164164
tcx.lift(cause).map(super::ImplDerivedObligation)
165165
}
166166
super::DerivedObligation(ref cause) => tcx.lift(cause).map(super::DerivedObligation),
167+
super::CompareImplConstObligation => Some(super::CompareImplConstObligation),
167168
super::CompareImplMethodObligation {
168169
item_name,
169170
impl_item_def_id,

0 commit comments

Comments
 (0)