Skip to content

Commit 91f38e2

Browse files
committed
Port crate name to the new attribute system
1 parent 8df154b commit 91f38e2

Some content is hidden

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

41 files changed

+350
-217
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3803,7 +3803,6 @@ dependencies = [
38033803
"rustc_error_messages",
38043804
"rustc_fluent_macro",
38053805
"rustc_hashes",
3806-
"rustc_hir_id",
38073806
"rustc_index",
38083807
"rustc_lexer",
38093808
"rustc_lint_defs",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct CrateNameParser;
4+
5+
impl<S: Stage> SingleAttributeParser<S> for CrateNameParser {
6+
const PATH: &[Symbol] = &[sym::crate_name];
7+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
8+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
9+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
10+
11+
// FIXME: crate name is allowed on all targets and ignored,
12+
// even though it should only be valid on crates of course
13+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
14+
15+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
16+
let ArgParser::NameValue(n) = args else {
17+
cx.expected_name_value(cx.attr_span, None);
18+
return None;
19+
};
20+
21+
let Some(name) = n.value_as_str() else {
22+
cx.expected_string_literal(n.value_span, Some(n.value_as_lit()));
23+
return None;
24+
};
25+
26+
Some(AttributeKind::CrateName { name, name_span: n.value_span, style: cx.attr_style })
27+
}
28+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) mod cfg;
3535
pub(crate) mod cfg_old;
3636
pub(crate) mod codegen_attrs;
3737
pub(crate) mod confusables;
38+
pub(crate) mod crate_level;
3839
pub(crate) mod deprecation;
3940
pub(crate) mod dummy;
4041
pub(crate) mod inline;

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::LitKind;
2-
use rustc_ast::attr::{AttributeExt, first_attr_value_str_by_name};
2+
use rustc_ast::attr::AttributeExt;
33
use rustc_feature::is_builtin_attr_name;
44
use rustc_hir::RustcVersion;
55
use rustc_span::{Symbol, sym};
@@ -27,10 +27,6 @@ pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
2727
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
2828
}
2929

