Skip to content

Commit 72e02b0

Browse files
authored
Rollup merge of #78208 - liketechnik:issue-69399, r=oli-obk
replace `#[allow_internal_unstable]` with `#[rustc_allow_const_fn_unstable]` for `const fn`s `#[allow_internal_unstable]` is currently used to side-step feature gate and stability checks. While it was originally only meant to be used only on macros, its use was expanded to `const fn`s. This pr adds stricter checks for the usage of `#[allow_internal_unstable]` (only on macros) and introduces the `#[rustc_allow_const_fn_unstable]` attribute for usage on `const fn`s. This pr does not change any of the functionality associated with the use of `#[allow_internal_unstable]` on macros or the usage of `#[rustc_allow_const_fn_unstable]` (instead of `#[allow_internal_unstable]`) on `const fn`s (see #69399 (comment)). Note: The check for `#[rustc_allow_const_fn_unstable]` currently only validates that the attribute is used on a function, because I don't know how I would check if the function is a `const fn` at the place of the check. I therefore openend this as a 'draft pull request'. Closes #69399 r? @oli-obk
2 parents dbdc61f + ac2c599 commit 72e02b0

31 files changed

+177
-41
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,13 +1013,28 @@ pub fn allow_internal_unstable<'a>(
10131013
sess: &'a Session,
10141014
attrs: &'a [Attribute],
10151015
) -> Option<impl Iterator<Item = Symbol> + 'a> {
1016-
let attrs = sess.filter_by_name(attrs, sym::allow_internal_unstable);
1016+
allow_unstable(sess, attrs, sym::allow_internal_unstable)
1017+
}
1018+
1019+
pub fn rustc_allow_const_fn_unstable<'a>(
1020+
sess: &'a Session,
1021+
attrs: &'a [Attribute],
1022+
) -> Option<impl Iterator<Item = Symbol> + 'a> {
1023+
allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
1024+
}
1025+
1026+
fn allow_unstable<'a>(
1027+
sess: &'a Session,
1028+
attrs: &'a [Attribute],
1029+
symbol: Symbol,
1030+
) -> Option<impl Iterator<Item = Symbol> + 'a> {
1031+
let attrs = sess.filter_by_name(attrs, symbol);
10171032
let list = attrs
10181033
.filter_map(move |attr| {
10191034
attr.meta_item_list().or_else(|| {
10201035
sess.diagnostic().span_err(
10211036
attr.span,
1022-
"`allow_internal_unstable` expects a list of feature names",
1037+
&format!("`{}` expects a list of feature names", symbol.to_ident_string()),
10231038
);
10241039
None
10251040
})
@@ -1029,8 +1044,10 @@ pub fn allow_internal_unstable<'a>(
10291044
Some(list.into_iter().filter_map(move |it| {
10301045
let name = it.ident().map(|ident| ident.name);
10311046
if name.is_none() {
1032-
sess.diagnostic()
1033-
.span_err(it.span(), "`allow_internal_unstable` expects feature names");
1047+
sess.diagnostic().span_err(
1048+
it.span(),
1049+
&format!("`{}` expects feature names", symbol.to_ident_string()),
1050+
);
10341051
}
10351052
name
10361053
}))

compiler/rustc_feature/src/active.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ declare_features! (
210210
/// it is not on path for eventual stabilization).
211211
(active, no_niche, "1.42.0", None, None),
212212

213+
/// Allows using `#[rustc_allow_const_fn_unstable]`.
214+
/// This is an attribute on `const fn` for the same
215+
/// purpose as `#[allow_internal_unstable]`.
216+
(active, rustc_allow_const_fn_unstable, "1.49.0", Some(69399), None),
217+
213218
// no-tracking-issue-end
214219

215220
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
379379
allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
380380
"allow_internal_unstable side-steps feature gating and stability checks",
381381
),
382+
gated!(
383+
rustc_allow_const_fn_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
384+
"rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
385+
),
382386
gated!(
383387
allow_internal_unsafe, Normal, template!(Word),
384388
"allow_internal_unsafe side-steps the unsafe_code lint",

compiler/rustc_mir/src/transform/check_consts/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
7979
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
8080
}
8181

82-
pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {
82+
pub fn rustc_allow_const_fn_unstable(
83+
tcx: TyCtxt<'tcx>,
84+
def_id: DefId,
85+
feature_gate: Symbol,
86+
) -> bool {
8387
let attrs = tcx.get_attrs(def_id);
84-
attr::allow_internal_unstable(&tcx.sess, attrs)
88+
attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs)
8589
.map_or(false, |mut features| features.any(|name| name == feature_gate))
8690
}
8791

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ impl Validator<'mir, 'tcx> {
292292

293293
Status::Unstable(gate) if self.tcx.features().enabled(gate) => {
294294
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
295-
&& !super::allow_internal_unstable(self.tcx, self.def_id().to_def_id(), gate);
295+
&& !super::rustc_allow_const_fn_unstable(
296+
self.tcx,
297+
self.def_id().to_def_id(),
298+
gate,
299+
);
296300
if unstable_in_stable {
297301
emit_unstable_in_stable_error(self.ccx, span, gate);
298302
}
@@ -807,7 +811,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
807811
}
808812

809813
// Calling an unstable function *always* requires that the corresponding gate
810-
// be enabled, even if the function has `#[allow_internal_unstable(the_gate)]`.
814+
// be enabled, even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
811815
if !tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == gate) {
812816
self.check_op(ops::FnCallUnstable(callee, Some(gate)));
813817
return;
@@ -821,7 +825,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
821825

822826
// Otherwise, we are something const-stable calling a const-unstable fn.
823827

824-
if super::allow_internal_unstable(tcx, caller, gate) {
828+
if super::rustc_allow_const_fn_unstable(tcx, caller, gate) {
825829
return;
826830
}
827831

@@ -967,8 +971,8 @@ fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol
967971
)
968972
.span_suggestion(
969973
attr_span,
970-
"otherwise `#[allow_internal_unstable]` can be used to bypass stability checks",
971-
format!("#[allow_internal_unstable({})]\n", gate),
974+
"otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks",
975+
format!("#[rustc_allow_const_fn_unstable({})]\n", gate),
972976
Applicability::MaybeIncorrect,
973977
)
974978
.emit();

compiler/rustc_passes/src/check_attr.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ impl CheckAttrVisitor<'tcx> {
8585
self.check_export_name(&attr, span, target)
8686
} else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
8787
self.check_rustc_args_required_const(&attr, span, target, item)
88+
} else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
89+
self.check_allow_internal_unstable(&attr, span, target, &attrs)
90+
} else if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) {
91+
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
8892
} else {
8993
// lint-only checks
9094
if self.tcx.sess.check_name(attr, sym::cold) {
@@ -719,6 +723,55 @@ impl CheckAttrVisitor<'tcx> {
719723
}
720724
}
721725
}
726+
727+
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
728+
/// (Allows proc_macro functions)
729+
fn check_allow_internal_unstable(
730+
&self,
731+
attr: &Attribute,
732+
span: &Span,
733+
target: Target,
734+
attrs: &[Attribute],
735+
) -> bool {
736+
debug!("Checking target: {:?}", target);
737+
if target == Target::Fn {
738+
for attr in attrs {
739+
if self.tcx.sess.is_proc_macro_attr(attr) {
740+
debug!("Is proc macro attr");
741+
return true;
742+
}
743+
}
744+
debug!("Is not proc macro attr");
745+
}
746+
self.tcx
747+
.sess
748+
.struct_span_err(attr.span, "attribute should be applied to a macro")
749+
.span_label(*span, "not a macro")
750+
.emit();
751+
false
752+
}
753+
754+
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
755+
/// (Allows proc_macro functions)
756+
fn check_rustc_allow_const_fn_unstable(
757+
&self,
758+
hir_id: HirId,
759+
attr: &Attribute,
760+
span: &Span,
761+
target: Target,
762+
) -> bool {
763+
if let Target::Fn | Target::Method(_) = target {
764+
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id)) {
765+
return true;
766+
}
767+
}
768+
self.tcx
769+
.sess
770+
.struct_span_err(attr.span, "attribute should be applied to `const fn`")
771+
.span_label(*span, "not a `const fn`")
772+
.emit();
773+
false
774+
}
722775
}
723776

