Skip to content

Commit bb4de55

Browse files
committed
Auto merge of #154808 - JonathanBrouwer:attr_cleanup, r=<try>
Post-attribute ports cleanup pt. 1 try-job: i686-msvc-2
2 parents bcded33 + 89db636 commit bb4de55

File tree

10 files changed

+269
-1252
lines changed

10 files changed

+269
-1252
lines changed

compiler/rustc_attr_parsing/src/validate_attr.rs

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

65
use rustc_ast::token::Delimiter;
76
use rustc_ast::tokenstream::DelimSpan;
87
use rustc_ast::{
98
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
109
};
11-
use rustc_errors::{Applicability, FatalError, PResult};
12-
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
10+
use rustc_errors::{Applicability, PResult};
11+
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
1312
use rustc_hir::AttrPath;
1413
use rustc_hir::lints::AttributeLintKind;
1514
use rustc_parse::parse_in;
@@ -19,43 +18,23 @@ use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1918
use rustc_session::parse::ParseSess;
2019
use rustc_span::{Span, Symbol, sym};
2120

22-
use crate::{AttributeParser, Late, session_diagnostics as errors};
21+
use crate::session_diagnostics as errors;
2322

2423
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
25-
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
24+
// Built-in attributes are parsed in their respective attribute parsers, so can be ignored here
25+
if attr.is_doc_comment()
26+
|| attr.name().is_some_and(|name| BUILTIN_ATTRIBUTE_MAP.contains_key(&name))
2627
{
2728
return;
2829
}
2930

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

173-
fn emit_malformed_attribute(
152+
pub fn emit_malformed_attribute(
174153
psess: &ParseSess,
175154
style: ast::AttrStyle,
176155
span: Span,
@@ -232,15 +211,3 @@ fn emit_malformed_attribute(
232211
err.emit();
233212
}
234213
}
235-
236-
pub fn emit_fatal_malformed_builtin_attribute(
237-
psess: &ParseSess,
238-
attr: &Attribute,
239-
name: Symbol,
240-
) -> ! {
241-
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
242-
emit_malformed_attribute(psess, attr.style, attr.span, name, template);
243-
// This is fatal, otherwise it will likely cause a cascade of other errors
244-
// (and an error here is expected to be very rare).
245-
FatalError.raise()
246-
}

compiler/rustc_expand/src/errors.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -603,21 +603,3 @@ 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: 3 additions & 18 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, Early, EvalConfigResult, ShouldEmit, eval_config_entry,
19-
parse_cfg, validate_attr,
18+
AttributeParser, CFG_TEMPLATE, EvalConfigResult, ShouldEmit, eval_config_entry, parse_cfg,
19+
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_ATTRIBUTES, UNUSED_DOC_COMMENTS};
33+
use rustc_session::lint::builtin::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,21 +2274,6 @@ 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-
);
22922277
}
22932278
}
22942279
}

compiler/rustc_expand/src/module.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ 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;
5+
use rustc_attr_parsing::validate_attr::emit_malformed_attribute;
66
use rustc_errors::{Diag, ErrorGuaranteed};
7+
use rustc_feature::template;
78
use rustc_parse::lexer::StripTokens;
89
use rustc_parse::{exp, new_parser_from_file, unwrap_or_emit_fatal};
910
use rustc_session::Session;
1011
use rustc_session::parse::ParseSess;
12+
use rustc_span::fatal_error::FatalError;
1113
use rustc_span::{Ident, Span, sym};
1214
use thin_vec::ThinVec;
1315

@@ -184,6 +186,7 @@ pub(crate) fn mod_file_path_from_attr(
184186
attrs: &[Attribute],
185187
dir_path: &Path,
186188
) -> Option<PathBuf> {
189+
// FIXME(154781) use a parsed attribute here
187190
// Extract path string from first `#[path = "path_string"]` attribute.
188191
let first_path = attrs.iter().find(|at| at.has_name(sym::path))?;
189192
let Some(path_sym) = first_path.value_str() else {
@@ -195,7 +198,17 @@ pub(crate) fn mod_file_path_from_attr(
195198
// Usually bad forms are checked during semantic analysis via
196199
// `TyCtxt::check_mod_attrs`), but by the time that runs the macro
197200
// is expanded, and it doesn't give an error.
198-
validate_attr::emit_fatal_malformed_builtin_attribute(&sess.psess, first_path, sym::path);
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()
199212
};
200213

201214
let path_str = path_sym.as_str();

0 commit comments

Comments
 (0)