Skip to content

Commit a11e2b1

Browse files
authored
Rollup merge of #92917 - jackh726:issue-91762-2, r=nikomatsakis
Don't constrain projection predicates with inference vars in GAT substs cc #91762 Not a fix, but a mitigation to prevent a backwards-compatible hazard where we normalize using a predicate only because it's the only one available, but shouldn't. This would constrain an inference variable which didn't really want. We already do this when selecting a projection candidate, which isn't always correct. But changing that is a problem for a different day. Also found out that a suggestion for `await`ing a future was using the wrong substs. r? ``@nikomatsakis``
2 parents 8429dcd + 7ad48bd commit a11e2b1

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24732473
// `T`
24742474
substs: self.tcx.mk_substs_trait(
24752475
trait_pred.self_ty().skip_binder(),
2476-
self.fresh_substs_for_item(span, item_def_id),
2476+
&self.fresh_substs_for_item(span, item_def_id)[1..],
24772477
),
24782478
// `Future::Output`
24792479
item_def_id,

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

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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// check-fail
2+
3+
// FIXME(generic_associated_types): We almost certaintly want this to pass, but
4+
// it's particularly difficult currently, because we need a way of specifying
5+
// that `<Self::Base as Functor>::With<T> = Self` without using that when we have
6+
// a `U`. See `https://github.com/rust-lang/rust/pull/92728` for a (hacky)
7+
// solution. This might be better to just wait for Chalk.
8+
9+
#![feature(generic_associated_types)]
10+
11+
pub trait Functor {
12+
type With<T>;
13+
14+
fn fmap<T, U>(this: Self::With<T>) -> Self::With<U>;
15+
}
16+
17+
pub trait FunctorExt<T>: Sized {
18+
type Base: Functor<With<T> = Self>;
19+
20+
fn fmap<U>(self) {
21+
let arg: <Self::Base as Functor>::With<T>;
22+
let ret: <Self::Base as Functor>::With<U>;
23+
24+
arg = self;
25+
ret = <Self::Base as Functor>::fmap(arg);
26+
//~^ type annotations needed
27+
}
28+
}
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-91762.rs:25:15
3+
|
4+
LL | ret = <Self::Base as Functor>::fmap(arg);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated function `fmap`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)