Skip to content

Commit b00abbe

Browse files
authored
Rollup merge of rust-lang#68114 - ecstatic-morse:fix-feature-gating, r=Centril
Don't require `allow_internal_unstable` unless `staged_api` is enabled. rust-lang#63770 changed `qualify_min_const_fn` to require `allow_internal_unstable` for *all* crates that used an unstable feature, regardless of whether `staged_api` was enabled or the `fn` that used that feature was stably const. In practice, this meant that every crate in the ecosystem that wanted to use nightly features added `#![feature(const_fn)]`, which skips `qualify_min_const_fn` entirely. After this PR, crates that do not have `#![feature(staged_api)]` will only need to enable the feature they are interested in. For example, `#![feature(const_if_match)]` will be enough to enable `if` and `match` in constants. Crates with `staged_api` (e.g., `libstd`) require `#[allow_internal_unstable]` to be added to a function if it uses nightly features unless that function is also marked `#[rustc_const_unstable]`. This prevents proliferation of `#[allow_internal_unstable]` into functions that are not callable in a `const` context on stable. r? @oli-obk (author of rust-lang#63770) cc @Centril
2 parents 544c981 + fc30825 commit b00abbe

19 files changed

+66
-41
lines changed

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
#![feature(bound_cloned)]
7171
#![feature(cfg_target_has_atomic)]
7272
#![feature(concat_idents)]
73-
#![feature(const_fn)]
7473
#![feature(const_if_match)]
7574
#![feature(const_panic)]
7675
#![feature(const_fn_union)]

src/libcore/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#![feature(slice_internals)]
3232
#![feature(slice_partition_dedup)]
3333
#![feature(int_error_matching)]
34-
#![feature(const_fn)]
3534
#![feature(array_value_iter)]
3635
#![feature(iter_partition_in_place)]
3736
#![feature(iter_is_partitioned)]

src/libproc_macro/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#![feature(nll)]
2222
#![feature(staged_api)]
2323
#![feature(allow_internal_unstable)]
24-
#![feature(const_fn)]
2524
#![feature(decl_macro)]
2625
#![feature(extern_types)]
2726
#![feature(in_band_lifetimes)]

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#![feature(bool_to_option)]
3232
#![feature(box_patterns)]
3333
#![feature(box_syntax)]
34-
#![feature(const_fn)]
3534
#![feature(const_transmute)]
3635
#![feature(core_intrinsics)]
3736
#![feature(drain_filter)]

src/librustc_hir/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/hir.html
44
55
#![feature(crate_visibility_modifier)]
6-
#![feature(const_fn)]
6+
#![feature(const_fn)] // For the unsizing cast on `&[]`
77
#![feature(in_band_lifetimes)]
88
#![feature(specialization)]
99
#![recursion_limit = "256"]

src/librustc_mir/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
1313
#![feature(box_syntax)]
1414
#![feature(crate_visibility_modifier)]
1515
#![feature(core_intrinsics)]
16-
#![feature(const_fn)]
1716
#![feature(decl_macro)]
1817
#![feature(drain_filter)]
1918
#![feature(exhaustive_patterns)]

src/librustc_mir/transform/qualify_min_const_fn.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,22 @@ fn check_place(
281281
Ok(())
282282
}
283283

