Skip to content

Commit 3a63bf0

Browse files
committed
validate rustc_allow_const_fn_unstable targets
Adds a check to make sure `#[rustc_allow_const_fn_unstable]` can be applied only to functions.
1 parent 7258740 commit 3a63bf0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

compiler/rustc_passes/src/check_attr.rs

+22
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ impl CheckAttrVisitor<'tcx> {
8787
self.check_rustc_args_required_const(&attr, span, target, item)
8888
} else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
8989
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(&attr, span, target)
9092
} else {
9193
// lint-only checks
9294
if self.tcx.sess.check_name(attr, sym::cold) {
@@ -791,6 +793,26 @@ impl CheckAttrVisitor<'tcx> {
791793
.emit();
792794
false
793795
}
796+
797+
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
798+
/// (Allows proc_macro functions)
799+
fn check_rustc_allow_const_fn_unstable(
800+
&self,
801+
attr: &Attribute,
802+
span: &Span,
803+
target: Target,
804+
) -> bool {
805+
if let Target::Fn | Target::Method(_) = target {
806+
// FIXME Check that this isn't just a function, but a const fn
807+
return true;
808+
}
809+
self.tcx
810+
.sess
811+
.struct_span_err(attr.span, "attribute should be applied to `const fn`")
812+
.span_label(*span, "not a `const fn`")
813+
.emit();
814+
false
815+
}
794816
}
795817

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

0 commit comments

Comments
 (0)