724777
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {

compiler/rustc_passes/src/check_const.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> CheckConstVisitor<'tcx> {
8787

8888
let is_feature_allowed = |feature_gate| {
8989
// All features require that the corresponding gate be enabled,
90-
// even if the function has `#[allow_internal_unstable(the_gate)]`.
90+
// even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
9191
if !tcx.features().enabled(feature_gate) {
9292
return false;
9393
}
@@ -105,8 +105,8 @@ impl<'tcx> CheckConstVisitor<'tcx> {
105105
}
106106

107107
// However, we cannot allow stable `const fn`s to use unstable features without an explicit
108-
// opt-in via `allow_internal_unstable`.
109-
attr::allow_internal_unstable(&tcx.sess, &tcx.get_attrs(def_id))
108+
// opt-in via `rustc_allow_const_fn_unstable`.
109+
attr::rustc_allow_const_fn_unstable(&tcx.sess, &tcx.get_attrs(def_id))
110110
.map_or(false, |mut features| features.any(|name| name == feature_gate))
111111
};
112112

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ symbols! {
894894
rustc,
895895
rustc_allocator,
896896
rustc_allocator_nounwind,
897+
rustc_allow_const_fn_unstable,
897898
rustc_args_required_const,
898899
rustc_attrs,
899900
rustc_builtin_macro,

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#![allow(explicit_outlives_requirements)]
7373
#![allow(incomplete_features)]
7474
#![deny(unsafe_op_in_unsafe_fn)]
75+
#![cfg_attr(not(bootstrap), feature(rustc_allow_const_fn_unstable))]
7576
#![cfg_attr(not(test), feature(generator_trait))]
7677
#![cfg_attr(test, feature(test))]
7778
#![cfg_attr(test, feature(new_uninit))]

library/alloc/src/raw_vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ impl<T> RawVec<T, Global> {
150150
impl<T, A: AllocRef> RawVec<T, A> {
151151
/// Like `new`, but parameterized over the choice of allocator for
152152
/// the returned `RawVec`.
153-
#[allow_internal_unstable(const_fn)]
153+
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_fn))]
154+
#[cfg_attr(bootstrap, allow_internal_unstable(const_fn))]
154155
pub const fn new_in(alloc: A) -> Self {
155156
// `cap: 0` means "unallocated". zero-sized types are ignored.
156157
Self { ptr: Unique::dangling(), cap: 0, alloc }

0 commit comments

Comments
 (0)