From a5e5ea1646367b82864af3a2a508993d76b792af Mon Sep 17 00:00:00 2001 From: cgswords Date: Fri, 15 Jul 2016 13:13:17 -0700 Subject: General MetaItem encapsulation rewrites. --- src/libsyntax/attr.rs | 94 ++++++++++++++++++++++++++++++++++++------- src/libsyntax/ext/build.rs | 23 +++-------- src/libsyntax/ext/expand.rs | 5 +-- src/libsyntax/feature_gate.rs | 15 ++++--- 4 files changed, 95 insertions(+), 42 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 67f73d4dd4f..8bd3f5195cc 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -17,8 +17,8 @@ pub use self::IntType::*; use ast; use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaItemKind}; use ast::{Expr, Item, Local, Stmt, StmtKind}; -use codemap::{spanned, dummy_spanned, Spanned}; -use syntax_pos::{Span, BytePos}; +use codemap::{respan, spanned, dummy_spanned, Spanned}; +use syntax_pos::{Span, BytePos, DUMMY_SP}; use errors::Handler; use feature_gate::{Features, GatedCfg}; use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration}; @@ -92,6 +92,13 @@ pub trait AttrMetaMethods { /// Gets a list of inner meta items from a list MetaItem type. fn meta_item_list(&self) -> Option<&[P]>; + /// Indicates if the attribute is a Word. + fn is_word(&self) -> bool; + /// Indicates if the attribute is a Value String. + fn is_value_str(&self) -> bool; + /// Indicates if the attribute is a Meta-Item List. + fn is_meta_item_list(&self) -> bool; + fn span(&self) -> Span; } @@ -108,8 +115,14 @@ impl AttrMetaMethods for Attribute { self.meta().value_str() } fn meta_item_list(&self) -> Option<&[P]> { - self.node.value.meta_item_list() + self.meta().meta_item_list() } + + fn is_word(&self) -> bool { self.meta().is_word() } + fn is_value_str(&self) -> bool { self.meta().is_value_str() } + + fn is_meta_item_list(&self) -> bool { self.meta().is_meta_item_list() } + fn span(&self) -> Span { self.meta().span } } @@ -140,6 +153,18 @@ impl AttrMetaMethods for MetaItem { _ => None } } + + fn is_word(&self) -> bool { + match self.node { + MetaItemKind::Word(_) => true, + _ => false, + } + } + + fn is_value_str(&self) -> bool { self.value_str().is_some() } + + fn is_meta_item_list(&self) -> bool { self.meta_item_list().is_some() } + fn span(&self) -> Span { self.span } } @@ -150,6 +175,9 @@ impl AttrMetaMethods for P { fn meta_item_list(&self) -> Option<&[P]> { (**self).meta_item_list() } + fn is_word(&self) -> bool { (**self).is_word() } + fn is_value_str(&self) -> bool { (**self).is_value_str() } + fn is_meta_item_list(&self) -> bool { (**self).is_meta_item_list() } fn span(&self) -> Span { (**self).span() } } @@ -194,22 +222,38 @@ impl AttributeMethods for Attribute { pub fn mk_name_value_item_str(name: InternedString, value: InternedString) -> P { let value_lit = dummy_spanned(ast::LitKind::Str(value, ast::StrStyle::Cooked)); - mk_name_value_item(name, value_lit) + mk_spanned_name_value_item(DUMMY_SP, name, value_lit) } pub fn mk_name_value_item(name: InternedString, value: ast::Lit) -> P { - P(dummy_spanned(MetaItemKind::NameValue(name, value))) + mk_spanned_name_value_item(DUMMY_SP, name, value) } pub fn mk_list_item(name: InternedString, items: Vec>) -> P { - P(dummy_spanned(MetaItemKind::List(name, items))) + mk_spanned_list_item(DUMMY_SP, name, items) } pub fn mk_word_item(name: InternedString) -> P { - P(dummy_spanned(MetaItemKind::Word(name))) + mk_spanned_word_item(DUMMY_SP, name) +} + +pub fn mk_spanned_name_value_item(sp: Span, name: InternedString, value: ast::Lit) + -> P { + P(respan(sp,MetaItemKind::NameValue(name, value))) +} + +pub fn mk_spanned_list_item(sp: Span, name: InternedString, items: Vec>) + -> P { + P(respan(sp, MetaItemKind::List(name, items))) } +pub fn mk_spanned_word_item(sp: Span, name: InternedString) -> P { + P(respan(sp,MetaItemKind::Word(name))) +} + + + thread_local! { static NEXT_ATTR_ID: Cell = Cell::new(0) } pub fn mk_attr_id() -> AttrId { @@ -223,21 +267,43 @@ pub fn mk_attr_id() -> AttrId { /// Returns an inner attribute with the given value. pub fn mk_attr_inner(id: AttrId, item: P) -> Attribute { - dummy_spanned(Attribute_ { - id: id, - style: ast::AttrStyle::Inner, - value: item, - is_sugared_doc: false, - }) + mk_spanned_attr_inner(DUMMY_SP, id, item) } +/// Returns an innter attribute with the given value and span. +pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: P) -> Attribute { + respan(sp, + Attribute_ { + id: id, + style: ast::AttrStyle::Inner, + value: item, + is_sugared_doc: false, + }) +} + + /// Returns an outer attribute with the given value. pub fn mk_attr_outer(id: AttrId, item: P) -> Attribute { + mk_spanned_attr_outer(DUMMY_SP, id, item) +} + +/// Returns an outer attribute with the given value and span. +pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: P) -> Attribute { + respan(sp, + Attribute_ { + id: id, + style: ast::AttrStyle::Outer, + value: item, + is_sugared_doc: false, + }) +} + +pub fn mk_doc_attr_outer(id: AttrId, item: P, is_sugared_doc: bool) -> Attribute { dummy_spanned(Attribute_ { id: id, style: ast::AttrStyle::Outer, value: item, - is_sugared_doc: false, + is_sugared_doc: is_sugared_doc, }) } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 435241f426e..5d6429f7bdf 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1135,30 +1135,19 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn attribute(&self, sp: Span, mi: P) -> ast::Attribute { - respan(sp, ast::Attribute_ { - id: attr::mk_attr_id(), - style: ast::AttrStyle::Outer, - value: mi, - is_sugared_doc: false, - }) + attr::mk_spanned_attr_outer(sp, attr::mk_attr_id(), mi) } fn meta_word(&self, sp: Span, w: InternedString) -> P { - P(respan(sp, ast::MetaItemKind::Word(w))) + attr::mk_spanned_word_item(sp, w) } - fn meta_list(&self, - sp: Span, - name: InternedString, - mis: Vec> ) + fn meta_list(&self, sp: Span, name: InternedString, mis: Vec>) -> P { - P(respan(sp, ast::MetaItemKind::List(name, mis))) + attr::mk_spanned_list_item(sp, name, mis) } - fn meta_name_value(&self, - sp: Span, - name: InternedString, - value: ast::LitKind) + fn meta_name_value(&self, sp: Span, name: InternedString, value: ast::LitKind) -> P { - P(respan(sp, ast::MetaItemKind::NameValue(name, respan(sp, value)))) + attr::mk_spanned_name_value_item(sp, name, respan(sp, value)) } fn item_use(&self, sp: Span, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 18342f2e38c..5293d2ab000 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -302,9 +302,8 @@ fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool }; if is_use { - match attr.node.value.node { - ast::MetaItemKind::Word(..) => (), - _ => fld.cx.span_err(attr.span, "arguments to macro_use are not allowed here"), + if !attr.is_word() { + fld.cx.span_err(attr.span, "arguments to macro_use are not allowed here"); } return true; } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 27485ee65fc..5a718dd48e3 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1108,14 +1108,13 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute]) -> F } Some(list) => { for mi in list { - let name = match mi.node { - ast::MetaItemKind::Word(ref word) => (*word).clone(), - _ => { - span_err!(span_handler, mi.span, E0556, - "malformed feature, expected just one word"); - continue - } - }; + let name = if mi.is_word() { + mi.name() + } else { + span_err!(span_handler, mi.span, E0556, + "malformed feature, expected just one word"); + continue + }; if let Some(&(_, _, _, setter)) = ACTIVE_FEATURES.iter() .find(|& &(n, _, _, _)| name == n) { *(setter(&mut features)) = true; -- cgit 1.4.1-3-g733a5 From 5553901146fa80c652abdc514b38360a0ae7418d Mon Sep 17 00:00:00 2001 From: cgswords Date: Sun, 17 Jul 2016 21:45:06 -0700 Subject: Adressed PR comments. --- src/librustc/lint/context.rs | 18 +++++++++--------- src/librustc_driver/lib.rs | 9 +++------ src/librustc_incremental/assert_dep_graph.rs | 8 ++------ src/librustc_lint/builtin.rs | 8 +------- src/librustdoc/clean/mod.rs | 22 +--------------------- src/libsyntax/attr.rs | 21 ++++++++++----------- src/libsyntax_pos/lib.rs | 4 ++++ 7 files changed, 30 insertions(+), 60 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 0a1e7005f9f..27e1d0520dc 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -367,18 +367,18 @@ pub fn gather_attr(attr: &ast::Attribute) let meta = &attr.node.value; let metas = if let Some(metas) = meta.meta_item_list() { - metas - } else { - out.push(Err(meta.span)); - return out; - }; + metas + } else { + out.push(Err(meta.span)); + return out; + }; for meta in metas { out.push(if meta.is_word() { - Ok((meta.name().clone(), level, meta.span)) - } else { - Err(meta.span) - }); + Ok((meta.name().clone(), level, meta.span)) + } else { + Err(meta.span) + }); } out diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 9df1dea4567..07a2605026a 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -394,11 +394,10 @@ fn check_cfg(sopts: &config::Options, let mut saw_invalid_predicate = false; for item in sopts.cfg.iter() { if item.is_meta_item_list() { - saw_invalid_predicate = true; saw_invalid_predicate = true; handler.emit(&MultiSpan::new(), &format!("invalid predicate in --cfg command line argument: `{}`", - pred), + item.name()), errors::Level::Fatal); } } @@ -651,10 +650,8 @@ impl RustcDefaultCalls { if cfg.is_word() { println!("{}", cfg.name()); } else if cfg.is_value_str() { - let rhs = cfg.value_str(); - match rhs { - Some(s) => println!("{}=\"{}\"", cfg.name(), s), - None => continue, + if let Some(s) = cfg.value_str() { + println!("{}=\"{}\"", cfg.name(), s); } } else if cfg.is_meta_item_list() { // Right now there are not and should not be any diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 0d327414c8f..774c5ca6d6b 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -114,9 +114,7 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> { id = Some(meta_item.name().clone()); } else { // FIXME better-encapsulate meta_item (don't directly access `node`) - self.tcx.sess.span_err( - meta_item.span(), - &format!("unexpected meta-item {:?}", meta_item.node)); + span_bug!(meta_item.span(), "unexpected meta-item {:?}", meta_item.node) } } let id = id.unwrap_or(InternedString::new(ID)); @@ -133,9 +131,7 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> { id = Some(meta_item.name().clone()); } else { // FIXME better-encapsulate meta_item (don't directly access `node`) - self.tcx.sess.span_err( - meta_item.span(), - &format!("unexpected meta-item {:?}", meta_item.node)); + span_bug!(meta_item.span(), "unexpected meta-item {:?}", meta_item.node) } } let dep_node = match dep_node_interned { diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index a498004b390..7547e28625c 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -298,13 +298,7 @@ impl MissingDoc { } } - let has_doc = attrs.iter().any(|a| { - if a.is_value_str() && a.name() == "doc" { - true - } else { - false - } - }); + let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc"); if !has_doc { cx.span_lint(MISSING_DOCS, sp, &format!("missing documentation for {}", desc)); diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 8d69c55ecf1..6883c22d675 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -504,7 +504,7 @@ impl Clean for ast::MetaItem { NameValue(self.name().to_string(), v.to_string()) } else { // must be a list let l = self.meta_item_list().unwrap(); - List(self.name().to_string(), l.clean(cx)) + List(self.name().to_string(), l.clean(cx)) } } } @@ -2589,26 +2589,6 @@ impl ToSource for syntax_pos::Span { } } -// fn lit_to_string(lit: &ast::Lit) -> String { -// match lit.node { -// ast::LitKind::Str(ref st, _) => st.to_string(), -// ast::LitKind::ByteStr(ref data) => format!("{:?}", data), -// ast::LitKind::Byte(b) => { -// let mut res = String::from("b'"); -// for c in (b as char).escape_default() { -// res.push(c); -// } -// res.push('\''); -// res -// }, -// ast::LitKind::Char(c) => format!("'{}'", c), -// ast::LitKind::Int(i, _t) => i.to_string(), -// ast::LitKind::Float(ref f, _t) => f.to_string(), -// ast::LitKind::FloatUnsuffixed(ref f) => f.to_string(), -// ast::LitKind::Bool(b) => b.to_string(), -// } -// } - fn name_from_pat(p: &hir::Pat) -> String { use rustc::hir::*; debug!("Trying to get a name from pattern: {:?}", p); diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 8bd3f5195cc..b622f6861b3 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -94,10 +94,16 @@ pub trait AttrMetaMethods { /// Indicates if the attribute is a Word. fn is_word(&self) -> bool; + /// Indicates if the attribute is a Value String. - fn is_value_str(&self) -> bool; + fn is_value_str(&self) -> bool { + self.value_str().is_some() + } + /// Indicates if the attribute is a Meta-Item List. - fn is_meta_item_list(&self) -> bool; + fn is_meta_item_list(&self) -> bool { + self.meta_item_list().is_some() + } fn span(&self) -> Span; } @@ -119,9 +125,6 @@ impl AttrMetaMethods for Attribute { } fn is_word(&self) -> bool { self.meta().is_word() } - fn is_value_str(&self) -> bool { self.meta().is_value_str() } - - fn is_meta_item_list(&self) -> bool { self.meta().is_meta_item_list() } fn span(&self) -> Span { self.meta().span } } @@ -161,10 +164,6 @@ impl AttrMetaMethods for MetaItem { } } - fn is_value_str(&self) -> bool { self.value_str().is_some() } - - fn is_meta_item_list(&self) -> bool { self.meta_item_list().is_some() } - fn span(&self) -> Span { self.span } } @@ -240,7 +239,7 @@ pub fn mk_word_item(name: InternedString) -> P { pub fn mk_spanned_name_value_item(sp: Span, name: InternedString, value: ast::Lit) -> P { - P(respan(sp,MetaItemKind::NameValue(name, value))) + P(respan(sp, MetaItemKind::NameValue(name, value))) } pub fn mk_spanned_list_item(sp: Span, name: InternedString, items: Vec>) @@ -249,7 +248,7 @@ pub fn mk_spanned_list_item(sp: Span, name: InternedString, items: Vec P { - P(respan(sp,MetaItemKind::Word(name))) + P(respan(sp, MetaItemKind::Word(name))) } diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index c96be8fec2b..b1ec7fd0ab8 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -254,6 +254,10 @@ pub const NO_EXPANSION: ExpnId = ExpnId(!0); // For code appearing from the command line pub const COMMAND_LINE_EXPN: ExpnId = ExpnId(!1); +// For code generated by a procedural macro, without knowing which +// Used in `qquote!` +pub const PROC_EXPN: ExpnId = ExpnId(!2); + impl ExpnId { pub fn from_u32(id: u32) -> ExpnId { ExpnId(id) -- cgit 1.4.1-3-g733a5