Skip to content

Commit 770a9cf

Browse files
committed
fix expected/found order on impl trait projection mismatch
1 parent f24ce9b commit 770a9cf

File tree

13 files changed

+69
-46
lines changed

13 files changed

+69
-46
lines changed

compiler/rustc_middle/src/traits/mod.rs

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

324324
/// #[feature(trivial_bounds)] is not enabled
325325
TrivialBound,
326+
327+
/// If `X` is the concrete type of an opaque type `impl Y`, then `X` must implement `Y`
328+
OpaqueType,
326329
}
327330

328331
impl ObligationCauseCode<'_> {

compiler/rustc_middle/src/ty/error.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,18 @@ impl<T> Trait<T> for X {
509509
"consider constraining the associated type `{}` to `{}`",
510510
values.found, values.expected,
511511
);
512-
if !self.suggest_constraint(
512+
if !(self.suggest_constraining_opaque_associated_type(
513+
db,
514+
&msg,
515+
proj_ty,
516+
values.expected,
517+
) || self.suggest_constraint(
513518
db,
514519
&msg,
515520
body_owner_def_id,
516521
proj_ty,
517522
values.expected,
518-
) {
523+
)) {
519524
db.help(&msg);
520525
db.note(
521526
"for more information, visit \
@@ -699,20 +704,7 @@ impl<T> Trait<T> for X {
699704
}
700705
}
701706

702-
if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() {
703-
// When the expected `impl Trait` is not defined in the current item, it will come from
704-
// a return type. This can occur when dealing with `TryStream` (#71035).
705-
if self.constrain_associated_type_structured_suggestion(
706-
db,
707-
self.def_span(def_id),
708-
&assoc,
709-
proj_ty.trait_ref_and_own_substs(self).1,
710-
values.found,
711-
&msg,
712-
) {
713-
return;
714-
}
715-
}
707+
self.suggest_constraining_opaque_associated_type(db, &msg, proj_ty, values.found);
716708

717709
if self.point_at_associated_type(db, body_owner_def_id, values.found) {
718710
return;
@@ -750,6 +742,30 @@ fn foo(&self) -> Self::T { String::new() }
750742
}
751743
}
752744

745+
/// When the expected `impl Trait` is not defined in the current item, it will come from
746+
/// a return type. This can occur when dealing with `TryStream` (#71035).
747+
fn suggest_constraining_opaque_associated_type(
748+
self,
749+
db: &mut DiagnosticBuilder<'_>,
750+
msg: &str,
751+
proj_ty: &ty::ProjectionTy<'tcx>,
752+
ty: Ty<'tcx>,
753+
) -> bool {
754+
let assoc = self.associated_item(proj_ty.item_def_id);
755+
if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() {
756+
self.constrain_associated_type_structured_suggestion(
757+
db,
758+
self.def_span(def_id),
759+
&assoc,
760+
proj_ty.trait_ref_and_own_substs(self).1,
761+
ty,
762+
&msg,
763+
)
764+
} else {
765+
false
766+
}
767+
}
768+
753769
fn point_at_methods_that_satisfy_associated_type(
754770
self,
755771
db: &mut DiagnosticBuilder<'_>,

compiler/rustc_trait_selection/src/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
11711171
// This also instantiates nested instances of `impl Trait`.
11721172
let predicate = self.instantiate_opaque_types_in_map(predicate);
11731173

1174-
let cause = traits::ObligationCause::new(span, self.body_id, traits::MiscObligation);
1174+
let cause = traits::ObligationCause::new(span, self.body_id, traits::OpaqueType);
11751175

11761176
// Require that the predicate holds for the concrete type.
11771177
debug!("instantiate_opaque_types: predicate={:?}", predicate);

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

+1
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
11941194
ObligationCauseCode::ItemObligation(_)
11951195
| ObligationCauseCode::BindingObligation(_, _)
11961196
| ObligationCauseCode::ObjectCastObligation(_)
1197+
| ObligationCauseCode::OpaqueType
11971198
);
11981199

