From 06c4cc44b61cab331d0db1f0592ec7d172abf881 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 31 May 2024 08:56:38 +0000 Subject: [PATCH 1/2] Also resolve the type of constants, even if we already turned it into an error constant --- compiler/rustc_hir_typeck/src/writeback.rs | 1 + .../const_generic_type.rs | 10 ++++++++++ .../const_generic_type.stderr | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/ui/type-alias-impl-trait/const_generic_type.rs create mode 100644 tests/ui/type-alias-impl-trait/const_generic_type.stderr diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index 31caa52d26710..e337105f01104 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -865,6 +865,7 @@ impl<'cx, 'tcx> TypeFolder> for Resolver<'cx, 'tcx> { self.handle_term(ct, ty::Const::outer_exclusive_binder, |tcx, guar| { ty::Const::new_error(tcx, guar, ct.ty()) }) + .super_fold_with(self) } fn fold_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { diff --git a/tests/ui/type-alias-impl-trait/const_generic_type.rs b/tests/ui/type-alias-impl-trait/const_generic_type.rs new file mode 100644 index 0000000000000..3af122fc4e33a --- /dev/null +++ b/tests/ui/type-alias-impl-trait/const_generic_type.rs @@ -0,0 +1,10 @@ +//@edition: 2021 + +#![feature(type_alias_impl_trait)] +type Bar = impl std::fmt::Display; + +async fn test() {} +//~^ ERROR: type annotations needed +//~| ERROR: `Bar` is forbidden as the type of a const generic parameter + +fn main() {} diff --git a/tests/ui/type-alias-impl-trait/const_generic_type.stderr b/tests/ui/type-alias-impl-trait/const_generic_type.stderr new file mode 100644 index 0000000000000..5501f23c8bb12 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/const_generic_type.stderr @@ -0,0 +1,19 @@ +error[E0283]: type annotations needed + --> $DIR/const_generic_type.rs:6:1 + | +LL | async fn test() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type + | + = note: cannot satisfy `_: std::fmt::Display` + +error: `Bar` is forbidden as the type of a const generic parameter + --> $DIR/const_generic_type.rs:6:24 + | +LL | async fn test() {} + | ^^^^^^^^^^ + | + = note: the only supported types are integers, `bool` and `char` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0283`. From befcdec7778bc901f47fa8ebd4d5e322a8bd187e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Fri, 31 May 2024 08:58:56 +0000 Subject: [PATCH 2/2] Check that we can constrain the hidden tpye of a TAIT used in a const generic type --- .../const_generic_type.infer.stderr | 10 ++++++++++ ..._type.stderr => const_generic_type.no_infer.stderr} | 8 ++++---- tests/ui/type-alias-impl-trait/const_generic_type.rs | 10 +++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 tests/ui/type-alias-impl-trait/const_generic_type.infer.stderr rename tests/ui/type-alias-impl-trait/{const_generic_type.stderr => const_generic_type.no_infer.stderr} (73%) diff --git a/tests/ui/type-alias-impl-trait/const_generic_type.infer.stderr b/tests/ui/type-alias-impl-trait/const_generic_type.infer.stderr new file mode 100644 index 0000000000000..6a1a770228da7 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/const_generic_type.infer.stderr @@ -0,0 +1,10 @@ +error: `Bar` is forbidden as the type of a const generic parameter + --> $DIR/const_generic_type.rs:7:24 + | +LL | async fn test() { + | ^^^^^^^^^^ + | + = note: the only supported types are integers, `bool` and `char` + +error: aborting due to 1 previous error + diff --git a/tests/ui/type-alias-impl-trait/const_generic_type.stderr b/tests/ui/type-alias-impl-trait/const_generic_type.no_infer.stderr similarity index 73% rename from tests/ui/type-alias-impl-trait/const_generic_type.stderr rename to tests/ui/type-alias-impl-trait/const_generic_type.no_infer.stderr index 5501f23c8bb12..a1a69bfaca372 100644 --- a/tests/ui/type-alias-impl-trait/const_generic_type.stderr +++ b/tests/ui/type-alias-impl-trait/const_generic_type.no_infer.stderr @@ -1,15 +1,15 @@ error[E0283]: type annotations needed - --> $DIR/const_generic_type.rs:6:1 + --> $DIR/const_generic_type.rs:7:1 | -LL | async fn test() {} +LL | async fn test() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type | = note: cannot satisfy `_: std::fmt::Display` error: `Bar` is forbidden as the type of a const generic parameter - --> $DIR/const_generic_type.rs:6:24 + --> $DIR/const_generic_type.rs:7:24 | -LL | async fn test() {} +LL | async fn test() { | ^^^^^^^^^^ | = note: the only supported types are integers, `bool` and `char` diff --git a/tests/ui/type-alias-impl-trait/const_generic_type.rs b/tests/ui/type-alias-impl-trait/const_generic_type.rs index 3af122fc4e33a..95a5e1c62861d 100644 --- a/tests/ui/type-alias-impl-trait/const_generic_type.rs +++ b/tests/ui/type-alias-impl-trait/const_generic_type.rs @@ -1,10 +1,14 @@ //@edition: 2021 +//@revisions: infer no_infer #![feature(type_alias_impl_trait)] type Bar = impl std::fmt::Display; -async fn test() {} -//~^ ERROR: type annotations needed -//~| ERROR: `Bar` is forbidden as the type of a const generic parameter +async fn test() { + //[no_infer]~^ ERROR: type annotations needed + //~^^ ERROR: `Bar` is forbidden as the type of a const generic parameter + #[cfg(infer)] + let x: u32 = N; +} fn main() {}