Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not accidentally treat multi-segment meta-items as single-segment #58899

Merged
merged 7 commits into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor away NestedMetaItemKind
Remove methods `Attribute::span` and `MetaItem::span` duplicating public fields
  • Loading branch information
petrochenkov committed Mar 16, 2019
commit 0cf96131f444f15a2a4ac80f46a86f35fad8309a
4 changes: 2 additions & 2 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
20 changes: 10 additions & 10 deletions src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")]`"#));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/assert_module_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
};
Expand All @@ -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..]);
Expand Down Expand Up @@ -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),
);
Expand Down Expand Up @@ -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"
);
Expand Down Expand Up @@ -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
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
}),
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}
}

Expand Down
Loading