Skip to content

Commit 2101f29

Browse files
committed
Auto merge of #155209 - JonathanBrouwer:attr_cleanup2, r=<try>
Post-attribute ports cleanup pt. 1 (again) try-job: i686-msvc-2
2 parents dab8d9d + 9b1a213 commit 2101f29

10 files changed

Lines changed: 268 additions & 963 deletions

File tree

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_ast::tokenstream::DelimSpan;
88
use rustc_ast::{
99
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
1010
};
11-
use rustc_errors::{Applicability, FatalError, PResult};
12-
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
11+
use rustc_errors::{Applicability, PResult};
12+
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute, template};
1313
use rustc_hir::AttrPath;
1414
use rustc_hir::lints::AttributeLintKind;
1515
use rustc_parse::parse_in;
@@ -31,14 +31,22 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
3131
// Check input tokens for built-in and key-value attributes.
3232
match builtin_attr_info {
3333
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
34-
Some(BuiltinAttribute { name, template, .. }) => {
34+
Some(BuiltinAttribute { name, .. }) => {
3535
if AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(&name)) {
3636
return;
3737
}
3838
match parse_meta(psess, attr) {
3939
// Don't check safety again, we just did that
4040
Ok(meta) => {
41-
check_builtin_meta_item(psess, &meta, attr.style, *name, *template, false)
41+
// FIXME The only unparsed builtin attributes that are left are the lint attributes, so we can hardcode the template here
42+
let lint_attrs = [sym::forbid, sym::allow, sym::warn, sym::deny, sym::expect];
43+
assert!(lint_attrs.contains(name));
44+
45+
let template = template!(
46+
List: &["lint1", "lint1, lint2, ...", r#"lint1, lint2, lint3, reason = "...""#],
47+
"https://doc.rust-lang.org/reference/attributes/diagnostics.html#lint-check-attributes"
48+
);
49+
check_builtin_meta_item(psess, &meta, attr.style, *name, template, false)
4250
}
4351
Err(err) => {
4452
err.emit();
@@ -169,7 +177,7 @@ pub fn check_builtin_meta_item(
169177
}
170178
}
171179

172-
fn emit_malformed_attribute(
180+
pub fn emit_malformed_attribute(
173181
psess: &ParseSess,
174182
style: ast::AttrStyle,
175183
span: Span,
@@ -231,15 +239,3 @@ fn emit_malformed_attribute(
231239
err.emit();
232240
}
233241
}
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/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)