Reject dangling attributes in where clauses

This commit is contained in:
cijiugechu
2026-04-10 21:45:26 +08:00
parent 1948ee19e9
commit 3c6cf27ae4
4 changed files with 57 additions and 0 deletions
+8
View File
@@ -1304,6 +1304,14 @@ pub(crate) struct ExpectedStatementAfterOuterAttr {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("attribute without where predicates")]
pub(crate) struct AttrWithoutWherePredicates {
#[primary_span]
#[label("attributes are only permitted when preceding predicates")]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag("found a documentation comment that doesn't document anything", code = E0585)]
#[help("doc comments must come before what they document, if a comment was intended use `//`")]
@@ -473,6 +473,17 @@ fn parse_where_clause_common(
}
}
} else {
if let [.., last] = &attrs[..] {
if last.is_doc_comment() {
this.dcx().emit_err(errors::DocCommentDoesNotDocumentAnything {
span: last.span,
missing_comma: None,
});
} else {
this.dcx()
.emit_err(errors::AttrWithoutWherePredicates { span: last.span });
}
}
None
};
let predicate = kind.map(|kind| ast::WherePredicate {
@@ -0,0 +1,21 @@
// Regression test for <https://github.com/rust-lang/rust/issues/155073>
#![crate_type = "lib"]
#![feature(where_clause_attrs)]
fn f<T>()
where
T: Copy,
#[cfg(true)]
#[cfg(false)]
//~^ ERROR attribute without where predicates
{
}
fn g<T>()
where
T: Copy,
/// dangling
//~^ ERROR found a documentation comment that doesn't document anything
{
}
@@ -0,0 +1,17 @@
error: attribute without where predicates
--> $DIR/where-clause-attrs-without-predicate.rs:10:5
|
LL | #[cfg(false)]
| ^^^^^^^^^^^^^ attributes are only permitted when preceding predicates
error[E0585]: found a documentation comment that doesn't document anything
--> $DIR/where-clause-attrs-without-predicate.rs:18:5
|
LL | /// dangling
| ^^^^^^^^^^^^
|
= help: doc comments must come before what they document, if a comment was intended use `//`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0585`.