From 3d080a4a77274abd747f780b446e6fedce14fac7 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 00:23:38 +0100 Subject: introduce crate rustc_feature and move active, accepted, and removed to it --- src/libsyntax/lib.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 22b49862f49..f0d857673e7 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -92,7 +92,20 @@ pub mod attr; pub mod expand; pub use syntax_pos::source_map; pub mod entry; -pub mod feature_gate; +pub mod feature_gate { + mod check; + pub use check::{ + check_crate, check_attribute, get_features, feature_err, emit_feature_err, + Stability, GateIssue, UnstableFeatures, + EXPLAIN_STMT_ATTR_SYNTAX, EXPLAIN_UNSIZED_TUPLE_COERCION, + }; + mod builtin_attrs; + pub use builtin_attrs::{ + AttributeGate, AttributeType, GatedCfg, + BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, + deprecated_attributes, is_builtin_attr, is_builtin_attr_name, + }; +} pub mod mut_visit; pub mod ptr; pub mod show_span; -- cgit 1.4.1-3-g733a5 From 048201fa7a69b3c3795e76186bc954d06d2ca368 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 00:39:51 +0100 Subject: move Stability to rustc_feature --- src/librustc_feature/lib.rs | 8 ++++++++ src/librustc_lint/builtin.rs | 3 ++- src/libsyntax/feature_gate/builtin_attrs.rs | 4 ++-- src/libsyntax/feature_gate/check.rs | 8 -------- src/libsyntax/lib.rs | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc_feature/lib.rs b/src/librustc_feature/lib.rs index c6388f47cfe..5d248f75216 100644 --- a/src/librustc_feature/lib.rs +++ b/src/librustc_feature/lib.rs @@ -56,6 +56,14 @@ impl Feature { } } +#[derive(Copy, Clone, Debug)] +pub enum Stability { + Unstable, + // First argument is tracking issue link; second argument is an optional + // help message, which defaults to "remove this attribute". + Deprecated(&'static str, Option<&'static str>), +} + pub use accepted::ACCEPTED_FEATURES; pub use active::{ACTIVE_FEATURES, Features, INCOMPLETE_FEATURES}; pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 3515ef13e79..06616d537de 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -34,6 +34,7 @@ use lint::{LateContext, LintContext, LintArray}; use lint::{LintPass, LateLintPass, EarlyLintPass, EarlyContext}; use rustc::util::nodemap::FxHashSet; +use rustc_feature::Stability; use syntax::tokenstream::{TokenTree, TokenStream}; use syntax::ast::{self, Expr}; @@ -42,7 +43,7 @@ use syntax::attr::{self, HasAttrs, AttributeTemplate}; use syntax::source_map::Spanned; use syntax::edition::Edition; use syntax::feature_gate::{AttributeGate, AttributeType}; -use syntax::feature_gate::{Stability, deprecated_attributes}; +use syntax::feature_gate::deprecated_attributes; use syntax_pos::{BytePos, Span}; use syntax::symbol::{Symbol, kw, sym}; use syntax::errors::{Applicability, DiagnosticBuilder}; diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index e6f61e8fcbe..70f923c2783 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -4,8 +4,8 @@ use AttributeType::*; use AttributeGate::*; use super::check::{emit_feature_err, GateIssue}; -use super::check::{Stability, EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABLE}; -use rustc_feature::Features; +use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABLE}; +use rustc_feature::{Features, Stability}; use crate::ast; use crate::attr::AttributeTemplate; diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 7d959756f23..19e615f40fa 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -22,14 +22,6 @@ use rustc_error_codes::*; use std::env; use std::num::NonZeroU32; -#[derive(Copy, Clone, Debug)] -pub enum Stability { - Unstable, - // First argument is tracking issue link; second argument is an optional - // help message, which defaults to "remove this attribute" - Deprecated(&'static str, Option<&'static str>), -} - macro_rules! gate_feature_fn { ($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $level: expr) => {{ let (cx, has_feature, span, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index f0d857673e7..e33aa8f1f2b 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -96,7 +96,7 @@ pub mod feature_gate { mod check; pub use check::{ check_crate, check_attribute, get_features, feature_err, emit_feature_err, - Stability, GateIssue, UnstableFeatures, + GateIssue, UnstableFeatures, EXPLAIN_STMT_ATTR_SYNTAX, EXPLAIN_UNSIZED_TUPLE_COERCION, }; mod builtin_attrs; -- cgit 1.4.1-3-g733a5 From 8ad4d15f3888f1339d52632d40e0a47697dd2a24 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 00:56:46 +0100 Subject: move AttributeTemplate to builtin_attrs --- src/librustc_lint/builtin.rs | 4 ++-- src/librustc_parse/validate_attr.rs | 17 ++++++++++++++--- src/libsyntax/attr/builtin.rs | 25 ------------------------- src/libsyntax/feature_gate/builtin_attrs.rs | 17 ++++++++++++++++- src/libsyntax/lib.rs | 2 +- src/libsyntax_ext/util.rs | 2 +- 6 files changed, 34 insertions(+), 33 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 06616d537de..284b23a14e7 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -39,10 +39,10 @@ use rustc_feature::Stability; use syntax::tokenstream::{TokenTree, TokenStream}; use syntax::ast::{self, Expr}; use syntax::ptr::P; -use syntax::attr::{self, HasAttrs, AttributeTemplate}; +use syntax::attr::{self, HasAttrs}; use syntax::source_map::Spanned; use syntax::edition::Edition; -use syntax::feature_gate::{AttributeGate, AttributeType}; +use syntax::feature_gate::{AttributeGate, AttributeTemplate, AttributeType}; use syntax::feature_gate::deprecated_attributes; use syntax_pos::{BytePos, Span}; use syntax::symbol::{Symbol, kw, sym}; diff --git a/src/librustc_parse/validate_attr.rs b/src/librustc_parse/validate_attr.rs index bbe0dc1c35f..e6284858a21 100644 --- a/src/librustc_parse/validate_attr.rs +++ b/src/librustc_parse/validate_attr.rs @@ -1,8 +1,9 @@ //! Meta-syntax validation logic of attributes for post-expansion. use errors::{PResult, Applicability}; -use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem}; -use syntax::attr::{AttributeTemplate, mk_name_value_item_str}; +use syntax::feature_gate::AttributeTemplate; +use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem, MetaItemKind}; +use syntax::attr::mk_name_value_item_str; use syntax::early_buffered_lints::BufferedEarlyLintId; use syntax::feature_gate::BUILTIN_ATTRIBUTE_MAP; use syntax::token; @@ -41,6 +42,16 @@ pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Meta }) } +/// Checks that the given meta-item is compatible with this `AttributeTemplate`. +fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool { + match meta { + MetaItemKind::Word => template.word, + MetaItemKind::List(..) => template.list.is_some(), + MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(), + MetaItemKind::NameValue(..) => false, + } +} + pub fn check_builtin_attribute( sess: &ParseSess, attr: &Attribute, @@ -57,7 +68,7 @@ pub fn check_builtin_attribute( name == sym::test || name == sym::bench; match parse_meta(sess, attr) { - Ok(meta) => if !should_skip(name) && !template.compatible(&meta.kind) { + Ok(meta) => if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) { let error_msg = format!("malformed `{}` attribute input", name); let mut msg = "attribute must be of the form ".to_owned(); let mut suggestions = vec![]; diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index eaa4a64025a..be53d802eec 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -25,31 +25,6 @@ enum AttrError { UnsupportedLiteral(&'static str, /* is_bytestr */ bool), } -/// A template that the attribute input must match. -/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now. -#[derive(Clone, Copy)] -pub struct AttributeTemplate { - pub word: bool, - pub list: Option<&'static str>, - pub name_value_str: Option<&'static str>, -} - -impl AttributeTemplate { - pub fn only_word() -> Self { - Self { word: true, list: None, name_value_str: None } - } - - /// Checks that the given meta-item is compatible with this template. - pub fn compatible(&self, meta_item_kind: &ast::MetaItemKind) -> bool { - match meta_item_kind { - ast::MetaItemKind::Word => self.word, - ast::MetaItemKind::List(..) => self.list.is_some(), - ast::MetaItemKind::NameValue(lit) if lit.kind.is_str() => self.name_value_str.is_some(), - ast::MetaItemKind::NameValue(..) => false, - } - } -} - fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) { let diag = &sess.span_diagnostic; match error { diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index 70f923c2783..7435b2e056a 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -8,7 +8,6 @@ use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABL use rustc_feature::{Features, Stability}; use crate::ast; -use crate::attr::AttributeTemplate; use crate::sess::ParseSess; use syntax_pos::symbol::{Symbol, sym}; @@ -16,6 +15,7 @@ use syntax_pos::Span; use rustc_data_structures::fx::FxHashMap; use lazy_static::lazy_static; + type GateFn = fn(&Features) -> bool; macro_rules! cfg_fn { @@ -108,6 +108,21 @@ impl AttributeGate { } } +/// A template that the attribute input must match. +/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now. +#[derive(Clone, Copy)] +pub struct AttributeTemplate { + pub word: bool, + pub list: Option<&'static str>, + pub name_value_str: Option<&'static str>, +} + +impl AttributeTemplate { + pub fn only_word() -> Self { + Self { word: true, list: None, name_value_str: None } + } +} + /// A convenience macro for constructing attribute templates. /// E.g., `template!(Word, List: "description")` means that the attribute /// supports forms `#[attr]` and `#[attr(description)]`. diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index e33aa8f1f2b..8e95fe3c34b 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -101,7 +101,7 @@ pub mod feature_gate { }; mod builtin_attrs; pub use builtin_attrs::{ - AttributeGate, AttributeType, GatedCfg, + AttributeGate, AttributeTemplate, AttributeType, GatedCfg, BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, deprecated_attributes, is_builtin_attr, is_builtin_attr_name, }; diff --git a/src/libsyntax_ext/util.rs b/src/libsyntax_ext/util.rs index e59daab1770..893fd402e83 100644 --- a/src/libsyntax_ext/util.rs +++ b/src/libsyntax_ext/util.rs @@ -1,7 +1,7 @@ use rustc_parse::validate_attr; use syntax_pos::Symbol; use syntax::ast::MetaItem; -use syntax::attr::AttributeTemplate; +use syntax::feature_gate::AttributeTemplate; use syntax_expand::base::ExtCtxt; pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) { -- cgit 1.4.1-3-g733a5 From 6cab02cf149787db6c9e30ee0ceed97768bbe9aa Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 01:57:53 +0100 Subject: simplify gated cfgs logic --- src/librustc_driver/lib.rs | 21 +++++++---------- src/libsyntax/attr/builtin.rs | 24 +++++++++++++------ src/libsyntax/feature_gate/builtin_attrs.rs | 36 +++++------------------------ src/libsyntax/lib.rs | 2 +- 4 files changed, 32 insertions(+), 51 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 13829b842fd..22f130ed3c9 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -62,9 +62,9 @@ use std::time::Instant; use syntax::ast; use syntax::source_map::FileLoader; -use syntax::feature_gate::{GatedCfg, UnstableFeatures}; -use syntax::symbol::sym; -use syntax_pos::{DUMMY_SP, FileName}; +use syntax::feature_gate::{find_gated_cfg, UnstableFeatures}; +use syntax_pos::symbol::sym; +use syntax_pos::FileName; pub mod pretty; mod args; @@ -677,12 +677,6 @@ impl RustcDefaultCalls { .is_nightly_build(); let mut cfgs = sess.parse_sess.config.iter().filter_map(|&(name, ref value)| { - let gated_cfg = GatedCfg::gate(&ast::MetaItem { - path: ast::Path::from_ident(ast::Ident::with_dummy_span(name)), - kind: ast::MetaItemKind::Word, - span: DUMMY_SP, - }); - // Note that crt-static is a specially recognized cfg // directive that's printed out here as part of // rust-lang/rust#37406, but in general the @@ -693,10 +687,11 @@ impl RustcDefaultCalls { // through to build scripts. let value = value.as_ref().map(|s| s.as_str()); let value = value.as_ref().map(|s| s.as_ref()); - if name != sym::target_feature || value != Some("crt-static") { - if !allow_unstable_cfg && gated_cfg.is_some() { - return None - } + if (name != sym::target_feature || value != Some("crt-static")) + && !allow_unstable_cfg + && find_gated_cfg(|cfg_sym| cfg_sym == name).is_some() + { + return None; } if let Some(value) = value { diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index be53d802eec..4e7738439b2 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -1,8 +1,8 @@ //! Parsing and validation of builtin attributes use crate::ast::{self, Attribute, MetaItem, NestedMetaItem}; +use crate::feature_gate::{find_gated_cfg, emit_feature_err, GatedCfg, GateIssue}; use crate::print::pprust; -use crate::feature_gate::GatedCfg; use crate::sess::ParseSess; use errors::{Applicability, Handler}; @@ -531,8 +531,9 @@ pub fn find_crate_name(attrs: &[Attribute]) -> Option { /// Tests if a cfg-pattern matches the cfg set pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool { eval_condition(cfg, sess, &mut |cfg| { - if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) { - gated_cfg.check_and_emit(sess, feats); + let gate = find_gated_cfg(|sym| cfg.check_name(sym)); + if let (Some(feats), Some(gated_cfg)) = (features, gate) { + gate_cfg(&gated_cfg, cfg.span, sess, feats); } let error = |span, msg| { sess.span_diagnostic.span_err(span, msg); true }; if cfg.path.segments.len() != 1 { @@ -561,12 +562,21 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat }) } +fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &Features) { + let (cfg, feature, has_feature) = gated_cfg; + if !has_feature(features) && !cfg_span.allows_unstable(*feature) { + let explain = format!("`cfg({})` is experimental and subject to change", cfg); + emit_feature_err(sess, *feature, cfg_span, GateIssue::Language, &explain); + } +} + /// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to /// evaluate individual items. -pub fn eval_condition(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F) - -> bool - where F: FnMut(&ast::MetaItem) -> bool -{ +pub fn eval_condition( + cfg: &ast::MetaItem, + sess: &ParseSess, + eval: &mut impl FnMut(&ast::MetaItem) -> bool, +) -> bool { match cfg.kind { ast::MetaItemKind::List(ref mis) => { for mi in mis.iter() { diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index 7435b2e056a..36916de57fc 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -3,19 +3,15 @@ use AttributeType::*; use AttributeGate::*; -use super::check::{emit_feature_err, GateIssue}; use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABLE}; use rustc_feature::{Features, Stability}; use crate::ast; -use crate::sess::ParseSess; use syntax_pos::symbol::{Symbol, sym}; -use syntax_pos::Span; use rustc_data_structures::fx::FxHashMap; use lazy_static::lazy_static; - type GateFn = fn(&Features) -> bool; macro_rules! cfg_fn { @@ -24,39 +20,19 @@ macro_rules! cfg_fn { } } +pub type GatedCfg = (Symbol, Symbol, GateFn); + /// `cfg(...)`'s that are feature gated. -const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[ +const GATED_CFGS: &[GatedCfg] = &[ // (name in cfg, feature, function to check if the feature is enabled) (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)), (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), ]; -#[derive(Debug)] -pub struct GatedCfg { - span: Span, - index: usize, -} - -impl GatedCfg { - pub fn gate(cfg: &ast::MetaItem) -> Option { - GATED_CFGS.iter() - .position(|info| cfg.check_name(info.0)) - .map(|idx| { - GatedCfg { - span: cfg.span, - index: idx - } - }) - } - - pub fn check_and_emit(&self, sess: &ParseSess, features: &Features) { - let (cfg, feature, has_feature) = GATED_CFGS[self.index]; - if !has_feature(features) && !self.span.allows_unstable(feature) { - let explain = format!("`cfg({})` is experimental and subject to change", cfg); - emit_feature_err(sess, feature, self.span, GateIssue::Language, &explain); - } - } +/// Find a gated cfg determined by the `pred`icate which is given the cfg's name. +pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> { + GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym)) } // If you change this, please modify `src/doc/unstable-book` as well. You must diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 8e95fe3c34b..7f28ebb2b2d 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -101,7 +101,7 @@ pub mod feature_gate { }; mod builtin_attrs; pub use builtin_attrs::{ - AttributeGate, AttributeTemplate, AttributeType, GatedCfg, + AttributeGate, AttributeTemplate, AttributeType, find_gated_cfg, GatedCfg, BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, deprecated_attributes, is_builtin_attr, is_builtin_attr_name, }; -- cgit 1.4.1-3-g733a5 From d04b838a44b914411d967e32704d66d7f9e0b16f Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 02:20:07 +0100 Subject: move is_builtin_attr to syntax::attr --- src/librustc_passes/ast_validation.rs | 3 +-- src/librustc_resolve/build_reduced_graph.rs | 4 +--- src/libsyntax/attr/builtin.rs | 6 ++++++ src/libsyntax/feature_gate/builtin_attrs.rs | 11 ++--------- src/libsyntax/lib.rs | 2 +- src/libsyntax_expand/expand.rs | 4 ++-- 6 files changed, 13 insertions(+), 17 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index e189b7175f9..5a29a56ad54 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -14,7 +14,6 @@ use rustc_parse::validate_attr; use syntax::ast::*; use syntax::attr; use syntax::expand::is_proc_macro_attr; -use syntax::feature_gate::is_builtin_attr; use syntax::print::pprust; use syntax::source_map::Spanned; use syntax::symbol::{kw, sym}; @@ -257,7 +256,7 @@ impl<'a> AstValidator<'a> { .flat_map(|i| i.attrs.as_ref()) .filter(|attr| { let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn]; - !arr.contains(&attr.name_or_empty()) && is_builtin_attr(attr) + !arr.contains(&attr.name_or_empty()) && attr::is_builtin_attr(attr) }) .for_each(|attr| if attr.is_doc_comment() { let mut err = self.err_handler().struct_span_err( diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 6694ddc53d4..a621d54cf92 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -29,10 +29,8 @@ use errors::Applicability; use syntax::ast::{Name, Ident}; use syntax::attr; - use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId}; use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind}; -use syntax::feature_gate::is_builtin_attr; use syntax::token::{self, Token}; use syntax::print::pprust; use syntax::{span_err, struct_span_err}; @@ -1231,7 +1229,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { } fn visit_attribute(&mut self, attr: &'b ast::Attribute) { - if !attr.is_doc_comment() && is_builtin_attr(attr) { + if !attr.is_doc_comment() && attr::is_builtin_attr(attr) { self.r.builtin_attrs.push( (attr.get_normal_item().path.segments[0].ident, self.parent_scope) ); diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index 4e7738439b2..2623386f584 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -2,6 +2,7 @@ use crate::ast::{self, Attribute, MetaItem, NestedMetaItem}; use crate::feature_gate::{find_gated_cfg, emit_feature_err, GatedCfg, GateIssue}; +use crate::feature_gate::is_builtin_attr_name; use crate::print::pprust; use crate::sess::ParseSess; @@ -10,12 +11,17 @@ use std::num::NonZeroU32; use syntax_pos::hygiene::Transparency; use syntax_pos::{symbol::Symbol, symbol::sym, Span}; use rustc_feature::Features; + use rustc_macros::HashStable_Generic; use super::{mark_used, MetaItemKind}; use rustc_error_codes::*; +pub fn is_builtin_attr(attr: &Attribute) -> bool { + attr.ident().filter(|ident| is_builtin_attr_name(ident.name)).is_some() +} + enum AttrError { MultipleItem(String), UnknownMetaItem(String, &'static [&'static str]), diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs index 3e08e2af494..9b764d64c49 100644 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ b/src/libsyntax/feature_gate/builtin_attrs.rs @@ -3,12 +3,9 @@ use AttributeType::*; use AttributeGate::*; +use rustc_data_structures::fx::FxHashMap; use rustc_feature::{Features, Stability}; - -use crate::ast; - use syntax_pos::symbol::{Symbol, sym}; -use rustc_data_structures::fx::FxHashMap; use lazy_static::lazy_static; type GateFn = fn(&Features) -> bool; @@ -580,14 +577,10 @@ pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> { BUILTIN_ATTRIBUTES.iter().filter(|(.., gate)| gate.is_deprecated()).collect() } -pub fn is_builtin_attr_name(name: ast::Name) -> bool { +pub fn is_builtin_attr_name(name: Symbol) -> bool { BUILTIN_ATTRIBUTE_MAP.get(&name).is_some() } -pub fn is_builtin_attr(attr: &ast::Attribute) -> bool { - attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).is_some() -} - lazy_static! { pub static ref BUILTIN_ATTRIBUTE_MAP: FxHashMap = { let mut map = FxHashMap::default(); diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 7f28ebb2b2d..74036bdcfb7 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -103,7 +103,7 @@ pub mod feature_gate { pub use builtin_attrs::{ AttributeGate, AttributeTemplate, AttributeType, find_gated_cfg, GatedCfg, BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, - deprecated_attributes, is_builtin_attr, is_builtin_attr_name, + deprecated_attributes, is_builtin_attr_name, }; } pub mod mut_visit; diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index d8033759033..e4c4f4c43e6 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -12,9 +12,9 @@ use rustc_parse::parser::Parser; use rustc_parse::validate_attr; use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path}; use syntax::ast::{MacStmtStyle, StmtKind, ItemKind}; -use syntax::attr::{self, HasAttrs}; +use syntax::attr::{self, HasAttrs, is_builtin_attr}; use syntax::source_map::respan; -use syntax::feature_gate::{self, GateIssue, is_builtin_attr, emit_feature_err}; +use syntax::feature_gate::{self, GateIssue, emit_feature_err}; use syntax::mut_visit::*; use syntax::print::pprust; use syntax::ptr::P; -- cgit 1.4.1-3-g733a5 From 70234f16dfbd22a7315401d46f5f7da2729463b5 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 02:34:18 +0100 Subject: builtin_attrs.rs -> rustc_feature --- Cargo.lock | 4 + src/librustc_driver/Cargo.toml | 1 + src/librustc_driver/lib.rs | 3 +- src/librustc_feature/Cargo.toml | 2 + src/librustc_feature/builtin_attrs.rs | 595 ++++++++++++++++++++++++++++ src/librustc_feature/lib.rs | 6 + src/librustc_lint/builtin.rs | 3 +- src/librustc_lint/unused.rs | 5 +- src/librustc_parse/validate_attr.rs | 3 +- src/librustc_resolve/Cargo.toml | 1 + src/librustc_resolve/diagnostics.rs | 2 +- src/librustc_resolve/macros.rs | 6 +- src/libsyntax/attr/builtin.rs | 9 +- src/libsyntax/feature_gate/builtin_attrs.rs | 594 --------------------------- src/libsyntax/feature_gate/check.rs | 2 +- src/libsyntax/lib.rs | 6 - src/libsyntax_ext/util.rs | 2 +- 17 files changed, 624 insertions(+), 620 deletions(-) create mode 100644 src/librustc_feature/builtin_attrs.rs delete mode 100644 src/libsyntax/feature_gate/builtin_attrs.rs (limited to 'src/libsyntax/lib.rs') diff --git a/Cargo.lock b/Cargo.lock index 9ed2f021344..67259aed8eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3575,6 +3575,7 @@ dependencies = [ "rustc_data_structures", "rustc_error_codes", "rustc_errors", + "rustc_feature", "rustc_interface", "rustc_lint", "rustc_metadata", @@ -3612,6 +3613,8 @@ dependencies = [ name = "rustc_feature" version = "0.0.0" dependencies = [ + "lazy_static 1.3.0", + "rustc_data_structures", "syntax_pos", ] @@ -3855,6 +3858,7 @@ dependencies = [ "rustc_data_structures", "rustc_error_codes", "rustc_errors", + "rustc_feature", "rustc_metadata", "smallvec 1.0.0", "syntax", diff --git a/src/librustc_driver/Cargo.toml b/src/librustc_driver/Cargo.toml index 2b7e4d35248..d1cb4cbeb9b 100644 --- a/src/librustc_driver/Cargo.toml +++ b/src/librustc_driver/Cargo.toml @@ -19,6 +19,7 @@ rustc_target = { path = "../librustc_target" } rustc_lint = { path = "../librustc_lint" } rustc_data_structures = { path = "../librustc_data_structures" } errors = { path = "../librustc_errors", package = "rustc_errors" } +rustc_feature = { path = "../librustc_feature" } rustc_metadata = { path = "../librustc_metadata" } rustc_mir = { path = "../librustc_mir" } rustc_parse = { path = "../librustc_parse" } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 22f130ed3c9..89ef402bdb5 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -44,6 +44,7 @@ use errors::{PResult, registry::Registry}; use rustc_interface::interface; use rustc_interface::util::get_codegen_sysroot; use rustc_data_structures::sync::SeqCst; +use rustc_feature::find_gated_cfg; use rustc_serialize::json::ToJson; @@ -62,7 +63,7 @@ use std::time::Instant; use syntax::ast; use syntax::source_map::FileLoader; -use syntax::feature_gate::{find_gated_cfg, UnstableFeatures}; +use syntax::feature_gate::UnstableFeatures; use syntax_pos::symbol::sym; use syntax_pos::FileName; diff --git a/src/librustc_feature/Cargo.toml b/src/librustc_feature/Cargo.toml index 22e8366804b..40ce922947b 100644 --- a/src/librustc_feature/Cargo.toml +++ b/src/librustc_feature/Cargo.toml @@ -10,4 +10,6 @@ path = "lib.rs" doctest = false [dependencies] +rustc_data_structures = { path = "../librustc_data_structures" } +lazy_static = "1.0.0" syntax_pos = { path = "../libsyntax_pos" } diff --git a/src/librustc_feature/builtin_attrs.rs b/src/librustc_feature/builtin_attrs.rs new file mode 100644 index 00000000000..f72df00a8e8 --- /dev/null +++ b/src/librustc_feature/builtin_attrs.rs @@ -0,0 +1,595 @@ +//! Built-in attributes and `cfg` flag gating. + +use AttributeType::*; +use AttributeGate::*; + +use crate::{Features, Stability}; + +use rustc_data_structures::fx::FxHashMap; +use syntax_pos::symbol::{Symbol, sym}; +use lazy_static::lazy_static; + +type GateFn = fn(&Features) -> bool; + +macro_rules! cfg_fn { + ($field: ident) => { + (|features| { features.$field }) as GateFn + } +} + +pub type GatedCfg = (Symbol, Symbol, GateFn); + +/// `cfg(...)`'s that are feature gated. +const GATED_CFGS: &[GatedCfg] = &[ + // (name in cfg, feature, function to check if the feature is enabled) + (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)), + (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), + (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), +]; + +/// Find a gated cfg determined by the `pred`icate which is given the cfg's name. +pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> { + GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym)) +} + +// If you change this, please modify `src/doc/unstable-book` as well. You must +// move that documentation into the relevant place in the other docs, and +// remove the chapter on the flag. + +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum AttributeType { + /// Normal, builtin attribute that is consumed + /// by the compiler before the unused_attribute check + Normal, + + /// Builtin attribute that may not be consumed by the compiler + /// before the unused_attribute check. These attributes + /// will be ignored by the unused_attribute lint + Whitelisted, + + /// Builtin attribute that is only allowed at the crate level + CrateLevel, +} + +#[derive(Clone, Copy)] +pub enum AttributeGate { + /// Is gated by a given feature gate, reason + /// and function to check if enabled + Gated(Stability, Symbol, &'static str, fn(&Features) -> bool), + + /// Ungated attribute, can be used on all release channels + Ungated, +} + +// fn() is not Debug +impl std::fmt::Debug for AttributeGate { + fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match *self { + Self::Gated(ref stab, name, expl, _) => + write!(fmt, "Gated({:?}, {}, {})", stab, name, expl), + Self::Ungated => write!(fmt, "Ungated") + } + } +} + +impl AttributeGate { + fn is_deprecated(&self) -> bool { + match *self { + Self::Gated(Stability::Deprecated(_, _), ..) => true, + _ => false, + } + } +} + +/// A template that the attribute input must match. +/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now. +#[derive(Clone, Copy)] +pub struct AttributeTemplate { + pub word: bool, + pub list: Option<&'static str>, + pub name_value_str: Option<&'static str>, +} + +impl AttributeTemplate { + pub fn only_word() -> Self { + Self { word: true, list: None, name_value_str: None } + } +} + +/// A convenience macro for constructing attribute templates. +/// E.g., `template!(Word, List: "description")` means that the attribute +/// supports forms `#[attr]` and `#[attr(description)]`. +macro_rules! template { + (Word) => { template!(@ true, None, None) }; + (List: $descr: expr) => { template!(@ false, Some($descr), None) }; + (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) }; + (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) }; + (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) }; + (List: $descr1: expr, NameValueStr: $descr2: expr) => { + template!(@ false, Some($descr1), Some($descr2)) + }; + (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => { + template!(@ true, Some($descr1), Some($descr2)) + }; + (@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate { + word: $word, list: $list, name_value_str: $name_value_str + } }; +} + +macro_rules! ungated { + ($attr:ident, $typ:expr, $tpl:expr $(,)?) => { + (sym::$attr, $typ, $tpl, Ungated) + }; +} + +macro_rules! gated { + ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => { + (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate))) + }; + ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => { + (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr))) + }; +} + +macro_rules! rustc_attr { + (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => { + rustc_attr!( + $attr, $typ, $tpl, + concat!("the `#[", stringify!($attr), "]` attribute is just used for rustc unit tests \ + and will never be stable", + ), + ) + }; + ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => { + (sym::$attr, $typ, $tpl, + Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs))) + }; +} + +macro_rules! experimental { + ($attr:ident) => { + concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature") + } +} + +const IMPL_DETAIL: &str = "internal implementation detail"; +const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable"; + +pub type BuiltinAttribute = (Symbol, AttributeType, AttributeTemplate, AttributeGate); + +/// Attributes that have a special meaning to rustc or rustdoc. +pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ + // ========================================================================== + // Stable attributes: + // ========================================================================== + + // Condtional compilation: + ungated!(cfg, Normal, template!(List: "predicate")), + ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")), + + // Testing: + ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")), + ungated!( + should_panic, Normal, + template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"), + ), + // FIXME(Centril): This can be used on stable but shouldn't. + ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")), + + // Macros: + ungated!(derive, Normal, template!(List: "Trait1, Trait2, ...")), + ungated!(automatically_derived, Normal, template!(Word)), + // FIXME(#14407) + ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")), + ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`. + ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")), + ungated!(proc_macro, Normal, template!(Word)), + ungated!( + proc_macro_derive, Normal, + template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"), + ), + ungated!(proc_macro_attribute, Normal, template!(Word)), + + // Lints: + ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), + ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), + ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), + ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), + ungated!(must_use, Whitelisted, template!(Word, NameValueStr: "reason")), + // FIXME(#14407) + ungated!( + deprecated, Normal, + template!( + Word, + List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#, + NameValueStr: "reason" + ), + ), + + // Crate properties: + ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")), + ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")), + ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")), + + // ABI, linking, symbols, and FFI + ungated!( + link, Whitelisted, + template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ cfg = "...""#), + ), + ungated!(link_name, Whitelisted, template!(NameValueStr: "name")), + ungated!(no_link, Normal, template!(Word)), + ungated!(repr, Normal, template!(List: "C")), + ungated!(export_name, Whitelisted, template!(NameValueStr: "name")), + ungated!(link_section, Whitelisted, template!(NameValueStr: "name")), + ungated!(no_mangle, Whitelisted, template!(Word)), + ungated!(used, Whitelisted, template!(Word)), + + // Limits: + ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")), + ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")), + + // Entry point: + ungated!(main, Normal, template!(Word)), + ungated!(start, Normal, template!(Word)), + ungated!(no_start, CrateLevel, template!(Word)), + ungated!(no_main, CrateLevel, template!(Word)), + + // Modules, prelude, and resolution: + ungated!(path, Normal, template!(NameValueStr: "file")), + ungated!(no_std, CrateLevel, template!(Word)), + ungated!(no_implicit_prelude, Normal, template!(Word)), + ungated!(non_exhaustive, Whitelisted, template!(Word)), + + // Runtime + ungated!(windows_subsystem, Whitelisted, template!(NameValueStr: "windows|console")), + ungated!(panic_handler, Normal, template!(Word)), // RFC 2070 + + // Code generation: + ungated!(inline, Whitelisted, template!(Word, List: "always|never")), + ungated!(cold, Whitelisted, template!(Word)), + ungated!(no_builtins, Whitelisted, template!(Word)), + ungated!(target_feature, Whitelisted, template!(List: r#"enable = "name""#)), + + // FIXME: #14408 whitelist docs since rustdoc looks at them + ungated!(doc, Whitelisted, template!(List: "hidden|inline|...", NameValueStr: "string")), + + // ========================================================================== + // Unstable attributes: + // ========================================================================== + + // Linking: + gated!(naked, Whitelisted, template!(Word), naked_functions, experimental!(naked)), + gated!( + link_args, Normal, template!(NameValueStr: "args"), + "the `link_args` attribute is experimental and not portable across platforms, \ + it is recommended to use `#[link(name = \"foo\")] instead", + ), + gated!( + link_ordinal, Whitelisted, template!(List: "ordinal"), raw_dylib, + experimental!(link_ordinal) + ), + + // Plugins: + ( + sym::plugin_registrar, Normal, template!(Word), + Gated( + Stability::Deprecated( + "https://github.com/rust-lang/rust/pull/64675", + Some("may be removed in a future compiler version"), + ), + sym::plugin_registrar, + "compiler plugins are deprecated", + cfg_fn!(plugin_registrar) + ) + ), + ( + sym::plugin, CrateLevel, template!(List: "name|name(args)"), + Gated( + Stability::Deprecated( + "https://github.com/rust-lang/rust/pull/64675", + Some("may be removed in a future compiler version"), + ), + sym::plugin, + "compiler plugins are deprecated", + cfg_fn!(plugin) + ) + ), + + // Testing: + gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)), + gated!( + test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks, + "custom test frameworks are an unstable feature", + ), + // RFC #1268 + gated!(marker, Normal, template!(Word), marker_trait_attr, experimental!(marker)), + gated!( + thread_local, Whitelisted, template!(Word), + "`#[thread_local]` is an experimental feature, and does not currently handle destructors", + ), + gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)), + // RFC 2412 + gated!( + optimize, Whitelisted, template!(List: "size|speed"), optimize_attribute, + experimental!(optimize), + ), + + gated!(ffi_returns_twice, Whitelisted, template!(Word), experimental!(ffi_returns_twice)), + gated!(track_caller, Whitelisted, template!(Word), experimental!(track_caller)), + gated!( + register_attr, CrateLevel, template!(List: "attr1, attr2, ..."), + experimental!(register_attr), + ), + gated!( + register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), + experimental!(register_tool), + ), + + // ========================================================================== + // Internal attributes: Stability, deprecation, and unsafe: + // ========================================================================== + + ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")), + // FIXME(#14407) -- only looked at on-demand so we can't + // guarantee they'll have already been checked. + ungated!( + rustc_deprecated, Whitelisted, + template!(List: r#"since = "version", reason = "...""#) + ), + // FIXME(#14407) + ungated!(stable, Whitelisted, template!(List: r#"feature = "name", since = "version""#)), + // FIXME(#14407) + ungated!( + unstable, Whitelisted, + template!(List: r#"feature = "name", reason = "...", issue = "N""#), + ), + gated!( + rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), + "the `#[rustc_const_unstable]` attribute is an internal feature", + ), + gated!( + allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), + "allow_internal_unstable side-steps feature gating and stability checks", + ), + gated!( + allow_internal_unsafe, Normal, template!(Word), + "allow_internal_unsafe side-steps the unsafe_code lint", + ), + + // ========================================================================== + // Internal attributes: Type system related: + // ========================================================================== + + gated!(fundamental, Whitelisted, template!(Word), experimental!(fundamental)), + gated!( + // RFC #1445. + structural_match, Whitelisted, template!(Word), + "the semantics of constant patterns is not yet settled", + ), + gated!( + may_dangle, Normal, template!(Word), dropck_eyepatch, + "`may_dangle` has unstable semantics and may be removed in the future", + ), + + // ========================================================================== + // Internal attributes: Runtime related: + // ========================================================================== + + rustc_attr!(rustc_allocator, Whitelisted, template!(Word), IMPL_DETAIL), + rustc_attr!(rustc_allocator_nounwind, Whitelisted, template!(Word), IMPL_DETAIL), + gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)), + gated!( + default_lib_allocator, Whitelisted, template!(Word), allocator_internals, + experimental!(default_lib_allocator), + ), + gated!( + needs_allocator, Normal, template!(Word), allocator_internals, + experimental!(needs_allocator), + ), + gated!(panic_runtime, Whitelisted, template!(Word), experimental!(panic_runtime)), + gated!(needs_panic_runtime, Whitelisted, template!(Word), experimental!(needs_panic_runtime)), + gated!( + unwind, Whitelisted, template!(List: "allowed|aborts"), unwind_attributes, + experimental!(unwind), + ), + gated!( + compiler_builtins, Whitelisted, template!(Word), + "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \ + which contains compiler-rt intrinsics and will never be stable", + ), + gated!( + sanitizer_runtime, Whitelisted, template!(Word), + "the `#[sanitizer_runtime]` attribute is used to identify crates that contain the runtime \ + of a sanitizer and will never be stable", + ), + gated!( + profiler_runtime, Whitelisted, template!(Word), + "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \ + which contains the profiler runtime and will never be stable", + ), + + // ========================================================================== + // Internal attributes, Linkage: + // ========================================================================== + + gated!( + linkage, Whitelisted, template!(NameValueStr: "external|internal|..."), + "the `linkage` attribute is experimental and not portable across platforms", + ), + rustc_attr!(rustc_std_internal_symbol, Whitelisted, template!(Word), INTERNAL_UNSTABLE), + + // ========================================================================== + // Internal attributes, Macro related: + // ========================================================================== + + rustc_attr!(rustc_builtin_macro, Whitelisted, template!(Word), IMPL_DETAIL), + rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE), + rustc_attr!( + rustc_macro_transparency, Whitelisted, + template!(NameValueStr: "transparent|semitransparent|opaque"), + "used internally for testing macro hygiene", + ), + + // ========================================================================== + // Internal attributes, Diagnostics related: + // ========================================================================== + + rustc_attr!( + rustc_on_unimplemented, Whitelisted, + template!( + List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#, + NameValueStr: "message" + ), + INTERNAL_UNSTABLE + ), + // Whitelists "identity-like" conversion methods to suggest on type mismatch. + rustc_attr!(rustc_conversion_suggestion, Whitelisted, template!(Word), INTERNAL_UNSTABLE), + + // ========================================================================== + // Internal attributes, Const related: + // ========================================================================== + + rustc_attr!(rustc_promotable, Whitelisted, template!(Word), IMPL_DETAIL), + rustc_attr!(rustc_allow_const_fn_ptr, Whitelisted, template!(Word), IMPL_DETAIL), + rustc_attr!(rustc_args_required_const, Whitelisted, template!(List: "N"), INTERNAL_UNSTABLE), + + // ========================================================================== + // Internal attributes, Layout related: + // ========================================================================== + + rustc_attr!( + rustc_layout_scalar_valid_range_start, Whitelisted, template!(List: "value"), + "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \ + niche optimizations in libcore and will never be stable", + ), + rustc_attr!( + rustc_layout_scalar_valid_range_end, Whitelisted, template!(List: "value"), + "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \ + niche optimizations in libcore and will never be stable", + ), + rustc_attr!( + rustc_nonnull_optimization_guaranteed, Whitelisted, template!(Word), + "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \ + niche optimizations in libcore and will never be stable", + ), + + // ========================================================================== + // Internal attributes, Misc: + // ========================================================================== + gated!( + lang, Normal, template!(NameValueStr: "name"), lang_items, + "language items are subject to change", + ), + ( + sym::rustc_diagnostic_item, + Normal, + template!(NameValueStr: "name"), + Gated( + Stability::Unstable, + sym::rustc_attrs, + "diagnostic items compiler internal support for linting", + cfg_fn!(rustc_attrs), + ), + ), + ( + sym::no_debug, Whitelisted, template!(Word), + Gated( + Stability::Deprecated("https://github.com/rust-lang/rust/issues/29721", None), + sym::no_debug, + "the `#[no_debug]` attribute was an experimental feature that has been \ + deprecated due to lack of demand", + cfg_fn!(no_debug) + ) + ), + gated!( + // Used in resolve: + prelude_import, Whitelisted, template!(Word), + "`#[prelude_import]` is for use by rustc only", + ), + gated!( + rustc_paren_sugar, Normal, template!(Word), unboxed_closures, + "unboxed_closures are still evolving", + ), + rustc_attr!( + rustc_inherit_overflow_checks, Whitelisted, template!(Word), + "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \ + overflow checking behavior of several libcore functions that are inlined \ + across crates and will never be stable", + ), + rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"), + "the `#[rustc_reservation_impl]` attribute is internally used \ + for reserving for `for From for T` impl" + ), + rustc_attr!( + rustc_test_marker, Normal, template!(Word), + "the `#[rustc_test_marker]` attribute is used internally to track tests", + ), + + // ========================================================================== + // Internal attributes, Testing: + // ========================================================================== + + rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)), + rustc_attr!(TEST, rustc_variance, Normal, template!(Word)), + rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")), + rustc_attr!(TEST, rustc_regions, Normal, template!(Word)), + rustc_attr!( + TEST, rustc_error, Whitelisted, + template!(Word, List: "delay_span_bug_from_inside_query") + ), + rustc_attr!(TEST, rustc_dump_user_substs, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_if_this_changed, Whitelisted, template!(Word, List: "DepNode")), + rustc_attr!(TEST, rustc_then_this_would_need, Whitelisted, template!(List: "DepNode")), + rustc_attr!( + TEST, rustc_dirty, Whitelisted, + template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#), + ), + rustc_attr!( + TEST, rustc_clean, Whitelisted, + template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#), + ), + rustc_attr!( + TEST, rustc_partition_reused, Whitelisted, + template!(List: r#"cfg = "...", module = "...""#), + ), + rustc_attr!( + TEST, rustc_partition_codegened, Whitelisted, + template!(List: r#"cfg = "...", module = "...""#), + ), + rustc_attr!( + TEST, rustc_expected_cgu_reuse, Whitelisted, + template!(List: r#"cfg = "...", module = "...", kind = "...""#), + ), + rustc_attr!(TEST, rustc_synthetic, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_symbol_name, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_def_path, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_mir, Whitelisted, template!(List: "arg1, arg2, ...")), + rustc_attr!(TEST, rustc_dump_program_clauses, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_dump_env_program_clauses, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_object_lifetime_default, Whitelisted, template!(Word)), + rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)), + gated!( + omit_gdb_pretty_printer_section, Whitelisted, template!(Word), + "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite", + ), +]; + +pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> { + BUILTIN_ATTRIBUTES.iter().filter(|(.., gate)| gate.is_deprecated()).collect() +} + +pub fn is_builtin_attr_name(name: Symbol) -> bool { + BUILTIN_ATTRIBUTE_MAP.get(&name).is_some() +} + +lazy_static! { + pub static ref BUILTIN_ATTRIBUTE_MAP: FxHashMap = { + let mut map = FxHashMap::default(); + for attr in BUILTIN_ATTRIBUTES.iter() { + if map.insert(attr.0, attr).is_some() { + panic!("duplicate builtin attribute `{}`", attr.0); + } + } + map + }; +} diff --git a/src/librustc_feature/lib.rs b/src/librustc_feature/lib.rs index 5d248f75216..bda1449d85c 100644 --- a/src/librustc_feature/lib.rs +++ b/src/librustc_feature/lib.rs @@ -15,6 +15,7 @@ mod accepted; mod removed; mod active; +mod builtin_attrs; use std::fmt; use std::num::NonZeroU32; @@ -67,3 +68,8 @@ pub enum Stability { pub use accepted::ACCEPTED_FEATURES; pub use active::{ACTIVE_FEATURES, Features, INCOMPLETE_FEATURES}; pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; +pub use builtin_attrs::{ + AttributeGate, AttributeTemplate, AttributeType, find_gated_cfg, GatedCfg, + BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, + deprecated_attributes, is_builtin_attr_name, +}; diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 284b23a14e7..5d3a6cccc4e 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -34,6 +34,7 @@ use lint::{LateContext, LintContext, LintArray}; use lint::{LintPass, LateLintPass, EarlyLintPass, EarlyContext}; use rustc::util::nodemap::FxHashSet; +use rustc_feature::{AttributeGate, AttributeTemplate, AttributeType, deprecated_attributes}; use rustc_feature::Stability; use syntax::tokenstream::{TokenTree, TokenStream}; @@ -42,8 +43,6 @@ use syntax::ptr::P; use syntax::attr::{self, HasAttrs}; use syntax::source_map::Spanned; use syntax::edition::Edition; -use syntax::feature_gate::{AttributeGate, AttributeTemplate, AttributeType}; -use syntax::feature_gate::deprecated_attributes; use syntax_pos::{BytePos, Span}; use syntax::symbol::{Symbol, kw, sym}; use syntax::errors::{Applicability, DiagnosticBuilder}; diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 9f293bdaa10..f7de7ec7e18 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -1,3 +1,4 @@ +use rustc::hir; use rustc::hir::def::{Res, DefKind}; use rustc::hir::def_id::DefId; use rustc::lint; @@ -7,19 +8,17 @@ use rustc::ty::adjustment; use rustc_data_structures::fx::FxHashMap; use lint::{LateContext, EarlyContext, LintContext, LintArray}; use lint::{LintPass, EarlyLintPass, LateLintPass}; +use rustc_feature::{AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; use syntax::ast; use syntax::attr; use syntax::errors::{Applicability, pluralize}; -use syntax::feature_gate::{AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; use syntax::print::pprust; use syntax::symbol::{kw, sym}; use syntax::symbol::Symbol; use syntax::util::parser; use syntax_pos::{Span, BytePos}; -use rustc::hir; - use log::debug; declare_lint! { diff --git a/src/librustc_parse/validate_attr.rs b/src/librustc_parse/validate_attr.rs index e6284858a21..a3c9e266593 100644 --- a/src/librustc_parse/validate_attr.rs +++ b/src/librustc_parse/validate_attr.rs @@ -1,11 +1,10 @@ //! Meta-syntax validation logic of attributes for post-expansion. use errors::{PResult, Applicability}; -use syntax::feature_gate::AttributeTemplate; +use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP}; use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem, MetaItemKind}; use syntax::attr::mk_name_value_item_str; use syntax::early_buffered_lints::BufferedEarlyLintId; -use syntax::feature_gate::BUILTIN_ATTRIBUTE_MAP; use syntax::token; use syntax::tokenstream::TokenTree; use syntax::sess::ParseSess; diff --git a/src/librustc_resolve/Cargo.toml b/src/librustc_resolve/Cargo.toml index 6cce893f8ec..caca20e4221 100644 --- a/src/librustc_resolve/Cargo.toml +++ b/src/librustc_resolve/Cargo.toml @@ -20,6 +20,7 @@ arena = { path = "../libarena" } errors = { path = "../librustc_errors", package = "rustc_errors" } syntax_pos = { path = "../libsyntax_pos" } rustc_data_structures = { path = "../librustc_data_structures" } +rustc_feature = { path = "../librustc_feature" } rustc_metadata = { path = "../librustc_metadata" } rustc_error_codes = { path = "../librustc_error_codes" } smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index e134b8b92ac..4dcafb6d279 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -9,8 +9,8 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId}; use rustc::session::Session; use rustc::ty::{self, DefIdTree}; use rustc::util::nodemap::FxHashSet; +use rustc_feature::BUILTIN_ATTRIBUTES; use syntax::ast::{self, Ident, Path}; -use syntax::feature_gate::BUILTIN_ATTRIBUTES; use syntax::source_map::SourceMap; use syntax::struct_span_err; use syntax::symbol::{Symbol, kw}; diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 21c24f9da1c..cc5bc2b41dc 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -12,18 +12,18 @@ use rustc::middle::stability; use rustc::session::Session; use rustc::util::nodemap::FxHashSet; use rustc::{ty, lint, span_bug}; +use rustc_feature::is_builtin_attr_name; use syntax::ast::{self, NodeId, Ident}; use syntax::attr::{self, StabilityLevel}; use syntax::edition::Edition; -use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name}; -use syntax::feature_gate::GateIssue; +use syntax::feature_gate::{emit_feature_err, GateIssue}; use syntax::print::pprust; -use syntax::symbol::{Symbol, kw, sym}; use syntax_expand::base::{self, InvocationRes, Indeterminate}; use syntax_expand::base::SyntaxExtension; use syntax_expand::expand::{AstFragment, AstFragmentKind, Invocation, InvocationKind}; use syntax_expand::compile_declarative_macro; use syntax_pos::hygiene::{self, ExpnId, ExpnData, ExpnKind}; +use syntax_pos::symbol::{Symbol, kw, sym}; use syntax_pos::{Span, DUMMY_SP}; use std::{mem, ptr}; diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index 2623386f584..bbcb8cc3c62 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -1,8 +1,8 @@ //! Parsing and validation of builtin attributes +use super::{mark_used, MetaItemKind}; use crate::ast::{self, Attribute, MetaItem, NestedMetaItem}; -use crate::feature_gate::{find_gated_cfg, emit_feature_err, GatedCfg, GateIssue}; -use crate::feature_gate::is_builtin_attr_name; +use crate::feature_gate::{emit_feature_err, GateIssue}; use crate::print::pprust; use crate::sess::ParseSess; @@ -10,12 +10,9 @@ use errors::{Applicability, Handler}; use std::num::NonZeroU32; use syntax_pos::hygiene::Transparency; use syntax_pos::{symbol::Symbol, symbol::sym, Span}; -use rustc_feature::Features; - +use rustc_feature::{Features, find_gated_cfg, GatedCfg, is_builtin_attr_name}; use rustc_macros::HashStable_Generic; -use super::{mark_used, MetaItemKind}; - use rustc_error_codes::*; pub fn is_builtin_attr(attr: &Attribute) -> bool { diff --git a/src/libsyntax/feature_gate/builtin_attrs.rs b/src/libsyntax/feature_gate/builtin_attrs.rs deleted file mode 100644 index 9b764d64c49..00000000000 --- a/src/libsyntax/feature_gate/builtin_attrs.rs +++ /dev/null @@ -1,594 +0,0 @@ -//! Built-in attributes and `cfg` flag gating. - -use AttributeType::*; -use AttributeGate::*; - -use rustc_data_structures::fx::FxHashMap; -use rustc_feature::{Features, Stability}; -use syntax_pos::symbol::{Symbol, sym}; -use lazy_static::lazy_static; - -type GateFn = fn(&Features) -> bool; - -macro_rules! cfg_fn { - ($field: ident) => { - (|features| { features.$field }) as GateFn - } -} - -pub type GatedCfg = (Symbol, Symbol, GateFn); - -/// `cfg(...)`'s that are feature gated. -const GATED_CFGS: &[GatedCfg] = &[ - // (name in cfg, feature, function to check if the feature is enabled) - (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)), - (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), - (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)), -]; - -/// Find a gated cfg determined by the `pred`icate which is given the cfg's name. -pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> { - GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym)) -} - -// If you change this, please modify `src/doc/unstable-book` as well. You must -// move that documentation into the relevant place in the other docs, and -// remove the chapter on the flag. - -#[derive(Copy, Clone, PartialEq, Debug)] -pub enum AttributeType { - /// Normal, builtin attribute that is consumed - /// by the compiler before the unused_attribute check - Normal, - - /// Builtin attribute that may not be consumed by the compiler - /// before the unused_attribute check. These attributes - /// will be ignored by the unused_attribute lint - Whitelisted, - - /// Builtin attribute that is only allowed at the crate level - CrateLevel, -} - -#[derive(Clone, Copy)] -pub enum AttributeGate { - /// Is gated by a given feature gate, reason - /// and function to check if enabled - Gated(Stability, Symbol, &'static str, fn(&Features) -> bool), - - /// Ungated attribute, can be used on all release channels - Ungated, -} - -// fn() is not Debug -impl std::fmt::Debug for AttributeGate { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match *self { - Self::Gated(ref stab, name, expl, _) => - write!(fmt, "Gated({:?}, {}, {})", stab, name, expl), - Self::Ungated => write!(fmt, "Ungated") - } - } -} - -impl AttributeGate { - fn is_deprecated(&self) -> bool { - match *self { - Self::Gated(Stability::Deprecated(_, _), ..) => true, - _ => false, - } - } -} - -/// A template that the attribute input must match. -/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now. -#[derive(Clone, Copy)] -pub struct AttributeTemplate { - pub word: bool, - pub list: Option<&'static str>, - pub name_value_str: Option<&'static str>, -} - -impl AttributeTemplate { - pub fn only_word() -> Self { - Self { word: true, list: None, name_value_str: None } - } -} - -/// A convenience macro for constructing attribute templates. -/// E.g., `template!(Word, List: "description")` means that the attribute -/// supports forms `#[attr]` and `#[attr(description)]`. -macro_rules! template { - (Word) => { template!(@ true, None, None) }; - (List: $descr: expr) => { template!(@ false, Some($descr), None) }; - (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) }; - (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) }; - (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) }; - (List: $descr1: expr, NameValueStr: $descr2: expr) => { - template!(@ false, Some($descr1), Some($descr2)) - }; - (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => { - template!(@ true, Some($descr1), Some($descr2)) - }; - (@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate { - word: $word, list: $list, name_value_str: $name_value_str - } }; -} - -macro_rules! ungated { - ($attr:ident, $typ:expr, $tpl:expr $(,)?) => { - (sym::$attr, $typ, $tpl, Ungated) - }; -} - -macro_rules! gated { - ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => { - (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate))) - }; - ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => { - (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr))) - }; -} - -macro_rules! rustc_attr { - (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => { - rustc_attr!( - $attr, $typ, $tpl, - concat!("the `#[", stringify!($attr), "]` attribute is just used for rustc unit tests \ - and will never be stable", - ), - ) - }; - ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => { - (sym::$attr, $typ, $tpl, - Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs))) - }; -} - -macro_rules! experimental { - ($attr:ident) => { - concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature") - } -} - -const IMPL_DETAIL: &str = "internal implementation detail"; -const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable"; - -pub type BuiltinAttribute = (Symbol, AttributeType, AttributeTemplate, AttributeGate); - -/// Attributes that have a special meaning to rustc or rustdoc. -pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ - // ========================================================================== - // Stable attributes: - // ========================================================================== - - // Condtional compilation: - ungated!(cfg, Normal, template!(List: "predicate")), - ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")), - - // Testing: - ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")), - ungated!( - should_panic, Normal, - template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"), - ), - // FIXME(Centril): This can be used on stable but shouldn't. - ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")), - - // Macros: - ungated!(derive, Normal, template!(List: "Trait1, Trait2, ...")), - ungated!(automatically_derived, Normal, template!(Word)), - // FIXME(#14407) - ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")), - ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`. - ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")), - ungated!(proc_macro, Normal, template!(Word)), - ungated!( - proc_macro_derive, Normal, - template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"), - ), - ungated!(proc_macro_attribute, Normal, template!(Word)), - - // Lints: - ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), - ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), - ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), - ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)), - ungated!(must_use, Whitelisted, template!(Word, NameValueStr: "reason")), - // FIXME(#14407) - ungated!( - deprecated, Normal, - template!( - Word, - List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#, - NameValueStr: "reason" - ), - ), - - // Crate properties: - ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")), - ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")), - ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")), - - // ABI, linking, symbols, and FFI - ungated!( - link, Whitelisted, - template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ cfg = "...""#), - ), - ungated!(link_name, Whitelisted, template!(NameValueStr: "name")), - ungated!(no_link, Normal, template!(Word)), - ungated!(repr, Normal, template!(List: "C")), - ungated!(export_name, Whitelisted, template!(NameValueStr: "name")), - ungated!(link_section, Whitelisted, template!(NameValueStr: "name")), - ungated!(no_mangle, Whitelisted, template!(Word)), - ungated!(used, Whitelisted, template!(Word)), - - // Limits: - ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")), - ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")), - - // Entry point: - ungated!(main, Normal, template!(Word)), - ungated!(start, Normal, template!(Word)), - ungated!(no_start, CrateLevel, template!(Word)), - ungated!(no_main, CrateLevel, template!(Word)), - - // Modules, prelude, and resolution: - ungated!(path, Normal, template!(NameValueStr: "file")), - ungated!(no_std, CrateLevel, template!(Word)), - ungated!(no_implicit_prelude, Normal, template!(Word)), - ungated!(non_exhaustive, Whitelisted, template!(Word)), - - // Runtime - ungated!(windows_subsystem, Whitelisted, template!(NameValueStr: "windows|console")), - ungated!(panic_handler, Normal, template!(Word)), // RFC 2070 - - // Code generation: - ungated!(inline, Whitelisted, template!(Word, List: "always|never")), - ungated!(cold, Whitelisted, template!(Word)), - ungated!(no_builtins, Whitelisted, template!(Word)), - ungated!(target_feature, Whitelisted, template!(List: r#"enable = "name""#)), - - // FIXME: #14408 whitelist docs since rustdoc looks at them - ungated!(doc, Whitelisted, template!(List: "hidden|inline|...", NameValueStr: "string")), - - // ========================================================================== - // Unstable attributes: - // ========================================================================== - - // Linking: - gated!(naked, Whitelisted, template!(Word), naked_functions, experimental!(naked)), - gated!( - link_args, Normal, template!(NameValueStr: "args"), - "the `link_args` attribute is experimental and not portable across platforms, \ - it is recommended to use `#[link(name = \"foo\")] instead", - ), - gated!( - link_ordinal, Whitelisted, template!(List: "ordinal"), raw_dylib, - experimental!(link_ordinal) - ), - - // Plugins: - ( - sym::plugin_registrar, Normal, template!(Word), - Gated( - Stability::Deprecated( - "https://github.com/rust-lang/rust/pull/64675", - Some("may be removed in a future compiler version"), - ), - sym::plugin_registrar, - "compiler plugins are deprecated", - cfg_fn!(plugin_registrar) - ) - ), - ( - sym::plugin, CrateLevel, template!(List: "name|name(args)"), - Gated( - Stability::Deprecated( - "https://github.com/rust-lang/rust/pull/64675", - Some("may be removed in a future compiler version"), - ), - sym::plugin, - "compiler plugins are deprecated", - cfg_fn!(plugin) - ) - ), - - // Testing: - gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)), - gated!( - test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks, - "custom test frameworks are an unstable feature", - ), - // RFC #1268 - gated!(marker, Normal, template!(Word), marker_trait_attr, experimental!(marker)), - gated!( - thread_local, Whitelisted, template!(Word), - "`#[thread_local]` is an experimental feature, and does not currently handle destructors", - ), - gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)), - // RFC 2412 - gated!( - optimize, Whitelisted, template!(List: "size|speed"), optimize_attribute, - experimental!(optimize), - ), - - gated!(ffi_returns_twice, Whitelisted, template!(Word), experimental!(ffi_returns_twice)), - gated!(track_caller, Whitelisted, template!(Word), experimental!(track_caller)), - gated!( - register_attr, CrateLevel, template!(List: "attr1, attr2, ..."), - experimental!(register_attr), - ), - gated!( - register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), - experimental!(register_tool), - ), - - // ========================================================================== - // Internal attributes: Stability, deprecation, and unsafe: - // ========================================================================== - - ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")), - // FIXME(#14407) -- only looked at on-demand so we can't - // guarantee they'll have already been checked. - ungated!( - rustc_deprecated, Whitelisted, - template!(List: r#"since = "version", reason = "...""#) - ), - // FIXME(#14407) - ungated!(stable, Whitelisted, template!(List: r#"feature = "name", since = "version""#)), - // FIXME(#14407) - ungated!( - unstable, Whitelisted, - template!(List: r#"feature = "name", reason = "...", issue = "N""#), - ), - gated!( - rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), - "the `#[rustc_const_unstable]` attribute is an internal feature", - ), - gated!( - allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), - "allow_internal_unstable side-steps feature gating and stability checks", - ), - gated!( - allow_internal_unsafe, Normal, template!(Word), - "allow_internal_unsafe side-steps the unsafe_code lint", - ), - - // ========================================================================== - // Internal attributes: Type system related: - // ========================================================================== - - gated!(fundamental, Whitelisted, template!(Word), experimental!(fundamental)), - gated!( - // RFC #1445. - structural_match, Whitelisted, template!(Word), - "the semantics of constant patterns is not yet settled", - ), - gated!( - may_dangle, Normal, template!(Word), dropck_eyepatch, - "`may_dangle` has unstable semantics and may be removed in the future", - ), - - // ========================================================================== - // Internal attributes: Runtime related: - // ========================================================================== - - rustc_attr!(rustc_allocator, Whitelisted, template!(Word), IMPL_DETAIL), - rustc_attr!(rustc_allocator_nounwind, Whitelisted, template!(Word), IMPL_DETAIL), - gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)), - gated!( - default_lib_allocator, Whitelisted, template!(Word), allocator_internals, - experimental!(default_lib_allocator), - ), - gated!( - needs_allocator, Normal, template!(Word), allocator_internals, - experimental!(needs_allocator), - ), - gated!(panic_runtime, Whitelisted, template!(Word), experimental!(panic_runtime)), - gated!(needs_panic_runtime, Whitelisted, template!(Word), experimental!(needs_panic_runtime)), - gated!( - unwind, Whitelisted, template!(List: "allowed|aborts"), unwind_attributes, - experimental!(unwind), - ), - gated!( - compiler_builtins, Whitelisted, template!(Word), - "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \ - which contains compiler-rt intrinsics and will never be stable", - ), - gated!( - sanitizer_runtime, Whitelisted, template!(Word), - "the `#[sanitizer_runtime]` attribute is used to identify crates that contain the runtime \ - of a sanitizer and will never be stable", - ), - gated!( - profiler_runtime, Whitelisted, template!(Word), - "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \ - which contains the profiler runtime and will never be stable", - ), - - // ========================================================================== - // Internal attributes, Linkage: - // ========================================================================== - - gated!( - linkage, Whitelisted, template!(NameValueStr: "external|internal|..."), - "the `linkage` attribute is experimental and not portable across platforms", - ), - rustc_attr!(rustc_std_internal_symbol, Whitelisted, template!(Word), INTERNAL_UNSTABLE), - - // ========================================================================== - // Internal attributes, Macro related: - // ========================================================================== - - rustc_attr!(rustc_builtin_macro, Whitelisted, template!(Word), IMPL_DETAIL), - rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE), - rustc_attr!( - rustc_macro_transparency, Whitelisted, - template!(NameValueStr: "transparent|semitransparent|opaque"), - "used internally for testing macro hygiene", - ), - - // ========================================================================== - // Internal attributes, Diagnostics related: - // ========================================================================== - - rustc_attr!( - rustc_on_unimplemented, Whitelisted, - template!( - List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#, - NameValueStr: "message" - ), - INTERNAL_UNSTABLE - ), - // Whitelists "identity-like" conversion methods to suggest on type mismatch. - rustc_attr!(rustc_conversion_suggestion, Whitelisted, template!(Word), INTERNAL_UNSTABLE), - - // ========================================================================== - // Internal attributes, Const related: - // ========================================================================== - - rustc_attr!(rustc_promotable, Whitelisted, template!(Word), IMPL_DETAIL), - rustc_attr!(rustc_allow_const_fn_ptr, Whitelisted, template!(Word), IMPL_DETAIL), - rustc_attr!(rustc_args_required_const, Whitelisted, template!(List: "N"), INTERNAL_UNSTABLE), - - // ========================================================================== - // Internal attributes, Layout related: - // ========================================================================== - - rustc_attr!( - rustc_layout_scalar_valid_range_start, Whitelisted, template!(List: "value"), - "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \ - niche optimizations in libcore and will never be stable", - ), - rustc_attr!( - rustc_layout_scalar_valid_range_end, Whitelisted, template!(List: "value"), - "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \ - niche optimizations in libcore and will never be stable", - ), - rustc_attr!( - rustc_nonnull_optimization_guaranteed, Whitelisted, template!(Word), - "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \ - niche optimizations in libcore and will never be stable", - ), - - // ========================================================================== - // Internal attributes, Misc: - // ========================================================================== - gated!( - lang, Normal, template!(NameValueStr: "name"), lang_items, - "language items are subject to change", - ), - ( - sym::rustc_diagnostic_item, - Normal, - template!(NameValueStr: "name"), - Gated( - Stability::Unstable, - sym::rustc_attrs, - "diagnostic items compiler internal support for linting", - cfg_fn!(rustc_attrs), - ), - ), - ( - sym::no_debug, Whitelisted, template!(Word), - Gated( - Stability::Deprecated("https://github.com/rust-lang/rust/issues/29721", None), - sym::no_debug, - "the `#[no_debug]` attribute was an experimental feature that has been \ - deprecated due to lack of demand", - cfg_fn!(no_debug) - ) - ), - gated!( - // Used in resolve: - prelude_import, Whitelisted, template!(Word), - "`#[prelude_import]` is for use by rustc only", - ), - gated!( - rustc_paren_sugar, Normal, template!(Word), unboxed_closures, - "unboxed_closures are still evolving", - ), - rustc_attr!( - rustc_inherit_overflow_checks, Whitelisted, template!(Word), - "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \ - overflow checking behavior of several libcore functions that are inlined \ - across crates and will never be stable", - ), - rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"), - "the `#[rustc_reservation_impl]` attribute is internally used \ - for reserving for `for From for T` impl" - ), - rustc_attr!( - rustc_test_marker, Normal, template!(Word), - "the `#[rustc_test_marker]` attribute is used internally to track tests", - ), - - // ========================================================================== - // Internal attributes, Testing: - // ========================================================================== - - rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)), - rustc_attr!(TEST, rustc_variance, Normal, template!(Word)), - rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")), - rustc_attr!(TEST, rustc_regions, Normal, template!(Word)), - rustc_attr!( - TEST, rustc_error, Whitelisted, - template!(Word, List: "delay_span_bug_from_inside_query") - ), - rustc_attr!(TEST, rustc_dump_user_substs, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_if_this_changed, Whitelisted, template!(Word, List: "DepNode")), - rustc_attr!(TEST, rustc_then_this_would_need, Whitelisted, template!(List: "DepNode")), - rustc_attr!( - TEST, rustc_dirty, Whitelisted, - template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#), - ), - rustc_attr!( - TEST, rustc_clean, Whitelisted, - template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#), - ), - rustc_attr!( - TEST, rustc_partition_reused, Whitelisted, - template!(List: r#"cfg = "...", module = "...""#), - ), - rustc_attr!( - TEST, rustc_partition_codegened, Whitelisted, - template!(List: r#"cfg = "...", module = "...""#), - ), - rustc_attr!( - TEST, rustc_expected_cgu_reuse, Whitelisted, - template!(List: r#"cfg = "...", module = "...", kind = "...""#), - ), - rustc_attr!(TEST, rustc_synthetic, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_symbol_name, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_def_path, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_mir, Whitelisted, template!(List: "arg1, arg2, ...")), - rustc_attr!(TEST, rustc_dump_program_clauses, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_dump_env_program_clauses, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_object_lifetime_default, Whitelisted, template!(Word)), - rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)), - gated!( - omit_gdb_pretty_printer_section, Whitelisted, template!(Word), - "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite", - ), -]; - -pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> { - BUILTIN_ATTRIBUTES.iter().filter(|(.., gate)| gate.is_deprecated()).collect() -} - -pub fn is_builtin_attr_name(name: Symbol) -> bool { - BUILTIN_ATTRIBUTE_MAP.get(&name).is_some() -} - -lazy_static! { - pub static ref BUILTIN_ATTRIBUTE_MAP: FxHashMap = { - let mut map = FxHashMap::default(); - for attr in BUILTIN_ATTRIBUTES.iter() { - if map.insert(attr.0, attr).is_some() { - panic!("duplicate builtin attribute `{}`", attr.0); - } - } - map - }; -} diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index e89c1f910d3..b5dc7d4db2b 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -1,6 +1,6 @@ use rustc_feature::{ACCEPTED_FEATURES, ACTIVE_FEATURES, Features, Feature, State as FeatureState}; use rustc_feature::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; -use super::builtin_attrs::{AttributeGate, BUILTIN_ATTRIBUTE_MAP}; +use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP}; use crate::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId}; use crate::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData}; diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 74036bdcfb7..9db2007ea9c 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -99,12 +99,6 @@ pub mod feature_gate { GateIssue, UnstableFeatures, EXPLAIN_STMT_ATTR_SYNTAX, EXPLAIN_UNSIZED_TUPLE_COERCION, }; - mod builtin_attrs; - pub use builtin_attrs::{ - AttributeGate, AttributeTemplate, AttributeType, find_gated_cfg, GatedCfg, - BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, - deprecated_attributes, is_builtin_attr_name, - }; } pub mod mut_visit; pub mod ptr; diff --git a/src/libsyntax_ext/util.rs b/src/libsyntax_ext/util.rs index 893fd402e83..f7bd9a05604 100644 --- a/src/libsyntax_ext/util.rs +++ b/src/libsyntax_ext/util.rs @@ -1,7 +1,7 @@ use rustc_parse::validate_attr; +use rustc_feature::AttributeTemplate; use syntax_pos::Symbol; use syntax::ast::MetaItem; -use syntax::feature_gate::AttributeTemplate; use syntax_expand::base::ExtCtxt; pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) { -- cgit 1.4.1-3-g733a5 From db89679ebce6ed3b9bcb3f5cea430084b40a3a1f Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 02:40:28 +0100 Subject: inline two explanation constants --- src/librustc_parse/config.rs | 9 ++------- src/librustc_typeck/check/coercion.rs | 12 +++++++----- src/libsyntax/feature_gate/check.rs | 6 ------ src/libsyntax/lib.rs | 1 - 4 files changed, 9 insertions(+), 19 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc_parse/config.rs b/src/librustc_parse/config.rs index 9c309f60515..dde320aed21 100644 --- a/src/librustc_parse/config.rs +++ b/src/librustc_parse/config.rs @@ -11,12 +11,7 @@ use crate::validate_attr; use rustc_feature::Features; use syntax::attr::HasAttrs; -use syntax::feature_gate::{ - feature_err, - EXPLAIN_STMT_ATTR_SYNTAX, - get_features, - GateIssue, -}; +use syntax::feature_gate::{feature_err, get_features, GateIssue}; use syntax::attr; use syntax::ast; use syntax::edition::Edition; @@ -218,7 +213,7 @@ impl<'a> StripUnconfigured<'a> { sym::stmt_expr_attributes, attr.span, GateIssue::Language, - EXPLAIN_STMT_ATTR_SYNTAX); + "attributes on expressions are experimental"); if attr.is_doc_comment() { err.help("`///` is for documentation comments. For a plain comment, use `//`."); diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 871acb2726a..593f8e74ba9 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -644,11 +644,13 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { } if has_unsized_tuple_coercion && !self.tcx.features().unsized_tuple_coercion { - feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, - sym::unsized_tuple_coercion, - self.cause.span, - feature_gate::GateIssue::Language, - feature_gate::EXPLAIN_UNSIZED_TUPLE_COERCION); + feature_gate::emit_feature_err( + &self.tcx.sess.parse_sess, + sym::unsized_tuple_coercion, + self.cause.span, + feature_gate::GateIssue::Language, + "unsized tuple coercion is not stable enough for use and is subject to change", + ); } Ok(coercion) diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index b5dc7d4db2b..c53f5f41e32 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -150,12 +150,6 @@ fn leveled_feature_err<'a, S: Into>( const EXPLAIN_BOX_SYNTAX: &str = "box expression syntax is experimental; you can call `Box::new` instead"; -pub const EXPLAIN_STMT_ATTR_SYNTAX: &str = - "attributes on expressions are experimental"; - -pub const EXPLAIN_UNSIZED_TUPLE_COERCION: &str = - "unsized tuple coercion is not stable enough for use and is subject to change"; - struct PostExpansionVisitor<'a> { parse_sess: &'a ParseSess, features: &'a Features, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 9db2007ea9c..fda95374f64 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -97,7 +97,6 @@ pub mod feature_gate { pub use check::{ check_crate, check_attribute, get_features, feature_err, emit_feature_err, GateIssue, UnstableFeatures, - EXPLAIN_STMT_ATTR_SYNTAX, EXPLAIN_UNSIZED_TUPLE_COERCION, }; } pub mod mut_visit; -- cgit 1.4.1-3-g733a5 From b45f21d38e7e127d257c6299b9da00fdc57476b9 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 02:50:47 +0100 Subject: move UnstableFeatures -> rustc_feature --- src/librustc/session/config.rs | 6 ++-- src/librustc_codegen_llvm/lib.rs | 1 + src/librustc_codegen_llvm/llvm_util.rs | 2 +- src/librustc_driver/lib.rs | 6 ++-- src/librustc_feature/lib.rs | 34 ++++++++++++++++++++ src/librustdoc/core.rs | 2 +- src/librustdoc/externalfiles.rs | 2 +- src/librustdoc/html/render.rs | 2 +- src/librustdoc/markdown.rs | 2 +- src/librustdoc/passes/collect_intra_doc_links.rs | 2 +- src/librustdoc/test.rs | 2 +- src/libsyntax/feature_gate/check.rs | 40 ++---------------------- src/libsyntax/lib.rs | 2 +- src/libsyntax/sess.rs | 4 +-- 14 files changed, 52 insertions(+), 55 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 6733250e1e8..fbfae721bbe 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -7,6 +7,7 @@ use crate::session::{early_error, early_warn, Session}; use crate::session::search_paths::SearchPath; use rustc_data_structures::fx::FxHashSet; +use rustc_feature::UnstableFeatures; use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel}; use rustc_target::spec::{Target, TargetTriple}; @@ -16,7 +17,6 @@ use syntax::ast; use syntax::source_map::{FileName, FilePathMapping}; use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION}; use syntax::symbol::{sym, Symbol}; -use syntax::feature_gate::UnstableFeatures; use errors::emitter::HumanReadableErrorType; use errors::{ColorConfig, FatalError, Handler}; @@ -2701,7 +2701,7 @@ pub fn parse_crate_types_from_list(list_list: Vec) -> Result), } +#[derive(Clone, Copy, Hash)] +pub enum UnstableFeatures { + /// Hard errors for unstable features are active, as on beta/stable channels. + Disallow, + /// Allow features to be activated, as on nightly. + Allow, + /// Errors are bypassed for bootstrapping. This is required any time + /// during the build that feature-related lints are set to warn or above + /// because the build turns on warnings-as-errors and uses lots of unstable + /// features. As a result, this is always required for building Rust itself. + Cheat +} + +impl UnstableFeatures { + pub fn from_environment() -> UnstableFeatures { + // `true` if this is a feature-staged build, i.e., on the beta or stable channel. + let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some(); + // `true` if we should enable unstable features for bootstrapping. + let bootstrap = std::env::var("RUSTC_BOOTSTRAP").is_ok(); + match (disable_unstable_features, bootstrap) { + (_, true) => UnstableFeatures::Cheat, + (true, _) => UnstableFeatures::Disallow, + (false, _) => UnstableFeatures::Allow + } + } + + pub fn is_nightly_build(&self) -> bool { + match *self { + UnstableFeatures::Allow | UnstableFeatures::Cheat => true, + UnstableFeatures::Disallow => false, + } + } +} + pub use accepted::ACCEPTED_FEATURES; pub use active::{ACTIVE_FEATURES, Features, INCOMPLETE_FEATURES}; pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 612f3c69871..9e03896d980 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -12,12 +12,12 @@ use rustc::session::DiagnosticOutput; use rustc::util::nodemap::{FxHashMap, FxHashSet}; use rustc_interface::interface; use rustc_driver::abort_on_err; +use rustc_feature::UnstableFeatures; use rustc_resolve as resolve; use syntax::ast::CRATE_NODE_ID; use syntax::source_map; use syntax::attr; -use syntax::feature_gate::UnstableFeatures; use errors::json::JsonEmitter; use syntax::symbol::sym; use syntax_pos::DUMMY_SP; diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index 56f1191feed..7945850ef08 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -2,7 +2,7 @@ use std::fs; use std::path::Path; use std::str; use errors; -use crate::syntax::feature_gate::UnstableFeatures; +use rustc_feature::UnstableFeatures; use crate::syntax::edition::Edition; use crate::html::markdown::{IdMap, ErrorCodes, Markdown, Playground}; diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index ba94cb82c00..b5c1a77a387 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -45,7 +45,6 @@ use errors; use serialize::json::{ToJson, Json, as_json}; use syntax::ast; use syntax::edition::Edition; -use syntax::feature_gate::UnstableFeatures; use syntax::print::pprust; use syntax::source_map::FileName; use syntax::symbol::{Symbol, sym}; @@ -56,6 +55,7 @@ use rustc::middle::stability; use rustc::hir; use rustc::util::nodemap::{FxHashMap, FxHashSet}; use rustc_data_structures::flock; +use rustc_feature::UnstableFeatures; use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, Mutability}; use crate::config::RenderOptions; diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 8431271e62d..7dc3df23a6d 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -6,7 +6,7 @@ use errors; use testing; use syntax::edition::Edition; use syntax::source_map::DUMMY_SP; -use syntax::feature_gate::UnstableFeatures; +use rustc_feature::UnstableFeatures; use crate::externalfiles::{LoadStringError, load_string}; use crate::config::{Options, RenderOptions}; diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index d8f2dbca835..3c021ae7465 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -5,10 +5,10 @@ use rustc::hir; use rustc::lint as lint; use rustc::ty; use rustc_resolve::ParentScope; +use rustc_feature::UnstableFeatures; use syntax; use syntax::ast::{self, Ident}; use syntax_expand::base::SyntaxExtensionKind; -use syntax::feature_gate::UnstableFeatures; use syntax::symbol::Symbol; use syntax_pos::DUMMY_SP; diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 22f209b8bad..cf5fb06fa56 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -1,4 +1,5 @@ use rustc_data_structures::sync::Lrc; +use rustc_feature::UnstableFeatures; use rustc_interface::interface; use rustc_target::spec::TargetTriple; use rustc::hir; @@ -9,7 +10,6 @@ use syntax::ast; use syntax::with_globals; use syntax::source_map::SourceMap; use syntax::edition::Edition; -use syntax::feature_gate::UnstableFeatures; use std::env; use std::io::{self, Write}; use std::panic; diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index c53f5f41e32..141b324baa8 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -1,6 +1,6 @@ -use rustc_feature::{ACCEPTED_FEATURES, ACTIVE_FEATURES, Features, Feature, State as FeatureState}; -use rustc_feature::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; +use rustc_feature::{ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP}; +use rustc_feature::{Features, Feature, State as FeatureState, UnstableFeatures}; use crate::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId}; use crate::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData}; @@ -18,8 +18,6 @@ use log::debug; use rustc_error_codes::*; - -use std::env; use std::num::NonZeroU32; macro_rules! gate_feature_fn { @@ -880,40 +878,6 @@ pub fn check_crate(krate: &ast::Crate, visit::walk_crate(&mut visitor, krate); } -#[derive(Clone, Copy, Hash)] -pub enum UnstableFeatures { - /// Hard errors for unstable features are active, as on beta/stable channels. - Disallow, - /// Allow features to be activated, as on nightly. - Allow, - /// Errors are bypassed for bootstrapping. This is required any time - /// during the build that feature-related lints are set to warn or above - /// because the build turns on warnings-as-errors and uses lots of unstable - /// features. As a result, this is always required for building Rust itself. - Cheat -} - -impl UnstableFeatures { - pub fn from_environment() -> UnstableFeatures { - // `true` if this is a feature-staged build, i.e., on the beta or stable channel. - let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some(); - // `true` if we should enable unstable features for bootstrapping. - let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok(); - match (disable_unstable_features, bootstrap) { - (_, true) => UnstableFeatures::Cheat, - (true, _) => UnstableFeatures::Disallow, - (false, _) => UnstableFeatures::Allow - } - } - - pub fn is_nightly_build(&self) -> bool { - match *self { - UnstableFeatures::Allow | UnstableFeatures::Cheat => true, - UnstableFeatures::Disallow => false, - } - } -} - fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate, unstable: UnstableFeatures) { if !unstable.is_nightly_build() { for attr in krate.attrs.iter().filter(|attr| attr.check_name(sym::feature)) { diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index fda95374f64..c2d887c9267 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -96,7 +96,7 @@ pub mod feature_gate { mod check; pub use check::{ check_crate, check_attribute, get_features, feature_err, emit_feature_err, - GateIssue, UnstableFeatures, + GateIssue, }; } pub mod mut_visit; diff --git a/src/libsyntax/sess.rs b/src/libsyntax/sess.rs index 740e9dfe459..aa9217c1b69 100644 --- a/src/libsyntax/sess.rs +++ b/src/libsyntax/sess.rs @@ -3,15 +3,15 @@ use crate::ast::{CrateConfig, NodeId}; use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId}; -use crate::source_map::{SourceMap, FilePathMapping}; -use crate::feature_gate::UnstableFeatures; use errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder}; use rustc_data_structures::fx::{FxHashSet, FxHashMap}; use rustc_data_structures::sync::{Lrc, Lock, Once}; +use rustc_feature::UnstableFeatures; use syntax_pos::{Symbol, Span, MultiSpan}; use syntax_pos::edition::Edition; use syntax_pos::hygiene::ExpnId; +use syntax_pos::source_map::{SourceMap, FilePathMapping}; use std::path::PathBuf; use std::str; -- cgit 1.4.1-3-g733a5 From 79077f13ffa9194161a0ed574b036794ab1a8c36 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 30 Nov 2019 07:40:28 +0100 Subject: move GateIssue to rustc_feature & simplify emit_feature_err --- src/librustc/lint/levels.rs | 6 +-- src/librustc/middle/stability.rs | 24 +++++++----- src/librustc_feature/lib.rs | 34 +++++++++++++++- src/librustc_metadata/native_libs.rs | 36 +++++++++-------- src/librustc_mir/transform/check_consts/ops.rs | 48 +++++++++++------------ src/librustc_parse/config.rs | 3 +- src/librustc_passes/check_const.rs | 10 +---- src/librustc_resolve/macros.rs | 11 ++---- src/librustc_typeck/astconv.rs | 5 +-- src/librustc_typeck/check/coercion.rs | 6 +-- src/librustc_typeck/check/mod.rs | 32 ++++++++------- src/librustc_typeck/check/wfcheck.rs | 6 +-- src/librustc_typeck/collect.rs | 12 +++--- src/libsyntax/attr/builtin.rs | 4 +- src/libsyntax/feature_gate/check.rs | 54 ++++++-------------------- src/libsyntax/lib.rs | 5 +-- src/libsyntax_expand/expand.rs | 28 ++++++------- 17 files changed, 159 insertions(+), 165 deletions(-) (limited to 'src/libsyntax/lib.rs') diff --git a/src/librustc/lint/levels.rs b/src/librustc/lint/levels.rs index 27bf9649324..619ca724214 100644 --- a/src/librustc/lint/levels.rs +++ b/src/librustc/lint/levels.rs @@ -232,13 +232,13 @@ impl<'a> LintLevelsBuilder<'a> { // don't have any lint names (`#[level(reason = "foo")]`) if let ast::LitKind::Str(rationale, _) = name_value.kind { if !self.sess.features_untracked().lint_reasons { - feature_gate::emit_feature_err( + feature_gate::feature_err( &self.sess.parse_sess, sym::lint_reasons, item.span, - feature_gate::GateIssue::Language, "lint reasons are experimental" - ); + ) + .emit(); } reason = Some(rationale); } else { diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs index 411a47423c5..54aafe2114d 100644 --- a/src/librustc/middle/stability.rs +++ b/src/librustc/middle/stability.rs @@ -13,11 +13,12 @@ use crate::ty::query::Providers; use crate::middle::privacy::AccessLevels; use crate::session::{DiagnosticMessageId, Session}; use errors::DiagnosticBuilder; +use rustc_feature::GateIssue; use syntax::symbol::{Symbol, sym}; use syntax_pos::{Span, MultiSpan}; use syntax::ast::{Attribute, CRATE_NODE_ID}; use syntax::errors::Applicability; -use syntax::feature_gate::{GateIssue, emit_feature_err}; +use syntax::feature_gate::{feature_err, feature_err_issue}; use syntax::attr::{self, Stability, Deprecation, RustcDeprecation}; use crate::ty::{self, TyCtxt}; use crate::util::nodemap::{FxHashSet, FxHashMap}; @@ -512,9 +513,8 @@ pub fn report_unstable( if is_soft { soft_handler(lint::builtin::SOFT_UNSTABLE, span, &msg) } else { - emit_feature_err( - &sess.parse_sess, feature, span, GateIssue::Library(issue), &msg - ); + feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg) + .emit(); } } } @@ -842,15 +842,19 @@ impl Visitor<'tcx> for Checker<'tcx> { let ty = self.tcx.type_of(def_id); if adt_def.has_dtor(self.tcx) { - emit_feature_err(&self.tcx.sess.parse_sess, - sym::untagged_unions, item.span, GateIssue::Language, - "unions with `Drop` implementations are unstable"); + feature_err( + &self.tcx.sess.parse_sess, sym::untagged_unions, item.span, + "unions with `Drop` implementations are unstable" + ) + .emit(); } else { let param_env = self.tcx.param_env(def_id); if !param_env.can_type_implement_copy(self.tcx, ty).is_ok() { - emit_feature_err(&self.tcx.sess.parse_sess, - sym::untagged_unions, item.span, GateIssue::Language, - "unions with non-`Copy` fields are unstable"); + feature_err( + &self.tcx.sess.parse_sess, sym::untagged_unions, item.span, + "unions with non-`Copy` fields are unstable" + ) + .emit(); } } } diff --git a/src/librustc_feature/lib.rs b/src/librustc_feature/lib.rs index 82e60889316..c38bb3740af 100644 --- a/src/librustc_feature/lib.rs +++ b/src/librustc_feature/lib.rs @@ -49,8 +49,7 @@ pub struct Feature { } impl Feature { - // FIXME(Centril): privatize again. - pub fn issue(&self) -> Option { + fn issue(&self) -> Option { self.issue.and_then(|i| NonZeroU32::new(i)) } } @@ -97,6 +96,37 @@ impl UnstableFeatures { } } +fn find_lang_feature_issue(feature: Symbol) -> Option { + if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) { + // FIXME (#28244): enforce that active features have issue numbers + // assert!(info.issue().is_some()) + info.issue() + } else { + // search in Accepted, Removed, or Stable Removed features + let found = ACCEPTED_FEATURES + .iter() + .chain(REMOVED_FEATURES) + .chain(STABLE_REMOVED_FEATURES) + .find(|t| t.name == feature); + match found { + Some(found) => found.issue(), + None => panic!("feature `{}` is not declared anywhere", feature), + } + } +} + +pub enum GateIssue { + Language, + Library(Option) +} + +pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option { + match issue { + GateIssue::Language => find_lang_feature_issue(feature), + GateIssue::Library(lib) => lib, + } +} + pub use accepted::ACCEPTED_FEATURES; pub use active::{ACTIVE_FEATURES, Features, INCOMPLETE_FEATURES}; pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; diff --git a/src/librustc_metadata/native_libs.rs b/src/librustc_metadata/native_libs.rs index e577b238c86..425e5d1d821 100644 --- a/src/librustc_metadata/native_libs.rs +++ b/src/librustc_metadata/native_libs.rs @@ -7,7 +7,7 @@ use rustc::util::nodemap::FxHashSet; use rustc_target::spec::abi::Abi; use syntax::attr; use syntax::source_map::Span; -use syntax::feature_gate::{self, GateIssue}; +use syntax::feature_gate::feature_err; use syntax::symbol::{kw, sym, Symbol}; use syntax::{span_err, struct_span_err}; @@ -158,27 +158,29 @@ impl Collector<'tcx> { } } if lib.cfg.is_some() && !self.tcx.features().link_cfg { - feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, - sym::link_cfg, - span.unwrap(), - GateIssue::Language, - "is unstable"); + feature_err(&self.tcx.sess.parse_sess, sym::link_cfg, span.unwrap(), "is unstable") + .emit(); } if lib.kind == cstore::NativeStaticNobundle && - !self.tcx.features().static_nobundle { - feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, - sym::static_nobundle, - span.unwrap_or_else(|| syntax_pos::DUMMY_SP), - GateIssue::Language, - "kind=\"static-nobundle\" is unstable"); + !self.tcx.features().static_nobundle + { + feature_err( + &self.tcx.sess.parse_sess, + sym::static_nobundle, + span.unwrap_or_else(|| syntax_pos::DUMMY_SP), + "kind=\"static-nobundle\" is unstable" + ) + .emit(); } if lib.kind == cstore::NativeRawDylib && !self.tcx.features().raw_dylib { - feature_gate::emit_feature_err(&self.tcx.sess.parse_sess, - sym::raw_dylib, - span.unwrap_or_else(|| syntax_pos::DUMMY_SP), - GateIssue::Language, - "kind=\"raw-dylib\" is unstable"); + feature_err( + &self.tcx.sess.parse_sess, + sym::raw_dylib, + span.unwrap_or_else(|| syntax_pos::DUMMY_SP), + "kind=\"raw-dylib\" is unstable" + ) + .emit(); } self.libs.push(lib); } diff --git a/src/librustc_mir/transform/check_consts/ops.rs b/src/librustc_mir/transform/check_consts/ops.rs index acad56be604..a4f12a4e54f 100644 --- a/src/librustc_mir/transform/check_consts/ops.rs +++ b/src/librustc_mir/transform/check_consts/ops.rs @@ -4,7 +4,7 @@ use rustc::hir::def_id::DefId; use rustc::mir::BorrowKind; use rustc::session::config::nightly_options; use rustc::ty::TyCtxt; -use syntax::feature_gate::{emit_feature_err, GateIssue}; +use syntax::feature_gate::feature_err; use syntax::symbol::sym; use syntax_pos::{Span, Symbol}; @@ -222,13 +222,13 @@ impl NonConstOp for Panic { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( + feature_err( &item.tcx.sess.parse_sess, sym::const_panic, span, - GateIssue::Language, &format!("panicking in {}s is unstable", item.const_kind()), - ); + ) + .emit(); } } @@ -240,13 +240,13 @@ impl NonConstOp for RawPtrComparison { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( + feature_err( &item.tcx.sess.parse_sess, sym::const_compare_raw_pointers, span, - GateIssue::Language, &format!("comparing raw pointers inside {}", item.const_kind()), - ); + ) + .emit(); } } @@ -258,14 +258,14 @@ impl NonConstOp for RawPtrDeref { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( - &item.tcx.sess.parse_sess, sym::const_raw_ptr_deref, - span, GateIssue::Language, + feature_err( + &item.tcx.sess.parse_sess, sym::const_raw_ptr_deref, span, &format!( "dereferencing raw pointers in {}s is unstable", item.const_kind(), ), - ); + ) + .emit(); } } @@ -277,14 +277,14 @@ impl NonConstOp for RawPtrToIntCast { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( - &item.tcx.sess.parse_sess, sym::const_raw_ptr_to_usize_cast, - span, GateIssue::Language, + feature_err( + &item.tcx.sess.parse_sess, sym::const_raw_ptr_to_usize_cast, span, &format!( "casting pointers to integers in {}s is unstable", item.const_kind(), ), - ); + ) + .emit(); } } @@ -334,11 +334,11 @@ impl NonConstOp for Transmute { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( - &item.tcx.sess.parse_sess, sym::const_transmute, - span, GateIssue::Language, - &format!("The use of std::mem::transmute() \ - is gated in {}s", item.const_kind())); + feature_err( + &item.tcx.sess.parse_sess, sym::const_transmute, span, + &format!("The use of std::mem::transmute() is gated in {}s", item.const_kind()) + ) + .emit(); } } @@ -355,10 +355,10 @@ impl NonConstOp for UnionAccess { } fn emit_error(&self, item: &Item<'_, '_>, span: Span) { - emit_feature_err( - &item.tcx.sess.parse_sess, sym::const_fn_union, - span, GateIssue::Language, + feature_err( + &item.tcx.sess.parse_sess, sym::const_fn_union, span, "unions in const fn are unstable", - ); + ) + .emit(); } } diff --git a/src/librustc_parse/config.rs b/src/librustc_parse/config.rs index dde320aed21..6293858ed4e 100644 --- a/src/librustc_parse/config.rs +++ b/src/librustc_parse/config.rs @@ -11,7 +11,7 @@ use crate::validate_attr; use rustc_feature::Features; use syntax::attr::HasAttrs; -use syntax::feature_gate::{feature_err, get_features, GateIssue}; +use syntax::feature_gate::{feature_err, get_features}; use syntax::attr; use syntax::ast; use syntax::edition::Edition; @@ -212,7 +212,6 @@ impl<'a> StripUnconfigured<'a> { let mut err = feature_err(self.sess, sym::stmt_expr_attributes, attr.span, - GateIssue::Language, "attributes on expressions are experimental"); if attr.is_doc_comment() { diff --git a/src/librustc_passes/check_const.rs b/src/librustc_passes/check_const.rs index 697a3ae873c..63c6e60de79 100644 --- a/src/librustc_passes/check_const.rs +++ b/src/librustc_passes/check_const.rs @@ -15,7 +15,7 @@ use rustc::ty::TyCtxt; use rustc::ty::query::Providers; use rustc_feature::Features; use syntax::ast::Mutability; -use syntax::feature_gate::{emit_feature_err, GateIssue}; +use syntax::feature_gate::feature_err; use syntax::span_err; use syntax_pos::{sym, Span}; use rustc_error_codes::*; @@ -141,13 +141,7 @@ impl<'tcx> CheckConstVisitor<'tcx> { | NonConstExpr::Match(hir::MatchSource::Normal) | NonConstExpr::Match(hir::MatchSource::IfDesugar { .. }) | NonConstExpr::Match(hir::MatchSource::IfLetDesugar { .. }) - => emit_feature_err( - &self.tcx.sess.parse_sess, - sym::const_if_match, - span, - GateIssue::Language, - &msg - ), + => feature_err(&self.tcx.sess.parse_sess, sym::const_if_match, span, &msg).emit(), _ => span_err!(self.tcx.sess, span, E0744, "{}", msg), } diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index cc5bc2b41dc..9e7098da49f 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -16,7 +16,7 @@ use rustc_feature::is_builtin_attr_name; use syntax::ast::{self, NodeId, Ident}; use syntax::attr::{self, StabilityLevel}; use syntax::edition::Edition; -use syntax::feature_gate::{emit_feature_err, GateIssue}; +use syntax::feature_gate::feature_err; use syntax::print::pprust; use syntax_expand::base::{self, InvocationRes, Indeterminate}; use syntax_expand::base::SyntaxExtension; @@ -346,13 +346,8 @@ impl<'a> Resolver<'a> { 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, - ); + feature_err(&self.session.parse_sess, sym::rustc_attrs, segment.ident.span, msg) + .emit(); } } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index a33b2e32c86..dafb89badd7 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -23,7 +23,7 @@ use crate::require_c_abi_if_c_variadic; use smallvec::SmallVec; use syntax::ast; use syntax::errors::pluralize; -use syntax::feature_gate::{GateIssue, emit_feature_err}; +use syntax::feature_gate::feature_err; use syntax::util::lev_distance::find_best_match_for_name; use syntax::symbol::sym; use syntax_pos::{DUMMY_SP, Span, MultiSpan}; @@ -914,8 +914,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } else { "parenthetical notation is only stable when used with `Fn`-family traits" }; - emit_feature_err(&self.tcx().sess.parse_sess, sym::unboxed_closures, - span, GateIssue::Language, msg); + feature_err(&self.tcx().sess.parse_sess, sym::unboxed_closures, span, msg).emit(); } self.create_substs_for_ast_path(span, diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 593f8e74ba9..901a2192e20 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -644,13 +644,13 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { } if has_unsized_tuple_coercion && !self.tcx.features().unsized_tuple_coercion { - feature_gate::emit_feature_err( + feature_gate::feature_err( &self.tcx.sess.parse_sess, sym::unsized_tuple_coercion, self.cause.span, - feature_gate::GateIssue::Language, "unsized tuple coercion is not stable enough for use and is subject to change", - ); + ) + .emit(); } Ok(coercion) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 930241262b0..84607fb6c81 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -125,7 +125,7 @@ use syntax_pos::{self, BytePos, Span, MultiSpan}; use syntax_pos::hygiene::DesugaringKind; use syntax::ast; use syntax::attr; -use syntax::feature_gate::{GateIssue, emit_feature_err}; +use syntax::feature_gate::feature_err; use syntax::source_map::{DUMMY_SP, original_sp}; use syntax::symbol::{kw, sym, Ident}; use syntax::util::parser::ExprPrecedence; @@ -2373,13 +2373,13 @@ fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { if adt.is_enum() { if !tcx.features().transparent_enums { - emit_feature_err( + feature_err( &tcx.sess.parse_sess, sym::transparent_enums, sp, - GateIssue::Language, "transparent enums are unstable", - ); + ) + .emit(); } if adt.variants.len() != 1 { bad_variant_count(tcx, adt, sp, def_id); @@ -2391,11 +2391,13 @@ fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) { } if adt.is_union() && !tcx.features().transparent_unions { - emit_feature_err(&tcx.sess.parse_sess, - sym::transparent_unions, - sp, - GateIssue::Language, - "transparent unions are unstable"); + feature_err( + &tcx.sess.parse_sess, + sym::transparent_unions, + sp, + "transparent unions are unstable", + ) + .emit(); } // For each field, figure out if it's known to be a ZST and align(1) @@ -2452,11 +2454,13 @@ pub fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, vs: &'tcx [hir::Variant], i let repr_type_ty = def.repr.discr_type().to_ty(tcx); if repr_type_ty == tcx.types.i128 || repr_type_ty == tcx.types.u128 { if !tcx.features().repr128 { - emit_feature_err(&tcx.sess.parse_sess, - sym::repr128, - sp, - GateIssue::Language, - "repr with 128-bit type is unstable"); + feature_err( + &tcx.sess.parse_sess, + sym::repr128, + sp, + "repr with 128-bit type is unstable", + ) + .emit(); } } diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index b491b103313..20b6b01de57 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -10,7 +10,7 @@ use rustc::middle::lang_items; use rustc::infer::opaque_types::may_define_opaque_type; use syntax::ast; -use syntax::feature_gate::{self, GateIssue}; +use syntax::feature_gate; use syntax_pos::Span; use syntax::symbol::sym; use errors::DiagnosticBuilder; @@ -830,13 +830,13 @@ fn check_method_receiver<'fcx, 'tcx>( &fcx.tcx.sess.parse_sess, sym::arbitrary_self_types, span, - GateIssue::Language, &format!( "`{}` cannot be used as the type of `self` without \ the `arbitrary_self_types` feature", receiver_ty, ), - ).help(HELP_FOR_SELF_TYPE) + ) + .help(HELP_FOR_SELF_TYPE) .emit(); } else { // Report error; would not have worked with `arbitrary_self_types`. diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 652f081e176..ab9845c89f5 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1494,16 +1494,16 @@ fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { _ => None, }; if let Some(unsupported_type) = err { - feature_gate::emit_feature_err( + feature_gate::feature_err( &tcx.sess.parse_sess, sym::const_compare_raw_pointers, hir_ty.span, - feature_gate::GateIssue::Language, &format!( "using {} as const generic parameters is unstable", unsupported_type ), - ); + ) + .emit(); }; } if ty::search_for_structural_match_violation( @@ -2522,13 +2522,13 @@ fn from_target_feature( None => true, }; if !allowed && id.is_local() { - feature_gate::emit_feature_err( + feature_gate::feature_err( &tcx.sess.parse_sess, feature_gate.unwrap(), item.span(), - feature_gate::GateIssue::Language, &format!("the target feature `{}` is currently unstable", feature), - ); + ) + .emit(); } Some(Symbol::intern(feature)) })); diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index bbcb8cc3c62..3c10f27b60a 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -2,7 +2,7 @@ use super::{mark_used, MetaItemKind}; use crate::ast::{self, Attribute, MetaItem, NestedMetaItem}; -use crate::feature_gate::{emit_feature_err, GateIssue}; +use crate::feature_gate::feature_err; use crate::print::pprust; use crate::sess::ParseSess; @@ -569,7 +569,7 @@ fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &F let (cfg, feature, has_feature) = gated_cfg; if !has_feature(features) && !cfg_span.allows_unstable(*feature) { let explain = format!("`cfg({})` is experimental and subject to change", cfg); - emit_feature_err(sess, *feature, cfg_span, GateIssue::Language, &explain); + feature_err(sess, *feature, cfg_span, &explain).emit() } } diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index ea0eff1eed9..c90f4c6f19f 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -1,6 +1,7 @@ use rustc_feature::{ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP}; use rustc_feature::{Features, Feature, State as FeatureState, UnstableFeatures}; +use rustc_feature::{find_feature_issue, GateIssue}; use crate::ast::{self, AssocTyConstraint, AssocTyConstraintKind, NodeId}; use crate::ast::{GenericParam, GenericParamKind, PatKind, RangeEnd, VariantData}; @@ -18,8 +19,6 @@ use log::debug; use rustc_error_codes::*; -use std::num::NonZeroU32; - macro_rules! gate_feature_fn { ($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $level: expr) => {{ let (cx, has_feature, span, @@ -48,30 +47,6 @@ pub fn check_attribute(attr: &ast::Attribute, parse_sess: &ParseSess, features: PostExpansionVisitor { parse_sess, features }.visit_attribute(attr) } -fn find_lang_feature_issue(feature: Symbol) -> Option { - if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) { - // FIXME (#28244): enforce that active features have issue numbers - // assert!(info.issue().is_some()) - info.issue() - } else { - // search in Accepted, Removed, or Stable Removed features - let found = ACCEPTED_FEATURES - .iter() - .chain(REMOVED_FEATURES) - .chain(STABLE_REMOVED_FEATURES) - .find(|t| t.name == feature); - match found { - Some(found) => found.issue(), - None => panic!("feature `{}` is not declared anywhere", feature), - } - } -} - -pub enum GateIssue { - Language, - Library(Option) -} - #[derive(Debug, Copy, Clone, PartialEq)] pub enum GateStrength { /// A hard error. (Most feature gates should use this.) @@ -80,41 +55,35 @@ pub enum GateStrength { Soft, } -pub fn emit_feature_err( - sess: &ParseSess, +pub fn feature_err<'a>( + sess: &'a ParseSess, feature: Symbol, - span: Span, - issue: GateIssue, + span: impl Into, explain: &str, -) { - feature_err(sess, feature, span, issue, explain).emit(); +) -> DiagnosticBuilder<'a> { + feature_err_issue(sess, feature, span, GateIssue::Language, explain) } -pub fn feature_err<'a, S: Into>( +pub fn feature_err_issue<'a>( sess: &'a ParseSess, feature: Symbol, - span: S, + span: impl Into, issue: GateIssue, explain: &str, ) -> DiagnosticBuilder<'a> { leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard) } -fn leveled_feature_err<'a, S: Into>( +fn leveled_feature_err<'a>( sess: &'a ParseSess, feature: Symbol, - span: S, + span: impl Into, issue: GateIssue, explain: &str, level: GateStrength, ) -> DiagnosticBuilder<'a> { let diag = &sess.span_diagnostic; - let issue = match issue { - GateIssue::Language => find_lang_feature_issue(feature), - GateIssue::Library(lib) => lib, - }; - let mut err = match level { GateStrength::Hard => { diag.struct_span_err_with_code(span, explain, stringify_error_code!(E0658)) @@ -122,7 +91,7 @@ fn leveled_feature_err<'a, S: Into>( GateStrength::Soft => diag.struct_span_warn(span, explain), }; - if let Some(n) = issue { + if let Some(n) = find_feature_issue(feature, issue) { err.note(&format!( "for more information, see https://github.com/rust-lang/rust/issues/{}", n, @@ -257,7 +226,6 @@ impl<'a> PostExpansionVisitor<'a> { self.parse_sess, sym::arbitrary_enum_discriminant, discriminant_spans.clone(), - crate::feature_gate::GateIssue::Language, "custom discriminant values are not allowed in enums with tuple or struct variants", ); for sp in discriminant_spans { diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index c2d887c9267..3d4a5d624c1 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -94,10 +94,7 @@ pub use syntax_pos::source_map; pub mod entry; pub mod feature_gate { mod check; - pub use check::{ - check_crate, check_attribute, get_features, feature_err, emit_feature_err, - GateIssue, - }; + pub use check::{check_crate, check_attribute, get_features, feature_err, feature_err_issue}; } pub mod mut_visit; pub mod ptr; diff --git a/src/libsyntax_expand/expand.rs b/src/libsyntax_expand/expand.rs index e4c4f4c43e6..a6ced1439c5 100644 --- a/src/libsyntax_expand/expand.rs +++ b/src/libsyntax_expand/expand.rs @@ -14,7 +14,7 @@ use syntax::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path}; use syntax::ast::{MacStmtStyle, StmtKind, ItemKind}; use syntax::attr::{self, HasAttrs, is_builtin_attr}; use syntax::source_map::respan; -use syntax::feature_gate::{self, GateIssue, emit_feature_err}; +use syntax::feature_gate::{self, feature_err}; use syntax::mut_visit::*; use syntax::print::pprust; use syntax::ptr::P; @@ -727,13 +727,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if self.cx.ecfg.proc_macro_hygiene() { return } - emit_feature_err( + feature_err( self.cx.parse_sess, sym::proc_macro_hygiene, span, - GateIssue::Language, &format!("custom attributes cannot be applied to {}", kind), - ); + ) + .emit(); } fn gate_proc_macro_input(&self, annotatable: &Annotatable) { @@ -745,13 +745,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn visit_item(&mut self, item: &'ast ast::Item) { match &item.kind { ast::ItemKind::Mod(module) if !module.inline => { - emit_feature_err( + feature_err( self.parse_sess, sym::proc_macro_hygiene, item.span, - GateIssue::Language, "non-inline modules in proc macro input are unstable", - ); + ) + .emit(); } _ => {} } @@ -790,13 +790,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if self.cx.ecfg.proc_macro_hygiene() { return } - emit_feature_err( + feature_err( self.cx.parse_sess, sym::proc_macro_hygiene, span, - GateIssue::Language, &format!("procedural macros cannot be expanded to {}", kind), - ); + ) + .emit(); } fn parse_ast_fragment( @@ -992,9 +992,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { if let Some(attr) = &attr { if !self.cx.ecfg.custom_inner_attributes() && attr.style == ast::AttrStyle::Inner && !attr.has_name(sym::test) { - emit_feature_err(&self.cx.parse_sess, sym::custom_inner_attributes, - attr.span, GateIssue::Language, - "non-builtin inner attributes are unstable"); + feature_err( + &self.cx.parse_sess, sym::custom_inner_attributes, attr.span, + "non-builtin inner attributes are unstable" + ) + .emit(); } } attr -- cgit 1.4.1-3-g733a5