Skip to content

Commit

Permalink
unconstrained NormalizesTo term for opaques
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Apr 2, 2024
1 parent 2531d08 commit 0e1619d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 42 deletions.
20 changes: 6 additions & 14 deletions compiler/rustc_trait_selection/src/solve/alias_relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

use super::EvalCtxt;
use rustc_infer::traits::query::NoSolution;
use rustc_infer::traits::solve::GoalSource;
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
use rustc_middle::ty::{self, Ty};

Expand Down Expand Up @@ -147,25 +146,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
return None;
}

let ty::Alias(kind, alias) = *ty.kind() else {
let ty::Alias(_, alias) = *ty.kind() else {
return Some(ty);
};

match self.commit_if_ok(|this| {
let tcx = this.tcx();
let normalized_ty = this.next_ty_infer();
let normalizes_to = ty::NormalizesTo { alias, term: normalized_ty.into() };
match kind {
ty::AliasKind::Opaque => {
// HACK: Unlike for associated types, `normalizes-to` for opaques
// is currently not treated as a function. We do not erase the
// expected term.
this.add_goal(GoalSource::Misc, Goal::new(tcx, param_env, normalizes_to));
}
ty::AliasKind::Projection | ty::AliasKind::Inherent | ty::AliasKind::Weak => {
this.add_normalizes_to_goal(Goal::new(tcx, param_env, normalizes_to))
}
}
this.add_normalizes_to_goal(Goal::new(
tcx,
param_env,
ty::NormalizesTo { alias, term: normalized_ty.into() },
));
this.try_evaluate_added_goals()?;
Ok(this.resolve_vars_if_possible(normalized_ty))
}) {
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
&mut self,
goal: Goal<'tcx, NormalizesTo<'tcx>>,
) -> QueryResult<'tcx> {
let def_id = goal.predicate.def_id();
let def_kind = self.tcx().def_kind(def_id);
match def_kind {
DefKind::OpaqueTy => return self.normalize_opaque_type(goal),
_ => self.set_is_normalizes_to_goal(),
}

self.set_is_normalizes_to_goal();
debug_assert!(self.term_is_fully_unconstrained(goal));
let def_id = goal.predicate.def_id();
match self.tcx().def_kind(def_id) {
DefKind::AssocTy | DefKind::AssocConst => {
match self.tcx().associated_item(def_id).container {
Expand All @@ -52,6 +47,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
}
DefKind::AnonConst => self.normalize_anon_const(goal),
DefKind::TyAlias => self.normalize_weak_type(goal),
DefKind::OpaqueTy => self.normalize_opaque_type(goal),
kind => bug!("unknown DefKind {} in normalizes-to goal: {goal:#?}", kind.descr(def_id)),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
}
}

let expected = self.structurally_normalize_ty(goal.param_env, expected)?;
if expected.is_ty_var() {
return self
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
}

// Otherwise, define a new opaque type
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;
self.add_item_bounds_for_hidden_type(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0283]: type annotations needed
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
|
LL | cmp_eq
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq`
Expand All @@ -16,13 +16,13 @@ LL | cmp_eq::<A, B, O>
| +++++++++++

error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O == for<'a, 'b> fn(..., ...) -> ... {cmp_eq::<..., ..., ...>}`
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
|
LL | cmp_eq
| ^^^^^^

error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O == for<'a, 'b> fn(..., ...) -> ... {cmp_eq::<..., ..., ...>}`
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
|
LL | cmp_eq
| ^^^^^^
Expand All @@ -35,14 +35,25 @@ error[E0275]: overflow evaluating the requirement `for<'a, 'b> fn(<O as Scalar>:
LL | ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
| ___________________________________________________^
LL | |
LL | | cmp_eq
LL | |
LL | |
LL | | cmp_eq
... |
LL | |
LL | | }
| |_^

error: aborting due to 4 previous errors
error[E0275]: overflow evaluating the requirement `impl for<'a, 'b> Fn(<A as Scalar>::RefType<'a>, <B as Scalar>::RefType<'b>) -> O: Sized`
--> $DIR/ambig-hr-projection-issue-93340.rs:14:6
|
LL | ) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | cmp_eq
| ------ this returned value is of type `for<'a, 'b> fn(<O as Scalar>::RefType<'a>, <_ as Scalar>::RefType<'b>) -> _ {cmp_eq::<O, _, _>}`
|
= note: the return type of a function must have a statically known size

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0275, E0283.
For more information about an error, try `rustc --explain E0275`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0283]: type annotations needed
--> $DIR/ambig-hr-projection-issue-93340.rs:16:5
--> $DIR/ambig-hr-projection-issue-93340.rs:17:5
|
LL | cmp_eq
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn cmp_eq<'a, 'b, A: Scalar, B: Scalar, O: Scalar>(a: A::RefType<'a>, b: B::RefT
fn build_expression<A: Scalar, B: Scalar, O: Scalar>(
) -> impl Fn(A::RefType<'_>, B::RefType<'_>) -> O {
//[next]~^ ERROR overflow evaluating the requirement
//[next]~| ERROR overflow evaluating the requirement
cmp_eq
//~^ ERROR type annotations needed
//[next]~| ERROR overflow evaluating the requirement
Expand Down
28 changes: 22 additions & 6 deletions tests/ui/impl-trait/recursive-coroutine-boxed.next.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0282]: type annotations needed
--> $DIR/recursive-coroutine-boxed.rs:12:23
--> $DIR/recursive-coroutine-boxed.rs:14:23
|
LL | let mut gen = Box::pin(foo());
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Box`
Expand All @@ -12,12 +12,28 @@ help: consider specifying the generic argument
LL | let mut gen = Box::<T>::pin(foo());
| +++++

error[E0282]: type annotations needed
--> $DIR/recursive-coroutine-boxed.rs:9:13
error[E0308]: mismatched types
--> $DIR/recursive-coroutine-boxed.rs:13:5
|
LL | fn foo() -> impl Coroutine<Yield = (), Return = ()> {
| ---------------------------------------
| |
| the expected opaque type
| expected `impl Coroutine<Yield = (), Return = ()>` because of return type
...
LL | / || {
LL | | let mut gen = Box::pin(foo());
LL | |
LL | | let mut r = gen.as_mut().resume(());
... |
LL | | }
LL | | }
| |_____^ types differ
|
LL | fn foo() -> impl Coroutine<Yield = (), Return = ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for opaque type `impl Coroutine<Yield = (), Return = ()>`
= note: expected opaque type `impl Coroutine<Yield = (), Return = ()>`
found coroutine `{coroutine@$DIR/recursive-coroutine-boxed.rs:13:5: 13:7}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0282`.
Some errors have detailed explanations: E0282, E0308.
For more information about an error, try `rustc --explain E0282`.
6 changes: 4 additions & 2 deletions tests/ui/impl-trait/recursive-coroutine-boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use std::ops::{Coroutine, CoroutineState};

fn foo() -> impl Coroutine<Yield = (), Return = ()> {
//[next]~^ ERROR type annotations needed
|| {
// FIXME(-Znext-solver): this fails with a mismatched types as the
// hidden type of the opaque ends up as {type error}. We should not
// emit errors for such goals.
|| { //[next]~ ERROR mismatched types
let mut gen = Box::pin(foo());
//[next]~^ ERROR type annotations needed
let mut r = gen.as_mut().resume(());
Expand Down

0 comments on commit 0e1619d

Please sign in to comment.