Skip to content

Commit 5d9c713

Browse files
committed
Added checks for attribute in type case
1 parent f63685d commit 5d9c713

11 files changed

+181
-19
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ parse_attr_without_generics = attribute without generic parameters
7171
parse_attribute_on_param_type = attributes cannot be applied to a function parameter's type
7272
.label = attributes are not allowed here
7373
74+
parse_attribute_on_type = attributes cannot be applied to a type
75+
.label = attributes are not allowed here
76+
77+
parse_attribute_on_generic_type = attributes cannot be applied to generic type arguments
78+
.label = attributes are not allowed here
79+
7480
parse_bad_assoc_type_bounds = bounds on associated types do not belong here
7581
.label = belongs in `where` clause
7682

compiler/rustc_parse/src/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,22 @@ pub(crate) struct AttributeOnParamType {
14891489
pub span: Span,
14901490
}
14911491

1492+
#[derive(Diagnostic)]
1493+
#[diag(parse_attribute_on_type)]
1494+
pub(crate) struct AttributeOnType {
1495+
#[primary_span]
1496+
#[label]
1497+
pub span: Span,
1498+
}
1499+
1500+
#[derive(Diagnostic)]
1501+
#[diag(parse_attribute_on_generic_type)]
1502+
pub(crate) struct AttributeOnGenericType {
1503+
#[primary_span]
1504+
#[label]
1505+
pub span: Span,
1506+
}
1507+
14921508
#[derive(Diagnostic)]
14931509
#[diag(parse_pattern_method_param_without_body, code = E0642)]
14941510
pub(crate) struct PatternMethodParamWithoutBody {

compiler/rustc_parse/src/parser/path.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
1717
use super::{Parser, Restrictions, TokenType};
1818
use crate::ast::{PatKind, TyKind};
1919
use crate::errors::{
20-
self, FnPathFoundNamedParams, PathFoundAttributeInParams, PathFoundCVariadicParams,
21-
PathSingleColon, PathTripleColon,
20+
self, AttributeOnGenericType, FnPathFoundNamedParams, PathFoundAttributeInParams,
21+
PathFoundCVariadicParams, PathSingleColon, PathTripleColon,
2222
};
2323
use crate::exp;
2424
use crate::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
@@ -880,6 +880,21 @@ impl<'a> Parser<'a> {
880880
&mut self,
881881
ty_generics: Option<&Generics>,
882882
) -> PResult<'a, Option<GenericArg>> {
883+
let attrs_wrapper = self.parse_outer_attributes()?;
884+
885+
if !attrs_wrapper.is_empty() {
886+
// Parse attribute.
887+
let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
888+
889+
let attr_span = raw_attrs[0].span.to(raw_attrs.last().unwrap().span);
890+
891+
let ty_after_attr = self.parse_ty()?;
892+
let full_span_for_err = attr_span.to(ty_after_attr.span);
893+
894+
let guar = self.dcx().emit_err(AttributeOnGenericType { span: attr_span });
895+
896+
return Ok(Some(GenericArg::Type(self.mk_ty(full_span_for_err, TyKind::Err(guar)))));
897+
}
883898
let start = self.token.span;
884899
let arg = if self.check_lifetime() && self.look_ahead(1, |t| !t.is_like_plus()) {
885900
// Parse lifetime argument.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use thin_vec::{ThinVec, thin_vec};
1414

1515
use super::{Parser, PathStyle, SeqSep, TokenType, Trailing};
1616
use crate::errors::{
17-
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
18-
FnPtrWithGenerics, FnPtrWithGenericsSugg, HelpUseLatestEdition, InvalidDynKeyword,
19-
LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType,
20-
ReturnTypesUseThinArrow,
17+
self, AttributeOnType, DynAfterMut, ExpectedFnPathFoundFnKeyword,
18+
ExpectedMutOrConstInRawPointerType, FnPtrWithGenerics, FnPtrWithGenericsSugg,
19+
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
20+
NestedCVariadicType, ReturnTypesUseThinArrow,
2121
};
2222
use crate::parser::item::FrontMatterParsingMode;
2323
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
@@ -254,6 +254,20 @@ impl<'a> Parser<'a> {
254254
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
255255
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
256256

257+
let attrs_wrapper = self.parse_outer_attributes()?;
258+
if !attrs_wrapper.is_empty() {
259+
let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
260+
261+
let attr_span = raw_attrs[0].span.to(raw_attrs.last().unwrap().span);
262+
263+
let ty_after_attr = self.parse_ty()?;
264+
let full_span_for_err = attr_span.to(ty_after_attr.span);
265+
266+
let guar = self.dcx().emit_err(AttributeOnType { span: attr_span });
267+
268+
return Ok(self.mk_ty(full_span_for_err, TyKind::Err(guar)));
269+
}
270+
257271
if let Some(ty) = self.eat_metavar_seq_with_matcher(
258272
|mv_kind| matches!(mv_kind, MetaVarKind::Ty { .. }),
259273
|this| this.parse_ty_no_question_mark_recover(),

tests/ui/parser/attribute-on-type.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Regression test for: <https://github.com/rust-lang/rust/issues/144132>
2+
//! <https://github.com/rust-lang/rust/issues/135017>
3+
4+
struct Foo<T>(T);
5+
6+
fn main() {
7+
let foo: Foo<#[cfg(not(wrong))] String> = todo!();
8+
//~^ ERROR attributes cannot be applied to generic type arguments
9+
10+
let _generic: Box<#[attr] i32> = Box::new(1);
11+
//~^ ERROR attributes cannot be applied to generic type arguments
12+
13+
let _assignment: #[attr] i32 = Box::new(1);
14+
//~^ ERROR attributes cannot be applied to a type
15+
16+
let _complex: Vec<#[derive(Debug)] String> = vec![];
17+
//~^ ERROR attributes cannot be applied to generic type arguments
18+
19+
let _nested: Box<Vec<#[cfg(feature = "test")] u64>> = Box::new(vec![]);
20+
//~^ ERROR attributes cannot be applied to generic type arguments
21+
}
22+
23+
fn f(_param: #[attr] i32) {}
24+
//~^ ERROR attributes cannot be applied to a function parameter's type
25+
26+
fn g() -> #[attr] i32 { 0 }
27+
//~^ ERROR attributes cannot be applied to a type
28+
29+
struct S {
30+
field: #[attr] i32,
31+
//~^ ERROR attributes cannot be applied to a type
32+
field1: (#[attr] i32, i32),
33+
//~^ ERROR attributes cannot be applied to a type
34+
}
35+
36+
type Tuple = (#[attr] i32, String);
37+
//~^ ERROR attributes cannot be applied to a type
38+
39+
impl #[attr] S {}
40+
//~^ ERROR attributes cannot be applied to a type
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
error: attributes cannot be applied to generic type arguments
2+
--> $DIR/attribute-on-type.rs:7:18
3+
|
4+
LL | let foo: Foo<#[cfg(not(wrong))] String> = todo!();
5+
| ^^^^^^^^^^^^^^^^^^ attributes are not allowed here
6+
7+
error: attributes cannot be applied to generic type arguments
8+
--> $DIR/attribute-on-type.rs:10:23
9+
|
10+
LL | let _generic: Box<#[attr] i32> = Box::new(1);
11+
| ^^^^^^^ attributes are not allowed here
12+
13+
error: attributes cannot be applied to a type
14+
--> $DIR/attribute-on-type.rs:13:22
15+
|
16+
LL | let _assignment: #[attr] i32 = Box::new(1);
17+
| ^^^^^^^ attributes are not allowed here
18+
19+
error: attributes cannot be applied to generic type arguments
20+
--> $DIR/attribute-on-type.rs:16:23
21+
|
22+
LL | let _complex: Vec<#[derive(Debug)] String> = vec![];
23+
| ^^^^^^^^^^^^^^^^ attributes are not allowed here
24+
25+
error: attributes cannot be applied to generic type arguments
26+
--> $DIR/attribute-on-type.rs:19:26
27+
|
28+
LL | let _nested: Box<Vec<#[cfg(feature = "test")] u64>> = Box::new(vec![]);
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^ attributes are not allowed here
30+
31+
error: attributes cannot be applied to a function parameter's type
32+
--> $DIR/attribute-on-type.rs:23:14
33+
|
34+
LL | fn f(_param: #[attr] i32) {}
35+
| ^^^^^^^ attributes are not allowed here
36+
37+
error: attributes cannot be applied to a type
38+
--> $DIR/attribute-on-type.rs:26:11
39+
|
40+
LL | fn g() -> #[attr] i32 { 0 }
41+
| ^^^^^^^ attributes are not allowed here
42+
43+
error: attributes cannot be applied to a type
44+
--> $DIR/attribute-on-type.rs:30:12
45+
|
46+
LL | field: #[attr] i32,
47+
| ^^^^^^^ attributes are not allowed here
48+
49+
error: attributes cannot be applied to a type
50+
--> $DIR/attribute-on-type.rs:32:14
51+
|
52+
LL | field1: (#[attr] i32, i32),
53+
| ^^^^^^^ attributes are not allowed here
54+
55+
error: attributes cannot be applied to a type
56+
--> $DIR/attribute-on-type.rs:36:15
57+
|
58+
LL | type Tuple = (#[attr] i32, String);
59+
| ^^^^^^^ attributes are not allowed here
60+
61+
error: attributes cannot be applied to a type
62+
--> $DIR/attribute-on-type.rs:39:6
63+
|
64+
LL | impl #[attr] S {}
65+
| ^^^^^^^ attributes are not allowed here
66+
67+
error: aborting due to 11 previous errors
68+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
22
x::<#[a]y::<z>>
3-
//~^ ERROR invalid const generic expression
3+
//~^ ERROR attributes cannot be applied to generic type arguments
44
//~| ERROR cannot find value `x` in this scope
55
}

tests/ui/parser/issues/issue-103143.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
error: invalid const generic expression
2-
--> $DIR/issue-103143.rs:2:13
1+
error: attributes cannot be applied to generic type arguments
2+
--> $DIR/issue-103143.rs:2:9
33
|
44
LL | x::<#[a]y::<z>>
5-
| ^^^^^^
6-
|
7-
help: expressions must be enclosed in braces to be used as const generic arguments
8-
|
9-
LL | x::<#[a]{ y::<z> }>
10-
| + +
5+
| ^^^^ attributes are not allowed here
116

127
error[E0425]: cannot find value `x` in this scope
138
--> $DIR/issue-103143.rs:2:5

tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
pub struct Example(#[allow(dead_code)] usize)
55
where
66
(): Sized;
7-
//~^^^ ERROR where clauses are not allowed before tuple struct bodies
7+
//~^ ERROR attributes cannot be applied to a type
8+
//~^^^^ ERROR where clauses are not allowed before tuple struct bodies
89

910
struct _Demo(pub usize, usize)
1011
where

tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub struct Example
55
where
66
(): Sized,
77
(#[allow(dead_code)] usize);
8-
//~^^^ ERROR where clauses are not allowed before tuple struct bodies
8+
//~^ ERROR attributes cannot be applied to a type
9+
//~^^^^ ERROR where clauses are not allowed before tuple struct bodies
910

1011
struct _Demo
1112
where

0 commit comments

Comments
 (0)