30-
pub fn find_crate_name(attrs: &[impl AttributeExt]) -> Option<Symbol> {
31-
first_attr_value_str_by_name(attrs, sym::crate_name)
32-
}
33-
3430
pub fn is_doc_alias_attrs_contain_symbol<'tcx, T: AttributeExt + 'tcx>(
3531
attrs: impl Iterator<Item = &'tcx T>,
3632
symbol: Symbol,

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::attributes::codegen_attrs::{
2323
NoMangleParser, OptimizeParser, TargetFeatureParser, TrackCallerParser, UsedParser,
2424
};
2525
use crate::attributes::confusables::ConfusablesParser;
26+
use crate::attributes::crate_level::CrateNameParser;
2627
use crate::attributes::deprecation::DeprecationParser;
2728
use crate::attributes::dummy::DummyParser;
2829
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -165,6 +166,7 @@ attribute_parsers!(
165166

166167
// tidy-alphabetical-start
167168
Single<CoverageParser>,
169+
Single<CrateNameParser>,
168170
Single<CustomMirParser>,
169171
Single<DeprecationParser>,
170172
Single<DummyParser>,
@@ -312,7 +314,9 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
312314
/// The span of the attribute currently being parsed
313315
pub(crate) attr_span: Span,
314316

317+
/// Whether it is an inner or outer attribute
315318
pub(crate) attr_style: AttrStyle,
319+
316320
/// The expected structure of the attribute.
317321
///
318322
/// Used in reporting errors to give a hint to users what the attribute *should* look like.

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ impl<'sess> AttributeParser<'sess, Early> {
5050
target_span: Span,
5151
target_node_id: NodeId,
5252
features: Option<&'sess Features>,
53+
) -> Option<Attribute> {
54+
Self::parse_limited_should_emit(
55+
sess,
56+
attrs,
57+
sym,
58+
target_span,
59+
target_node_id,
60+
features,
61+
ShouldEmit::Nothing,
62+
)
63+
}
64+
65+
/// Usually you want `parse_limited`, which defaults to no errors.
66+
pub fn parse_limited_should_emit(
67+
sess: &'sess Session,
68+
attrs: &[ast::Attribute],
69+
sym: Symbol,
70+
target_span: Span,
71+
target_node_id: NodeId,
72+
features: Option<&'sess Features>,
73+
should_emit: ShouldEmit,
5374
) -> Option<Attribute> {
5475
let mut parsed = Self::parse_limited_all(
5576
sess,
@@ -59,7 +80,7 @@ impl<'sess> AttributeParser<'sess, Early> {
5980
target_span,
6081
target_node_id,
6182
features,
62-
ShouldEmit::Nothing,
83+
should_emit,
6384
);
6485
assert!(parsed.len() <= 1);
6586
parsed.pop()
@@ -84,9 +105,8 @@ impl<'sess> AttributeParser<'sess, Early> {
84105
target,
85106
OmitDoc::Skip,
86107
std::convert::identity,
87-
|_lint| {
88-
// FIXME: Can't emit lints here for now
89-
// This branch can be hit when an attribute produces a warning during early parsing (such as attributes on macro calls)
108+
|lint| {
109+
crate::lints::emit_attribute_lint(&lint, sess);
90110
},
91111
)
92112
}
@@ -121,8 +141,8 @@ impl<'sess> AttributeParser<'sess, Early> {
121141
cx: &mut parser,
122142
target_span,
123143
target_id: target_node_id,
124-
emit_lint: &mut |_lint| {
125-
panic!("can't emit lints here for now (nothing uses this atm)");
144+
emit_lint: &mut |lint| {
145+
crate::lints::emit_attribute_lint(&lint, sess);
126146
},
127147
},
128148
attr_span: attr.span,

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ pub mod validate_attr;
106106

107107
pub use attributes::cfg::{CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg_attr};
108108
pub use attributes::cfg_old::*;
109-
pub use attributes::util::{
110-
find_crate_name, is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version,
111-
};
109+
pub use attributes::util::{is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version};
112110
pub use context::{Early, Late, OmitDoc, ShouldEmit};
113111
pub use interface::AttributeParser;
114112
pub use lints::emit_attribute_lint;

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::borrow::Cow;
22

33
use rustc_errors::{DiagArgValue, LintEmitter};
4+
use rustc_hir::Target;
45
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
5-
use rustc_hir::{HirId, Target};
66
use rustc_span::sym;
77

88
use crate::session_diagnostics;
99

10-
pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emitter: L) {
10+
pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<L::Id>, lint_emitter: L) {
1111
let AttributeLint { id, span, kind } = lint;
1212

1313
match kind {
@@ -51,7 +51,7 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emi
5151
*id,
5252
*span,
5353
session_diagnostics::InvalidTargetLint {
54-
name,
54+
name: name.clone(),
5555
target: target.plural_name(),
5656
applied: DiagArgValue::StrListSepByAnd(
5757
applied.into_iter().map(|i| Cow::Owned(i.to_string())).collect(),

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ pub(crate) struct EmptyAttributeList {
484484
#[diag(attr_parsing_invalid_target_lint)]
485485
#[warning]
486486
#[help]
487-
pub(crate) struct InvalidTargetLint<'a> {
488-
pub name: &'a AttrPath,
489-
pub target: &'a str,
487+
pub(crate) struct InvalidTargetLint {
488+
pub name: AttrPath,
489+
pub target: &'static str,
490490
pub applied: DiagArgValue,
491491
pub only: &'static str,
492492
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]

compiler/rustc_errors/Cargo.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ rustc_error_codes = { path = "../rustc_error_codes" }
1414
rustc_error_messages = { path = "../rustc_error_messages" }
1515
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1616
rustc_hashes = { path = "../rustc_hashes" }
17-
rustc_hir_id = { path = "../rustc_hir_id" }
1817
rustc_index = { path = "../rustc_index" }
1918
rustc_lexer = { path = "../rustc_lexer" }
2019
rustc_lint_defs = { path = "../rustc_lint_defs" }
@@ -30,8 +29,4 @@ tracing = "0.1"
3029

3130
[target.'cfg(windows)'.dependencies.windows]
3231
version = "0.61.0"
33-
features = [
34-
"Win32_Foundation",
35-
"Win32_Security",
36-
"Win32_System_Threading",
37-
]
32+
features = ["Win32_Foundation", "Win32_Security", "Win32_System_Threading"]

0 commit comments

Comments
 (0)