Skip to content

Commit 7e0f5b5

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

File tree

50 files changed

+1502
-1325
lines changed

Some content is hidden

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

50 files changed

+1502
-1325
lines changed

Cargo.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3341,6 +3341,7 @@ dependencies = [
33413341
"rustc_expand",
33423342
"rustc_feature",
33433343
"rustc_fluent_macro",
3344+
"rustc_hir",
33443345
"rustc_index",
33453346
"rustc_lexer",
33463347
"rustc_lint_defs",
@@ -3597,6 +3598,7 @@ dependencies = [
35973598
"rustc_abi",
35983599
"rustc_ast",
35993600
"rustc_ast_pretty",
3601+
"rustc_attr_data_structures",
36003602
"rustc_data_structures",
36013603
"rustc_error_codes",
36023604
"rustc_error_messages",
@@ -3631,6 +3633,7 @@ dependencies = [
36313633
"rustc_errors",
36323634
"rustc_feature",
36333635
"rustc_fluent_macro",
3636+
"rustc_hir",
36343637
"rustc_lexer",
36353638
"rustc_lint_defs",
36363639
"rustc_macros",
@@ -4319,6 +4322,7 @@ version = "0.0.0"
43194322
dependencies = [
43204323
"bitflags",
43214324
"rustc_abi",
4325+
"rustc_ast",
43224326
"rustc_data_structures",
43234327
"rustc_hir",
43244328
"rustc_middle",
@@ -4415,6 +4419,7 @@ dependencies = [
44154419
"punycode",
44164420
"rustc-demangle",
44174421
"rustc_abi",
4422+
"rustc_ast",
44184423
"rustc_data_structures",
44194424
"rustc_errors",
44204425
"rustc_hashes",

compiler/rustc_ast_lowering/src/lib.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
874874

875875
debug_assert_eq!(id.owner, self.current_hir_id_owner);
876876
let ret = self.arena.alloc_from_iter(lowered_attrs);
877-
debug_assert!(!ret.is_empty());
878-
self.attrs.insert(id.local_id, ret);
879-
ret
877+
878+
// this is possible if an item contained syntactical attribute,
879+
// but none of them parse succesfully or all of them were ignored
880+
// for not being built-in attributes at all. They could be remaining
881+
// unexpanded attributes used as markers in proc-macro derives for example.
882+
// This will have emitted some diagnostics for the misparse, but will then
883+
// not emit the attribute making the list empty.
884+
if ret.is_empty() {
885+
&[]
886+
} else {
887+
self.attrs.insert(id.local_id, ret);
888+
ret
889+
}
880890
}
881891
}
882892

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
@@ -178,18 +178,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
178178
);
179179
}
180180
}
181-
182-
// Emit errors for non-staged-api crates.
183-
if !self.features.staged_api() {
184-
if attr.has_name(sym::unstable)
185-
|| attr.has_name(sym::stable)
186-
|| attr.has_name(sym::rustc_const_unstable)
187-
|| attr.has_name(sym::rustc_const_stable)
188-
|| attr.has_name(sym::rustc_default_body_unstable)
189-
{
190-
self.sess.dcx().emit_err(errors::StabilityOutsideStd { span: attr.span });
191-
}
192-
}
193181
}
194182

195183
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 {
@@ -72,6 +74,8 @@ pub enum ReprAttr {
7274
ReprSimd,
7375
ReprTransparent,
7476
ReprAlign(Align),
77+
// this one is just so we can emit a lint for it
78+
ReprEmpty,
7579
}
7680
pub use ReprAttr::*;
7781

@@ -150,10 +154,44 @@ impl Deprecation {
150154
/// happen.
151155
///
152156
/// For more docs, look in [`rustc_attr`](https://doc.rust-lang.org/stable/nightly-rustc/rustc_attr/index.html)
153-
// FIXME(jdonszelmann): rename to AttributeKind once hir::AttributeKind is dissolved
154157
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
155158
pub enum AttributeKind {
156159
// tidy-alphabetical-start
157-
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
160+
AllowConstFnUnstable(ThinVec<Symbol>),
161+
AllowInternalUnstable(ThinVec<(Symbol, Span)>),
162+
BodyStability {
163+
stability: DefaultBodyStability,
164+
/// Span of the `#[rustc_default_body_unstable(...)]` attribute
165+
span: Span,
166+
},
167+
Confusables {
168+
symbols: ThinVec<Symbol>,
169+
// FIXME(jdonszelmann): remove when target validation code is moved
170+
first_span: Span,
171+
},
172+
ConstStability {
173+
stability: PartialConstStability,
174+
/// Span of the `#[rustc_const_stable(...)]` or `#[rustc_const_unstable(...)]` attribute
175+
span: Span,
176+
},
177+
ConstStabilityIndirect,
178+
Deprecation {
179+
deprecation: Deprecation,
180+
span: Span,
181+
},
182+
Diagnostic(DiagnosticAttribute),
183+
DocComment {
184+
style: AttrStyle,
185+
kind: CommentKind,
186+
span: Span,
187+
comment: Symbol,
188+
},
189+
MacroTransparency(Transparency),
190+
Repr(ThinVec<(ReprAttr, Span)>),
191+
Stability {
192+
stability: Stability,
193+
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute
194+
span: Span,
195+
},
158196
// tidy-alphabetical-end
159197
}

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(
8-
sess: &Session,
9-
attrs: &[impl AttributeExt],
10-
) -> impl Iterator<Item = Symbol> {
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(
15-
sess: &Session,
16-
attrs: &[impl AttributeExt],
17-
) -> impl Iterator<Item = Symbol> {
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(
22-
sess: &Session,
23-
attrs: &[impl AttributeExt],
39+
fn parse_unstable<'a>(
40+
cx: &AcceptContext<'_>,
41+
args: &'a ArgParser<'a>,
2442
symbol: Symbol,
25-
) -> impl Iterator<Item = Symbol> {
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)