From 3c6cf27ae423145e25bc9f6d88cd57d54d29e9a9 Mon Sep 17 00:00:00 2001 From: cijiugechu Date: Fri, 10 Apr 2026 21:45:26 +0800 Subject: [PATCH] Reject dangling attributes in where clauses --- compiler/rustc_parse/src/errors.rs | 8 +++++++ compiler/rustc_parse/src/parser/generics.rs | 11 ++++++++++ .../where-clause-attrs-without-predicate.rs | 21 +++++++++++++++++++ ...here-clause-attrs-without-predicate.stderr | 17 +++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 tests/ui/parser/where-clause-attrs-without-predicate.rs create mode 100644 tests/ui/parser/where-clause-attrs-without-predicate.stderr diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 6faaafcc0100..cc1e0ff85dae 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -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 `//`")] diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index 8c02092fd678..969c8548f68b 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -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 { diff --git a/tests/ui/parser/where-clause-attrs-without-predicate.rs b/tests/ui/parser/where-clause-attrs-without-predicate.rs new file mode 100644 index 000000000000..367a4dcd4d34 --- /dev/null +++ b/tests/ui/parser/where-clause-attrs-without-predicate.rs @@ -0,0 +1,21 @@ +// Regression test for + +#![crate_type = "lib"] +#![feature(where_clause_attrs)] + +fn f() +where + T: Copy, + #[cfg(true)] + #[cfg(false)] + //~^ ERROR attribute without where predicates +{ +} + +fn g() +where + T: Copy, + /// dangling + //~^ ERROR found a documentation comment that doesn't document anything +{ +} diff --git a/tests/ui/parser/where-clause-attrs-without-predicate.stderr b/tests/ui/parser/where-clause-attrs-without-predicate.stderr new file mode 100644 index 000000000000..c4914238a5db --- /dev/null +++ b/tests/ui/parser/where-clause-attrs-without-predicate.stderr @@ -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`.