Skip to content

Commit 15a7cac

Browse files
Rollup merge of rust-lang#155864 - JonathanBrouwer:doc-attr-span, r=GuillaumeGomez
Fix panic for doc attributes on where predicates Fixes rust-lang#155804 r? @GuillaumeGomez
2 parents 6779e67 + 75fa098 commit 15a7cac

8 files changed

Lines changed: 85 additions & 8 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ impl DocParser {
701701
for i in items.mixed() {
702702
match i {
703703
MetaItemOrLitParser::MetaItemParser(mip) => {
704+
if self.nb_doc_attrs == 0 {
705+
self.attribute.first_span = cx.attr_span;
706+
}
704707
self.nb_doc_attrs += 1;
705708
self.parse_single_doc_attr_item(cx, mip);
706709
}

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,16 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
292292
// in where clauses. After that, this function would become useless.
293293
let spans = attrs
294294
.into_iter()
295-
// FIXME: We shouldn't need to special-case `doc`!
296-
.filter(|attr| {
297-
matches!(
298-
attr,
299-
Attribute::Parsed(AttributeKind::DocComment { .. } | AttributeKind::Doc(_))
300-
| Attribute::Unparsed(_)
301-
)
295+
.filter_map(|attr| {
296+
match attr {
297+
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => Some(*span),
298+
// FIXME: We shouldn't need to special-case `doc`!
299+
Attribute::Parsed(AttributeKind::Doc(attr)) => Some(attr.first_span),
300+
// Checked during attribute parsing target checking
301+
Attribute::Parsed(_) => None,
302+
Attribute::Unparsed(attr) => Some(attr.span),
303+
}
302304
})
303-
.map(|attr| attr.span())
304305
.collect::<Vec<_>>();
305306
if !spans.is_empty() {
306307
self.dcx()

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@ pub struct CfgHideShow {
544544

545545
#[derive(Clone, Debug, Default, HashStable_Generic, Decodable, PrintAttribute)]
546546
pub struct DocAttribute {
547+
pub first_span: Span,
548+
547549
pub aliases: FxIndexMap<Symbol, Span>,
548550
pub hidden: Option<Span>,
549551
// Because we need to emit the error if there is more than one `inline` attribute on an item
@@ -581,6 +583,7 @@ pub struct DocAttribute {
581583
impl<E: rustc_span::SpanEncoder> rustc_serialize::Encodable<E> for DocAttribute {
582584
fn encode(&self, encoder: &mut E) {
583585
let DocAttribute {
586+
first_span,
584587
aliases,
585588
hidden,
586589
inline,
@@ -603,6 +606,7 @@ impl<E: rustc_span::SpanEncoder> rustc_serialize::Encodable<E> for DocAttribute
603606
test_attrs,
604607
no_crate_inject,
605608
} = self;
609+
rustc_serialize::Encodable::<E>::encode(first_span, encoder);
606610
rustc_serialize::Encodable::<E>::encode(aliases, encoder);
607611
rustc_serialize::Encodable::<E>::encode(hidden, encoder);
608612

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
10271027
/// [`check_doc_inline`]: Self::check_doc_inline
10281028
fn check_doc_attrs(&self, attr: &DocAttribute, hir_id: HirId, target: Target) {
10291029
let DocAttribute {
1030+
first_span: _,
10301031
aliases,
10311032
// valid pretty much anywhere, not checked here?
10321033
// FIXME: should we?

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,7 @@ fn add_without_unwanted_attributes<'hir>(
27802780
hir::Attribute::Parsed(AttributeKind::Doc(box d)) => {
27812781
// Remove attributes from `normal` that should not be inherited by `use` re-export.
27822782
let DocAttribute {
2783+
first_span: _,
27832784
aliases,
27842785
hidden,
27852786
inline,

src/librustdoc/json/conversions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ fn maybe_from_hir_attr(attr: &hir::Attribute, item_id: ItemId, tcx: TyCtxt<'_>)
960960
}
961961

962962
let DocAttribute {
963+
first_span: _,
963964
aliases,
964965
hidden,
965966
inline,

tests/ui/attributes/where-doc.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(where_clause_attrs)]
2+
#![allow(invalid_doc_attributes)]
3+
4+
fn test()
5+
where
6+
#[doc(alias = ":(")]
7+
//~^ ERROR most attributes are not supported in `where` clauses
8+
//~| ERROR `#[doc(alias = "...")]` isn't allowed on where predicate
9+
():,
10+
11+
#[doc(hidden)]
12+
//~^ ERROR most attributes are not supported in `where` clauses
13+
():,
14+
15+
#[doc = ""]
16+
//~^ ERROR most attributes are not supported in `where` clauses
17+
():,
18+
19+
// == That the doc attributes below don't trigger the error is a bug
20+
#[doc()]
21+
():,
22+
23+
#[doc(5)]
24+
():,
25+
26+
#[doc]
27+
():,
28+
29+
#[doc = 5]
30+
():,
31+
32+
{ }
33+
34+
fn main() {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: most attributes are not supported in `where` clauses
2+
--> $DIR/where-doc.rs:6:1
3+
|
4+
LL | #[doc(alias = ":(")]
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: only `#[cfg]` and `#[cfg_attr]` are supported
8+
9+
error: most attributes are not supported in `where` clauses
10+
--> $DIR/where-doc.rs:11:1
11+
|
12+
LL | #[doc(hidden)]
13+
| ^^^^^^^^^^^^^^
14+
|
15+
= help: only `#[cfg]` and `#[cfg_attr]` are supported
16+
17+
error: most attributes are not supported in `where` clauses
18+
--> $DIR/where-doc.rs:15:1
19+
|
20+
LL | #[doc = ""]
21+
| ^^^^^^^^^^^
22+
|
23+
= help: only `#[cfg]` and `#[cfg_attr]` are supported
24+
25+
error: `#[doc(alias = "...")]` isn't allowed on where predicate
26+
--> $DIR/where-doc.rs:6:15
27+
|
28+
LL | #[doc(alias = ":(")]
29+
| ^^^^
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)