Skip to content

Commit 30107e8

Browse files
Revert rust-lang#154808 because it is based on rust-lang#152369
This reverts commit 0c94559, reversing changes made to 3352861.
1 parent 4c42051 commit 30107e8

10 files changed

Lines changed: 1252 additions & 269 deletions

File tree

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Meta-syntax validation logic of attributes for post-expansion.
22
33
use std::convert::identity;
4+
use std::slice;
45

56
use rustc_ast::token::Delimiter;
67
use rustc_ast::tokenstream::DelimSpan;
78
use rustc_ast::{
89
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
910
};
10-
use rustc_errors::{Applicability, PResult};
11-
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
11+
use rustc_errors::{Applicability, FatalError, PResult};
12+
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
1213
use rustc_hir::AttrPath;
1314
use rustc_hir::lints::AttributeLintKind;
1415
use rustc_parse::parse_in;
@@ -17,23 +18,43 @@ use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1718
use rustc_session::parse::ParseSess;
1819
use rustc_span::{Span, Symbol, sym};
1920

20-
use crate::session_diagnostics as errors;
21+
use crate::{AttributeParser, Late, session_diagnostics as errors};
2122

2223
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
23-
// Built-in attributes are parsed in their respective attribute parsers, so can be ignored here
24-
if attr.is_doc_comment()
25-
|| attr.name().is_some_and(|name| BUILTIN_ATTRIBUTE_MAP.contains_key(&name))
24+
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
2625
{
2726
return;
2827
}
2928

30-
let attr_item = attr.get_normal_item();
31-
if let AttrArgs::Eq { .. } = attr_item.args.unparsed_ref().unwrap() {
32-
// All key-value attributes are restricted to meta-item syntax.
33-
match parse_meta(psess, attr) {
34-
Ok(_) => {}
35-
Err(err) => {
36-
err.emit();
29+
let builtin_attr_info = attr.name().and_then(|name| BUILTIN_ATTRIBUTE_MAP.get(&name));
30+
31+
// Check input tokens for built-in and key-value attributes.
32+
match builtin_attr_info {
33+
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
34+
Some(BuiltinAttribute { name, template, .. }) => {
35+
if AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(&name)) {
36+
return;
37+
}
38+
match parse_meta(psess, attr) {
39+
// Don't check safety again, we just did that
40+
Ok(meta) => {
41+
check_builtin_meta_item(psess, &meta, attr.style, *name, *template, false)
42+
}
43+
Err(err) => {
44+
err.emit();
45+
}
46+
}
47+
}
48+
_ => {
49+
let attr_item = attr.get_normal_item();
50+
if let AttrArgs::Eq { .. } = attr_item.args.unparsed_ref().unwrap() {
51+
// All key-value attributes are restricted to meta-item syntax.
52+
match parse_meta(psess, attr) {
53+
Ok(_) => {}
54+
Err(err) => {
55+
err.emit();
56+
}
57+
}
3758
}
3859
}
3960
}
@@ -148,7 +169,7 @@ pub fn check_builtin_meta_item(
148169
}
149170
}
150171

151-
pub fn emit_malformed_attribute(
172+
fn emit_malformed_attribute(
152173
psess: &ParseSess,
153174
style: ast::AttrStyle,
154175
span: Span,
@@ -210,3 +231,15 @@ pub fn emit_malformed_attribute(
210231
err.emit();
211232
}
212233
}
234+
235+
pub fn emit_fatal_malformed_builtin_attribute(
236+
psess: &ParseSess,
237+
attr: &Attribute,
238+
name: Symbol,
239+
) -> ! {
240+
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
241+
emit_malformed_attribute(psess, attr.style, attr.span, name, template);
242+
// This is fatal, otherwise it will likely cause a cascade of other errors
243+
// (and an error here is expected to be very rare).
244+
FatalError.raise()
245+
}

compiler/rustc_expand/src/errors.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,21 @@ pub(crate) struct TrailingMacro {
603603
pub is_trailing: bool,
604604
pub name: Ident,
605605
}
606+
607+
#[derive(Diagnostic)]
608+
#[diag("unused attribute `{$attr_name}`")]
609+
pub(crate) struct UnusedBuiltinAttribute {
610+
#[note(
611+
"the built-in attribute `{$attr_name}` will be ignored, since it's applied to the macro invocation `{$macro_name}`"
612+
)]
613+
pub invoc_span: Span,
614+
pub attr_name: Symbol,
615+
pub macro_name: String,
616+
#[suggestion(
617+
"remove the attribute",
618+
code = "",
619+
applicability = "machine-applicable",
620+
style = "tool-only"
621+
)]
622+
pub attr_span: Span,
623+
}

