Skip to content

Commit 25d63eb

Browse files
Use early attribute parsing for #[path] in rustc_expand
1 parent 7881f78 commit 25d63eb

2 files changed

Lines changed: 7 additions & 26 deletions

File tree

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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};
11+
use rustc_errors::{Applicability, PResult};
1212
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
1313
use rustc_hir::AttrPath;
1414
use rustc_hir::lints::AttributeLintKind;
@@ -231,15 +231,3 @@ fn emit_malformed_attribute(
231231
err.emit();
232232
}
233233
}
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: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ 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::AttributeParser;
66
use rustc_errors::{Diag, ErrorGuaranteed};
7+
use rustc_hir::attrs::AttributeKind;
78
use rustc_parse::lexer::StripTokens;
89
use rustc_parse::{exp, new_parser_from_file, unwrap_or_emit_fatal};
910
use rustc_session::Session;
@@ -185,19 +186,11 @@ pub(crate) fn mod_file_path_from_attr(
185186
dir_path: &Path,
186187
) -> Option<PathBuf> {
187188
// Extract path string from first `#[path = "path_string"]` attribute.
188-
let first_path = attrs.iter().find(|at| at.has_name(sym::path))?;
189-
let Some(path_sym) = first_path.value_str() else {
190-
// This check is here mainly to catch attempting to use a macro,
191-
// such as `#[path = concat!(...)]`. This isn't supported because
192-
// otherwise the `InvocationCollector` would need to defer loading
193-
// a module until the `#[path]` attribute was expanded, and it
194-
// doesn't support that (and would likely add a bit of complexity).
195-
// Usually bad forms are checked during semantic analysis via
196-
// `TyCtxt::check_mod_attrs`), but by the time that runs the macro
197-
// is expanded, and it doesn't give an error.
198-
validate_attr::emit_fatal_malformed_builtin_attribute(&sess.psess, first_path, sym::path);
189+
let Some(rustc_hir::Attribute::Parsed(AttributeKind::Path(path_sym, _))) =
190+
AttributeParser::parse_limited(sess, attrs, sym::path, None)
191+
else {
192+
return None;
199193
};
200-
201194
let path_str = path_sym.as_str();
202195

203196
// On windows, the base path might have the form

0 commit comments

Comments
 (0)