From e1d76818b2b505e88bcd0c965da945e7e2e4329e Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 2 Oct 2020 12:02:41 -0700 Subject: [PATCH 1/8] Add `#![feature(const_fn_impl)]` --- compiler/rustc_feature/src/active.rs | 3 +++ .../rustc_mir/src/transform/check_consts/ops.rs | 13 +++++++++++-- compiler/rustc_span/src/symbol.rs | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index cd018ae1204..19da310b3b2 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -596,6 +596,9 @@ declare_features! ( /// Allows rustc to inject a default alloc_error_handler (active, default_alloc_error_handler, "1.48.0", Some(66741), None), + /// Allows argument and return position `impl Trait` in a `const fn`. + (active, const_fn_impl_trait, "1.48.0", Some(77463), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs index 9a1b77e994d..91008f00930 100644 --- a/compiler/rustc_mir/src/transform/check_consts/ops.rs +++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs @@ -559,11 +559,20 @@ pub mod ty { pub struct ImplTrait; impl NonConstOp for ImplTrait { fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status { - mcf_status_in_item(ccx) + if ccx.const_kind() != hir::ConstContext::ConstFn { + Status::Allowed + } else { + Status::Unstable(sym::const_fn_impl_trait) + } } fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { - mcf_build_error(ccx, span, "`impl Trait` in const fn is unstable") + feature_err( + &ccx.tcx.sess.parse_sess, + sym::const_fn_impl_trait, + span, + &format!("`impl Trait` is not allowed in {}s", ccx.const_kind()), + ) } } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 6309b00f5f5..123f1f76765 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -355,6 +355,7 @@ symbols! { const_fn, const_fn_floating_point_arithmetic, const_fn_fn_ptr_basics, + const_fn_impl_trait, const_fn_transmute, const_fn_union, const_generics, From c959eefa74858785b1d06e6a62c51a905aa75ca7 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 2 Oct 2020 12:11:24 -0700 Subject: [PATCH 2/8] Add requisite feature gates in the standard library --- library/core/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 49cec162762..d96e82f1d5e 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -83,6 +83,7 @@ #![feature(const_fn)] #![feature(const_fn_union)] #![feature(const_assume)] +#![cfg_attr(not(bootstrap), feature(const_fn_impl_trait))] #![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))] #![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))] #![feature(const_generics)] From 7a0c66bad10a7a97111d2438f29be418c16e0a73 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 2 Oct 2020 12:12:02 -0700 Subject: [PATCH 3/8] Use new feature gate in `impl-trait` tests --- src/test/ui/type-alias-impl-trait/issue-53096.rs | 2 +- .../type-alias-impl-trait/issue-53678-generator-and-const-fn.rs | 2 +- src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs | 2 +- src/test/ui/type-alias-impl-trait/structural-match.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/ui/type-alias-impl-trait/issue-53096.rs b/src/test/ui/type-alias-impl-trait/issue-53096.rs index bdf426bbd37..1d9b56835a2 100644 --- a/src/test/ui/type-alias-impl-trait/issue-53096.rs +++ b/src/test/ui/type-alias-impl-trait/issue-53096.rs @@ -1,5 +1,5 @@ // check-pass -#![feature(const_fn, const_fn_fn_ptr_basics)] +#![feature(const_fn_impl_trait, const_fn_fn_ptr_basics)] #![feature(type_alias_impl_trait)] type Foo = impl Fn() -> usize; diff --git a/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs b/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs index e7f93732430..eaba1ade0e1 100644 --- a/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs +++ b/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs @@ -1,6 +1,6 @@ // check-pass -#![feature(const_fn, generators, generator_trait, type_alias_impl_trait)] +#![feature(const_fn_impl_trait, generators, generator_trait, type_alias_impl_trait)] use std::ops::Generator; diff --git a/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs b/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs index d50835608fa..e0bf5762b4a 100644 --- a/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs +++ b/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs @@ -1,4 +1,4 @@ -#![feature(const_fn, type_alias_impl_trait)] +#![feature(const_fn_impl_trait, type_alias_impl_trait)] type Bar = impl Send; diff --git a/src/test/ui/type-alias-impl-trait/structural-match.rs b/src/test/ui/type-alias-impl-trait/structural-match.rs index a3ff4ad1d47..9cedcd4ae48 100644 --- a/src/test/ui/type-alias-impl-trait/structural-match.rs +++ b/src/test/ui/type-alias-impl-trait/structural-match.rs @@ -1,4 +1,4 @@ -#![feature(const_fn, type_alias_impl_trait)] +#![feature(const_fn_impl_trait, type_alias_impl_trait)] type Foo = impl Send; From 2e412fc8e4165eb88aa552ff43d1e91968455ad2 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 2 Oct 2020 12:12:57 -0700 Subject: [PATCH 4/8] Bless test outupt --- .../min_const_fn/min_const_fn_impl_trait.rs | 5 ++--- .../min_const_fn/min_const_fn_impl_trait.stderr | 16 ++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs index 9cc9b69ac0b..7c64d5522c4 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs @@ -1,9 +1,8 @@ struct AlanTuring(T); -const fn no_rpit2() -> AlanTuring { - //~^ ERROR `impl Trait` in const fn is unstable +const fn no_rpit2() -> AlanTuring { //~ `impl Trait` AlanTuring(0) } -const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable +const fn no_rpit() -> impl std::fmt::Debug {} //~ `impl Trait` fn main() {} diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr index a62a340332d..7229e007f40 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr @@ -1,21 +1,21 @@ -error[E0723]: `impl Trait` in const fn is unstable +error[E0658]: `impl Trait` is not allowed in constant functions --> $DIR/min_const_fn_impl_trait.rs:2:24 | LL | const fn no_rpit2() -> AlanTuring { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: see issue #57563 for more information - = help: add `#![feature(const_fn)]` to the crate attributes to enable + = note: see issue #77463 for more information + = help: add `#![feature(const_fn_impl_trait)]` to the crate attributes to enable -error[E0723]: `impl Trait` in const fn is unstable - --> $DIR/min_const_fn_impl_trait.rs:7:23 +error[E0658]: `impl Trait` is not allowed in constant functions + --> $DIR/min_const_fn_impl_trait.rs:6:23 | LL | const fn no_rpit() -> impl std::fmt::Debug {} | ^^^^^^^^^^^^^^^^^^^^ | - = note: see issue #57563 for more information - = help: add `#![feature(const_fn)]` to the crate attributes to enable + = note: see issue #77463 for more information + = help: add `#![feature(const_fn_impl_trait)]` to the crate attributes to enable error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0723`. +For more information about this error, try `rustc --explain E0658`. From af03b1143ede7bcb3f579fa3cf3ea3e579e2c2fc Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Fri, 2 Oct 2020 14:57:12 -0700 Subject: [PATCH 5/8] Make `min_const_fn` `impl Trait` test into a gate test --- src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs | 2 ++ .../ui/consts/min_const_fn/min_const_fn_impl_trait.stderr | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs index 7c64d5522c4..a8642d18b56 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs @@ -1,3 +1,5 @@ +// gate-test-const_fn_impl_trait + struct AlanTuring(T); const fn no_rpit2() -> AlanTuring { //~ `impl Trait` AlanTuring(0) diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr index 7229e007f40..1a49b59733d 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr @@ -1,5 +1,5 @@ error[E0658]: `impl Trait` is not allowed in constant functions - --> $DIR/min_const_fn_impl_trait.rs:2:24 + --> $DIR/min_const_fn_impl_trait.rs:4:24 | LL | const fn no_rpit2() -> AlanTuring { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | const fn no_rpit2() -> AlanTuring { = help: add `#![feature(const_fn_impl_trait)]` to the crate attributes to enable error[E0658]: `impl Trait` is not allowed in constant functions - --> $DIR/min_const_fn_impl_trait.rs:6:23 + --> $DIR/min_const_fn_impl_trait.rs:8:23 | LL | const fn no_rpit() -> impl std::fmt::Debug {} | ^^^^^^^^^^^^^^^^^^^^ From c4ef5fdf8f79af3354df5a43c43501b1f03997d5 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Mon, 5 Oct 2020 20:30:13 -0700 Subject: [PATCH 6/8] Remove `fn` from feature name --- compiler/rustc_feature/src/active.rs | 2 +- compiler/rustc_mir/src/transform/check_consts/ops.rs | 4 ++-- compiler/rustc_span/src/symbol.rs | 2 +- library/core/src/lib.rs | 2 +- src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs | 2 +- .../ui/consts/min_const_fn/min_const_fn_impl_trait.stderr | 4 ++-- src/test/ui/type-alias-impl-trait/issue-53096.rs | 2 +- .../issue-53678-generator-and-const-fn.rs | 2 +- src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs | 2 +- src/test/ui/type-alias-impl-trait/structural-match.rs | 2 +- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 19da310b3b2..1982d098542 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -597,7 +597,7 @@ declare_features! ( (active, default_alloc_error_handler, "1.48.0", Some(66741), None), /// Allows argument and return position `impl Trait` in a `const fn`. - (active, const_fn_impl_trait, "1.48.0", Some(77463), None), + (active, const_impl_trait, "1.48.0", Some(77463), None), // ------------------------------------------------------------------------- // feature-group-end: actual feature gates diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs index 91008f00930..e0570ab622e 100644 --- a/compiler/rustc_mir/src/transform/check_consts/ops.rs +++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs @@ -562,14 +562,14 @@ pub mod ty { if ccx.const_kind() != hir::ConstContext::ConstFn { Status::Allowed } else { - Status::Unstable(sym::const_fn_impl_trait) + Status::Unstable(sym::const_impl_trait) } } fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { feature_err( &ccx.tcx.sess.parse_sess, - sym::const_fn_impl_trait, + sym::const_impl_trait, span, &format!("`impl Trait` is not allowed in {}s", ccx.const_kind()), ) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 123f1f76765..a2184c00a28 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -355,11 +355,11 @@ symbols! { const_fn, const_fn_floating_point_arithmetic, const_fn_fn_ptr_basics, - const_fn_impl_trait, const_fn_transmute, const_fn_union, const_generics, const_if_match, + const_impl_trait, const_in_array_repeat_expressions, const_indexing, const_let, diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index d96e82f1d5e..3aa68aa1d8d 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -83,7 +83,7 @@ #![feature(const_fn)] #![feature(const_fn_union)] #![feature(const_assume)] -#![cfg_attr(not(bootstrap), feature(const_fn_impl_trait))] +#![cfg_attr(not(bootstrap), feature(const_impl_trait))] #![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))] #![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))] #![feature(const_generics)] diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs index a8642d18b56..e062c9f0aa3 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.rs @@ -1,4 +1,4 @@ -// gate-test-const_fn_impl_trait +// gate-test-const_impl_trait struct AlanTuring(T); const fn no_rpit2() -> AlanTuring { //~ `impl Trait` diff --git a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr index 1a49b59733d..01c797cd96b 100644 --- a/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr +++ b/src/test/ui/consts/min_const_fn/min_const_fn_impl_trait.stderr @@ -5,7 +5,7 @@ LL | const fn no_rpit2() -> AlanTuring { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: see issue #77463 for more information - = help: add `#![feature(const_fn_impl_trait)]` to the crate attributes to enable + = help: add `#![feature(const_impl_trait)]` to the crate attributes to enable error[E0658]: `impl Trait` is not allowed in constant functions --> $DIR/min_const_fn_impl_trait.rs:8:23 @@ -14,7 +14,7 @@ LL | const fn no_rpit() -> impl std::fmt::Debug {} | ^^^^^^^^^^^^^^^^^^^^ | = note: see issue #77463 for more information - = help: add `#![feature(const_fn_impl_trait)]` to the crate attributes to enable + = help: add `#![feature(const_impl_trait)]` to the crate attributes to enable error: aborting due to 2 previous errors diff --git a/src/test/ui/type-alias-impl-trait/issue-53096.rs b/src/test/ui/type-alias-impl-trait/issue-53096.rs index 1d9b56835a2..6e1973bd18a 100644 --- a/src/test/ui/type-alias-impl-trait/issue-53096.rs +++ b/src/test/ui/type-alias-impl-trait/issue-53096.rs @@ -1,5 +1,5 @@ // check-pass -#![feature(const_fn_impl_trait, const_fn_fn_ptr_basics)] +#![feature(const_impl_trait, const_fn_fn_ptr_basics)] #![feature(type_alias_impl_trait)] type Foo = impl Fn() -> usize; diff --git a/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs b/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs index eaba1ade0e1..4582d5386f0 100644 --- a/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs +++ b/src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs @@ -1,6 +1,6 @@ // check-pass -#![feature(const_fn_impl_trait, generators, generator_trait, type_alias_impl_trait)] +#![feature(const_impl_trait, generators, generator_trait, type_alias_impl_trait)] use std::ops::Generator; diff --git a/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs b/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs index e0bf5762b4a..58f0f5b2f65 100644 --- a/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs +++ b/src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs @@ -1,4 +1,4 @@ -#![feature(const_fn_impl_trait, type_alias_impl_trait)] +#![feature(const_impl_trait, type_alias_impl_trait)] type Bar = impl Send; diff --git a/src/test/ui/type-alias-impl-trait/structural-match.rs b/src/test/ui/type-alias-impl-trait/structural-match.rs index 9cedcd4ae48..74ffa608426 100644 --- a/src/test/ui/type-alias-impl-trait/structural-match.rs +++ b/src/test/ui/type-alias-impl-trait/structural-match.rs @@ -1,4 +1,4 @@ -#![feature(const_fn_impl_trait, type_alias_impl_trait)] +#![feature(const_impl_trait, type_alias_impl_trait)] type Foo = impl Send; From 9beb6f81e4112eccf9965d46421a1da351b0593a Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Mon, 5 Oct 2020 20:30:32 -0700 Subject: [PATCH 7/8] Make `impl Trait` unstable in all contexts --- compiler/rustc_mir/src/transform/check_consts/ops.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs index e0570ab622e..63b20c7c027 100644 --- a/compiler/rustc_mir/src/transform/check_consts/ops.rs +++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs @@ -558,12 +558,8 @@ pub mod ty { #[derive(Debug)] pub struct ImplTrait; impl NonConstOp for ImplTrait { - fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status { - if ccx.const_kind() != hir::ConstContext::ConstFn { - Status::Allowed - } else { - Status::Unstable(sym::const_impl_trait) - } + fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status { + Status::Unstable(sym::const_impl_trait) } fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { From b5693a39d9d7d1b5404c188899bf7d983c79dfe3 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Mon, 5 Oct 2020 21:45:55 -0700 Subject: [PATCH 8/8] Update error code page --- .../rustc_error_codes/src/error_codes/E0723.md | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0723.md b/compiler/rustc_error_codes/src/error_codes/E0723.md index 95d47ab21cb..bc224421915 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0723.md +++ b/compiler/rustc_error_codes/src/error_codes/E0723.md @@ -3,12 +3,8 @@ An unstable feature in `const` contexts was used. Erroneous code example: ```compile_fail,E0723 -trait T {} - -impl T for () {} - -const fn foo() -> impl T { // error: `impl Trait` in const fn is unstable - () +const fn foo(_: T) { // error! + // ... } ``` @@ -18,11 +14,7 @@ feature flag: ``` #![feature(const_fn)] -trait T {} - -impl T for () {} - -const fn foo() -> impl T { - () +const fn foo(_: T) { // ok! + // ... } ```