compiler/rustc_expand/src/expand.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_ast::{
1515
use rustc_ast_pretty::pprust;
1616
use rustc_attr_parsing::parser::AllowExprMetavar;
1717
use rustc_attr_parsing::{
18-
AttributeParser, CFG_TEMPLATE, EvalConfigResult, ShouldEmit, eval_config_entry, parse_cfg,
19-
validate_attr,
18+
AttributeParser, CFG_TEMPLATE, Early, EvalConfigResult, ShouldEmit, eval_config_entry,
19+
parse_cfg, validate_attr,
2020
};
2121
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
2222
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -30,7 +30,7 @@ use rustc_parse::parser::{
3030
RecoverColon, RecoverComma, Recovery, token_descr,
3131
};
3232
use rustc_session::Session;
33-
use rustc_session::lint::builtin::UNUSED_DOC_COMMENTS;
33+
use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
3434
use rustc_session::parse::feature_err;
3535
use rustc_span::hygiene::SyntaxContext;
3636
use rustc_span::{ErrorGuaranteed, FileName, Ident, LocalExpnId, Span, Symbol, sym};
@@ -2274,6 +2274,21 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22742274
self.cx.current_expansion.lint_node_id,
22752275
crate::errors::MacroCallUnusedDocComment { span: attr.span },
22762276
);
2277+
} else if rustc_attr_parsing::is_builtin_attr(attr)
2278+
&& !AttributeParser::<Early>::is_parsed_attribute(&attr.path())
2279+
{
2280+
let attr_name = attr.name().unwrap();
2281+
self.cx.sess.psess.buffer_lint(
2282+
UNUSED_ATTRIBUTES,
2283+
attr.span,
2284+
self.cx.current_expansion.lint_node_id,
2285+
crate::errors::UnusedBuiltinAttribute {
2286+
attr_name,
2287+
macro_name: pprust::path_to_string(&call.path),
2288+
invoc_span: call.path.span,
2289+
attr_span: attr.span,
2290+
},
2291+
);
22772292
}
22782293
}
22792294
}

compiler/rustc_expand/src/module.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ use std::iter::once;
22
use std::path::{self, Path, PathBuf};
33

44
use rustc_ast::{AttrVec, Attribute, Inline, Item, ModSpans};
5-
use rustc_attr_parsing::validate_attr::emit_malformed_attribute;
5+
use rustc_attr_parsing::validate_attr;
66
use rustc_errors::{Diag, ErrorGuaranteed};
7-
use rustc_feature::template;
87
use rustc_parse::lexer::StripTokens;
98
use rustc_parse::{exp, new_parser_from_file, unwrap_or_emit_fatal};
109
use rustc_session::Session;
1110
use rustc_session::parse::ParseSess;
12-
use rustc_span::fatal_error::FatalError;
1311
use rustc_span::{Ident, Span, sym};
1412
use thin_vec::ThinVec;
1513

@@ -186,7 +184,6 @@ pub(crate) fn mod_file_path_from_attr(
186184
attrs: &[Attribute],
187185
dir_path: &Path,
188186
) -> Option<PathBuf> {
189-
// FIXME(154781) use a parsed attribute here
190187
// Extract path string from first `#[path = "path_string"]` attribute.
191188
let first_path = attrs.iter().find(|at| at.has_name(sym::path))?;
192189
let Some(path_sym) = first_path.value_str() else {
@@ -198,17 +195,7 @@ pub(crate) fn mod_file_path_from_attr(
198195
// Usually bad forms are checked during semantic analysis via
199196
// `TyCtxt::check_mod_attrs`), but by the time that runs the macro
200197
// is expanded, and it doesn't give an error.
201-
emit_malformed_attribute(
202-
&sess.psess,
203-
first_path.style,
204-
first_path.span,
205-
sym::path,
206-
template!(
207-
NameValueStr: "file",
208-
"https://doc.rust-lang.org/reference/items/modules.html#the-path-attribute"
209-
),
210-
);
211-
FatalError.raise()
198+
validate_attr::emit_fatal_malformed_builtin_attribute(&sess.psess, first_path, sym::path);
212199
};
213200

214201
let path_str = path_sym.as_str();

0 commit comments

Comments
 (0)