From: Vadim Petrochenkov Date: Sat, 24 Mar 2018 18:17:27 +0000 (+0300) Subject: Use `Ident` instead of `Name` in `MetaItem` X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=3a30bad6de92fd00f24b8ba9547798cb1afa1ba3;p=rust.git Use `Ident` instead of `Name` in `MetaItem` --- diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index ec2aeed8ad8..b13c289394a 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -3503,12 +3503,10 @@ fn lower_expr(&mut self, e: &Expr) -> hir::Expr { let attr = { // allow(unreachable_code) let allow = { - let allow_ident = self.str_to_ident("allow"); - let uc_ident = self.str_to_ident("unreachable_code"); - let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident); - let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item); - let uc_spanned = respan(e.span, uc_nested); - attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned]) + let allow_ident = Ident::from_str("allow").with_span_pos(e.span); + let uc_ident = Ident::from_str("unreachable_code").with_span_pos(e.span); + let uc_nested = attr::mk_nested_word_item(uc_ident); + attr::mk_list_item(e.span, allow_ident, vec![uc_nested]) }; attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow) }; diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index f58300fd90f..3bb4c86e7c2 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -341,7 +341,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>( }); impl_stable_hash_for!(struct ::syntax::ast::MetaItem { - name, + ident, node, span }); diff --git a/src/librustc/lint/levels.rs b/src/librustc/lint/levels.rs index 8a899a35ecb..e8b536d5267 100644 --- a/src/librustc/lint/levels.rs +++ b/src/librustc/lint/levels.rs @@ -221,7 +221,7 @@ pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush { continue } }; - let name = word.name(); + let name = word.ident.name; match store.check_lint_name(&name.as_str()) { CheckLintNameResult::Ok(ids) => { let src = LintSource::Node(name, li.span); diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 59d1a298eaa..8cb87e7e080 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1683,12 +1683,12 @@ pub fn parse_cfgspecs(cfgspecs: Vec) -> ast::CrateConfig { } else if meta_item.is_meta_item_list() { let msg = format!( "invalid predicate in --cfg command line argument: `{}`", - meta_item.name() + meta_item.ident ); early_error(ErrorOutputType::default(), &msg) } - (meta_item.name(), meta_item.value_str()) + (meta_item.ident.name, meta_item.value_str()) }) .collect::() } diff --git a/src/librustc/traits/on_unimplemented.rs b/src/librustc/traits/on_unimplemented.rs index 8c2c1cfa454..a1018cb946a 100644 --- a/src/librustc/traits/on_unimplemented.rs +++ b/src/librustc/traits/on_unimplemented.rs @@ -190,7 +190,7 @@ pub fn evaluate(&self, for command in self.subcommands.iter().chain(Some(self)).rev() { if let Some(ref condition) = command.condition { if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| { - options.contains(&(c.name().as_str().to_string(), + options.contains(&(c.ident.name.as_str().to_string(), match c.value_str().map(|s| s.as_str().to_string()) { Some(s) => Some(s), None => None diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index b461431c7bb..6f88b0aecb6 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -1045,7 +1045,7 @@ fn print_crate_info(trans: &TransCrate, let mut cfgs = Vec::new(); for &(name, ref value) in sess.parse_sess.config.iter() { let gated_cfg = GatedCfg::gate(&ast::MetaItem { - name, + ident: ast::Ident::with_empty_ctxt(name), node: ast::MetaItemKind::Word, span: DUMMY_SP, }); diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 17a6176b79e..57311a7b588 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -110,7 +110,7 @@ fn argument(&self, attr: &ast::Attribute) -> Option { for list_item in attr.meta_item_list().unwrap_or_default() { match list_item.word() { Some(word) if value.is_none() => - value = Some(word.name().clone()), + value = Some(word.ident.name), _ => // FIXME better-encapsulate meta_item (don't directly access `node`) span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node), diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index 74e41ef9c93..454b6cbd27d 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -152,7 +152,7 @@ pub(crate) fn run

(self, } else { sess.span_err( item.span, - &format!("{} attribute requires a path", item.name())); + &format!("{} attribute requires a path", item.ident)); return None; } } diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index b95b7deab48..0542ca6fb24 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -717,7 +717,7 @@ fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImpor match attr.meta_item_list() { Some(names) => for attr in names { if let Some(word) = attr.word() { - imports.imports.push((word.name(), attr.span())); + imports.imports.push((word.ident.name, attr.span())); } else { span_err!(self.session, attr.span(), E0466, "bad macro import"); } @@ -731,7 +731,7 @@ fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImpor if let Some(names) = attr.meta_item_list() { for attr in names { if let Some(word) = attr.word() { - imports.reexports.push((word.name(), attr.span())); + imports.reexports.push((word.ident.name, attr.span())); } else { bad_macro_reexport(self, attr.span()); } diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index fe4f785aa91..a87e1df5efc 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -171,7 +171,7 @@ fn get_auto_trait_impl_for( let mut segments = path.segments.into_vec(); let last = segments.pop().unwrap(); - let real_name = name.as_ref().map(|n| Symbol::from(n.as_str())); + let real_name = name.map(|name| Symbol::intern(&name)); segments.push(hir::PathSegment::new( real_name.unwrap_or(last.name), diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs index c228f54217d..7f89b3e6b3a 100644 --- a/src/librustdoc/clean/cfg.rs +++ b/src/librustdoc/clean/cfg.rs @@ -67,7 +67,7 @@ fn parse_nested(nested_cfg: &NestedMetaItem) -> Result { /// If the content is not properly formatted, it will return an error indicating what and where /// the error is. pub fn parse(cfg: &MetaItem) -> Result { - let name = cfg.name(); + let name = cfg.ident.name; match cfg.node { MetaItemKind::Word => Ok(Cfg::Cfg(name, None)), MetaItemKind::NameValue(ref lit) => match lit.node { @@ -562,14 +562,14 @@ fn test_cfg_or() { fn test_parse_ok() { with_globals(|| { let mi = MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::Word, span: DUMMY_SP, }; assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all"))); let mi = MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::NameValue(dummy_spanned(LitKind::Str( Symbol::intern("done"), StrStyle::Cooked, @@ -579,15 +579,15 @@ fn test_parse_ok() { assert_eq!(Cfg::parse(&mi), Ok(name_value_cfg("all", "done"))); let mi = MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -597,15 +597,15 @@ fn test_parse_ok() { assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") & word_cfg("b"))); let mi = MetaItem { - name: Symbol::intern("any"), + ident: Ident::from_str("any"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -615,10 +615,10 @@ fn test_parse_ok() { assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") | word_cfg("b"))); let mi = MetaItem { - name: Symbol::intern("not"), + ident: Ident::from_str("not"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -628,26 +628,26 @@ fn test_parse_ok() { assert_eq!(Cfg::parse(&mi), Ok(!word_cfg("a"))); let mi = MetaItem { - name: Symbol::intern("not"), + ident: Ident::from_str("not"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("any"), + ident: Ident::from_str("any"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("c"), + ident: Ident::from_str("c"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -663,20 +663,20 @@ fn test_parse_ok() { assert_eq!(Cfg::parse(&mi), Ok(!(word_cfg("a") | (word_cfg("b") & word_cfg("c"))))); let mi = MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("c"), + ident: Ident::from_str("c"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -691,22 +691,22 @@ fn test_parse_ok() { fn test_parse_err() { with_globals(|| { let mi = MetaItem { - name: Symbol::intern("foo"), + ident: Ident::from_str("foo"), node: MetaItemKind::NameValue(dummy_spanned(LitKind::Bool(false))), span: DUMMY_SP, }; assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("not"), + ident: Ident::from_str("not"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -716,17 +716,17 @@ fn test_parse_err() { assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("not"), + ident: Ident::from_str("not"), node: MetaItemKind::List(vec![]), span: DUMMY_SP, }; assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("foo"), + ident: Ident::from_str("foo"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -736,15 +736,15 @@ fn test_parse_err() { assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("all"), + ident: Ident::from_str("all"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("foo"), + ident: Ident::from_str("foo"), node: MetaItemKind::List(vec![]), span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("b"), + ident: Ident::from_str("b"), node: MetaItemKind::Word, span: DUMMY_SP, })), @@ -754,15 +754,15 @@ fn test_parse_err() { assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("any"), + ident: Ident::from_str("any"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("a"), + ident: Ident::from_str("a"), node: MetaItemKind::Word, span: DUMMY_SP, })), dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("foo"), + ident: Ident::from_str("foo"), node: MetaItemKind::List(vec![]), span: DUMMY_SP, })), @@ -772,10 +772,10 @@ fn test_parse_err() { assert!(Cfg::parse(&mi).is_err()); let mi = MetaItem { - name: Symbol::intern("not"), + ident: Ident::from_str("not"), node: MetaItemKind::List(vec![ dummy_spanned(NestedMetaItemKind::MetaItem(MetaItem { - name: Symbol::intern("foo"), + ident: Ident::from_str("foo"), node: MetaItemKind::List(vec![]), span: DUMMY_SP, })), diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b8f7d3db9fd..b57c9589afa 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -21,9 +21,9 @@ use syntax; use syntax::abi::Abi; -use syntax::ast::{self, AttrStyle}; +use syntax::ast::{self, AttrStyle, Ident}; use syntax::attr; -use syntax::codemap::Spanned; +use syntax::codemap::{dummy_spanned, Spanned}; use syntax::feature_gate::UnstableFeatures; use syntax::ptr::P; use syntax::symbol::keywords; @@ -840,7 +840,8 @@ pub fn from_ast(diagnostic: &::errors::Handler, for attr in attrs.lists("target_feature") { if attr.check_name("enable") { if let Some(feat) = attr.value_str() { - let meta = attr::mk_name_value_item_str("target_feature".into(), feat); + let meta = attr::mk_name_value_item_str(Ident::from_str("target_feature"), + dummy_spanned(feat)); if let Ok(feat_cfg) = Cfg::parse(&meta) { cfg &= feat_cfg; } @@ -1146,7 +1147,7 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option fn macro_resolve(cx: &DocContext, path_str: &str) -> Option { use syntax::ext::base::{MacroKind, SyntaxExtension}; use syntax::ext::hygiene::Mark; - let segment = ast::PathSegment::from_ident(ast::Ident::from_str(path_str)); + let segment = ast::PathSegment::from_ident(Ident::from_str(path_str)); let path = ast::Path { segments: vec![segment], span: DUMMY_SP }; let mut resolver = cx.resolver.borrow_mut(); let mark = Mark::root(); @@ -1158,7 +1159,7 @@ fn macro_resolve(cx: &DocContext, path_str: &str) -> Option { } else { None } - } else if let Some(def) = resolver.all_macros.get(&path_str.into()) { + } else if let Some(def) = resolver.all_macros.get(&Symbol::intern(path_str)) { Some(*def) } else { None diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 8e6dcf8caf4..1c9ea261841 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -2966,7 +2966,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, } fn render_attribute(attr: &ast::MetaItem) -> Option { - let name = attr.name(); + let name = attr.ident.name; if attr.is_word() { Some(format!("{}", name)) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index e51702c2eb8..e7900af7f12 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -477,7 +477,7 @@ pub enum NestedMetaItemKind { /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]` #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct MetaItem { - pub name: Name, + pub ident: Ident, pub node: MetaItemKind, pub span: Span, } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 04232bf15bc..2812e1238e9 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -19,7 +19,7 @@ use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind}; use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind}; use codemap::{Spanned, respan, dummy_spanned}; -use syntax_pos::{Span, DUMMY_SP}; +use syntax_pos::Span; use errors::Handler; use feature_gate::{Features, GatedCfg}; use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; @@ -137,7 +137,7 @@ pub fn check_name(&self, name: &str) -> bool { /// Returns the name of the meta item, e.g. `foo` in `#[foo]`, /// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem pub fn name(&self) -> Option { - self.meta_item().and_then(|meta_item| Some(meta_item.name())) + self.meta_item().and_then(|meta_item| Some(meta_item.ident.name)) } /// Gets the string value if self is a MetaItem and the MetaItem is a @@ -154,7 +154,7 @@ pub fn name_value_literal(&self) -> Option<(Name, &Lit)> { if meta_item_list.len() == 1 { let nested_item = &meta_item_list[0]; if nested_item.is_literal() { - Some((meta_item.name(), nested_item.literal().unwrap())) + Some((meta_item.ident.name, nested_item.literal().unwrap())) } else { None } @@ -250,10 +250,6 @@ pub fn is_value_str(&self) -> bool { } impl MetaItem { - pub fn name(&self) -> Name { - self.name - } - pub fn value_str(&self) -> Option { match self.node { MetaItemKind::NameValue(ref v) => { @@ -283,7 +279,7 @@ pub fn is_word(&self) -> bool { pub fn span(&self) -> Span { self.span } pub fn check_name(&self, name: &str) -> bool { - self.name() == name + self.ident.name == name } pub fn is_value_str(&self) -> bool { @@ -300,8 +296,8 @@ impl Attribute { pub fn meta(&self) -> Option { let mut tokens = self.tokens.trees().peekable(); Some(MetaItem { - name: match self.path.segments.len() { - 1 => self.path.segments[0].ident.name, + ident: match self.path.segments.len() { + 1 => self.path.segments[0].ident, _ => return None, }, node: if let Some(node) = MetaItemKind::from_tokens(&mut tokens) { @@ -353,7 +349,7 @@ pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> { } Ok(MetaItem { - name: self.path.segments.last().unwrap().ident.name, + ident: self.path.segments.last().unwrap().ident, node: self.parse(sess, |parser| parser.parse_meta_item_kind())?, span: self.span, }) @@ -368,8 +364,8 @@ pub fn with_desugared_doc(&self, f: F) -> T where if self.is_sugared_doc { let comment = self.value_str().unwrap(); let meta = mk_name_value_item_str( - Symbol::intern("doc"), - Symbol::intern(&strip_doc_comment_decoration(&comment.as_str()))); + Ident::from_str("doc"), + dummy_spanned(Symbol::intern(&strip_doc_comment_decoration(&comment.as_str())))); let mut attr = if self.style == ast::AttrStyle::Outer { mk_attr_outer(self.span, self.id, meta) } else { @@ -385,37 +381,24 @@ pub fn with_desugared_doc(&self, f: F) -> T where /* Constructors */ -pub fn mk_name_value_item_str(name: Name, value: Symbol) -> MetaItem { - let value_lit = dummy_spanned(LitKind::Str(value, ast::StrStyle::Cooked)); - mk_spanned_name_value_item(DUMMY_SP, name, value_lit) +pub fn mk_name_value_item_str(ident: Ident, value: Spanned) -> MetaItem { + let value = respan(value.span, LitKind::Str(value.node, ast::StrStyle::Cooked)); + mk_name_value_item(ident.span.to(value.span), ident, value) } -pub fn mk_name_value_item(name: Name, value: ast::Lit) -> MetaItem { - mk_spanned_name_value_item(DUMMY_SP, name, value) +pub fn mk_name_value_item(span: Span, ident: Ident, value: ast::Lit) -> MetaItem { + MetaItem { ident, span, node: MetaItemKind::NameValue(value) } } -pub fn mk_list_item(name: Name, items: Vec) -> MetaItem { - mk_spanned_list_item(DUMMY_SP, name, items) +pub fn mk_list_item(span: Span, ident: Ident, items: Vec) -> MetaItem { + MetaItem { ident, span, node: MetaItemKind::List(items) } } -pub fn mk_list_word_item(name: Name) -> ast::NestedMetaItem { - dummy_spanned(NestedMetaItemKind::MetaItem(mk_spanned_word_item(DUMMY_SP, name))) +pub fn mk_word_item(ident: Ident) -> MetaItem { + MetaItem { ident, span: ident.span, node: MetaItemKind::Word } } - -pub fn mk_word_item(name: Name) -> MetaItem { - mk_spanned_word_item(DUMMY_SP, name) -} - -pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> MetaItem { - MetaItem { span: sp, name: name, node: MetaItemKind::NameValue(value) } -} - -pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec) -> MetaItem { - MetaItem { span: sp, name: name, node: MetaItemKind::List(items) } -} - -pub fn mk_spanned_word_item(sp: Span, name: Name) -> MetaItem { - MetaItem { span: sp, name: name, node: MetaItemKind::Word } +pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem { + respan(ident.span, NestedMetaItemKind::MetaItem(mk_word_item(ident))) } pub fn mk_attr_id() -> AttrId { @@ -436,11 +419,10 @@ pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute { /// Returns an inner attribute with the given value and span. pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute { - let ident = ast::Ident::with_empty_ctxt(item.name).with_span_pos(item.span); Attribute { id, style: ast::AttrStyle::Inner, - path: ast::Path::from_ident(ident), + path: ast::Path::from_ident(item.ident), tokens: item.node.tokens(item.span), is_sugared_doc: false, span: sp, @@ -455,11 +437,10 @@ pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute { /// Returns an outer attribute with the given value and span. pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute { - let ident = ast::Ident::with_empty_ctxt(item.name).with_span_pos(item.span); Attribute { id, style: ast::AttrStyle::Outer, - path: ast::Path::from_ident(ident), + path: ast::Path::from_ident(item.ident), tokens: item.node.tokens(item.span), is_sugared_doc: false, span: sp, @@ -472,7 +453,7 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute { Attribute { id, style, - path: ast::Path::from_ident(ast::Ident::from_str("doc").with_span_pos(span)), + path: ast::Path::from_ident(Ident::from_str("doc").with_span_pos(span)), tokens: MetaItemKind::NameValue(lit).tokens(span), is_sugared_doc: true, span, @@ -508,7 +489,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool { item.check_name("feature") && item.meta_item_list().map(|list| { list.iter().any(|mi| { - mi.word().map(|w| w.name() == feature_name) + mi.word().map(|w| w.ident.name == feature_name) .unwrap_or(false) }) }).unwrap_or(false) @@ -581,7 +562,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) { gated_cfg.check_and_emit(sess, feats); } - sess.config.contains(&(cfg.name(), cfg.value_str())) + sess.config.contains(&(cfg.ident.name, cfg.value_str())) }) } @@ -602,7 +583,7 @@ pub fn eval_condition(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F) // The unwraps below may look dangerous, but we've already asserted // that they won't fail with the loop above. - match &*cfg.name.as_str() { + match &*cfg.ident.name.as_str() { "any" => mis.iter().any(|mi| { eval_condition(mi.meta_item().unwrap(), sess, eval) }), @@ -695,7 +676,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler, let meta = meta.as_ref().unwrap(); let get = |meta: &MetaItem, item: &mut Option| { if item.is_some() { - handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name())); + handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.ident.name)); return false } if let Some(v) = meta.value_str() { @@ -714,14 +695,14 @@ macro_rules! get_meta { )+ for meta in metas { if let Some(mi) = meta.meta_item() { - match &*mi.name().as_str() { + match &*mi.ident.name.as_str() { $( stringify!($name) => if !get(mi, &mut $name) { continue 'outer }, )+ _ => { handle_errors(diagnostic, mi.span, - AttrError::UnknownMetaItem(mi.name())); + AttrError::UnknownMetaItem(mi.ident.name)); continue 'outer } } @@ -733,7 +714,7 @@ macro_rules! get_meta { } } - match &*meta.name.as_str() { + match &*meta.ident.name.as_str() { "rustc_deprecated" => { if rustc_depr.is_some() { span_err!(diagnostic, item_sp, E0540, @@ -788,13 +769,13 @@ macro_rules! get_meta { let mut issue = None; for meta in metas { if let Some(mi) = meta.meta_item() { - match &*mi.name().as_str() { + match &*mi.ident.name.as_str() { "feature" => if !get(mi, &mut feature) { continue 'outer }, "reason" => if !get(mi, &mut reason) { continue 'outer }, "issue" => if !get(mi, &mut issue) { continue 'outer }, _ => { handle_errors(diagnostic, meta.span, - AttrError::UnknownMetaItem(mi.name())); + AttrError::UnknownMetaItem(mi.ident.name)); continue 'outer } } @@ -844,12 +825,12 @@ macro_rules! get_meta { let mut since = None; for meta in metas { if let NestedMetaItemKind::MetaItem(ref mi) = meta.node { - match &*mi.name().as_str() { + match &*mi.ident.name.as_str() { "feature" => if !get(mi, &mut feature) { continue 'outer }, "since" => if !get(mi, &mut since) { continue 'outer }, _ => { handle_errors(diagnostic, meta.span, - AttrError::UnknownMetaItem(mi.name())); + AttrError::UnknownMetaItem(mi.ident.name)); continue 'outer } } @@ -936,7 +917,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler, depr = if let Some(metas) = attr.meta_item_list() { let get = |meta: &MetaItem, item: &mut Option| { if item.is_some() { - handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name())); + handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.ident.name)); return false } if let Some(v) = meta.value_str() { @@ -952,12 +933,12 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler, let mut note = None; for meta in metas { if let NestedMetaItemKind::MetaItem(ref mi) = meta.node { - match &*mi.name().as_str() { + match &*mi.ident.name.as_str() { "since" => if !get(mi, &mut since) { continue 'outer }, "note" => if !get(mi, &mut note) { continue 'outer }, _ => { handle_errors(diagnostic, meta.span, - AttrError::UnknownMetaItem(mi.name())); + AttrError::UnknownMetaItem(mi.ident.name)); continue 'outer } } @@ -1009,7 +990,7 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec let mut recognised = false; if let Some(mi) = item.word() { - let word = &*mi.name().as_str(); + let word = &*mi.ident.name.as_str(); let hint = match word { "C" => Some(ReprC), "packed" => Some(ReprPacked), @@ -1108,18 +1089,17 @@ pub fn is_signed(self) -> bool { impl MetaItem { fn tokens(&self) -> TokenStream { - let ident = TokenTree::Token(self.span, - Token::from_ast_ident(Ident::with_empty_ctxt(self.name))); + let ident = TokenTree::Token(self.span, Token::from_ast_ident(self.ident)); TokenStream::concat(vec![ident.into(), self.node.tokens(self.span)]) } fn from_tokens(tokens: &mut iter::Peekable) -> Option where I: Iterator, { - let (span, name) = match tokens.next() { - Some(TokenTree::Token(span, Token::Ident(ident, _))) => (span, ident.name), + let (span, ident) = match tokens.next() { + Some(TokenTree::Token(span, Token::Ident(ident, _))) => (span, ident), Some(TokenTree::Token(_, Token::Interpolated(ref nt))) => match nt.0 { - token::Nonterminal::NtIdent(ident, _) => (ident.span, ident.name), + token::Nonterminal::NtIdent(ident, _) => (ident.span, ident), token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()), _ => return None, }, @@ -1132,7 +1112,7 @@ fn from_tokens(tokens: &mut iter::Peekable) -> Option MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(span.hi()), _ => span.hi(), }; - Some(MetaItem { name, node, span: span.with_hi(hi) }) + Some(MetaItem { ident, node, span: span.with_hi(hi) }) } } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 40d9d242ae6..062f3ce1127 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1129,21 +1129,22 @@ fn attribute(&self, sp: Span, mi: ast::MetaItem) -> ast::Attribute { } fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem { - attr::mk_spanned_word_item(sp, w) + attr::mk_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp)) } fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem { - respan(sp, ast::NestedMetaItemKind::MetaItem(attr::mk_spanned_word_item(sp, w))) + attr::mk_nested_word_item(Ident::with_empty_ctxt(w).with_span_pos(sp)) } fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec) -> ast::MetaItem { - attr::mk_spanned_list_item(sp, name, mis) + attr::mk_list_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), mis) } fn meta_name_value(&self, sp: Span, name: ast::Name, value: ast::LitKind) -> ast::MetaItem { - attr::mk_spanned_name_value_item(sp, name, respan(sp, value)) + attr::mk_name_value_item(sp, Ident::with_empty_ctxt(name).with_span_pos(sp), + respan(sp, value)) } fn item_use(&self, sp: Span, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index bbfde736ef0..678c20402d6 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -733,7 +733,7 @@ fn expand_derive_invoc(&mut self, invoc.expansion_data.mark.set_expn_info(expn_info); let span = span.with_ctxt(self.cx.backtrace()); let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this - name: keywords::Invalid.name(), + ident: keywords::Invalid.ident(), span: DUMMY_SP, node: ast::MetaItemKind::Word, }; @@ -1279,15 +1279,16 @@ fn fold_attribute(&mut self, at: ast::Attribute) -> Option { let include_info = vec![ dummy_spanned(ast::NestedMetaItemKind::MetaItem( - attr::mk_name_value_item_str("file".into(), - file))), + attr::mk_name_value_item_str(Ident::from_str("file"), + dummy_spanned(file)))), dummy_spanned(ast::NestedMetaItemKind::MetaItem( - attr::mk_name_value_item_str("contents".into(), - (&*src).into()))), + attr::mk_name_value_item_str(Ident::from_str("contents"), + dummy_spanned(Symbol::intern(&src))))), ]; - items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem( - attr::mk_list_item("include".into(), include_info)))); + let include_ident = Ident::from_str("include"); + let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info); + items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(item))); } Err(_) => { self.cx.span_err(at.span, @@ -1300,7 +1301,7 @@ fn fold_attribute(&mut self, at: ast::Attribute) -> Option { } } - let meta = attr::mk_list_item("doc".into(), items); + let meta = attr::mk_list_item(DUMMY_SP, Ident::from_str("doc"), items); match at.style { ast::AttrStyle::Inner => Some(attr::mk_spanned_attr_inner(at.span, at.id, meta)), diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index b55a268800f..24bfb5d9088 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1054,7 +1054,7 @@ pub struct GatedCfg { impl GatedCfg { pub fn gate(cfg: &ast::MetaItem) -> Option { - let name = cfg.name().as_str(); + let name = cfg.ident.name.as_str(); GATED_CFGS.iter() .position(|info| info.0 == name) .map(|idx| { @@ -1811,7 +1811,7 @@ fn feature_removed(span_handler: &Handler, span: Span) { for mi in list { let name = if let Some(word) = mi.word() { - word.name() + word.ident.name } else { span_err!(span_handler, mi.span, E0556, "malformed feature, expected just one word"); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index e476a0c653e..ba6703b9c74 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -543,7 +543,7 @@ pub fn noop_fold_meta_list_item(li: NestedMetaItem, fld: &mut T) pub fn noop_fold_meta_item(mi: MetaItem, fld: &mut T) -> MetaItem { MetaItem { - name: mi.name, + ident: mi.ident, node: match mi.node { MetaItemKind::Word => MetaItemKind::Word, MetaItemKind::List(mis) => { diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 6f0e382c047..90f08ab1468 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -149,8 +149,7 @@ pub fn parse_path_and_tokens(&mut self) -> PResult<'a, (ast::Path, TokenStream)> }; Ok(if let Some(meta) = meta { self.bump(); - (ast::Path::from_ident(ast::Ident::with_empty_ctxt(meta.name).with_span_pos(meta.span)), - meta.node.tokens(meta.span)) + (ast::Path::from_ident(meta.ident), meta.node.tokens(meta.span)) } else { (self.parse_path(PathStyle::Mod)?, self.parse_tokens()) }) @@ -228,7 +227,7 @@ pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> { let lo = self.span; let ident = self.parse_ident()?; let node = self.parse_meta_item_kind()?; - Ok(ast::MetaItem { name: ident.name, node: node, span: lo.to(self.prev_span) }) + Ok(ast::MetaItem { ident, node: node, span: lo.to(self.prev_span) }) } pub fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 84c770605d5..e6da5bcaa3a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1957,7 +1957,7 @@ pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast: let meta_ident = match self.token { token::Interpolated(ref nt) => match nt.0 { token::NtMeta(ref meta) => match meta.node { - ast::MetaItemKind::Word => Some(ast::Ident::with_empty_ctxt(meta.name)), + ast::MetaItemKind::Word => Some(meta.ident), _ => None, }, _ => None, @@ -1966,7 +1966,7 @@ pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast: }; if let Some(ident) = meta_ident { self.bump(); - return Ok(ast::Path::from_ident(ident.with_span_pos(self.prev_span))); + return Ok(ast::Path::from_ident(ident)); } self.parse_path(style) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b233b145fdd..8d42206c5cc 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -26,7 +26,7 @@ use print::pp::Breaks::{Consistent, Inconsistent}; use ptr::P; use std_inject; -use symbol::{Symbol, keywords}; +use symbol::keywords; use syntax_pos::{DUMMY_SP, FileName}; use tokenstream::{self, TokenStream, TokenTree}; @@ -101,13 +101,13 @@ pub fn print_crate<'a>(cm: &'a CodeMap, // of the feature gate, so we fake them up here. // #![feature(prelude_import)] - let prelude_import_meta = attr::mk_list_word_item(Symbol::intern("prelude_import")); - let list = attr::mk_list_item(Symbol::intern("feature"), vec![prelude_import_meta]); + let pi_nested = attr::mk_nested_word_item(ast::Ident::from_str("prelude_import")); + let list = attr::mk_list_item(DUMMY_SP, ast::Ident::from_str("feature"), vec![pi_nested]); let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), list); s.print_attribute(&fake_attr)?; // #![no_std] - let no_std_meta = attr::mk_word_item(Symbol::intern("no_std")); + let no_std_meta = attr::mk_word_item(ast::Ident::from_str("no_std")); let fake_attr = attr::mk_attr_inner(DUMMY_SP, attr::mk_attr_id(), no_std_meta); s.print_attribute(&fake_attr)?; } @@ -768,15 +768,15 @@ fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> { self.ibox(INDENT_UNIT)?; match item.node { ast::MetaItemKind::Word => { - self.writer().word(&item.name.as_str())?; + self.writer().word(&item.ident.name.as_str())?; } ast::MetaItemKind::NameValue(ref value) => { - self.word_space(&item.name.as_str())?; + self.word_space(&item.ident.name.as_str())?; self.word_space("=")?; self.print_literal(value)?; } ast::MetaItemKind::List(ref items) => { - self.writer().word(&item.name.as_str())?; + self.writer().word(&item.ident.name.as_str())?; self.popen()?; self.commasep(Consistent, &items[..], diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 8ef24713188..63d7b3336a8 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -57,7 +57,7 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str> krate.module.items.insert(0, P(ast::Item { attrs: vec![attr::mk_attr_outer(DUMMY_SP, attr::mk_attr_id(), - attr::mk_word_item(Symbol::intern("macro_use")))], + attr::mk_word_item(ast::Ident::from_str("macro_use")))], vis: dummy_spanned(ast::VisibilityKind::Inherited), node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)), ident: ast::Ident::from_str(name), diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 308b53c1a38..fd2e760e9be 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -195,10 +195,10 @@ fn fold_item(&mut self, i: P) -> SmallVector> { EntryPointType::MainAttr | EntryPointType::Start => folded.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| { - let allow_str = Symbol::intern("allow"); - let dead_code_str = Symbol::intern("dead_code"); - let word_vec = vec![attr::mk_list_word_item(dead_code_str)]; - let allow_dead_code_item = attr::mk_list_item(allow_str, word_vec); + let allow_ident = Ident::from_str("allow"); + let dc_nested = attr::mk_nested_word_item(Ident::from_str("dead_code")); + let allow_dead_code_item = attr::mk_list_item(DUMMY_SP, allow_ident, + vec![dc_nested]); let allow_dead_code = attr::mk_attr_outer(DUMMY_SP, attr::mk_attr_id(), allow_dead_code_item); diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 279f1dec142..331b0fe5481 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -47,7 +47,7 @@ pub fn with_span_pos(self, span: Span) -> Ident { } pub fn without_first_quote(self) -> Ident { - Ident::new(Symbol::from(self.name.as_str().trim_left_matches('\'')), self.span) + Ident::new(Symbol::intern(self.name.as_str().trim_left_matches('\'')), self.span) } pub fn modern(self) -> Ident { @@ -147,12 +147,6 @@ pub fn as_u32(self) -> u32 { } } -impl<'a> From<&'a str> for Symbol { - fn from(string: &'a str) -> Symbol { - Symbol::intern(string) - } -} - impl fmt::Debug for Symbol { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let is_gensymed = with_interner(|interner| interner.is_gensymed(*self)); diff --git a/src/test/compile-fail-fulldeps/auxiliary/macro_crate_test.rs b/src/test/compile-fail-fulldeps/auxiliary/macro_crate_test.rs index bc51b4061ed..040f0b661be 100644 --- a/src/test/compile-fail-fulldeps/auxiliary/macro_crate_test.rs +++ b/src/test/compile-fail-fulldeps/auxiliary/macro_crate_test.rs @@ -117,7 +117,7 @@ fn expand_duplicate(cx: &mut ExtCtxt, let copy_name = match mi.node { ast::MetaItemKind::List(ref xs) => { if let Some(word) = xs[0].word() { - ast::Ident::with_empty_ctxt(word.name()) + word.ident } else { cx.span_err(mi.span, "Expected word"); return; diff --git a/src/test/run-pass-fulldeps/auxiliary/macro_crate_test.rs b/src/test/run-pass-fulldeps/auxiliary/macro_crate_test.rs index 6612fe45b81..2f80408ac1c 100644 --- a/src/test/run-pass-fulldeps/auxiliary/macro_crate_test.rs +++ b/src/test/run-pass-fulldeps/auxiliary/macro_crate_test.rs @@ -112,7 +112,7 @@ fn expand_duplicate(cx: &mut ExtCtxt, let copy_name = match mi.node { ast::MetaItemKind::List(ref xs) => { if let Some(word) = xs[0].word() { - ast::Ident::with_empty_ctxt(word.name()) + word.ident } else { cx.span_err(mi.span, "Expected word"); return;