Skip to content

Commit 6bcace2

Browse files
Continue migration to new Attribute API for doc attribute
1 parent 9dcd0f7 commit 6bcace2

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
use rustc_attr_data_structures::lints::AttributeLintKind;
2-
use rustc_attr_data_structures::{AttributeKind, DocAttribute, DocInline};
1+
// FIXME: to be removed
2+
#![allow(unused_imports)]
3+
34
use rustc_errors::MultiSpan;
45
use rustc_feature::template;
6+
use rustc_hir::attrs::{AttributeKind, DocAttribute, DocInline};
7+
use rustc_hir::lints::AttributeLintKind;
58
use rustc_span::{Span, Symbol, edition, sym};
69

710
use super::{AcceptMapping, AttributeParser};
11+
use super::prelude::{Allow, AllowedTargets, MethodKind, Target};
812
use crate::context::{AcceptContext, FinalizeContext, Stage};
913
use crate::fluent_generated as fluent;
1014
use crate::parser::{ArgParser, MetaItemOrLitParser, MetaItemParser, PathParser};
@@ -28,8 +32,8 @@ impl DocParser {
2832

2933
match path.word_sym() {
3034
Some(sym::no_crate_inject) => {
31-
if !args.no_args() {
32-
cx.expected_no_args(args.span().unwrap());
35+
if let Err(span) = args.no_args() {
36+
cx.expected_no_args(span);
3337
return;
3438
}
3539

@@ -42,16 +46,19 @@ impl DocParser {
4246
}
4347
Some(sym::attr) => {
4448
let Some(list) = args.list() else {
45-
cx.expected_list(args.span().unwrap_or(path.span()));
49+
cx.expected_list(cx.attr_span);
4650
return;
4751
};
4852

49-
self.attribute.test_attrs.push(todo!());
53+
// FIXME: convert list into a Vec of `AttributeKind`.
54+
for _ in list {
55+
// self.attribute.test_attrs.push(AttributeKind::parse());
56+
}
5057
}
5158
_ => {
5259
cx.expected_specific_argument(
5360
mip.span(),
54-
[sym::no_crate_inject.as_str(), sym::attr.as_str()].to_vec(),
61+
&[sym::no_crate_inject, sym::attr],
5562
);
5663
}
5764
}
@@ -162,8 +169,8 @@ impl DocParser {
162169
args: &ArgParser<'_>,
163170
inline: DocInline,
164171
) {
165-
if !args.no_args() {
166-
cx.expected_no_args(args.span().unwrap());
172+
if let Err(span) = args.no_args() {
173+
cx.expected_no_args(span);
167174
return;
168175
}
169176

@@ -192,8 +199,8 @@ impl DocParser {
192199

193200
macro_rules! no_args {
194201
($ident: ident) => {{
195-
if !args.no_args() {
196-
cx.expected_no_args(args.span().unwrap());
202+
if let Err(span) = args.no_args() {
203+
cx.expected_no_args(span);
197204
return;
198205
}
199206

@@ -239,7 +246,6 @@ impl DocParser {
239246
Some(sym::no_inline) => self.parse_inline(cx, path, args, DocInline::NoInline),
240247
Some(sym::masked) => no_args!(masked),
241248
Some(sym::cfg) => no_args!(cfg),
242-
Some(sym::cfg_hide) => no_args!(cfg_hide),
243249
Some(sym::notable_trait) => no_args!(notable_trait),
244250
Some(sym::keyword) => self.parse_keyword(cx, path, args),
245251
Some(sym::fake_variadic) => no_args!(fake_variadic),
@@ -323,19 +329,18 @@ impl DocParser {
323329
_ => {
324330
cx.expected_specific_argument(
325331
mip.span(),
326-
[
327-
sym::alias.as_str(),
328-
sym::hidden.as_str(),
329-
sym::html_favicon_url.as_str(),
330-
sym::html_logo_url.as_str(),
331-
sym::html_no_source.as_str(),
332-
sym::html_playground_url.as_str(),
333-
sym::html_root_url.as_str(),
334-
sym::inline.as_str(),
335-
sym::no_inline.as_str(),
336-
sym::test.as_str(),
337-
]
338-
.to_vec(),
332+
&[
333+
sym::alias,
334+
sym::hidden,
335+
sym::html_favicon_url,
336+
sym::html_logo_url,
337+
sym::html_no_source,
338+
sym::html_playground_url,
339+
sym::html_root_url,
340+
sym::inline,
341+
sym::no_inline,
342+
sym::test,
343+
],
339344
);
340345
}
341346
}
@@ -356,15 +361,17 @@ impl DocParser {
356361
MetaItemOrLitParser::MetaItemParser(mip) => {
357362
self.parse_single_doc_attr_item(cx, mip);
358363
}
359-
MetaItemOrLitParser::Lit(lit) => todo!("error should've used equals"),
364+
MetaItemOrLitParser::Lit(lit) => {
365+
cx.expected_name_value(i.span(), None);
366+
}
360367
MetaItemOrLitParser::Err(..) => {
361368
// already had an error here, move on.
362369
}
363370
}
364371
}
365372
}
366373
ArgParser::NameValue(v) => {
367-
panic!("this should be rare if at all possible");
374+
self.attribute.value.push((v, args.span()));
368375
}
369376
}
370377
}
@@ -373,11 +380,41 @@ impl DocParser {
373380
impl<S: Stage> AttributeParser<S> for DocParser {
374381
const ATTRIBUTES: AcceptMapping<Self, S> = &[(
375382
&[sym::doc],
376-
template!(List: "hidden|inline|...", NameValueStr: "string"),
383+
template!(List: &["hidden", "inline", "test"], NameValueStr: "string"),
377384
|this, cx, args| {
378385
this.accept_single_doc_attr(cx, args);
379386
},
380387
)];
388+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
389+
Allow(Target::ExternCrate),
390+
Allow(Target::Use),
391+
Allow(Target::Static),
392+
Allow(Target::Const),
393+
Allow(Target::Fn),
394+
Allow(Target::Mod),
395+
Allow(Target::ForeignMod),
396+
Allow(Target::TyAlias),
397+
Allow(Target::Enum),
398+
Allow(Target::Variant),
399+
Allow(Target::Struct),
400+
Allow(Target::Field),
401+
Allow(Target::Union),
402+
Allow(Target::Trait),
403+
Allow(Target::TraitAlias),
404+
Allow(Target::Impl { of_trait: true }),
405+
Allow(Target::Field { of_trait: true }),
406+
Allow(Target::AssocConst),
407+
Allow(Target::Method(MethodKind::Inherent)),
408+
Allow(Target::Method(MethodKind::Trait { body: true })),
409+
Allow(Target::Method(MethodKind::TraitImpl)),
410+
Allow(Target::AssocTy),
411+
Allow(Target::ForeignFn),
412+
Allow(Target::ForeignStatic),
413+
Allow(Target::ForeignTy),
414+
Allow(Target::MacroDef),
415+
Allow(Target::Crate),
416+
]);
417+
381418

382419
fn finalize(self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
383420
todo!()

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::attr::AttributeExt;
55
use rustc_feature::is_builtin_attr_name;
66
use rustc_hir::RustcVersion;
77
use rustc_hir::limit::Limit;
8-
use rustc_span::{Symbol, sym};
8+
use rustc_span::Symbol;
99

1010
use crate::context::{AcceptContext, Stage};
1111
use crate::parser::{ArgParser, NameValueParser};
@@ -32,7 +32,6 @@ pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
3232
|| attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
3333
}
3434

35-
3635
/// Parse a single integer.
3736
///
3837
/// Used by attributes that take a single integer as argument, such as
@@ -92,7 +91,3 @@ impl<S: Stage> AcceptContext<'_, '_, S> {
9291
None
9392
}
9493
}
95-
96-
pub fn find_crate_name(attrs: &[impl AttributeExt]) -> Option<Symbol> {
97-
first_attr_value_str_by_name(attrs, sym::crate_name)
98-
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -630,23 +630,6 @@ pub(crate) struct DocKeywordConflict {
630630
pub spans: MultiSpan,
631631
}
632632

633-
#[derive(Subdiagnostic)]
634-
pub(crate) enum UnusedNote {
635-
#[note(attr_parsing_unused_empty_lints_note)]
636-
EmptyList { name: Symbol },
637-
#[note(attr_parsing_unused_no_lints_note)]
638-
NoLints { name: Symbol },
639-
}
640-
641-
#[derive(LintDiagnostic)]
642-
#[diag(attr_parsing_unused)]
643-
pub(crate) struct Unused {
644-
#[suggestion(code = "", applicability = "machine-applicable")]
645-
pub attr_span: Span,
646-
#[subdiagnostic]
647-
pub note: UnusedNote,
648-
}
649-
650633
#[derive(Diagnostic)]
651634
#[diag(attr_parsing_link_ordinal_out_of_range)]
652635
#[note]

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ pub enum DocInline {
392392

393393
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
394394
pub struct DocAttribute {
395+
/// `/// doc` or `#[doc = "doc"]`.
396+
pub value: ThinVec<(Symbol, Span)>,
397+
395398
pub aliases: FxIndexMap<Symbol, Span>,
396399
pub hidden: Option<Span>,
397400
pub inline: Option<(DocInline, Span)>,
@@ -417,13 +420,14 @@ pub struct DocAttribute {
417420
pub rust_logo: Option<Span>,
418421

419422
// #[doc(test(...))]
420-
pub test_attrs: ThinVec<()>,
423+
pub test_attrs: ThinVec<AttributeKind>,
421424
pub no_crate_inject: Option<Span>,
422425
}
423426

424427
impl Default for DocAttribute {
425428
fn default() -> Self {
426429
Self {
430+
value: ThinVec::new(),
427431
aliases: FxIndexMap::default(),
428432
hidden: None,
429433
inline: None,

0 commit comments

Comments
 (0)