From db74efce69711fcee03d3338afcbca67c27ceee8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 17 Mar 2019 14:17:47 +0300 Subject: Make meta-item API compatible with `LocalInternedString::get` soundness fix --- src/libsyntax/attr/builtin.rs | 46 +++++++++++++++++++++---------------------- src/libsyntax/attr/mod.rs | 14 ++++++------- src/libsyntax/feature_gate.rs | 16 +++++++-------- 3 files changed, 38 insertions(+), 38 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs index cdfb83c6e56..74c952b076c 100644 --- a/src/libsyntax/attr/builtin.rs +++ b/src/libsyntax/attr/builtin.rs @@ -222,9 +222,9 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, )+ for meta in metas { if let Some(mi) = meta.meta_item() { - match mi.ident_str() { + match mi.name_or_empty().get() { $( - Some(stringify!($name)) + stringify!($name) => if !get(mi, &mut $name) { continue 'outer }, )+ _ => { @@ -252,7 +252,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, } } - match meta.ident_str().expect("not a stability level") { + match meta.name_or_empty().get() { "rustc_deprecated" => { if rustc_depr.is_some() { span_err!(diagnostic, item_sp, E0540, @@ -306,10 +306,10 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, let mut issue = None; for meta in metas { if let Some(mi) = meta.meta_item() { - match mi.ident_str() { - Some("feature") => if !get(mi, &mut feature) { continue 'outer }, - Some("reason") => if !get(mi, &mut reason) { continue 'outer }, - Some("issue") => if !get(mi, &mut issue) { continue 'outer }, + match mi.name_or_empty().get() { + "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( sess, @@ -377,10 +377,10 @@ fn find_stability_generic<'a, I>(sess: &ParseSess, for meta in metas { match meta { NestedMetaItem::MetaItem(mi) => { - match mi.ident_str() { - Some("feature") => + match mi.name_or_empty().get() { + "feature" => if !get(mi, &mut feature) { continue 'outer }, - Some("since") => + "since" => if !get(mi, &mut since) { continue 'outer }, _ => { handle_errors( @@ -532,14 +532,14 @@ 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.ident_str() { - Some("any") => mis.iter().any(|mi| { + match cfg.name_or_empty().get() { + "any" => mis.iter().any(|mi| { eval_condition(mi.meta_item().unwrap(), sess, eval) }), - Some("all") => mis.iter().all(|mi| { + "all" => mis.iter().all(|mi| { eval_condition(mi.meta_item().unwrap(), sess, eval) }), - Some("not") => { + "not" => { if mis.len() != 1 { span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern"); return false; @@ -635,9 +635,9 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess, for meta in list { match meta { NestedMetaItem::MetaItem(mi) => { - match mi.ident_str() { - Some("since") => if !get(mi, &mut since) { continue 'outer }, - Some("note") => if !get(mi, &mut note) { continue 'outer }, + match mi.name_or_empty().get() { + "since" => if !get(mi, &mut since) { continue 'outer }, + "note" => if !get(mi, &mut note) { continue 'outer }, _ => { handle_errors( sess, @@ -729,12 +729,12 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec { let mut recognised = false; if item.is_word() { - let hint = match item.ident_str() { - Some("C") => Some(ReprC), - Some("packed") => Some(ReprPacked(1)), - Some("simd") => Some(ReprSimd), - Some("transparent") => Some(ReprTransparent), - name => name.and_then(|name| int_type_of_word(name)).map(ReprInt), + let hint = match item.name_or_empty().get() { + "C" => Some(ReprC), + "packed" => Some(ReprPacked(1)), + "simd" => Some(ReprSimd), + "transparent" => Some(ReprTransparent), + name => int_type_of_word(name).map(ReprInt), }; if let Some(h) = hint { diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 1a8faa43fff..c0bd5c79b1d 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -22,7 +22,7 @@ use crate::parse::parser::Parser; use crate::parse::{self, ParseSess, PResult}; use crate::parse::token::{self, Token}; use crate::ptr::P; -use crate::symbol::Symbol; +use crate::symbol::{keywords, LocalInternedString, Symbol}; use crate::ThinVec; use crate::tokenstream::{TokenStream, TokenTree, DelimSpan}; use crate::GLOBALS; @@ -89,8 +89,8 @@ impl NestedMetaItem { pub fn ident(&self) -> Option { self.meta_item().and_then(|meta_item| meta_item.ident()) } - pub fn ident_str(&self) -> Option<&str> { - self.ident().map(|name| name.as_str().get()) + pub fn name_or_empty(&self) -> LocalInternedString { + self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() } /// Gets the string value if self is a MetaItem and the MetaItem is a @@ -167,8 +167,8 @@ impl Attribute { None } } - pub fn ident_str(&self) -> Option<&str> { - self.ident().map(|name| name.as_str().get()) + pub fn name_or_empty(&self) -> LocalInternedString { + self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() } pub fn value_str(&self) -> Option { @@ -205,8 +205,8 @@ impl MetaItem { None } } - pub fn ident_str(&self) -> Option<&str> { - self.ident().map(|name| name.as_str().get()) + pub fn name_or_empty(&self) -> LocalInternedString { + self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() } // #[attribute(name = "value")] diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 9beaabb0cd5..d1d0d79a705 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1341,9 +1341,9 @@ macro_rules! gate_feature { impl<'a> Context<'a> { fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) { debug!("check_attribute(attr = {:?})", attr); - let name = attr.ident_str(); + let name = attr.name_or_empty(); for &(n, ty, _template, ref gateage) in BUILTIN_ATTRIBUTES { - if name == Some(n) { + if name == n { if let Gated(_, name, desc, ref has_feature) = *gateage { if !attr.span.allows_unstable(name) { gate_feature_fn!( @@ -1373,7 +1373,7 @@ impl<'a> Context<'a> { } } if !attr::is_known(attr) { - if name.map_or(false, |name| name.starts_with("rustc_")) { + if name.starts_with("rustc_") { let msg = "unless otherwise specified, attributes with the prefix `rustc_` \ are reserved for internal compiler diagnostics"; gate_feature!(self, rustc_attrs, attr.span, msg); @@ -2054,12 +2054,12 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute], }; for mi in list { - let name = match mi.ident_str() { - Some(name) if mi.is_word() => name, - _ => continue, - }; + if !mi.is_word() { + continue; + } - if incomplete_features.iter().any(|f| *f == name) { + let name = mi.name_or_empty(); + if incomplete_features.iter().any(|f| name == *f) { span_handler.struct_span_warn( mi.span(), &format!( -- cgit 1.4.1-3-g733a5