diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 859118a4adee5..7059ffbf375f7 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -71,6 +71,17 @@ parse_attr_without_generics = attribute without generic parameters parse_attribute_on_param_type = attributes cannot be applied to a function parameter's type .label = attributes are not allowed here +parse_attribute_on_type = attributes cannot be applied to types + .label = attributes are not allowed here + .suggestion = remove attribute from here + +parse_attribute_on_generic_arg = attributes cannot be applied to generic arguments + .label = attributes are not allowed here + .suggestion = remove attribute from here + +parse_attribute_on_empty_type = attributes cannot be applied here + .label = attributes are not allowed here + parse_bad_assoc_type_bounds = bounds on associated types do not belong here .label = belongs in `where` clause diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 4aaaba01faeb3..48ff0394d463b 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1489,6 +1489,34 @@ pub(crate) struct AttributeOnParamType { pub span: Span, } +#[derive(Diagnostic)] +#[diag(parse_attribute_on_type)] +pub(crate) struct AttributeOnType { + #[primary_span] + #[label] + pub span: Span, + #[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")] + pub fix_span: Span, +} + +#[derive(Diagnostic)] +#[diag(parse_attribute_on_generic_arg)] +pub(crate) struct AttributeOnGenericArg { + #[primary_span] + #[label] + pub span: Span, + #[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")] + pub fix_span: Span, +} + +#[derive(Diagnostic)] +#[diag(parse_attribute_on_empty_type)] +pub(crate) struct AttributeOnEmptyType { + #[primary_span] + #[label] + pub span: Span, +} + #[derive(Diagnostic)] #[diag(parse_pattern_method_param_without_body, code = E0642)] pub(crate) struct PatternMethodParamWithoutBody { diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 1f4049f197fa8..8e65ab99c5e65 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -17,11 +17,11 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::{Parser, Restrictions, TokenType}; use crate::ast::{PatKind, TyKind}; use crate::errors::{ - self, FnPathFoundNamedParams, PathFoundAttributeInParams, PathFoundCVariadicParams, - PathSingleColon, PathTripleColon, + self, AttributeOnEmptyType, AttributeOnGenericArg, FnPathFoundNamedParams, + PathFoundAttributeInParams, PathFoundCVariadicParams, PathSingleColon, PathTripleColon, }; use crate::exp; -use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma}; +use crate::parser::{CommaRecoveryMode, ExprKind, RecoverColon, RecoverComma}; /// Specifies how to parse a path. #[derive(Copy, Clone, PartialEq)] @@ -880,6 +880,12 @@ impl<'a> Parser<'a> { &mut self, ty_generics: Option<&Generics>, ) -> PResult<'a, Option> { + let mut attr_span: Option = None; + if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) { + let attrs_wrapper = self.parse_outer_attributes()?; + let raw_attrs = attrs_wrapper.take_for_recovery(self.psess); + attr_span = Some(raw_attrs[0].span.to(raw_attrs.last().unwrap().span)); + } let start = self.token.span; let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) { // Parse lifetime argument. @@ -934,6 +940,9 @@ impl<'a> Parser<'a> { } } else if self.token.is_keyword(kw::Const) { return self.recover_const_param_declaration(ty_generics); + } else if let Some(attr_span) = attr_span { + let diag = self.dcx().create_err(AttributeOnEmptyType { span: attr_span }); + return Err(diag); } else { // Fall back by trying to parse a const-expr expression. If we successfully do so, // then we should report an error that it needs to be wrapped in braces. @@ -953,6 +962,22 @@ impl<'a> Parser<'a> { } } }; + + if let Some(attr_span) = attr_span { + let guar = self.dcx().emit_err(AttributeOnGenericArg { + span: attr_span, + fix_span: attr_span.until(arg.span()), + }); + return Ok(Some(match arg { + GenericArg::Type(_) => GenericArg::Type(self.mk_ty(attr_span, TyKind::Err(guar))), + GenericArg::Const(_) => { + let error_expr = self.mk_expr(attr_span, ExprKind::Err(guar)); + GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value: error_expr }) + } + GenericArg::Lifetime(lt) => GenericArg::Lifetime(lt), + })); + } + Ok(Some(arg)) } diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 740dd10ea8ba1..59048e42e6f2b 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -14,10 +14,10 @@ use thin_vec::{ThinVec, thin_vec}; use super::{Parser, PathStyle, SeqSep, TokenType, Trailing}; use crate::errors::{ - self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType, - FnPtrWithGenerics, FnPtrWithGenericsSugg, HelpUseLatestEdition, InvalidDynKeyword, - LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType, - ReturnTypesUseThinArrow, + self, AttributeOnEmptyType, AttributeOnType, DynAfterMut, ExpectedFnPathFoundFnKeyword, + ExpectedMutOrConstInRawPointerType, FnPtrWithGenerics, FnPtrWithGenericsSugg, + HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, + NestedCVariadicType, ReturnTypesUseThinArrow, }; use crate::parser::item::FrontMatterParsingMode; use crate::{exp, maybe_recover_from_interpolated_ty_qpath}; @@ -253,7 +253,27 @@ impl<'a> Parser<'a> { ) -> PResult<'a, P> { let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes; maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery); + if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) { + let attrs_wrapper = self.parse_outer_attributes()?; + let raw_attrs = attrs_wrapper.take_for_recovery(self.psess); + let attr_span = raw_attrs[0].span.to(raw_attrs.last().unwrap().span); + let (full_span, guar) = match self.parse_ty() { + Ok(ty) => { + let full_span = attr_span.until(ty.span); + let guar = self + .dcx() + .emit_err(AttributeOnType { span: attr_span, fix_span: full_span }); + (attr_span, guar) + } + Err(err) => { + err.cancel(); + let guar = self.dcx().emit_err(AttributeOnEmptyType { span: attr_span }); + (attr_span, guar) + } + }; + return Ok(self.mk_ty(full_span, TyKind::Err(guar))); + } if let Some(ty) = self.eat_metavar_seq_with_matcher( |mv_kind| matches!(mv_kind, MetaVarKind::Ty { .. }), |this| this.parse_ty_no_question_mark_recover(), diff --git a/tests/ui/parser/attribute-on-empty.rs b/tests/ui/parser/attribute-on-empty.rs new file mode 100644 index 0000000000000..5932377f73eca --- /dev/null +++ b/tests/ui/parser/attribute-on-empty.rs @@ -0,0 +1,29 @@ +//! Regression test for: +//! + +struct Baz(i32); + +fn main() { + let _: Baz<#[cfg(any())]> = todo!(); + //~^ ERROR attributes cannot be applied here +} + +fn f(_param: #[attr]) {} +//~^ ERROR attributes cannot be applied to a function parameter's type +//~| ERROR expected type, found `)` + +fn g() -> #[attr] { 0 } +//~^ ERROR attributes cannot be applied here + +struct S { + field: #[attr], + //~^ ERROR attributes cannot be applied here + field1: (#[attr], i32), + //~^ ERROR attributes cannot be applied here +} + +type Tuple = (#[attr], String); +//~^ ERROR attributes cannot be applied here + +impl #[attr] {} +//~^ ERROR attributes cannot be applied here diff --git a/tests/ui/parser/attribute-on-empty.stderr b/tests/ui/parser/attribute-on-empty.stderr new file mode 100644 index 0000000000000..7c4806c8704aa --- /dev/null +++ b/tests/ui/parser/attribute-on-empty.stderr @@ -0,0 +1,52 @@ +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:7:16 + | +LL | let _: Baz<#[cfg(any())]> = todo!(); + | - ^^^^^^^^^^^^^ attributes are not allowed here + | | + | while parsing the type for `_` + +error: attributes cannot be applied to a function parameter's type + --> $DIR/attribute-on-empty.rs:11:14 + | +LL | fn f(_param: #[attr]) {} + | ^^^^^^^ attributes are not allowed here + +error: expected type, found `)` + --> $DIR/attribute-on-empty.rs:11:21 + | +LL | fn f(_param: #[attr]) {} + | ^ expected type + +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:15:11 + | +LL | fn g() -> #[attr] { 0 } + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:19:12 + | +LL | field: #[attr], + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:21:14 + | +LL | field1: (#[attr], i32), + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:25:15 + | +LL | type Tuple = (#[attr], String); + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied here + --> $DIR/attribute-on-empty.rs:28:6 + | +LL | impl #[attr] {} + | ^^^^^^^ attributes are not allowed here + +error: aborting due to 8 previous errors + diff --git a/tests/ui/parser/attribute-on-type.fixed b/tests/ui/parser/attribute-on-type.fixed new file mode 100644 index 0000000000000..5024bfdc2bce9 --- /dev/null +++ b/tests/ui/parser/attribute-on-type.fixed @@ -0,0 +1,58 @@ +//! Regression test for: +//! + +//@ run-rustfix + +#![allow(dead_code, unused_variables)] + +struct Foo(T); +struct Bar<'a>(&'a i32); +struct Baz(i32); + +fn main() { + let foo: Foo = Foo(2i32); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: &'static str = "123"; + //~^ ERROR attributes cannot be applied to types + + let _: Bar<'static> = Bar(&123); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Baz<42> = Baz(42); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Foo = Foo(String::new()); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Bar<'static> = Bar(&456); + //~^ ERROR attributes cannot be applied to generic arguments + + let _generic: Box = Box::new(1); + //~^ ERROR attributes cannot be applied to generic arguments + + let _assignment: i32 = *Box::new(1); + //~^ ERROR attributes cannot be applied to types + + let _complex: Vec = vec![]; + //~^ ERROR attributes cannot be applied to generic arguments + + let _nested: Box> = Box::new(vec![]); + //~^ ERROR attributes cannot be applied to generic arguments +} + +fn g() -> i32 { 0 } +//~^ ERROR attributes cannot be applied to types + +struct S { + field: i32, + //~^ ERROR attributes cannot be applied to types + field1: (i32, i32), + //~^ ERROR attributes cannot be applied to types +} + +type Tuple = (i32, String); +//~^ ERROR attributes cannot be applied to types + +impl S {} +//~^ ERROR attributes cannot be applied to types diff --git a/tests/ui/parser/attribute-on-type.rs b/tests/ui/parser/attribute-on-type.rs new file mode 100644 index 0000000000000..196d322bdf8f3 --- /dev/null +++ b/tests/ui/parser/attribute-on-type.rs @@ -0,0 +1,58 @@ +//! Regression test for: +//! + +//@ run-rustfix + +#![allow(dead_code, unused_variables)] + +struct Foo(T); +struct Bar<'a>(&'a i32); +struct Baz(i32); + +fn main() { + let foo: Foo<#[cfg(not(wrong))] i32> = Foo(2i32); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: #[attr] &'static str = "123"; + //~^ ERROR attributes cannot be applied to types + + let _: Bar<#[cfg(any())] 'static> = Bar(&123); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Baz<#[cfg(any())] 42> = Baz(42); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Foo<#[cfg(not(wrong))]String> = Foo(String::new()); + //~^ ERROR attributes cannot be applied to generic arguments + + let _: Bar<#[cfg(any())] 'static> = Bar(&456); + //~^ ERROR attributes cannot be applied to generic arguments + + let _generic: Box<#[attr] i32> = Box::new(1); + //~^ ERROR attributes cannot be applied to generic arguments + + let _assignment: #[attr] i32 = *Box::new(1); + //~^ ERROR attributes cannot be applied to types + + let _complex: Vec<#[derive(Debug)] String> = vec![]; + //~^ ERROR attributes cannot be applied to generic arguments + + let _nested: Box> = Box::new(vec![]); + //~^ ERROR attributes cannot be applied to generic arguments +} + +fn g() -> #[attr] i32 { 0 } +//~^ ERROR attributes cannot be applied to types + +struct S { + field: #[attr] i32, + //~^ ERROR attributes cannot be applied to types + field1: (#[attr] i32, i32), + //~^ ERROR attributes cannot be applied to types +} + +type Tuple = (#[attr] i32, String); +//~^ ERROR attributes cannot be applied to types + +impl #[attr] S {} +//~^ ERROR attributes cannot be applied to types diff --git a/tests/ui/parser/attribute-on-type.stderr b/tests/ui/parser/attribute-on-type.stderr new file mode 100644 index 0000000000000..603c7e2be51a0 --- /dev/null +++ b/tests/ui/parser/attribute-on-type.stderr @@ -0,0 +1,92 @@ +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:13:18 + | +LL | let foo: Foo<#[cfg(not(wrong))] i32> = Foo(2i32); + | ^^^^^^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:16:12 + | +LL | let _: #[attr] &'static str = "123"; + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:19:16 + | +LL | let _: Bar<#[cfg(any())] 'static> = Bar(&123); + | ^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:22:16 + | +LL | let _: Baz<#[cfg(any())] 42> = Baz(42); + | ^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:25:16 + | +LL | let _: Foo<#[cfg(not(wrong))]String> = Foo(String::new()); + | ^^^^^^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:28:16 + | +LL | let _: Bar<#[cfg(any())] 'static> = Bar(&456); + | ^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:31:23 + | +LL | let _generic: Box<#[attr] i32> = Box::new(1); + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:34:22 + | +LL | let _assignment: #[attr] i32 = *Box::new(1); + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:37:23 + | +LL | let _complex: Vec<#[derive(Debug)] String> = vec![]; + | ^^^^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to generic arguments + --> $DIR/attribute-on-type.rs:40:26 + | +LL | let _nested: Box> = Box::new(vec![]); + | ^^^^^^^^^^^^^^^^^^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:44:11 + | +LL | fn g() -> #[attr] i32 { 0 } + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:48:12 + | +LL | field: #[attr] i32, + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:50:14 + | +LL | field1: (#[attr] i32, i32), + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:54:15 + | +LL | type Tuple = (#[attr] i32, String); + | ^^^^^^^ attributes are not allowed here + +error: attributes cannot be applied to types + --> $DIR/attribute-on-type.rs:57:6 + | +LL | impl #[attr] S {} + | ^^^^^^^ attributes are not allowed here + +error: aborting due to 15 previous errors + diff --git a/tests/ui/parser/issues/issue-103143.rs b/tests/ui/parser/issues/issue-103143.rs index a584274c40514..90f10fc1a089c 100644 --- a/tests/ui/parser/issues/issue-103143.rs +++ b/tests/ui/parser/issues/issue-103143.rs @@ -1,5 +1,5 @@ fn main() { x::<#[a]y::> - //~^ ERROR invalid const generic expression + //~^ ERROR attributes cannot be applied to generic arguments //~| ERROR cannot find value `x` in this scope } diff --git a/tests/ui/parser/issues/issue-103143.stderr b/tests/ui/parser/issues/issue-103143.stderr index 4035c69afa712..168a2077396c7 100644 --- a/tests/ui/parser/issues/issue-103143.stderr +++ b/tests/ui/parser/issues/issue-103143.stderr @@ -1,13 +1,8 @@ -error: invalid const generic expression - --> $DIR/issue-103143.rs:2:13 +error: attributes cannot be applied to generic arguments + --> $DIR/issue-103143.rs:2:9 | LL | x::<#[a]y::> - | ^^^^^^ - | -help: expressions must be enclosed in braces to be used as const generic arguments - | -LL | x::<#[a]{ y:: }> - | + + + | ^^^^ attributes are not allowed here error[E0425]: cannot find value `x` in this scope --> $DIR/issue-103143.rs:2:5 diff --git a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed index a851300a9828e..40028307a8cb1 100644 --- a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed +++ b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed @@ -1,7 +1,9 @@ // Regression test for issues #100790 and #106439. //@ run-rustfix -pub struct Example(#[allow(dead_code)] usize) +#![allow(dead_code)] + +pub struct Example(usize) where (): Sized; //~^^^ ERROR where clauses are not allowed before tuple struct bodies diff --git a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs index 10f435859f15a..d8dbb4238d206 100644 --- a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs +++ b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs @@ -1,10 +1,12 @@ // Regression test for issues #100790 and #106439. //@ run-rustfix +#![allow(dead_code)] + pub struct Example where (): Sized, -(#[allow(dead_code)] usize); +(usize); //~^^^ ERROR where clauses are not allowed before tuple struct bodies struct _Demo diff --git a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr index ddbf237e8662b..66dadd9fd4c24 100644 --- a/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr +++ b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr @@ -1,23 +1,23 @@ error: where clauses are not allowed before tuple struct bodies - --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:5:1 + --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:7:1 | LL | pub struct Example | ------- while parsing this tuple struct LL | / where LL | | (): Sized, | |______________^ unexpected where clause -LL | (#[allow(dead_code)] usize); - | --------------------------- the struct body +LL | (usize); + | ------- the struct body | help: move the body before the where clause | -LL ~ pub struct Example(#[allow(dead_code)] usize) +LL ~ pub struct Example(usize) LL | where LL ~ (): Sized; | error: where clauses are not allowed before tuple struct bodies - --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:11:1 + --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:13:1 | LL | struct _Demo | ----- while parsing this tuple struct