Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize late-bound projections with inference variables in fulfillment #94070

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ impl FlagComputation {

&ty::Projection(data) => {
self.add_flags(TypeFlags::HAS_TY_PROJECTION);
// Check if this projection type captures any late-bound variables in its substs
let old_outer = std::mem::replace(&mut self.outer_exclusive_binder, ty::INNERMOST);
self.add_projection_ty(data);
if self.outer_exclusive_binder > ty::INNERMOST {
self.add_flags(TypeFlags::HAS_LATE_IN_PROJECTION);
}
self.outer_exclusive_binder = self.outer_exclusive_binder.max(old_outer);
}

&ty::Opaque(_, substs) => {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
fn still_further_specializable(&self) -> bool {
self.has_type_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE)
}

// Indicates that this value has projection types which capture late-bound variables,
// which is a sign that the projection types within need further normalization. This
// is because we do not replace projections with inference variables when they capture
// late-bound variables.
fn has_late_bound_vars_in_projection(&self) -> bool {
self.has_type_flags(TypeFlags::HAS_LATE_IN_PROJECTION)
}
}

/// This trait is implemented for every folding traversal. There is a fold
Expand Down
63 changes: 43 additions & 20 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,34 +344,57 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
pending_obligation.stalled_on.truncate(0);

let infcx = self.selcx.infcx();
let obligation = &mut pending_obligation.obligation;

debug!(?obligation, "process_obligation pre-resolve");

if obligation.predicate.has_infer_types_or_consts() {
obligation.predicate =
self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
}

debug!(?obligation, ?obligation.cause, "process_obligation");
let mut predicate_changed = false;

let infcx = self.selcx.infcx();
if obligation.predicate.has_late_bound_vars_in_projection() {
let mut obligations = Vec::new();
let predicate = crate::traits::project::try_normalize_with_depth_to(
self.selcx,
obligation.param_env,
obligation.cause.clone(),
obligation.recursion_depth + 1,
obligation.predicate,
&mut obligations,
);
if predicate != obligation.predicate {
if obligations.is_empty() {
debug!(
"progress_changed_obligations: late-bound predicate updated, fast path: {} => {}",
obligation.predicate, predicate
);
// Optimization, since we don't need to return with Changed if we're
// just updating the predicate to a new normalized form.
predicate_changed = true;
obligation.predicate = predicate;
} else {
debug!(
"progress_changed_obligations: late-bound predicate updated, slow path: {} => {}, additional obligations: {:?}",
obligation.predicate, predicate, obligations
);
obligations.push(obligation.with(predicate));
return ProcessResult::Changed(mk_pending(obligations));
}
} else {
debug!(
"progress_changed_obligations: late-bound predicate not updated: {}",
obligation.predicate
);
}
}

