From 8e226e092eba650ce0e71d1336519e4045a8ba0e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 21 Feb 2024 14:59:43 +0000 Subject: [PATCH] Add some regression tests for opaque types and const generics --- .../src/traits/fulfill.rs | 3 +- .../src/traits/select/mod.rs | 3 +- tests/ui/const-generics/opaque_types.rs | 13 ++ tests/ui/const-generics/opaque_types.stderr | 125 ++++++++++++++++++ tests/ui/const-generics/opaque_types2.rs | 17 +++ tests/ui/const-generics/opaque_types2.stderr | 15 +++ 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 tests/ui/const-generics/opaque_types.rs create mode 100644 tests/ui/const-generics/opaque_types.stderr create mode 100644 tests/ui/const-generics/opaque_types2.rs create mode 100644 tests/ui/const-generics/opaque_types2.stderr diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index b5be9a2bcb3..77704460257 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -429,7 +429,8 @@ fn process_obligation( // as the cause of an overflow. ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => { match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq( - DefineOpaqueTypes::No, + // Only really excercised by generic_const_exprs + DefineOpaqueTypes::Yes, ct.ty(), ty, ) { diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 926044bd6a8..cd37f9db3e6 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -982,7 +982,8 @@ fn evaluate_predicate_recursively<'o>( ty::PredicateKind::Ambiguous => Ok(EvaluatedToAmbig), ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => { match self.infcx.at(&obligation.cause, obligation.param_env).eq( - DefineOpaqueTypes::No, + // Only really excercised by generic_const_exprs + DefineOpaqueTypes::Yes, ct.ty(), ty, ) { diff --git a/tests/ui/const-generics/opaque_types.rs b/tests/ui/const-generics/opaque_types.rs new file mode 100644 index 00000000000..ccf70f4fb37 --- /dev/null +++ b/tests/ui/const-generics/opaque_types.rs @@ -0,0 +1,13 @@ +#![feature(type_alias_impl_trait)] + +type Foo = impl Sized; +//~^ ERROR: cycle +//~| ERROR: cycle + +fn foo() {} +//~^ ERROR: `Foo` is forbidden as the type of a const generic parameter + +fn main() { + foo::<42>(); + //~^ ERROR: mismatched types +} diff --git a/tests/ui/const-generics/opaque_types.stderr b/tests/ui/const-generics/opaque_types.stderr new file mode 100644 index 00000000000..f03bca69a8b --- /dev/null +++ b/tests/ui/const-generics/opaque_types.stderr @@ -0,0 +1,125 @@ +error[E0308]: mismatched types + --> $DIR/opaque_types.rs:11:11 + | +LL | type Foo = impl Sized; + | ---------- the expected opaque type +... +LL | foo::<42>(); + | ^^ expected opaque type, found integer + | + = note: expected opaque type `Foo` + found type `{integer}` + +error[E0391]: cycle detected when computing type of `Foo::{opaque#0}` + --> $DIR/opaque_types.rs:3:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ + | +note: ...which requires computing type of opaque `Foo::{opaque#0}`... + --> $DIR/opaque_types.rs:3:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ +note: ...which requires type-checking `main`... + --> $DIR/opaque_types.rs:10:1 + | +LL | fn main() { + | ^^^^^^^^^ +note: ...which requires evaluating type-level constant... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires const-evaluating + checking `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires caching mir of `main::{constant#0}` for CTFE... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires elaborating drops for `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ + = note: ...which requires normalizing `Foo`... + = note: ...which again requires computing type of `Foo::{opaque#0}`, completing the cycle +note: cycle used when checking that `Foo::{opaque#0}` is well-formed + --> $DIR/opaque_types.rs:3:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + +error: `Foo` is forbidden as the type of a const generic parameter + --> $DIR/opaque_types.rs:7:17 + | +LL | fn foo() {} + | ^^^ + | + = note: the only supported types are integers, `bool` and `char` + +error[E0391]: cycle detected when computing type of opaque `Foo::{opaque#0}` + --> $DIR/opaque_types.rs:3:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ + | +note: ...which requires type-checking `main`... + --> $DIR/opaque_types.rs:10:1 + | +LL | fn main() { + | ^^^^^^^^^ +note: ...which requires evaluating type-level constant... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires const-evaluating + checking `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires caching mir of `main::{constant#0}` for CTFE... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires elaborating drops for `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires borrow-checking `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires promoting constants in MIR for `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ +note: ...which requires const checking `main::{constant#0}`... + --> $DIR/opaque_types.rs:11:11 + | +LL | foo::<42>(); + | ^^ + = note: ...which requires computing whether `Foo` is freeze... + = note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`... + = note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle +note: cycle used when computing type of `Foo::{opaque#0}` + --> $DIR/opaque_types.rs:3:12 + | +LL | type Foo = impl Sized; + | ^^^^^^^^^^ + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0308, E0391. +For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/const-generics/opaque_types2.rs b/tests/ui/const-generics/opaque_types2.rs new file mode 100644 index 00000000000..fd57438bb61 --- /dev/null +++ b/tests/ui/const-generics/opaque_types2.rs @@ -0,0 +1,17 @@ +#![feature(type_alias_impl_trait)] + +type Foo = impl Sized; + +fn foo() {} + +const C: Foo = 42; + +fn bar() +where + Foo:, +{ + foo::(); + //~^ ERROR: mismatched types +} + +fn main() {} diff --git a/tests/ui/const-generics/opaque_types2.stderr b/tests/ui/const-generics/opaque_types2.stderr new file mode 100644 index 00000000000..2fb1669b7bf --- /dev/null +++ b/tests/ui/const-generics/opaque_types2.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/opaque_types2.rs:13:11 + | +LL | type Foo = impl Sized; + | ---------- the found opaque type +... +LL | foo::(); + | ^ expected `u32`, found opaque type + | + = note: expected type `u32` + found opaque type `Foo` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`.