提交 d91b32b4 编写于 作者: B bors

Auto merge of #59256 - petrochenkov:derval2, r=Zoxc

Make meta-item API compatible with `LocalInternedString::get` soundness fix

r? @Zoxc
......@@ -177,16 +177,8 @@ fn check_repr(&self, item: &hir::Item, target: Target) {
let mut is_transparent = false;
for hint in &hints {
let name = if let Some(name) = hint.ident_str() {
name
} else {
// Invalid repr hint like repr(42). We don't check for unrecognized hints here
// (libsyntax does that), so just ignore it.
continue;
};
let (article, allowed_targets) = match name {
"C" | "align" => {
let (article, allowed_targets) = match hint.name_or_empty().get() {
name @ "C" | name @ "align" => {
is_c |= name == "C";
if target != Target::Struct &&
target != Target::Union &&
......
......@@ -194,7 +194,7 @@ pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
struct_span_err!(sess, span, E0452, "malformed lint attribute")
};
for attr in attrs {
let level = match attr.ident_str().and_then(|name| Level::from_str(name)) {
let level = match Level::from_str(&attr.name_or_empty()) {
None => continue,
Some(lvl) => lvl,
};
......
......@@ -723,12 +723,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
let attrs = tcx.hir().attrs_by_hir_id(id);
for attr in attrs {
if attr.ident_str().and_then(Level::from_str).is_some() {
return true;
}
}
false
attrs.iter().any(|attr| Level::from_str(&attr.name_or_empty()).is_some())
}
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
......
......@@ -65,9 +65,9 @@ fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
for meta in metas {
if let Some(mi) = meta.meta_item() {
// Find the `feature = ".."` meta-item.
match (mi.ident_str(), mi.value_str()) {
(Some("feature"), val) => feature = val,
(Some("since"), val) => since = val,
match (mi.name_or_empty().get(), mi.value_str()) {
("feature", val) => feature = val,
("since", val) => since = val,
_ => {}
}
}
......
......@@ -194,12 +194,11 @@ fn annotate<F>(&mut self, hir_id: HirId, attrs: &[Attribute],
} else {
// Emit errors for non-staged-api crates.
for attr in attrs {
if let Some(tag) = attr.ident_str() {
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
attr::mark_used(attr);
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
outside of the standard library");
}
let name = attr.name_or_empty();
if ["unstable", "stable", "rustc_deprecated"].contains(&name.get()) {
attr::mark_used(attr);
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
outside of the standard library");
}
}
......
......@@ -177,9 +177,9 @@ 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| {
c.ident_str().map_or(false, |name| {
c.ident().map_or(false, |ident| {
options.contains(&(
name.to_string(),
ident.to_string(),
c.value_str().map(|s| s.as_str().to_string())
))
})
......
......@@ -576,8 +576,8 @@ fn expect_associated_value(tcx: TyCtxt<'_, '_, '_>, item: &NestedMetaItem) -> as
if let Some(value) = item.value_str() {
value
} else {
let msg = if let Some(name) = item.ident_str() {
format!("associated value expected for `{}`", name)
let msg = if let Some(ident) = item.ident() {
format!("associated value expected for `{}`", ident)
} else {
"expected an associated value".to_string()
};
......
......@@ -756,8 +756,9 @@ fn get_lints(&self) -> LintArray {
impl EarlyLintPass for DeprecatedAttr {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
let name = attr.name_or_empty();
for &&(n, _, _, ref g) in &self.depr_attrs {
if attr.ident_str() == Some(n) {
if name == n {
if let &AttributeGate::Gated(Stability::Deprecated(link, suggestion),
ref name,
ref reason,
......
......@@ -267,21 +267,21 @@ fn check_attribute(&mut self, cx: &LateContext<'_, '_>, attr: &ast::Attribute) {
}
}
let name = attr.ident_str();
let name = attr.name_or_empty();
if !attr::is_used(attr) {
debug!("Emitting warning for: {:?}", attr);
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
// Is it a builtin attribute that must be used at the crate level?
let known_crate = BUILTIN_ATTRIBUTES.iter()
.find(|&&(builtin, ty, ..)| {
name == Some(builtin) && ty == AttributeType::CrateLevel
name == builtin && ty == AttributeType::CrateLevel
})
.is_some();
// Has a plugin registered this attribute as one that must be used at
// the crate level?
let plugin_crate = plugin_attributes.iter()
.find(|&&(ref x, t)| name == Some(x) && AttributeType::CrateLevel == t)
.find(|&&(ref x, t)| name == x.as_str() && AttributeType::CrateLevel == t)
.is_some();
if known_crate || plugin_crate {
let msg = match attr.style {
......
......@@ -53,8 +53,7 @@ fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item, attr: &Attribute)
// The `..` are the names of fields to dump.
let meta_items = attr.meta_item_list().unwrap_or_default();
for meta_item in meta_items {
let name = meta_item.ident_str().unwrap_or("");
match name {
match meta_item.name_or_empty().get() {
"abi" => {
self.tcx
.sess
......@@ -84,7 +83,7 @@ fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item, attr: &Attribute)
);
}
_ => {
name => {
self.tcx.sess.span_err(
meta_item.span(),
&format!("unrecognized field name `{}`", name),
......
......@@ -56,12 +56,12 @@ pub fn load_plugins(sess: &Session,
for plugin in plugins {
// plugins must have a name and can't be key = value
match plugin.ident_str() {
Some(name) if !plugin.is_value_str() => {
let args = plugin.meta_item_list().map(ToOwned::to_owned);
loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
},
_ => call_malformed_plugin_attribute(sess, attr.span),
let name = plugin.name_or_empty();
if !name.is_empty() && !plugin.is_value_str() {
let args = plugin.meta_item_list().map(ToOwned::to_owned);
loader.load_plugin(plugin.span(), &name, args.unwrap_or_default());
} else {
call_malformed_plugin_attribute(sess, attr.span);
}
}
}
......
......@@ -529,21 +529,21 @@ fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
let diag = ctxt.sess().diagnostic();
let name = attr.ident_str();
let name = attr.name_or_empty();
if attr.is_word() {
if name == Some("no_default_passes") {
if name == "no_default_passes" {
report_deprecated_attr("no_default_passes", diag);
if default_passes == passes::DefaultPassOption::Default {
default_passes = passes::DefaultPassOption::None;
}
}
} else if let Some(value) = attr.value_str() {
let sink = match name {
Some("passes") => {
let sink = match name.get() {
"passes" => {
report_deprecated_attr("passes = \"...\"", diag);
&mut manual_passes
},
Some("plugins") => {
"plugins" => {
report_deprecated_attr("plugins = \"...\"", diag);
eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
see CVE-2018-1000622");
......@@ -556,7 +556,7 @@ fn report_deprecated_attr(name: &str, diag: &errors::Handler) {
}
}
if attr.is_word() && name == Some("document_private_items") {
if attr.is_word() && name == "document_private_items" {
if default_passes == passes::DefaultPassOption::Default {
default_passes = passes::DefaultPassOption::Private;
}
......
......@@ -562,23 +562,23 @@ pub fn run(mut krate: clean::Crate,
// going to emit HTML
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
for attr in attrs.lists("doc") {
match (attr.ident_str(), attr.value_str()) {
(Some("html_favicon_url"), Some(s)) => {
match (attr.name_or_empty().get(), attr.value_str()) {
("html_favicon_url", Some(s)) => {
scx.layout.favicon = s.to_string();
}
(Some("html_logo_url"), Some(s)) => {
("html_logo_url", Some(s)) => {
scx.layout.logo = s.to_string();
}
(Some("html_playground_url"), Some(s)) => {
("html_playground_url", Some(s)) => {
markdown::PLAYGROUND.with(|slot| {
let name = krate.name.clone();
*slot.borrow_mut() = Some((Some(name), s.to_string()));
});
}
(Some("issue_tracker_base_url"), Some(s)) => {
("issue_tracker_base_url", Some(s)) => {
scx.issue_tracker_base_url = Some(s.to_string());
}
(Some("html_no_source"), None) if attr.is_word() => {
("html_no_source", None) if attr.is_word() => {
scx.include_sources = false;
}
_ => {}
......@@ -3751,7 +3751,7 @@ fn render_attributes(w: &mut fmt::Formatter<'_>, it: &clean::Item) -> fmt::Resul
let mut attrs = String::new();
for attr in &it.attrs.other_attrs {
if !attr.ident_str().map_or(false, |name| ATTRIBUTE_WHITELIST.contains(&name)) {
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty().get()) {
continue;
}
if let Some(s) = render_attribute(&attr.meta().unwrap()) {
......
......@@ -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<F>(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<ReprAttr> {
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 {
......
......@@ -22,7 +22,7 @@
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 @@ pub fn check_name(&self, name: &str) -> bool {
pub fn ident(&self) -> Option<Ident> {
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 @@ pub fn ident(&self) -> Option<Ident> {
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<Symbol> {
......@@ -205,8 +205,8 @@ pub fn ident(&self) -> Option<Ident> {
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")]
......
......@@ -1341,9 +1341,9 @@ struct Context<'a> {
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 @@ fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
}
}
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 @@ fn feature_removed(span_handler: &Handler, span: Span, reason: Option<&str>) {
};
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!(
......
......@@ -463,9 +463,10 @@ pub fn expand_ext(self,
let mut attrs = newitem.attrs.clone();
attrs.extend(item.attrs
.iter()
.filter(|a| a.ident_str().map_or(false, |name| {
["allow", "warn", "deny", "forbid", "stable", "unstable"].contains(&name)
}))
.filter(|a| {
["allow", "warn", "deny", "forbid", "stable", "unstable"]
.contains(&a.name_or_empty().get())
})
.cloned());
push(Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() })))
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册