Skip to content

Commit 7258740

Browse files
committed
validate allow_internal_unstable target
Adds a check to make sure `#[allow_internal_unstable]` can be applied only to macro definitions.
1 parent 05f4a9a commit 7258740

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ 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)
8890
} else {
8991
// lint-only checks
9092
if self.tcx.sess.check_name(attr, sym::cold) {
@@ -762,6 +764,33 @@ impl CheckAttrVisitor<'tcx> {
762764
}
763765
}
764766
}
767+
768+
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
769+
/// (Allows proc_macro functions)
770+
fn check_allow_internal_unstable(
771+
&self,
772+
attr: &Attribute,
773+
span: &Span,
774+
target: Target,
775+
attrs: &[Attribute],
776+
) -> bool {
777+
debug!("Checking target: {:?}", target);
778+
if target == Target::Fn {
779+
for attr in attrs {
780+
if self.tcx.sess.is_proc_macro_attr(attr) {
781+
debug!("Is proc macro attr");
782+
return true;
783+
}
784+
}
785+
debug!("Is not proc macro attr");
786+
}
787+
self.tcx
788+
.sess
789+
.struct_span_err(attr.span, "attribute should be applied to a macro")
790+
.span_label(*span, "not a macro")
791+
.emit();
792+
false
793+
}
765794
}
766795

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

src/test/ui/feature-gates/feature-gate-allow-internal-unstable-struct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// this needs a different test since this is done after expansion
33

44
#[allow_internal_unstable()] //~ ERROR allow_internal_unstable side-steps
5+
//~| ERROR attribute should
56
struct S;
67

78
fn main() {}

src/test/ui/feature-gates/feature-gate-allow-internal-unstable-struct.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ LL | #[allow_internal_unstable()]
66
|
77
= help: add `#![feature(allow_internal_unstable)]` to the crate attributes to enable
88

9-
error: aborting due to previous error
9+
error: attribute should be applied to a macro
10+
--> $DIR/feature-gate-allow-internal-unstable-struct.rs:4:1
11+
|
12+
LL | #[allow_internal_unstable()]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL |
15+
LL | struct S;
16+
| --------- not a macro
17+
18+
error: aborting due to 2 previous errors
1019

1120
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)