From 7df0052df8234be4c9075cec3c2d6414a28c87b1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 28 Nov 2020 16:11:25 +0100 Subject: [PATCH] Created NestedMetaItem::name_value_literal_span method --- compiler/rustc_ast/src/attr/mod.rs | 15 +++++++++++++++ compiler/rustc_attr/src/builtin.rs | 2 +- compiler/rustc_expand/src/expand.rs | 11 +++++------ compiler/rustc_middle/src/middle/limits.rs | 3 +-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 2ff65737444..30050002046 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -115,6 +115,10 @@ impl NestedMetaItem { pub fn is_meta_item_list(&self) -> bool { self.meta_item_list().is_some() } + + pub fn name_value_literal_span(&self) -> Option { + self.meta_item()?.name_value_literal_span() + } } impl Attribute { @@ -175,6 +179,13 @@ impl Attribute { pub fn is_value_str(&self) -> bool { self.value_str().is_some() } + + pub fn name_value_literal_span(&self) -> Option { + match self.kind { + AttrKind::Normal(ref item, _) => item.meta(self.span).and_then(|meta| meta.name_value_literal_span()), + AttrKind::DocComment(..) => None, + } + } } impl MetaItem { @@ -227,6 +238,10 @@ impl MetaItem { pub fn is_value_str(&self) -> bool { self.value_str().is_some() } + + pub fn name_value_literal_span(&self) -> Option { + Some(self.name_value_literal()?.span) + } } impl AttrItem { diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 364a3a1eeb5..bb7562bc80c 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -294,7 +294,7 @@ where or \"none\"", ) .span_label( - mi.name_value_literal().unwrap().span, + mi.name_value_literal_span().unwrap(), msg, ) .emit(); diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 4ba75c21cf0..37ff6b9b368 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -1603,23 +1603,22 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { items.push(ast::NestedMetaItem::MetaItem(item)); } Err(e) => { - let lit = - it.meta_item().and_then(|item| item.name_value_literal()).unwrap(); + let lit_span = it.name_value_literal_span().unwrap(); if e.kind() == ErrorKind::InvalidData { self.cx .struct_span_err( - lit.span, + lit_span, &format!("{} wasn't a utf-8 file", filename.display()), ) - .span_label(lit.span, "contains invalid utf-8") + .span_label(lit_span, "contains invalid utf-8") .emit(); } else { let mut err = self.cx.struct_span_err( - lit.span, + lit_span, &format!("couldn't read {}: {}", filename.display(), e), ); - err.span_label(lit.span, "couldn't read file"); + err.span_label(lit_span, "couldn't read file"); err.emit(); } diff --git a/compiler/rustc_middle/src/middle/limits.rs b/compiler/rustc_middle/src/middle/limits.rs index 41342764ba7..61f850c2fc1 100644 --- a/compiler/rustc_middle/src/middle/limits.rs +++ b/compiler/rustc_middle/src/middle/limits.rs @@ -43,8 +43,7 @@ fn update_limit( let value_span = attr .meta() - .and_then(|meta| meta.name_value_literal().cloned()) - .map(|lit| lit.span) + .and_then(|meta| meta.name_value_literal_span()) .unwrap_or(attr.span); let error_str = match e.kind() {