Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4437b4b

Browse files
committedOct 7, 2020
Auto merge of rust-lang#77464 - ecstatic-morse:const-fn-impl-trait, r=oli-obk
Give `impl Trait` in a `const fn` its own feature gate ...previously it was gated under `#![feature(const_fn)]`. I think we actually want to do this in all const-contexts? If so, this should be `#![feature(const_impl_trait)]` instead. I don't think there's any way to make use of `impl Trait` within a `const` initializer. cc rust-lang#77463 r? `@oli-obk`
2 parents 28928c7 + b5693a3 commit 4437b4b

File tree

11 files changed

+34
-31
lines changed

11 files changed

+34
-31
lines changed
 

‎compiler/rustc_error_codes/src/error_codes/E0723.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ An unstable feature in `const` contexts was used.
33
Erroneous code example:
44

55
```compile_fail,E0723
6-
trait T {}
7-
8-
impl T for () {}
9-
10-
const fn foo() -> impl T { // error: `impl Trait` in const fn is unstable
11-
()
6+
const fn foo<T: Copy>(_: T) { // error!
7+
// ...
128
}
139
```
1410

@@ -18,11 +14,7 @@ feature flag:
1814
```
1915
#![feature(const_fn)]
2016
21-
trait T {}
22-
23-
impl T for () {}
24-
25-
const fn foo() -> impl T {
26-
()
17+
const fn foo<T: Copy>(_: T) { // ok!
18+
// ...
2719
}
2820
```

‎compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ declare_features! (
596596
/// Allows rustc to inject a default alloc_error_handler
597597
(active, default_alloc_error_handler, "1.48.0", Some(66741), None),
598598

599+
/// Allows argument and return position `impl Trait` in a `const fn`.
600+
(active, const_impl_trait, "1.48.0", Some(77463), None),
601+
599602
// -------------------------------------------------------------------------
600603
// feature-group-end: actual feature gates
601604
// -------------------------------------------------------------------------

‎compiler/rustc_mir/src/transform/check_consts/ops.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,17 @@ pub mod ty {
558558
#[derive(Debug)]
559559
pub struct ImplTrait;
560560
impl NonConstOp for ImplTrait {
561-
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
562-
mcf_status_in_item(ccx)
561+
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
562+
Status::Unstable(sym::const_impl_trait)
563563
}
564564

565565
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
566-
mcf_build_error(ccx, span, "`impl Trait` in const fn is unstable")
566+
feature_err(
567+
&ccx.tcx.sess.parse_sess,
568+
sym::const_impl_trait,
569+
span,
570+
&format!("`impl Trait` is not allowed in {}s", ccx.const_kind()),
571+
)
567572
}
568573
}
569574

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ symbols! {
359359
const_fn_union,
360360
const_generics,
361361
const_if_match,
362+
const_impl_trait,
362363
const_in_array_repeat_expressions,
363364
const_indexing,
364365
const_let,

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#![feature(const_fn)]
8484
#![feature(const_fn_union)]
8585
#![feature(const_assume)]
86+
#![cfg_attr(not(bootstrap), feature(const_impl_trait))]
8687
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
8788
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
8889
#![feature(const_generics)]
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// gate-test-const_impl_trait
2+
13
struct AlanTuring<T>(T);
2-
const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> {
3-
//~^ ERROR `impl Trait` in const fn is unstable
4+
const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { //~ `impl Trait`
45
AlanTuring(0)
56
}
67

7-
const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
8+
const fn no_rpit() -> impl std::fmt::Debug {} //~ `impl Trait`
89

910
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
error[E0723]: `impl Trait` in const fn is unstable
2-
--> $DIR/min_const_fn_impl_trait.rs:2:24
1+
error[E0658]: `impl Trait` is not allowed in constant functions
2+
--> $DIR/min_const_fn_impl_trait.rs:4:24
33
|
44
LL | const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
7+
= note: see issue #77463 <https://github.com/rust-lang/rust/issues/77463> for more information
8+
= help: add `#![feature(const_impl_trait)]` to the crate attributes to enable
99

10-
error[E0723]: `impl Trait` in const fn is unstable
11-
--> $DIR/min_const_fn_impl_trait.rs:7:23
10+
error[E0658]: `impl Trait` is not allowed in constant functions
11+
--> $DIR/min_const_fn_impl_trait.rs:8:23
1212
|
1313
LL | const fn no_rpit() -> impl std::fmt::Debug {}
1414
| ^^^^^^^^^^^^^^^^^^^^
1515
|
16-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
16+
= note: see issue #77463 <https://github.com/rust-lang/rust/issues/77463> for more information
17+
= help: add `#![feature(const_impl_trait)]` to the crate attributes to enable
1818

1919
error: aborting due to 2 previous errors
2020

21-
For more information about this error, try `rustc --explain E0723`.
21+
For more information about this error, try `rustc --explain E0658`.

‎src/test/ui/type-alias-impl-trait/issue-53096.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// check-pass
2-
#![feature(const_fn, const_fn_fn_ptr_basics)]
2+
#![feature(const_impl_trait, const_fn_fn_ptr_basics)]
33
#![feature(type_alias_impl_trait)]
44

55
type Foo = impl Fn() -> usize;

‎src/test/ui/type-alias-impl-trait/issue-53678-generator-and-const-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(const_fn, generators, generator_trait, type_alias_impl_trait)]
3+
#![feature(const_impl_trait, generators, generator_trait, type_alias_impl_trait)]
44

55
use std::ops::Generator;
66

‎src/test/ui/type-alias-impl-trait/structural-match-no-leak.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(const_fn, type_alias_impl_trait)]
1+
#![feature(const_impl_trait, type_alias_impl_trait)]
22

33
type Bar = impl Send;
44

‎src/test/ui/type-alias-impl-trait/structural-match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(const_fn, type_alias_impl_trait)]
1+
#![feature(const_impl_trait, type_alias_impl_trait)]
22

33
type Foo = impl Send;
44

0 commit comments

Comments
 (0)