Skip to content
Permalink
Browse files

Refactor away `NestedMetaItemKind`

Remove methods `Attribute::span` and `MetaItem::span` duplicating public fields
  • Loading branch information...
petrochenkov committed Mar 3, 2019
1 parent 63116d3 commit 0cf96131f444f15a2a4ac80f46a86f35fad8309a
@@ -233,7 +233,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
_ => continue,
};
self.emit_repr_error(
hint.span,
hint.span(),
item.span,
&format!("attribute should be applied to {}", allowed_targets),
&format!("not {} {}", article, allowed_targets),
@@ -242,7 +242,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {

// Just point at all repr hints if there are any incompatibilities.
// This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
let hint_spans = hints.iter().map(|hint| hint.span);
let hint_spans = hints.iter().map(|hint| hint.span());

// Error on repr(transparent, <anything else>).
if is_transparent && hints.len() > 1 {
@@ -360,9 +360,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
}
}

impl_stable_hash_for_spanned!(::syntax::ast::NestedMetaItemKind);

impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem {
MetaItem(meta_item),
Literal(lit)
});
@@ -258,7 +258,7 @@ impl<'a> LintLevelsBuilder<'a> {
let meta_item = match li.meta_item() {
Some(meta_item) if meta_item.is_word() => meta_item,
_ => {
let mut err = bad_attr(li.span);
let mut err = bad_attr(li.span());
if let Some(item) = li.meta_item() {
if let ast::MetaItemKind::NameValue(_) = item.node {
if item.path == "reason" {
@@ -290,7 +290,7 @@ impl<'a> LintLevelsBuilder<'a> {
let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
match store.check_lint_name(&name.as_str(), tool_name) {
CheckLintNameResult::Ok(ids) => {
let src = LintSource::Node(name, li.span, reason);
let src = LintSource::Node(name, li.span(), reason);
for id in ids {
specs.insert(*id, (level, src));
}
@@ -301,7 +301,7 @@ impl<'a> LintLevelsBuilder<'a> {
Ok(ids) => {
let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
let src = LintSource::Node(
Symbol::intern(complete_name), li.span, reason
Symbol::intern(complete_name), li.span(), reason
);
for id in ids {
specs.insert(*id, (level, src));
@@ -323,18 +323,18 @@ impl<'a> LintLevelsBuilder<'a> {
lint,
lvl,
src,
Some(li.span.into()),
Some(li.span().into()),
&msg,
);
err.span_suggestion(
li.span,
li.span(),
"change it to",
new_lint_name.to_string(),
Applicability::MachineApplicable,
).emit();

let src = LintSource::Node(
Symbol::intern(&new_lint_name), li.span, reason
Symbol::intern(&new_lint_name), li.span(), reason
);
for id in ids {
specs.insert(*id, (level, src));
@@ -361,11 +361,11 @@ impl<'a> LintLevelsBuilder<'a> {
lint,
level,
src,
Some(li.span.into()),
Some(li.span().into()),
&msg);
if let Some(new_name) = renamed {
err.span_suggestion(
li.span,
li.span(),
"use the new name",
new_name,
Applicability::MachineApplicable
@@ -384,12 +384,12 @@ impl<'a> LintLevelsBuilder<'a> {
lint,
level,
src,
Some(li.span.into()),
Some(li.span().into()),
&msg);

if let Some(suggestion) = suggestion {
db.span_suggestion(
li.span,
li.span(),
"did you mean",
suggestion.to_string(),
Applicability::MachineApplicable,
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
{
if let Some(items) = item.meta_item_list() {
if let Ok(subcommand) =
Self::parse(tcx, trait_def_id, &items, item.span, false)
Self::parse(tcx, trait_def_id, &items, item.span(), false)
{
subcommands.push(subcommand);
} else {
@@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
}

// nothing found
parse_error(tcx, item.span,
parse_error(tcx, item.span(),
"this attribute must have a valid value",
"expected value here",
Some(r#"eg `#[rustc_on_unimplemented(message="foo")]`"#));
@@ -104,7 +104,7 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
value = Some(ident.name),
_ =>
// FIXME better-encapsulate meta_item (don't directly access `node`)
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node),
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item),
}
}
value
@@ -153,7 +153,7 @@ impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
return value;
} else {
self.tcx.sess.span_fatal(
item.span,
item.span(),
&format!("associated value expected for `{}`", name));
}
}
@@ -430,13 +430,13 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
if DepNode::has_label_string(label) {
if out.contains(label) {
self.tcx.sess.span_fatal(
item.span,
item.span(),
&format!("dep-node label `{}` is repeated", label));
}
out.insert(label.to_string());
} else {
self.tcx.sess.span_fatal(
item.span,
item.span(),
&format!("dep-node label `{}` not recognized", label));
}
}
@@ -582,7 +582,7 @@ fn expect_associated_value(tcx: TyCtxt<'_, '_, '_>, item: &NestedMetaItem) -> as
"expected an associated value".to_string()
};

tcx.sess.span_fatal(item.span, &msg);
tcx.sess.span_fatal(item.span(), &msg);
}
}

@@ -76,7 +76,7 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
k => {
struct_span_err!(self.tcx.sess, m.span, E0458,
"unknown kind: `{}`", k)
.span_label(item.span, "unknown kind").emit();
.span_label(item.span(), "unknown kind").emit();
cstore::NativeUnknown
}
};
@@ -86,7 +86,7 @@ impl<'a, 'tcx> VarianceTest<'a, 'tcx> {

_ => {
self.tcx.sess.span_err(
meta_item.span,
meta_item.span(),
&format!("unrecognized field name `{}`", name),
);
}
@@ -59,7 +59,7 @@ pub fn load_plugins(sess: &Session,
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());
loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
},
_ => call_malformed_plugin_attribute(sess, attr.span),
}
@@ -813,12 +813,12 @@ impl<'a> Resolver<'a> {
MetaItemKind::List(nested_metas) => for nested_meta in nested_metas {
match nested_meta.ident() {
Some(ident) if nested_meta.is_word() => single_imports.push(ident),
_ => ill_formed(nested_meta.span),
_ => ill_formed(nested_meta.span()),
}
}
MetaItemKind::NameValue(..) => ill_formed(meta.span),
}
None => ill_formed(attr.span()),
None => ill_formed(attr.span),
}
}
}
@@ -2326,7 +2326,7 @@ fn from_target_feature(
if !item.check_name("enable") {
let msg = "#[target_feature(..)] only accepts sub-keys of `enable` \
currently";
tcx.sess.span_err(item.span, &msg);
tcx.sess.span_err(item.span(), &msg);
continue;
}

@@ -2336,7 +2336,7 @@ fn from_target_feature(
None => {
let msg = "#[target_feature] attribute must be of the form \
#[target_feature(enable = \"..\")]";
tcx.sess.span_err(item.span, &msg);
tcx.sess.span_err(item.span(), &msg);
continue;
}
};
@@ -2352,7 +2352,7 @@ fn from_target_feature(
this target",
feature
);
let mut err = tcx.sess.struct_span_err(item.span, &msg);
let mut err = tcx.sess.struct_span_err(item.span(), &msg);

if feature.starts_with("+") {
let valid = whitelist.contains_key(&feature[1..]);
@@ -2387,7 +2387,7 @@ fn from_target_feature(
feature_gate::emit_feature_err(
&tcx.sess.parse_sess,
feature_gate.as_ref().unwrap(),
item.span,
item.span(),
feature_gate::GateIssue::Language,
&format!("the target feature `{}` is currently unstable", feature),
);
@@ -2549,7 +2549,7 @@ fn codegen_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> Codegen
} else {
span_err!(
tcx.sess.diagnostic(),
items[0].span,
items[0].span(),
E0535,
"invalid argument"
);
@@ -2583,7 +2583,7 @@ fn codegen_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> Codegen
} else if list_contains_name(&items[..], "speed") {
OptimizeAttr::Speed
} else {
err(items[0].span, "invalid argument");
err(items[0].span(), "invalid argument");
OptimizeAttr::None
}
}
@@ -8,7 +8,7 @@ use std::fmt::{self, Write};
use std::ops;

use syntax::symbol::Symbol;
use syntax::ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind, LitKind};
use syntax::ast::{MetaItem, MetaItemKind, NestedMetaItem, LitKind};
use syntax::parse::ParseSess;
use syntax::feature_gate::Features;

@@ -41,9 +41,9 @@ pub struct InvalidCfgError {
impl Cfg {
/// Parses a `NestedMetaItem` into a `Cfg`.
fn parse_nested(nested_cfg: &NestedMetaItem) -> Result<Cfg, InvalidCfgError> {
match nested_cfg.node {
NestedMetaItemKind::MetaItem(ref cfg) => Cfg::parse(cfg),
NestedMetaItemKind::Literal(ref lit) => Err(InvalidCfgError {
match nested_cfg {
NestedMetaItem::MetaItem(ref cfg) => Cfg::parse(cfg),
NestedMetaItem::Literal(ref lit) => Err(InvalidCfgError {
msg: "unexpected literal",
span: lit.span,
}),
@@ -442,9 +442,9 @@ mod test {
path: Path::from_ident(Ident::from_str(stringify!($name))),
node: MetaItemKind::List(vec![
$(
dummy_spanned(NestedMetaItemKind::MetaItem(
NestedMetaItem::MetaItem(
dummy_meta_item_word(stringify!($list)),
)),
),
)*
]),
span: DUMMY_SP,
@@ -456,7 +456,7 @@ mod test {
path: Path::from_ident(Ident::from_str(stringify!($name))),
node: MetaItemKind::List(vec![
$(
dummy_spanned(NestedMetaItemKind::MetaItem($list)),
NestedMetaItem::MetaItem($list),
)*
]),
span: DUMMY_SP,
@@ -777,15 +777,15 @@ pub struct Attributes {
impl Attributes {
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
use syntax::ast::NestedMetaItemKind::MetaItem;
use syntax::ast::NestedMetaItem::MetaItem;

if let ast::MetaItemKind::List(ref nmis) = mi.node {
if nmis.len() == 1 {
if let MetaItem(ref cfg_mi) = nmis[0].node {
if let MetaItem(ref cfg_mi) = nmis[0] {
if cfg_mi.check_name("cfg") {
if let ast::MetaItemKind::List(ref cfg_nmis) = cfg_mi.node {
if cfg_nmis.len() == 1 {
if let MetaItem(ref content_mi) = cfg_nmis[0].node {
if let MetaItem(ref content_mi) = cfg_nmis[0] {
return Some(content_mi);
}
}
@@ -443,14 +443,11 @@ pub struct Crate {
pub span: Span,
}

/// A spanned compile-time attribute list item.
pub type NestedMetaItem = Spanned<NestedMetaItemKind>;

/// Possible values inside of compile-time attribute lists.
///
/// E.g., the '..' in `#[name(..)]`.
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum NestedMetaItemKind {
pub enum NestedMetaItem {
/// A full MetaItem, for recursive meta items.
MetaItem(MetaItem),
/// A literal.
@@ -2207,7 +2204,7 @@ pub struct Item {
impl Item {
/// Return the span that encompasses the attributes.
pub fn span_with_attributes(&self) -> Span {
self.attrs.iter().fold(self.span, |acc, attr| acc.to(attr.span()))
self.attrs.iter().fold(self.span, |acc, attr| acc.to(attr.span))
}
}

Oops, something went wrong.

0 comments on commit 0cf9613

Please sign in to comment.
You can’t perform that action at this time.