284-
/// Returns whether `allow_internal_unstable(..., <feature_gate>, ...)` is present.
284+
/// Returns `true` if the given feature gate is allowed within the function with the given `DefId`.
285285
fn feature_allowed(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {
286+
// All features require that the corresponding gate be enabled,
287+
// even if the function has `#[allow_internal_unstable(the_gate)]`.
288+
if !tcx.features().enabled(feature_gate) {
289+
return false;
290+
}
291+
292+
// If this crate is not using stability attributes, or this function is not claiming to be a
293+
// stable `const fn`, that is all that is required.
294+
if !tcx.features().staged_api || tcx.has_attr(def_id, sym::rustc_const_unstable) {
295+
return true;
296+
}
297+
298+
// However, we cannot allow stable `const fn`s to use unstable features without an explicit
299+
// opt-in via `allow_internal_unstable`.
286300
attr::allow_internal_unstable(&tcx.get_attrs(def_id), &tcx.sess.diagnostic())
287301
.map_or(false, |mut features| features.any(|name| name == feature_gate))
288302
}

src/librustc_span/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
8-
#![feature(const_fn)]
98
#![feature(crate_visibility_modifier)]
109
#![feature(nll)]
1110
#![feature(optin_builtin_traits)]

src/librustdoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![feature(test)]
1313
#![feature(ptr_offset_from)]
1414
#![feature(crate_visibility_modifier)]
15-
#![feature(const_fn)]
1615
#![feature(drain_filter)]
1716
#![feature(never_type)]
1817
#![feature(unicode_internals)]

src/libsyntax/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
88
#![feature(bool_to_option)]
99
#![feature(box_syntax)]
10-
#![feature(const_fn)]
10+
#![feature(const_fn)] // For the `transmute` in `P::new`
1111
#![feature(const_transmute)]
1212
#![feature(crate_visibility_modifier)]
1313
#![feature(label_break_value)]

src/test/ui/consts/const-mut-refs/const_mut_refs.rs

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

33
#![feature(const_mut_refs)]
4-
#![feature(const_fn)]
54

65
struct Foo {
76
x: usize

src/test/ui/consts/control-flow/exhaustive-c-like-enum-match.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// check-pass
44

55
#![feature(const_if_match)]
6-
#![feature(const_fn)]
76

87
enum E {
98
A,

src/test/ui/consts/control-flow/feature-gate-const-if-match.if_match.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: fatal error triggered by #[rustc_error]
2-
--> $DIR/feature-gate-const-if-match.rs:109:1
2+
--> $DIR/feature-gate-const-if-match.rs:108:1
33
|
44
LL | / fn main() {
55
LL | | let _ = [0; {

src/test/ui/consts/control-flow/feature-gate-const-if-match.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#![feature(rustc_attrs)]
88
#![cfg_attr(if_match, feature(const_if_match))]
9-
#![feature(const_fn)]
109

1110
const _: i32 = if true { //[stock]~ ERROR `if` is not allowed in a `const`
1211
5

src/test/ui/consts/control-flow/feature-gate-const-if-match.stock.stderr

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: `if` is not allowed in a `const`
2-
--> $DIR/feature-gate-const-if-match.rs:11:16
2+
--> $DIR/feature-gate-const-if-match.rs:10:16
33
|
44
LL | const _: i32 = if true {
55
| ________________^
@@ -13,7 +13,7 @@ LL | | };
1313
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
1414

1515
error[E0658]: `if` is not allowed in a `const`
16-
--> $DIR/feature-gate-const-if-match.rs:17:16
16+
--> $DIR/feature-gate-const-if-match.rs:16:16
1717
|
1818
LL | const _: i32 = if let Some(true) = Some(false) {
1919
| ________________^
@@ -27,7 +27,7 @@ LL | | };
2727
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
2828

2929
error[E0658]: `match` is not allowed in a `const`
30-
--> $DIR/feature-gate-const-if-match.rs:23:16
30+
--> $DIR/feature-gate-const-if-match.rs:22:16
3131
|
3232
LL | const _: i32 = match 1 {
3333
| ________________^
@@ -41,7 +41,7 @@ LL | | };
4141
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
4242

4343
error[E0658]: `if` is not allowed in a `static`
44-
--> $DIR/feature-gate-const-if-match.rs:30:13
44+
--> $DIR/feature-gate-const-if-match.rs:29:13
4545
|
4646
LL | let x = if true { 0 } else { 1 };
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -50,7 +50,7 @@ LL | let x = if true { 0 } else { 1 };
5050
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
5151

5252
error[E0658]: `match` is not allowed in a `static`
53-
--> $DIR/feature-gate-const-if-match.rs:32:13
53+
--> $DIR/feature-gate-const-if-match.rs:31:13
5454
|
5555
LL | let x = match x { 0 => 1, _ => 0 };
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -59,7 +59,7 @@ LL | let x = match x { 0 => 1, _ => 0 };
5959
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
6060

6161
error[E0658]: `if` is not allowed in a `static`
62-
--> $DIR/feature-gate-const-if-match.rs:34:5
62+
--> $DIR/feature-gate-const-if-match.rs:33:5
6363
|
6464
LL | if let Some(x) = Some(x) { x } else { 1 }
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -68,7 +68,7 @@ LL | if let Some(x) = Some(x) { x } else { 1 }
6868
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
6969

7070
error[E0658]: `if` is not allowed in a `static mut`
71-
--> $DIR/feature-gate-const-if-match.rs:39:13
71+
--> $DIR/feature-gate-const-if-match.rs:38:13
7272
|
7373
LL | let x = if true { 0 } else { 1 };
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL | let x = if true { 0 } else { 1 };
7777
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
7878

7979
error[E0658]: `match` is not allowed in a `static mut`
80-
--> $DIR/feature-gate-const-if-match.rs:41:13
80+
--> $DIR/feature-gate-const-if-match.rs:40:13
8181
|
8282
LL | let x = match x { 0 => 1, _ => 0 };
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -86,7 +86,7 @@ LL | let x = match x { 0 => 1, _ => 0 };
8686
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
8787

8888
error[E0658]: `if` is not allowed in a `static mut`
89-
--> $DIR/feature-gate-const-if-match.rs:43:5
89+
--> $DIR/feature-gate-const-if-match.rs:42:5
9090
|
9191
LL | if let Some(x) = Some(x) { x } else { 1 }
9292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -95,7 +95,7 @@ LL | if let Some(x) = Some(x) { x } else { 1 }
9595
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
9696

9797
error[E0658]: `if` is not allowed in a `const fn`
98-
--> $DIR/feature-gate-const-if-match.rs:48:5
98+
--> $DIR/feature-gate-const-if-match.rs:47:5
9999
|
100100
LL | if true { 5 } else { 6 }
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -104,7 +104,7 @@ LL | if true { 5 } else { 6 }
104104
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
105105

106106
error[E0658]: `if` is not allowed in a `const fn`
107-
--> $DIR/feature-gate-const-if-match.rs:52:5
107+
--> $DIR/feature-gate-const-if-match.rs:51:5
108108
|
109109
LL | / if let Some(true) = a {
110110
LL | | 0
@@ -117,7 +117,7 @@ LL | | }
117117
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
118118

119119
error[E0658]: `match` is not allowed in a `const fn`
120-
--> $DIR/feature-gate-const-if-match.rs:60:5
120+
--> $DIR/feature-gate-const-if-match.rs:59:5
121121
|
122122
LL | / match i {
123123
LL | | i if i > 10 => i,
@@ -130,7 +130,7 @@ LL | | }
130130
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
131131

132132
error[E0658]: `if` is not allowed in a `const fn`
133-
--> $DIR/feature-gate-const-if-match.rs:91:17
133+
--> $DIR/feature-gate-const-if-match.rs:90:17
134134
|
135135
LL | let x = if y { 0 } else { 1 };
136136
| ^^^^^^^^^^^^^^^^^^^^^
@@ -139,7 +139,7 @@ LL | let x = if y { 0 } else { 1 };
139139
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
140140

141141
error[E0658]: `match` is not allowed in a `const fn`
142-
--> $DIR/feature-gate-const-if-match.rs:93:17
142+
--> $DIR/feature-gate-const-if-match.rs:92:17
143143
|
144144
LL | let x = match x { 0 => 1, _ => 0 };
145145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -148,7 +148,7 @@ LL | let x = match x { 0 => 1, _ => 0 };
148148
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
149149

150150
error[E0658]: `if` is not allowed in a `const fn`
151-
--> $DIR/feature-gate-const-if-match.rs:95:9
151+
--> $DIR/feature-gate-const-if-match.rs:94:9
152152
|
153153
LL | if let Some(x) = Some(x) { x } else { 1 }
154154
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -157,7 +157,7 @@ LL | if let Some(x) = Some(x) { x } else { 1 }
157157
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
158158

159159
error[E0658]: `if` is not allowed in a `const`
160-
--> $DIR/feature-gate-const-if-match.rs:111:17
160+
--> $DIR/feature-gate-const-if-match.rs:110:17
161161
|
162162
LL | let x = if false { 0 } else { 1 };
163163
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -166,7 +166,7 @@ LL | let x = if false { 0 } else { 1 };
166166
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
167167

168168
error[E0658]: `match` is not allowed in a `const`
169-
--> $DIR/feature-gate-const-if-match.rs:113:17
169+
--> $DIR/feature-gate-const-if-match.rs:112:17
170170
|
171171
LL | let x = match x { 0 => 1, _ => 0 };
172172
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -175,7 +175,7 @@ LL | let x = match x { 0 => 1, _ => 0 };
175175
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
176176

177177
error[E0658]: `if` is not allowed in a `const`
178-
--> $DIR/feature-gate-const-if-match.rs:115:9
178+
--> $DIR/feature-gate-const-if-match.rs:114:9
179179
|
180180
LL | if let Some(x) = Some(x) { x } else { 1 }
181181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -184,7 +184,7 @@ LL | if let Some(x) = Some(x) { x } else { 1 }
184184
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
185185

186186
error[E0658]: `if` is not allowed in a `const`
187-
--> $DIR/feature-gate-const-if-match.rs:68:21
187+
--> $DIR/feature-gate-const-if-match.rs:67:21
188188
|
189189
LL | const IF: i32 = if true { 5 } else { 6 };
190190
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -193,7 +193,7 @@ LL | const IF: i32 = if true { 5 } else { 6 };
193193
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
194194

195195
error[E0658]: `if` is not allowed in a `const`
196-
--> $DIR/feature-gate-const-if-match.rs:71:25
196+
--> $DIR/feature-gate-const-if-match.rs:70:25
197197
|
198198
LL | const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
199199
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -202,7 +202,7 @@ LL | const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
202202
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
203203

204204
error[E0658]: `match` is not allowed in a `const`
205-
--> $DIR/feature-gate-const-if-match.rs:74:24
205+
--> $DIR/feature-gate-const-if-match.rs:73:24
206206
|
207207
LL | const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
208208
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -211,7 +211,7 @@ LL | const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
211211
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
212212

213213
error[E0658]: `if` is not allowed in a `const`
214-
--> $DIR/feature-gate-const-if-match.rs:79:21
214+
--> $DIR/feature-gate-const-if-match.rs:78:21
215215
|
216216
LL | const IF: i32 = if true { 5 } else { 6 };
217217
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -220,7 +220,7 @@ LL | const IF: i32 = if true { 5 } else { 6 };
220220
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
221221

222222
error[E0658]: `if` is not allowed in a `const`
223-
--> $DIR/feature-gate-const-if-match.rs:82:25
223+
--> $DIR/feature-gate-const-if-match.rs:81:25
224224
|
225225
LL | const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
226226
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -229,7 +229,7 @@ LL | const IF_LET: i32 = if let Some(true) = None { 5 } else { 6 };
229229
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
230230

231231
error[E0658]: `match` is not allowed in a `const`
232-
--> $DIR/feature-gate-const-if-match.rs:85:24
232+
--> $DIR/feature-gate-const-if-match.rs:84:24
233233
|
234234
LL | const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
235235
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -238,7 +238,7 @@ LL | const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
238238
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
239239

240240
error[E0019]: constant contains unimplemented expression type
241-
--> $DIR/feature-gate-const-if-match.rs:115:21
241+
--> $DIR/feature-gate-const-if-match.rs:114:21
242242
|
243243
LL | if let Some(x) = Some(x) { x } else { 1 }
244244
| ^

src/test/ui/consts/control-flow/short-circuit-let.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#![feature(const_if_match)]
66
#![feature(const_panic)]
7-
#![feature(const_fn)]
87

98
const X: i32 = {
109
let mut x = 0;

src/test/ui/consts/control-flow/single_variant_match_ice.rs

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

3-
#![feature(const_if_match, const_fn)]
3+
#![feature(const_if_match)]
44

55
enum Foo {
66
Prob,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(staged_api)]
2+
#![feature(const_if_match)]
3+
4+
#[stable(feature = "rust1", since = "1.0.0")]
5+
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
6+
const fn foo() -> i32 {
7+
if true { 4 } else { 5 } //~ loops and conditional expressions are not stable in const fn
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0723]: loops and conditional expressions are not stable in const fn
2+
--> $DIR/internal-unstable-const.rs:7:5
3+
|
4+
LL | if true { 4 } else { 5 }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
8+
= help: add `#![feature(const_fn)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0723`.

0 commit comments

Comments
 (0)