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 https://github.com/rust-lang/rust/issues/69399#issuecomment-712911540). 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 rust-lang/rust#69399 r? @oli-obk
This commit is contained in:
commit
72e02b015e
31 changed files with 177 additions and 41 deletions
|
@ -85,6 +85,10 @@ impl CheckAttrVisitor<'tcx> {
|
|||
self.check_export_name(&attr, span, target)
|
||||
} else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
|
||||
self.check_rustc_args_required_const(&attr, span, target, item)
|
||||
} else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
|
||||
self.check_allow_internal_unstable(&attr, span, target, &attrs)
|
||||
} else if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) {
|
||||
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
|
||||
} else {
|
||||
// lint-only checks
|
||||
if self.tcx.sess.check_name(attr, sym::cold) {
|
||||
|
@ -719,6 +723,55 @@ impl CheckAttrVisitor<'tcx> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
||||
/// (Allows proc_macro functions)
|
||||
fn check_allow_internal_unstable(
|
||||
&self,
|
||||
attr: &Attribute,
|
||||
span: &Span,
|
||||
target: Target,
|
||||
attrs: &[Attribute],
|
||||
) -> bool {
|
||||
debug!("Checking target: {:?}", target);
|
||||
if target == Target::Fn {
|
||||
for attr in attrs {
|
||||
if self.tcx.sess.is_proc_macro_attr(attr) {
|
||||
debug!("Is proc macro attr");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
debug!("Is not proc macro attr");
|
||||
}
|
||||
self.tcx
|
||||
.sess
|
||||
.struct_span_err(attr.span, "attribute should be applied to a macro")
|
||||
.span_label(*span, "not a macro")
|
||||
.emit();
|
||||
false
|
||||
}
|
||||
|
||||
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
||||
/// (Allows proc_macro functions)
|
||||
fn check_rustc_allow_const_fn_unstable(
|
||||
&self,
|
||||
hir_id: HirId,
|
||||
attr: &Attribute,
|
||||
span: &Span,
|
||||
target: Target,
|
||||
) -> bool {
|
||||
if let Target::Fn | Target::Method(_) = target {
|
||||
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
self.tcx
|
||||
.sess
|
||||
.struct_span_err(attr.span, "attribute should be applied to `const fn`")
|
||||
.span_label(*span, "not a `const fn`")
|
||||
.emit();
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
||||
|
|
|
@ -87,7 +87,7 @@ impl<'tcx> CheckConstVisitor<'tcx> {
|
|||
|
||||
let is_feature_allowed = |feature_gate| {
|
||||
// All features require that the corresponding gate be enabled,
|
||||
// even if the function has `#[allow_internal_unstable(the_gate)]`.
|
||||
// even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
|
||||
if !tcx.features().enabled(feature_gate) {
|
||||
return false;
|
||||
}
|
||||
|
@ -105,8 +105,8 @@ impl<'tcx> CheckConstVisitor<'tcx> {
|
|||
}
|
||||
|
||||
// However, we cannot allow stable `const fn`s to use unstable features without an explicit
|
||||
// opt-in via `allow_internal_unstable`.
|
||||
attr::allow_internal_unstable(&tcx.sess, &tcx.get_attrs(def_id))
|
||||
// opt-in via `rustc_allow_const_fn_unstable`.
|
||||
attr::rustc_allow_const_fn_unstable(&tcx.sess, &tcx.get_attrs(def_id))
|
||||
.map_or(false, |mut features| features.any(|name| name == feature_gate))
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue