Skip to content

Commit 7ad48bd

Browse files
committed
Change inference var check to be in project_type
1 parent 3602e0e commit 7ad48bd

File tree

7 files changed

+27
-32
lines changed

7 files changed

+27
-32
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24702470
let projection_ty = ty::ProjectionTy {
24712471
// `T`
24722472
substs: self.tcx.mk_substs_trait(
2473-
trait_ref.self_ty().skip_binder(),
2473+
trait_pred.self_ty().skip_binder(),
24742474
&self.fresh_substs_for_item(span, item_def_id)[1..],
24752475
),
24762476
// `Future::Output`

compiler/rustc_trait_selection/src/traits/project.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,16 @@ fn project<'cx, 'tcx>(
10731073
return Ok(Projected::Progress(Progress::error(selcx.tcx())));
10741074
}
10751075

1076+
// If the obligation contains any inference types or consts in associated
1077+
// type substs, then we don't assemble any candidates.
1078+
// This isn't really correct, but otherwise we can end up in a case where
1079+
// we constrain inference variables by selecting a single predicate, when
1080+
// we need to stay general. See issue #91762.
1081+
let (_, predicate_own_substs) = obligation.predicate.trait_ref_and_own_substs(selcx.tcx());
1082+
if predicate_own_substs.iter().any(|g| g.has_infer_types_or_consts()) {
1083+
return Err(ProjectionError::TooManyCandidates);
1084+
}
1085+
10761086
let mut candidates = ProjectionCandidateSet::None;
10771087

10781088
// Make sure that the following procedures are kept in order. ParamEnv

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

-10
Original file line numberDiff line numberDiff line change
@@ -1521,16 +1521,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15211521
infer_predicate.projection_ty
15221522
};
15231523

1524-
// If the obligation contains any inference types or consts in associated
1525-
// type substs, then we don't match any projection candidates against it.
1526-
// This isn't really correct, but otherwise we can end up in a case where
1527-
// we constrain inference variables by selecting a single predicate, when
1528-
// we need to stay general. See issue #91762.
1529-
let (_, predicate_own_substs) =
1530-
obligation.predicate.trait_ref_and_own_substs(self.infcx.tcx);
1531-
if predicate_own_substs.iter().any(|g| g.has_infer_types_or_consts()) {
1532-
return false;
1533-
}
15341524
self.infcx
15351525
.at(&obligation.cause, obligation.param_env)
15361526
.sup(obligation.predicate, infer_projection)

src/test/ui/generic-associated-types/issue-74824.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<T> UnsafeCopy for T {}
1717
fn main() {
1818
let b = Box::new(42usize);
1919
let copy = <()>::copy(&b);
20+
//~^ type annotations needed
2021

2122
let raw_b = Box::deref(&b) as *const _;
2223
let raw_copy = Box::deref(&copy) as *const _;

src/test/ui/generic-associated-types/issue-74824.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ help: consider restricting type parameter `T`
2727
LL | type Copy<T: std::clone::Clone>: Copy = Box<T>;
2828
| +++++++++++++++++++
2929

30-
error: aborting due to 2 previous errors
30+
error[E0282]: type annotations needed
31+
--> $DIR/issue-74824.rs:19:16
32+
|
33+
LL | let copy = <()>::copy(&b);
34+
| ^^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated function `copy`
35+
36+
error: aborting due to 3 previous errors
3137

32-
For more information about this error, try `rustc --explain E0277`.
38+
Some errors have detailed explanations: E0277, E0282.
39+
For more information about an error, try `rustc --explain E0277`.

src/test/ui/generic-associated-types/issue-91762.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait FunctorExt<T>: Sized {
2323

2424
arg = self;
2525
ret = <Self::Base as Functor>::fmap(arg);
26-
//~^ mismatched types
26+
//~^ type annotations needed
2727
}
2828
}
2929

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/issue-91762.rs:25:45
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-91762.rs:25:15
33
|
4-
LL | / pub trait FunctorExt<T>: Sized {
5-
LL | | type Base: Functor<With<T> = Self>;
6-
LL | |
7-
LL | | fn fmap<U>(self) {
8-
... |
9-
LL | | ret = <Self::Base as Functor>::fmap(arg);
10-
| | ^^^ expected associated type, found type parameter `Self`
11-
LL | |
12-
LL | | }
13-
LL | | }
14-
| |_- this type parameter
15-
|
16-
= note: expected associated type `<<Self as FunctorExt<T>>::Base as Functor>::With<_>`
17-
found type parameter `Self`
18-
= note: you might be missing a type parameter or trait bound
4+
LL | ret = <Self::Base as Functor>::fmap(arg);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated function `fmap`
196

207
error: aborting due to previous error
218

22-
For more information about this error, try `rustc --explain E0308`.
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)