11991200
if let Err(error) = self.at(&obligation.cause, obligation.param_env).eq_exp(

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

+1
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18401840
| ObligationCauseCode::MethodReceiver
18411841
| ObligationCauseCode::ReturnNoExpression
18421842
| ObligationCauseCode::UnifyReceiver(..)
1843+
| ObligationCauseCode::OpaqueType
18431844
| ObligationCauseCode::MiscObligation => {}
18441845
ObligationCauseCode::SliceOrArrayElem => {
18451846
err.note("slice and array elements must have `Sized` type");

src/test/ui/associated-types/impl-trait-return-missing-constraint.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0271]: type mismatch resolving `<impl Bar as Foo>::Item == i32`
22
--> $DIR/impl-trait-return-missing-constraint.rs:25:13
33
|
44
LL | fn bar() -> impl Bar {
5-
| -------- the expected opaque type
5+
| -------- the found opaque type
66
...
77
LL | fn baz() -> impl Bar<Item = i32> {
8-
| ^^^^^^^^^^^^^^^^^^^^ expected associated type, found `i32`
8+
| ^^^^^^^^^^^^^^^^^^^^ expected `i32`, found associated type
99
|
10-
= note: expected associated type `<impl Bar as Foo>::Item`
11-
found type `i32`
10+
= note: expected type `i32`
11+
found associated type `<impl Bar as Foo>::Item`
1212
help: consider constraining the associated type `<impl Bar as Foo>::Item` to `i32`
1313
|
1414
LL | fn bar() -> impl Bar<Item = i32> {

src/test/ui/generator/type-mismatch-signature-deduction.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ error[E0271]: type mismatch resolving `<[generator@$DIR/type-mismatch-signature-
1616
--> $DIR/type-mismatch-signature-deduction.rs:5:13
1717
|
1818
LL | fn foo() -> impl Generator<Return = i32> {
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `i32`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found enum `Result`
2020
|
21-
= note: expected enum `Result<{integer}, _>`
22-
found type `i32`
21+
= note: expected type `i32`
22+
found enum `Result<{integer}, _>`
2323

2424
error: aborting due to 2 previous errors
2525

src/test/ui/impl-trait/bound-normalization-fail.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as imp
1111
--> $DIR/bound-normalization-fail.rs:27:32
1212
|
1313
LL | fn foo_fail<T: Trait>() -> impl FooLike<Output=T::Assoc> {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found associated type
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found `()`
1515
|
16-
= note: expected type `()`
17-
found associated type `<T as impl_trait::Trait>::Assoc`
16+
= note: expected associated type `<T as impl_trait::Trait>::Assoc`
17+
found type `()`
1818
help: consider constraining the associated type `<T as impl_trait::Trait>::Assoc` to `()`
1919
|
2020
LL | fn foo_fail<T: Trait<Assoc = ()>>() -> impl FooLike<Output=T::Assoc> {
@@ -30,10 +30,10 @@ error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as lif
3030
--> $DIR/bound-normalization-fail.rs:43:41
3131
|
3232
LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output=T::Assoc> {
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found associated type
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found `()`
3434
|
35-
= note: expected type `()`
36-
found associated type `<T as lifetimes::Trait<'static>>::Assoc`
35+
= note: expected associated type `<T as lifetimes::Trait<'static>>::Assoc`
36+
found type `()`
3737
help: consider constraining the associated type `<T as lifetimes::Trait<'static>>::Assoc` to `()`
3838
|
3939
LL | fn foo2_fail<'a, T: Trait<'a, Assoc = ()>>() -> impl FooLike<Output=T::Assoc> {

src/test/ui/impl-trait/equality2.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ LL | let _: i32 = Leak::leak(hide(0_i32));
3535
|
3636
= note: expected type `i32`
3737
found associated type `<impl Foo as Leak>::T`
38-
= help: consider constraining the associated type `<impl Foo as Leak>::T` to `i32`
39-
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
38+
help: consider constraining the associated type `<impl Foo as Leak>::T` to `i32`
39+
|
40+
LL | fn hide<T: Foo>(x: T) -> impl Foo<T = i32> {
41+
| ^^^^^^^^^
4042

4143
error[E0308]: mismatched types
4244
--> $DIR/equality2.rs:38:10

src/test/ui/impl-trait/issues/issue-70877.full_tait.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0271]: type mismatch resolving `<Bar as Iterator>::Item == Box<(dyn for<'
22
--> $DIR/issue-70877.rs:11:12
33
|
44
LL | type FooRet = impl std::fmt::Debug;
5-
| -------------------- the expected opaque type
5+
| -------------------- the found opaque type
66
...
77
LL | type Foo = impl Iterator<Item = FooItem>;
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found enum `Option`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found opaque type
99
|
10-
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`
11-
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
10+
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
11+
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`
1212

1313
error: aborting due to previous error
1414

src/test/ui/impl-trait/issues/issue-70877.min_tait.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0271]: type mismatch resolving `<Bar as Iterator>::Item == Box<(dyn for<'
22
--> $DIR/issue-70877.rs:11:12
33
|
44
LL | type FooRet = impl std::fmt::Debug;
5-
| -------------------- the expected opaque type
5+
| -------------------- the found opaque type
66
...
77
LL | type Foo = impl Iterator<Item = FooItem>;
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found enum `Option`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found opaque type
99
|
10-
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`
11-
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
10+
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
11+
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`
1212

1313
error: aborting due to previous error
1414

src/test/ui/type-alias-impl-trait/issue-63279.full_tait.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ error[E0271]: type mismatch resolving `<[closure@$DIR/issue-63279.rs:11:5: 11:28
1111
--> $DIR/issue-63279.rs:8:16
1212
|
1313
LL | type Closure = impl FnOnce();
14-
| ^^^^^^^^^^^^^ expected opaque type, found `()`
14+
| ^^^^^^^^^^^^^ expected `()`, found opaque type
1515
|
16-
= note: expected opaque type `impl FnOnce<()>`
17-
found unit type `()`
16+
= note: expected unit type `()`
17+
found opaque type `impl FnOnce<()>`
1818

1919
error: aborting due to previous error; 1 warning emitted
2020

src/test/ui/type-alias-impl-trait/issue-63279.min_tait.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ error[E0271]: type mismatch resolving `<[closure@$DIR/issue-63279.rs:11:5: 11:28
1111
--> $DIR/issue-63279.rs:8:16
1212
|
1313
LL | type Closure = impl FnOnce();
14-
| ^^^^^^^^^^^^^ expected opaque type, found `()`
14+
| ^^^^^^^^^^^^^ expected `()`, found opaque type
1515
|
16-
= note: expected opaque type `impl FnOnce<()>`
17-
found unit type `()`
16+
= note: expected unit type `()`
17+
found opaque type `impl FnOnce<()>`
1818

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)