if obligation.predicate.has_projections() {
let mut obligations = Vec::new();
let predicate = crate::traits::project::try_normalize_with_depth_to(
self.selcx,
obligation.param_env,
obligation.cause.clone(),
obligation.recursion_depth + 1,
obligation.predicate,
&mut obligations,
);
if predicate != obligation.predicate {
obligations.push(obligation.with(predicate));
return ProcessResult::Changed(mk_pending(obligations));
if !predicate_changed {
obligation.predicate =
self.selcx.infcx().resolve_vars_if_possible(obligation.predicate);
}
}

debug!(?obligation, ?obligation.cause, "process_obligation");

let binder = obligation.predicate.kind();
match binder.no_bound_vars() {
None => match binder.skip_binder() {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ bitflags! {

/// Does this value have `InferConst::Fresh`?
const HAS_CT_FRESH = 1 << 19;

// Does this value have any late-bound vars in any projection type substs?
const HAS_LATE_IN_PROJECTION = 1 << 20;
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
if !ty.references_error() {
let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
self.require_type_meets(ty, span, code, lang_item);
self.require_type_meets(
// We normalize this type because we possibly got it from HIR
self.normalize_associated_types_in(span, ty),
span,
code,
lang_item,
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/associated-types/issue-59324.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pub trait ThriftService<Bug: NotFoo>:
{
fn get_service(
//~^ ERROR the trait bound `Bug: Foo` is not satisfied
//~| ERROR the trait bound `Bug: Foo` is not satisfied
&self,
) -> Self::AssocType;
//~^ the trait bound `Bug: Foo` is not satisfied
}

fn with_factory<H>(factory: dyn ThriftService<()>) {}
Expand Down
11 changes: 6 additions & 5 deletions src/test/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | |
LL | |
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
... |
LL | |
LL | | ) -> Self::AssocType;
LL | | }
| |_^ the trait `Foo` is not implemented for `Bug`
|
Expand All @@ -23,7 +23,7 @@ LL | |
LL | |
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
... |
LL | |
LL | | ) -> Self::AssocType;
LL | | }
| |_^ the trait `Foo` is not implemented for `Bug`
|
Expand All @@ -37,6 +37,7 @@ error[E0277]: the trait bound `Bug: Foo` is not satisfied
|
LL | / fn get_service(
LL | |
LL | |
LL | | &self,
LL | | ) -> Self::AssocType;
| |_________________________^ the trait `Foo` is not implemented for `Bug`
Expand All @@ -47,10 +48,10 @@ LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++

error[E0277]: the trait bound `Bug: Foo` is not satisfied
--> $DIR/issue-59324.rs:19:10
--> $DIR/issue-59324.rs:16:8
|
LL | ) -> Self::AssocType;
| ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
LL | fn get_service(
| ^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
|
Expand Down
12 changes: 1 addition & 11 deletions src/test/ui/generic-associated-types/issue-91139.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
// revisions: migrate nll
//[nll]compile-flags: -Z borrowck=mir

// Since we are testing nll (and migration) explicitly as a separate
// revisions, don't worry about the --compare-mode=nll on this test.

// ignore-compare-mode-nll

//[nll] check-pass
//[migrate] check-fail
// check-pass

#![feature(generic_associated_types)]

Expand All @@ -25,7 +16,6 @@ impl<T> Foo<T> for () {

fn foo<T>() {
let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
//[migrate]~^ the parameter type `T` may not live long enough
}

pub fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ trait SomeTrait<'a> {
fn give_me_ice<T>() {
callee::<fn(&()) -> <T as SomeTrait<'_>>::Associated>();
//~^ ERROR the trait bound `T: SomeTrait<'_>` is not satisfied [E0277]
//~| ERROR the trait bound `T: SomeTrait<'_>` is not satisfied [E0277]
}

fn callee<T: Fn<(&'static (),)>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ help: consider restricting type parameter `T`
LL | fn give_me_ice<T: SomeTrait<'_>>() {
| +++++++++++++++

error[E0277]: the trait bound `T: SomeTrait<'_>` is not satisfied
--> $DIR/issue-85455.rs:8:14
|
LL | callee::<fn(&()) -> <T as SomeTrait<'_>>::Associated>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait<'_>` is not implemented for `T`
|
help: consider restricting type parameter `T`
|
LL | fn give_me_ice<T: SomeTrait<'_>>() {
| +++++++++++++++

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ note: previous use here
LL | fn two<T: Debug + Foo, U: Debug>(t: T, u: U) -> Two<T, U> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `A: Foo` is not satisfied
error[E0277]: the trait bound `A: Foo` is not satisfied in `(A, B, <A as Foo>::Bar)`
--> $DIR/generic_duplicate_param_use9.rs:7:18
|
LL | type Two<A, B> = impl Debug;
| ^^^^^^^^^^ the trait `Foo` is not implemented for `A`
| ^^^^^^^^^^ within `(A, B, <A as Foo>::Bar)`, the trait `Foo` is not implemented for `A`
|
= note: required because it appears within the type `(A, B, <A as Foo>::Bar)`
help: consider restricting type parameter `A`
|
LL | type Two<A: Foo, B> = impl Debug;
Expand All @@ -27,7 +28,7 @@ error[E0277]: `A` doesn't implement `Debug`
LL | type Two<A, B> = impl Debug;
| ^^^^^^^^^^ `A` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= note: required because of the requirements on the impl of `Debug` for `(A, B, _)`
= note: required because of the requirements on the impl of `Debug` for `(A, B, <A as Foo>::Bar)`
help: consider restricting type parameter `A`
|
LL | type Two<A: std::fmt::Debug, B> = impl Debug;
Expand All @@ -39,7 +40,7 @@ error[E0277]: `B` doesn't implement `Debug`
LL | type Two<A, B> = impl Debug;
| ^^^^^^^^^^ `B` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= note: required because of the requirements on the impl of `Debug` for `(A, B, _)`
= note: required because of the requirements on the impl of `Debug` for `(A, B, <A as Foo>::Bar)`
help: consider restricting type parameter `B`
|
LL | type Two<A, B: std::fmt::Debug> = impl Debug;
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/type-alias-impl-trait/issue-89686.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use std::future::Future;

type G<'a, T> = impl Future<Output = ()>;
//~^ ERROR: the trait bound `T: Trait` is not satisfied
//~^ ERROR <impl Future<Output = [async output]> as Future>::Output == ()
//~| ERROR the trait bound `T: Trait` is not satisfied

trait Trait {
type F: Future<Output = ()>;
Expand Down
24 changes: 22 additions & 2 deletions src/test/ui/type-alias-impl-trait/issue-89686.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
error[E0271]: type mismatch resolving `<impl Future<Output = [async output]> as Future>::Output == ()`
--> $DIR/issue-89686.rs:7:17
|
LL | type G<'a, T> = impl Future<Output = ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found associated type
...
LL | async move { self.f().await }
| ------------------ the found `async` block
|
::: $SRC_DIR/core/src/future/mod.rs:LL:COL
|
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
| ------------------------------- the found opaque type
|
= note: expected unit type `()`
found associated type `<impl Future<Output = [async output]> as Future>::Output`
= help: consider constraining the associated type `<impl Future<Output = [async output]> as Future>::Output` to `()`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

error[E0277]: the trait bound `T: Trait` is not satisfied
--> $DIR/issue-89686.rs:7:17
|
Expand All @@ -9,6 +28,7 @@ help: consider restricting type parameter `T`
LL | type G<'a, T: Trait> = impl Future<Output = ()>;
| +++++++

error: aborting due to previous error
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.