Skip to content

Commit 50d76d6

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 f02f338 + fc30825 commit 50d76d6

File tree

19 files changed

+66
-41
lines changed

19 files changed

+66
-41
lines changed

src/libcore/lib.rs

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 15 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)]

0 commit comments

Comments
 (0)