Skip to content

Commit

Permalink
Feature gate rustc attributes harder
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 25, 2019
1 parent 303f77e commit 7fbb936
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 26 deletions.
28 changes: 16 additions & 12 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use syntax::ext::base::{MacroKind, SyntaxExtension};
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
use syntax::feature_gate::{
feature_err, is_builtin_attr_name, AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES,
};
use syntax::feature_gate::{feature_err, emit_feature_err, is_builtin_attr_name};
use syntax::feature_gate::{AttributeGate, GateIssue, Stability, BUILTIN_ATTRIBUTES};
use syntax::symbol::{Symbol, kw, sym};
use syntax::visit::Visitor;
use syntax::util::lev_distance::find_best_match_for_name;
Expand Down Expand Up @@ -298,12 +297,25 @@ impl<'a> Resolver<'a> {
let res = self.resolve_macro_to_res_inner(path, kind, parent_scope, trace, force);

// Report errors and enforce feature gates for the resolved macro.
let features = self.session.features_untracked();
if res != Err(Determinacy::Undetermined) {
// Do not report duplicated errors on every undetermined resolution.
for segment in &path.segments {
if let Some(args) = &segment.args {
self.session.span_err(args.span(), "generic arguments in macro path");
}
if kind == MacroKind::Attr && !features.rustc_attrs &&
segment.ident.as_str().starts_with("rustc") {
let msg = "attributes starting with `rustc` are \
reserved for use by the `rustc` compiler";
emit_feature_err(
&self.session.parse_sess,
sym::rustc_attrs,
segment.ident.span,
GateIssue::Language,
msg,
);
}
}
}

Expand All @@ -320,18 +332,10 @@ impl<'a> Resolver<'a> {
}
Res::NonMacroAttr(attr_kind) => {
if kind == MacroKind::Attr {
let features = self.session.features_untracked();
if attr_kind == NonMacroAttrKind::Custom {
assert!(path.segments.len() == 1);
let name = path.segments[0].ident.as_str();
if name.starts_with("rustc_") {
if !features.rustc_attrs {
let msg = "unless otherwise specified, attributes with the prefix \
`rustc_` are reserved for internal compiler diagnostics";
self.report_unknown_attribute(path.span, &name, msg,
sym::rustc_attrs);
}
} else if !features.custom_attribute {
if !features.custom_attribute && !name.starts_with("rustc_") {
let msg = format!("The attribute `{}` is currently unknown to the \
compiler and may have meaning added to it in the \
future", path);
Expand Down
14 changes: 9 additions & 5 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,14 @@ impl<'a> Context<'a> {
}
debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
return;
} else {
for segment in &attr.path.segments {
if segment.ident.as_str().starts_with("rustc") {
let msg = "attributes starting with `rustc` are \
reserved for use by the `rustc` compiler";
gate_feature!(self, rustc_attrs, segment.ident.span, msg);
}
}
}
for &(n, ty) in self.plugin_attributes {
if attr.path == n {
Expand All @@ -1648,11 +1656,7 @@ impl<'a> Context<'a> {
}
}
if !attr::is_known(attr) {
if attr.name_or_empty().as_str().starts_with("rustc_") {
let msg = "unless otherwise specified, attributes with the prefix `rustc_` \
are reserved for internal compiler diagnostics";
gate_feature!(self, rustc_attrs, attr.span, msg);
} else if !is_macro {
if !is_macro && !attr.name_or_empty().as_str().starts_with("rustc_") {
// Only run the custom attribute lint during regular feature gate
// checking. Macro gating runs before the plugin attributes are
// registered, so we skip this in that case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fn error(_: fn()) {}

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_allow_const_fn_ptr]
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
const fn compiles(_: fn()) {}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
--> $DIR/allow_const_fn_ptr_feature_gate.rs:7:3
|
LL | #[rustc_allow_const_fn_ptr]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/feature-gates/feature-gate-rustc-attrs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Test that `#[rustc_*]` attributes are gated by `rustc_attrs` feature gate.

#[rustc_foo]
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/feature-gates/feature-gate-rustc-attrs.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
--> $DIR/feature-gate-rustc-attrs.rs:3:3
|
LL | #[rustc_foo]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/expand-to-unstable-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern crate derive_unstable_2;

#[derive(Unstable)]
//~^ ERROR: reserved for internal compiler
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
struct A;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/expand-to-unstable-2.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
--> $DIR/expand-to-unstable-2.rs:8:10
|
LL | #[derive(Unstable)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/reserved/reserved-attr-on-macro.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[rustc_attribute_should_be_reserved]
//~^ ERROR unless otherwise specified, attributes with the prefix `rustc_` are reserved
//~^ ERROR attributes starting with `rustc` are reserved for use by the `rustc` compiler
macro_rules! foo {
() => (());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/reserved/reserved-attr-on-macro.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
--> $DIR/reserved-attr-on-macro.rs:1:3
|
LL | #[rustc_attribute_should_be_reserved]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/attribute-typos.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0658]: unless otherwise specified, attributes with the prefix `rustc_` are reserved for internal compiler diagnostics
error[E0658]: attributes starting with `rustc` are reserved for use by the `rustc` compiler
--> $DIR/attribute-typos.rs:11:3
|
LL | #[rustc_err]
Expand Down

0 comments on commit 7fbb936

Please sign in to comment.