Skip to content

Commit e79f33c

Browse files
Rollup merge of rust-lang#155078 - cijiugechu:fix/where-clause-trailing-attrs, r=petrochenkov
Reject dangling attributes in where clauses Report `attribute without where predicates` for trailing outer attributes in where clauses. Closes rust-lang#155073 .
2 parents fccfac4 + 3c6cf27 commit e79f33c

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

compiler/rustc_parse/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,14 @@ pub(crate) struct ExpectedStatementAfterOuterAttr {
13041304
pub span: Span,
13051305
}
13061306

1307+
#[derive(Diagnostic)]
1308+
#[diag("attribute without where predicates")]
1309+
pub(crate) struct AttrWithoutWherePredicates {
1310+
#[primary_span]
1311+
#[label("attributes are only permitted when preceding predicates")]
1312+
pub span: Span,
1313+
}
1314+
13071315
#[derive(Diagnostic)]
13081316
#[diag("found a documentation comment that doesn't document anything", code = E0585)]
13091317
#[help("doc comments must come before what they document, if a comment was intended use `//`")]

compiler/rustc_parse/src/parser/generics.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ impl<'a> Parser<'a> {
473473
}
474474
}
475475
} else {
476+
if let [.., last] = &attrs[..] {
477+
if last.is_doc_comment() {
478+
this.dcx().emit_err(errors::DocCommentDoesNotDocumentAnything {
479+
span: last.span,
480+
missing_comma: None,
481+
});
482+
} else {
483+
this.dcx()
484+
.emit_err(errors::AttrWithoutWherePredicates { span: last.span });
485+
}
486+
}
476487
None
477488
};
478489
let predicate = kind.map(|kind| ast::WherePredicate {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/155073>
2+
3+
#![crate_type = "lib"]
4+
#![feature(where_clause_attrs)]
5+
6+
fn f<T>()
7+
where
8+
T: Copy,
9+
#[cfg(true)]
10+
#[cfg(false)]
11+
//~^ ERROR attribute without where predicates
12+
{
13+
}
14+
15+
fn g<T>()
16+
where
17+
T: Copy,
18+
/// dangling
19+
//~^ ERROR found a documentation comment that doesn't document anything
20+
{
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: attribute without where predicates
2+
--> $DIR/where-clause-attrs-without-predicate.rs:10:5
3+
|
4+
LL | #[cfg(false)]
5+
| ^^^^^^^^^^^^^ attributes are only permitted when preceding predicates
6+
7+
error[E0585]: found a documentation comment that doesn't document anything
8+
--> $DIR/where-clause-attrs-without-predicate.rs:18:5
9+
|
10+
LL | /// dangling
11+
| ^^^^^^^^^^^^
12+
|
13+
= help: doc comments must come before what they document, if a comment was intended use `//`
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0585`.

0 commit comments

Comments
 (0)