Skip to content

Commit a556c9b

Browse files
committed
Introduce new-style attribute parsers for several attributes
note: compiler compiles but librustdoc and clippy don't
1 parent 9aa5495 commit a556c9b

File tree

48 files changed

+1470
-1289
lines changed

Some content is hidden

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

48 files changed

+1470
-1289
lines changed

Cargo.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3478,6 +3478,7 @@ dependencies = [
34783478
"rustc_expand",
34793479
"rustc_feature",
34803480
"rustc_fluent_macro",
3481+
"rustc_hir",
34813482
"rustc_index",
34823483
"rustc_lexer",
34833484
"rustc_lint_defs",
@@ -3729,6 +3730,7 @@ dependencies = [
37293730
"rustc_abi",
37303731
"rustc_ast",
37313732
"rustc_ast_pretty",
3733+
"rustc_attr_data_structures",
37323734
"rustc_data_structures",
37333735
"rustc_error_codes",
37343736
"rustc_error_messages",
@@ -3762,6 +3764,7 @@ dependencies = [
37623764
"rustc_errors",
37633765
"rustc_feature",
37643766
"rustc_fluent_macro",
3767+
"rustc_hir",
37653768
"rustc_lexer",
37663769
"rustc_lint_defs",
37673770
"rustc_macros",
@@ -4440,6 +4443,7 @@ version = "0.0.0"
44404443
dependencies = [
44414444
"bitflags",
44424445
"rustc_abi",
4446+
"rustc_ast",
44434447
"rustc_data_structures",
44444448
"rustc_hir",
44454449
"rustc_middle",
@@ -4533,6 +4537,7 @@ dependencies = [
45334537
"punycode",
45344538
"rustc-demangle",
45354539
"rustc_abi",
4540+
"rustc_ast",
45364541
"rustc_data_structures",
45374542
"rustc_errors",
45384543
"rustc_hir",

compiler/rustc_ast_passes/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ ast_passes_precise_capturing_duplicated = duplicate `use<...>` precise capturing
207207
208208
ast_passes_precise_capturing_not_allowed_here = `use<...>` precise capturing syntax not allowed in {$loc}
209209
210-
ast_passes_stability_outside_std = stability attributes may not be used outside of the standard library
211-
212210
ast_passes_static_without_body =
213211
free static item without body
214212
.suggestion = provide a definition for the static

compiler/rustc_ast_passes/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -732,13 +732,6 @@ pub(crate) struct AssociatedSuggestion2 {
732732
pub potential_assoc: Ident,
733733
}
734734

735-
#[derive(Diagnostic)]
736-
#[diag(ast_passes_stability_outside_std, code = E0734)]
737-
pub(crate) struct StabilityOutsideStd {
738-
#[primary_span]
739-
pub span: Span,
740-
}
741-
742735
#[derive(Diagnostic)]
743736
#[diag(ast_passes_feature_on_non_nightly, code = E0554)]
744737
pub(crate) struct FeatureOnNonNightly {

compiler/rustc_ast_passes/src/feature_gate.rs

-12
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
208208
);
209209
}
210210
}
211-
212-
// Emit errors for non-staged-api crates.
213-
if !self.features.staged_api() {
214-
if attr.has_name(sym::unstable)
215-
|| attr.has_name(sym::stable)
216-
|| attr.has_name(sym::rustc_const_unstable)
217-
|| attr.has_name(sym::rustc_const_stable)
218-
|| attr.has_name(sym::rustc_default_body_unstable)
219-
{
220-
self.sess.dcx().emit_err(errors::StabilityOutsideStd { span: attr.span });
221-
}
222-
}
223211
}
224212

225213
fn visit_item(&mut self, i: &'a ast::Item) {

compiler/rustc_attr_data_structures/src/attributes.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use rustc_abi::Align;
22
use rustc_ast::token::CommentKind;
33
use rustc_ast::{self as ast, AttrStyle};
44
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
5+
use rustc_span::hygiene::Transparency;
56
use rustc_span::{Span, Symbol};
7+
use thin_vec::ThinVec;
68

7-
use crate::RustcVersion;
9+
use crate::{DefaultBodyStability, PartialConstStability, RustcVersion, Stability};
810

911
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
1012
pub enum InlineAttr {
@@ -60,6 +62,8 @@ pub enum ReprAttr {
6062
ReprSimd,
6163
ReprTransparent,
6264
ReprAlign(Align),
65+
// this one is just so we can emit a lint for it
66+
ReprEmpty,
6367
}
6468
pub use ReprAttr::*;
6569

@@ -137,10 +141,44 @@ impl Deprecation {
137141
/// happen.
138142
///
139143
/// For more docs, look in [`rustc_attr`](https://doc.rust-lang.org/stable/nightly-rustc/rustc_attr/index.html)
140-
// FIXME(jdonszelmann): rename to AttributeKind once hir::AttributeKind is dissolved
141144
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
142145
pub enum AttributeKind {
143146
// tidy-alphabetical-start
144-
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
147+
AllowConstFnUnstable(ThinVec<Symbol>),
148+
AllowInternalUnstable(ThinVec<(Symbol, Span)>),
149+
BodyStability {
150+
stability: DefaultBodyStability,
151+
/// Span of the `#[rustc_default_body_unstable(...)]` attribute
152+
span: Span,
153+
},
154+
Confusables {
155+
symbols: ThinVec<Symbol>,
156+
// FIXME(jdonszelmann): remove when target validation code is moved
157+
first_span: Span,
158+
},
159+
ConstStability {
160+
stability: PartialConstStability,
161+
/// Span of the `#[rustc_const_stable(...)]` or `#[rustc_const_unstable(...)]` attribute
162+
span: Span,
163+
},
164+
ConstStabilityIndirect,
165+
Deprecation {
166+
deprecation: Deprecation,
167+
span: Span,
168+
},
169+
Diagnostic(DiagnosticAttribute),
170+
DocComment {
171+
style: AttrStyle,
172+
kind: CommentKind,
173+
span: Span,
174+
comment: Symbol,
175+
},
176+
MacroTransparency(Transparency),
177+
Repr(ThinVec<(ReprAttr, Span)>),
178+
Stability {
179+
stability: Stability,
180+
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute
181+
span: Span,
182+
},
145183
// tidy-alphabetical-end
146184
}

compiler/rustc_attr_parsing/messages.ftl

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ attr_parsing_deprecated_item_suggestion =
66
.help = add `#![feature(deprecated_suggestion)]` to the crate root
77
.note = see #94785 for more details
88
9+
attr_parsing_empty_confusables =
10+
expected at least one confusable name
911
attr_parsing_expected_one_cfg_pattern =
1012
expected 1 cfg-pattern
1113
@@ -21,8 +23,8 @@ attr_parsing_expects_feature_list =
2123
attr_parsing_expects_features =
2224
`{$name}` expects feature names
2325
24-
attr_parsing_incorrect_meta_item =
25-
incorrect meta item
26+
attr_parsing_incorrect_meta_item = expected a quoted string literal
27+
attr_parsing_incorrect_meta_item_suggestion = consider surrounding this with quotes
2628
2729
attr_parsing_incorrect_repr_format_align_one_arg =
2830
incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
@@ -90,18 +92,18 @@ attr_parsing_non_ident_feature =
9092
9193
attr_parsing_repr_ident =
9294
meta item in `repr` must be an identifier
95+
9396
attr_parsing_rustc_allowed_unstable_pairing =
9497
`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute
9598
96-
attr_parsing_rustc_const_stable_indirect_pairing =
97-
`const_stable_indirect` attribute does not make sense on `rustc_const_stable` function, its behavior is already implied
98-
9999
attr_parsing_rustc_promotable_pairing =
100100
`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` or a `rustc_const_stable` attribute
101101
102102
attr_parsing_soft_no_args =
103103
`soft` should not have any arguments
104104
105+
attr_parsing_stability_outside_std = stability attributes may not be used outside of the standard library
106+
105107
attr_parsing_unknown_meta_item =
106108
unknown meta item '{$item}'
107109
.label = expected one of {$expected}
@@ -128,3 +130,8 @@ attr_parsing_unsupported_literal_generic =
128130
unsupported literal
129131
attr_parsing_unsupported_literal_suggestion =
130132
consider removing the prefix
133+
134+
attr_parsing_unused_multiple =
135+
multiple `{$name}` attributes
136+
.suggestion = remove this attribute
137+
.note = attribute also specified here
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,67 @@
1-
use rustc_ast::attr::{AttributeExt, filter_by_name};
2-
use rustc_session::Session;
3-
use rustc_span::{Symbol, sym};
1+
use std::iter;
42

3+
use rustc_attr_data_structures::AttributeKind;
4+
use rustc_span::{Span, Symbol, sym};
5+
6+
use super::{CombineAttributeParser, ConvertFn};
7+
use crate::context::AcceptContext;
8+
use crate::parser::ArgParser;
59
use crate::session_diagnostics;
610

7-
pub fn allow_internal_unstable<'a>(
8-
sess: &'a Session,
9-
attrs: &'a [impl AttributeExt],
10-
) -> impl Iterator<Item = Symbol> + 'a {
11-
allow_unstable(sess, attrs, sym::allow_internal_unstable)
11+
pub(crate) struct AllowInternalUnstableParser;
12+
impl CombineAttributeParser for AllowInternalUnstableParser {
13+
const PATH: &'static [rustc_span::Symbol] = &[sym::allow_internal_unstable];
14+
type Item = (Symbol, Span);
15+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowInternalUnstable;
16+
17+
fn extend<'a>(
18+
cx: &'a AcceptContext<'a>,
19+
args: &'a ArgParser<'a>,
20+
) -> impl IntoIterator<Item = Self::Item> + 'a {
21+
parse_unstable(cx, args, Self::PATH[0]).into_iter().zip(iter::repeat(cx.attr_span))
22+
}
1223
}
1324

14-
pub fn rustc_allow_const_fn_unstable<'a>(
15-
sess: &'a Session,
16-
attrs: &'a [impl AttributeExt],
17-
) -> impl Iterator<Item = Symbol> + 'a {
18-
allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable)
25+
pub(crate) struct AllowConstFnUnstableParser;
26+
impl CombineAttributeParser for AllowConstFnUnstableParser {
27+
const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_allow_const_fn_unstable];
28+
type Item = Symbol;
29+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowConstFnUnstable;
30+
31+
fn extend<'a>(
32+
cx: &'a AcceptContext<'a>,
33+
args: &'a ArgParser<'a>,
34+
) -> impl IntoIterator<Item = Self::Item> + 'a {
35+
parse_unstable(cx, args, Self::PATH[0])
36+
}
1937
}
2038

21-
fn allow_unstable<'a>(
22-
sess: &'a Session,
23-
attrs: &'a [impl AttributeExt],
39+
fn parse_unstable<'a>(
40+
cx: &AcceptContext<'_>,
41+
args: &'a ArgParser<'a>,
2442
symbol: Symbol,
25-
) -> impl Iterator<Item = Symbol> + 'a {
26-
let attrs = filter_by_name(attrs, symbol);
27-
let list = attrs
28-
.filter_map(move |attr| {
29-
attr.meta_item_list().or_else(|| {
30-
sess.dcx().emit_err(session_diagnostics::ExpectsFeatureList {
31-
span: attr.span(),
32-
name: symbol.to_ident_string(),
33-
});
34-
None
35-
})
36-
})
37-
.flatten();
38-
39-
list.into_iter().filter_map(move |it| {
40-
let name = it.ident().map(|ident| ident.name);
41-
if name.is_none() {
42-
sess.dcx().emit_err(session_diagnostics::ExpectsFeatures {
43-
span: it.span(),
43+
) -> impl IntoIterator<Item = Symbol> {
44+
let mut res = Vec::new();
45+
46+
let Some(list) = args.list() else {
47+
cx.emit_err(session_diagnostics::ExpectsFeatureList {
48+
span: cx.attr_span,
49+
name: symbol.to_ident_string(),
50+
});
51+
return res;
52+
};
53+
54+
for param in list.mixed() {
55+
let param_span = param.span();
56+
if let Some(ident) = param.meta_item().and_then(|i| i.word_without_args()) {
57+
res.push(ident.name);
58+
} else {
59+
cx.emit_err(session_diagnostics::ExpectsFeatures {
60+
span: param_span,
4461
name: symbol.to_ident_string(),
4562
});
4663
}
47-
name
48-
})
64+
}
65+
66+
res
4967
}

0 commit comments

Comments
 (0)