Skip to content

Commit 6aed01a

Browse files
authored
Rollup merge of rust-lang#58899 - petrochenkov:derval2, r=estebank
Do not accidentally treat multi-segment meta-items as single-segment Fixes rust-lang#55168 and many other regressions from rust-lang#50030 Basically, attributes like `#[any::prefix::foo]` were commonly interpreted as `#[foo]` due to `name()` successfully returning the last segment (this applies to nested things as well `#[attr(any::prefix::foo)]`).
2 parents 2eeca49 + f922e0d commit 6aed01a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+562
-506
lines changed

src/librustc/hir/check_attr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
166166
// ```
167167
let hints: Vec<_> = item.attrs
168168
.iter()
169-
.filter(|attr| attr.name() == "repr")
169+
.filter(|attr| attr.check_name("repr"))
170170
.filter_map(|attr| attr.meta_item_list())
171171
.flatten()
172172
.collect();
@@ -177,15 +177,15 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
177177
let mut is_transparent = false;
178178

179179
for hint in &hints {
180-
let name = if let Some(name) = hint.name() {
180+
let name = if let Some(name) = hint.ident_str() {
181181
name
182182
} else {
183183
// Invalid repr hint like repr(42). We don't check for unrecognized hints here
184184
// (libsyntax does that), so just ignore it.
185185
continue;
186186
};
187187

188-
let (article, allowed_targets) = match &*name.as_str() {
188+
let (article, allowed_targets) = match name {
189189
"C" | "align" => {
190190
is_c |= name == "C";
191191
if target != Target::Struct &&
@@ -233,7 +233,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
233233
_ => continue,
234234
};
235235
self.emit_repr_error(
236-
hint.span,
236+
hint.span(),
237237
item.span,
238238
&format!("attribute should be applied to {}", allowed_targets),
239239
&format!("not {} {}", article, allowed_targets),
@@ -242,7 +242,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
242242

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

247247
// Error on repr(transparent, <anything else>).
248248
if is_transparent && hints.len() > 1 {
@@ -313,7 +313,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
313313

314314
fn check_used(&self, item: &hir::Item, target: Target) {
315315
for attr in &item.attrs {
316-
if attr.name() == "used" && target != Target::Static {
316+
if attr.check_name("used") && target != Target::Static {
317317
self.tcx.sess
318318
.span_err(attr.span, "attribute must be applied to a `static` variable");
319319
}

src/librustc/ich/impls_syntax.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
197197
let filtered: SmallVec<[&ast::Attribute; 8]> = self
198198
.iter()
199199
.filter(|attr| {
200-
!attr.is_sugared_doc && !hcx.is_ignored_attr(attr.name())
200+
!attr.is_sugared_doc &&
201+
!attr.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name))
201202
})
202203
.collect();
203204

@@ -224,7 +225,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
224225
hcx: &mut StableHashingContext<'a>,
225226
hasher: &mut StableHasher<W>) {
226227
// Make sure that these have been filtered out.
227-
debug_assert!(!hcx.is_ignored_attr(self.name()));
228+
debug_assert!(!self.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name)));
228229
debug_assert!(!self.is_sugared_doc);
229230

230231
let ast::Attribute {
@@ -359,15 +360,13 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
359360
}
360361
}
361362

362-
impl_stable_hash_for_spanned!(::syntax::ast::NestedMetaItemKind);
363-
364-
impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
363+
impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem {
365364
MetaItem(meta_item),
366365
Literal(lit)
367366
});
368367

369368
impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
370-
ident,
369+
path,
371370
node,
372371
span
373372
});

src/librustc/lint/levels.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'a> LintLevelsBuilder<'a> {
193193
struct_span_err!(sess, span, E0452, "malformed lint attribute")
194194
};
195195
for attr in attrs {
196-
let level = match Level::from_str(&attr.name().as_str()) {
196+
let level = match attr.ident_str().and_then(|name| Level::from_str(name)) {
197197
None => continue,
198198
Some(lvl) => lvl,
199199
};
@@ -220,7 +220,7 @@ impl<'a> LintLevelsBuilder<'a> {
220220
match item.node {
221221
ast::MetaItemKind::Word => {} // actual lint names handled later
222222
ast::MetaItemKind::NameValue(ref name_value) => {
223-
if item.ident == "reason" {
223+
if item.path == "reason" {
224224
// found reason, reslice meta list to exclude it
225225
metas = &metas[0..metas.len()-1];
226226
// FIXME (#55112): issue unused-attributes lint if we thereby
@@ -254,13 +254,13 @@ impl<'a> LintLevelsBuilder<'a> {
254254
}
255255

256256
for li in metas {
257-
let word = match li.word() {
258-
Some(word) => word,
259-
None => {
260-
let mut err = bad_attr(li.span);
257+
let meta_item = match li.meta_item() {
258+
Some(meta_item) if meta_item.is_word() => meta_item,
259+
_ => {
260+
let mut err = bad_attr(li.span());
261261
if let Some(item) = li.meta_item() {
262262
if let ast::MetaItemKind::NameValue(_) = item.node {
263-
if item.ident == "reason" {
263+
if item.path == "reason" {
264264
err.help("reason in lint attribute must come last");
265265
}
266266
}
@@ -269,26 +269,27 @@ impl<'a> LintLevelsBuilder<'a> {
269269
continue;
270270
}
271271
};
272-
let tool_name = if let Some(lint_tool) = word.is_scoped() {
273-
if !attr::is_known_lint_tool(lint_tool) {
272+
let tool_name = if meta_item.path.segments.len() > 1 {
273+
let tool_ident = meta_item.path.segments[0].ident;
274+
if !attr::is_known_lint_tool(tool_ident) {
274275
span_err!(
275276
sess,
276-
lint_tool.span,
277+
tool_ident.span,
277278
E0710,
278279
"an unknown tool name found in scoped lint: `{}`",
279-
word.ident
280+
meta_item.path
280281
);
281282
continue;
282283
}
283284

284-
Some(lint_tool.as_str())
285+
Some(tool_ident.as_str())
285286
} else {
286287
None
287288
};
288-
let name = word.name();
289+
let name = meta_item.path.segments.last().expect("empty lint name").ident.name;
289290
match store.check_lint_name(&name.as_str(), tool_name) {
290291
CheckLintNameResult::Ok(ids) => {
291-
let src = LintSource::Node(name, li.span, reason);
292+
let src = LintSource::Node(name, li.span(), reason);
292293
for id in ids {
293294
specs.insert(*id, (level, src));
294295
}
@@ -299,7 +300,7 @@ impl<'a> LintLevelsBuilder<'a> {
299300
Ok(ids) => {
300301
let complete_name = &format!("{}::{}", tool_name.unwrap(), name);
301302
let src = LintSource::Node(
302-
Symbol::intern(complete_name), li.span, reason
303+
Symbol::intern(complete_name), li.span(), reason
303304
);
304305
for id in ids {
305306
specs.insert(*id, (level, src));
@@ -321,18 +322,18 @@ impl<'a> LintLevelsBuilder<'a> {
321322
lint,
322323
lvl,
323324
src,
324-
Some(li.span.into()),
325+
Some(li.span().into()),
325326
&msg,
326327
);
327328
err.span_suggestion(
328-
li.span,
329+
li.span(),
329330
"change it to",
330331
new_lint_name.to_string(),
331332
Applicability::MachineApplicable,
332333
).emit();
333334

334335
let src = LintSource::Node(
335-
Symbol::intern(&new_lint_name), li.span, reason
336+
Symbol::intern(&new_lint_name), li.span(), reason
336337
);
337338
for id in ids {
338339
specs.insert(*id, (level, src));
@@ -359,11 +360,11 @@ impl<'a> LintLevelsBuilder<'a> {
359360
lint,
360361
level,
361362
src,
362-
Some(li.span.into()),
363+
Some(li.span().into()),
363364
&msg);
364365
if let Some(new_name) = renamed {
365366
err.span_suggestion(
366-
li.span,
367+
li.span(),
367368
"use the new name",
368369
new_name,
369370
Applicability::MachineApplicable
@@ -382,12 +383,12 @@ impl<'a> LintLevelsBuilder<'a> {
382383
lint,
383384
level,
384385
src,
385-
Some(li.span.into()),
386+
Some(li.span().into()),
386387
&msg);
387388

388389
if let Some(suggestion) = suggestion {
389390
db.span_suggestion(
390-
li.span,
391+
li.span(),
391392
"did you mean",
392393
suggestion.to_string(),
393394
Applicability::MachineApplicable,

src/librustc/middle/lib_features.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
6363
for meta in metas {
6464
if let Some(mi) = meta.meta_item() {
6565
// Find the `feature = ".."` meta-item.
66-
match (&*mi.name().as_str(), mi.value_str()) {
67-
("feature", val) => feature = val,
68-
("since", val) => since = val,
66+
match (mi.ident_str(), mi.value_str()) {
67+
(Some("feature"), val) => feature = val,
68+
(Some("since"), val) => since = val,
6969
_ => {}
7070
}
7171
}

src/librustc/middle/stability.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
194194
} else {
195195
// Emit errors for non-staged-api crates.
196196
for attr in attrs {
197-
let tag = attr.name();
198-
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
199-
attr::mark_used(attr);
200-
self.tcx.sess.span_err(attr.span(), "stability attributes may not be used \
201-
outside of the standard library");
197+
if let Some(tag) = attr.ident_str() {
198+
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
199+
attr::mark_used(attr);
200+
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
201+
outside of the standard library");
202+
}
202203
}
203204
}
204205

src/librustc/session/config.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1824,7 +1824,7 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
18241824

18251825
match &mut parser.parse_meta_item() {
18261826
Ok(meta_item) if parser.token == token::Eof => {
1827-
if meta_item.ident.segments.len() != 1 {
1827+
if meta_item.path.segments.len() != 1 {
18281828
error!("argument key must be an identifier");
18291829
}
18301830
match &meta_item.node {
@@ -1835,7 +1835,8 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
18351835
error!("argument value must be a string");
18361836
}
18371837
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
1838-
return (meta_item.name(), meta_item.value_str());
1838+
let ident = meta_item.ident().expect("multi-segment cfg key");
1839+
return (ident.name, meta_item.value_str());
18391840
}
18401841
}
18411842
}

src/librustc/traits/on_unimplemented.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
107107
{
108108
if let Some(items) = item.meta_item_list() {
109109
if let Ok(subcommand) =
110-
Self::parse(tcx, trait_def_id, &items, item.span, false)
110+
Self::parse(tcx, trait_def_id, &items, item.span(), false)
111111
{
112112
subcommands.push(subcommand);
113113
} else {
@@ -118,7 +118,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
118118
}
119119

120120
// nothing found
121-
parse_error(tcx, item.span,
121+
parse_error(tcx, item.span(),
122122
"this attribute must have a valid value",
123123
"expected value here",
124124
Some(r#"eg `#[rustc_on_unimplemented(message="foo")]`"#));
@@ -177,10 +177,12 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
177177
for command in self.subcommands.iter().chain(Some(self)).rev() {
178178
if let Some(ref condition) = command.condition {
179179
if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
180-
options.contains(&(
181-
c.name().as_str().to_string(),
182-
c.value_str().map(|s| s.as_str().to_string())
183-
))
180+
c.ident_str().map_or(false, |name| {
181+
options.contains(&(
182+
name.to_string(),
183+
c.value_str().map(|s| s.as_str().to_string())
184+
))
185+
})
184186
}) {
185187
debug!("evaluate: skipping {:?} due to condition", command);
186188
continue

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ impl RustcDefaultCalls {
678678

679679
let mut cfgs = sess.parse_sess.config.iter().filter_map(|&(name, ref value)| {
680680
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
681-
ident: ast::Path::from_ident(ast::Ident::with_empty_ctxt(name)),
681+
path: ast::Path::from_ident(ast::Ident::with_empty_ctxt(name)),
682682
node: ast::MetaItemKind::Word,
683683
span: DUMMY_SP,
684684
});

src/librustc_incremental/assert_dep_graph.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
9999
fn argument(&self, attr: &ast::Attribute) -> Option<ast::Name> {
100100
let mut value = None;
101101
for list_item in attr.meta_item_list().unwrap_or_default() {
102-
match list_item.word() {
103-
Some(word) if value.is_none() =>
104-
value = Some(word.name()),
102+
match list_item.ident() {
103+
Some(ident) if list_item.is_word() && value.is_none() =>
104+
value = Some(ident.name),
105105
_ =>
106106
// FIXME better-encapsulate meta_item (don't directly access `node`)
107-
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node),
107+
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item),
108108
}
109109
}
110110
value

src/librustc_incremental/assert_module_sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
153153
return value;
154154
} else {
155155
self.tcx.sess.span_fatal(
156-
item.span,
156+
item.span(),
157157
&format!("associated value expected for `{}`", name));
158158
}
159159
}

src/librustc_incremental/persist/dirty_clean.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,13 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
430430
if DepNode::has_label_string(label) {
431431
if out.contains(label) {
432432
self.tcx.sess.span_fatal(
433-
item.span,
433+
item.span(),
434434
&format!("dep-node label `{}` is repeated", label));
435435
}
436436
out.insert(label.to_string());
437437
} else {
438438
self.tcx.sess.span_fatal(
439-
item.span,
439+
item.span(),
440440
&format!("dep-node label `{}` not recognized", label));
441441
}
442442
}
@@ -576,13 +576,13 @@ fn expect_associated_value(tcx: TyCtxt<'_, '_, '_>, item: &NestedMetaItem) -> as
576576
if let Some(value) = item.value_str() {
577577
value
578578
} else {
579-
let msg = if let Some(name) = item.name() {
579+
let msg = if let Some(name) = item.ident_str() {
580580
format!("associated value expected for `{}`", name)
581581
} else {
582582
"expected an associated value".to_string()
583583
};
584584

585-
tcx.sess.span_fatal(item.span, &msg);
585+
tcx.sess.span_fatal(item.span(), &msg);
586586
}
587587
}
588588

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ impl LintPass for DeprecatedAttr {
760760
impl EarlyLintPass for DeprecatedAttr {
761761
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
762762
for &&(n, _, _, ref g) in &self.depr_attrs {
763-
if attr.name() == n {
763+
if attr.ident_str() == Some(n) {
764764
if let &AttributeGate::Gated(Stability::Deprecated(link, suggestion),
765765
ref name,
766766
ref reason,

0 commit comments

Comments
 (0)