Skip to content

Commit 7aef596

Browse files
Clean up parse_repr_align function of ReprParser
1 parent e6b237a commit 7aef596

14 files changed

Lines changed: 100 additions & 139 deletions

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ fn parse_repr(cx: &mut AcceptContext<'_, '_>, param: &MetaItemParser) -> Option<
7070
match param.path().word_sym() {
7171
Some(sym::align) => {
7272
let l = cx.expect_list(param.args(), param.span())?;
73-
parse_repr_align(cx, l, param.span(), AlignKind::Align)
73+
parse_repr_align(cx, l, AlignKind::Align)
7474
}
7575
Some(sym::packed) => match param.args() {
7676
ArgParser::NoArgs => Some(ReprPacked(Align::ONE)),
77-
ArgParser::List(l) => parse_repr_align(cx, l, param.span(), AlignKind::Packed),
77+
ArgParser::List(l) => parse_repr_align(cx, l, AlignKind::Packed),
7878
ArgParser::NameValue(_) => {
7979
cx.adcx().expected_list_or_no_args(param.span());
8080
None
@@ -131,44 +131,17 @@ enum AlignKind {
131131
}
132132

133133
fn parse_repr_align(
134-
cx: &AcceptContext<'_, '_>,
134+
cx: &mut AcceptContext<'_, '_>,
135135
list: &MetaItemListParser,
136-
param_span: Span,
137136
align_kind: AlignKind,
138137
) -> Option<ReprAttr> {
139-
use AlignKind::*;
140-
141138
let Some(align) = list.as_single() else {
142-
match align_kind {
143-
Packed => {
144-
cx.emit_err(session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
145-
span: param_span,
146-
});
147-
}
148-
Align => {
149-
cx.emit_err(session_diagnostics::IncorrectReprFormatAlignOneArg {
150-
span: param_span,
151-
});
152-
}
153-
}
154-
139+
cx.adcx().expected_single_argument(list.span, list.len());
155140
return None;
156141
};
157142

158143
let Some(lit) = align.as_lit() else {
159-
match align_kind {
160-
Packed => {
161-
cx.emit_err(session_diagnostics::IncorrectReprFormatPackedExpectInteger {
162-
span: align.span(),
163-
});
164-
}
165-
Align => {
166-
cx.emit_err(session_diagnostics::IncorrectReprFormatExpectInteger {
167-
span: align.span(),
168-
});
169-
}
170-
}
171-
144+
cx.adcx().expected_integer_literal(align.span());
172145
return None;
173146
};
174147

@@ -178,12 +151,8 @@ fn parse_repr_align(
178151
AlignKind::Align => ReprAttr::ReprAlign(literal),
179152
}),
180153
Err(message) => {
181-
cx.emit_err(session_diagnostics::InvalidReprGeneric {
154+
cx.emit_err(session_diagnostics::InvalidAlignmentValue {
182155
span: lit.span,
183-
repr_arg: match align_kind {
184-
Packed => "packed".to_string(),
185-
Align => "align".to_string(),
186-
},
187156
error_part: message,
188157
});
189158
None
@@ -237,10 +206,7 @@ impl RustcAlignParser {
237206
};
238207

239208
let Some(lit) = align.as_lit() else {
240-
cx.emit_err(session_diagnostics::IncorrectReprFormatExpectInteger {
241-
span: align.span(),
242-
});
243-
209+
cx.adcx().expected_integer_literal(align.span());
244210
return;
245211
};
246212

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -182,45 +182,6 @@ pub(crate) struct MissingIssue {
182182
pub span: Span,
183183
}
184184

185-
// FIXME: Why is this the same error code as `InvalidReprHintNoParen` and `InvalidReprHintNoValue`?
186-
// It is more similar to `IncorrectReprFormatGeneric`.
187-
#[derive(Diagnostic)]
188-
#[diag("incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all", code = E0552)]
189-
pub(crate) struct IncorrectReprFormatPackedOneOrZeroArg {
190-
#[primary_span]
191-
pub span: Span,
192-
}
193-
#[derive(Diagnostic)]
194-
#[diag("incorrect `repr(packed)` attribute format: `packed` expects a literal integer as argument", code = E0552)]
195-
pub(crate) struct IncorrectReprFormatPackedExpectInteger {
196-
#[primary_span]
197-
pub span: Span,
198-
}
199-
200-
#[derive(Diagnostic)]
201-
#[diag("invalid `repr({$repr_arg})` attribute: {$error_part}", code = E0589)]
202-
pub(crate) struct InvalidReprGeneric {
203-
#[primary_span]
204-
pub span: Span,
205-
206-
pub repr_arg: String,
207-
pub error_part: String,
208-
}
209-
210-
#[derive(Diagnostic)]
211-
#[diag("incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses", code = E0693)]
212-
pub(crate) struct IncorrectReprFormatAlignOneArg {
213-
#[primary_span]
214-
pub span: Span,
215-
}
216-
217-
#[derive(Diagnostic)]
218-
#[diag("incorrect `repr(align)` attribute format: `align` expects a literal integer as argument", code = E0693)]
219-
pub(crate) struct IncorrectReprFormatExpectInteger {
220-
#[primary_span]
221-
pub span: Span,
222-
}
223-
224185
#[derive(Diagnostic)]
225186
#[diag("`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` or a `rustc_const_stable` attribute", code = E0717)]
226187
pub(crate) struct RustcPromotablePairing {

compiler/rustc_error_codes/src/error_codes/E0552.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
This error code was replaced by `E0539`.
3+
14
A unrecognized representation attribute was used.
25

36
Erroneous code example:
47

5-
```compile_fail,E0552
8+
```compile_fail
69
#[repr(D)] // error: unrecognized representation hint
710
struct MyStruct {
811
my_field: usize

compiler/rustc_error_codes/src/error_codes/E0693.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
This error code was replaced by `E0539`.
3+
14
`align` representation hint was incorrectly declared.
25

36
Erroneous code examples:
47

5-
```compile_fail,E0693
8+
```compile_fail
69
#[repr(align=8)] // error!
710
struct Align8(i8);
811

tests/ui/attributes/arg-error-issue-121425.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
const N: usize = 8;
44
#[repr(align(N))]
5-
//~^ ERROR: incorrect `repr(align)` attribute format
5+
//~^ ERROR: malformed `repr` attribute input
66
struct T;
77

88
#[repr(align('a'))]
9-
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
9+
//~^ ERROR: not an unsuffixed integer [E0589]
1010
struct H;
1111

1212
#[repr(align("str"))]
13-
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
13+
//~^ ERROR: not an unsuffixed integer [E0589]
1414
struct L;
1515

1616
#[repr(align())]
17-
//~^ ERROR: attribute format: `align` takes exactly one argument in parentheses
17+
//~^ ERROR: malformed `repr` attribute input
1818
struct X;
1919

2020
const P: usize = 8;
2121
#[repr(packed(P))]
22-
//~^ ERROR: attribute format: `packed` expects a literal integer as argument
22+
//~^ ERROR: malformed `repr` attribute input
2323
struct A;
2424

2525
#[repr(packed())]
26-
//~^ ERROR: attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
26+
//~^ ERROR: malformed `repr` attribute input
2727
struct B;
2828

2929
#[repr(packed)]
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,56 @@
1-
error[E0693]: incorrect `repr(align)` attribute format: `align` expects a literal integer as argument
2-
--> $DIR/arg-error-issue-121425.rs:4:14
1+
error[E0539]: malformed `repr` attribute input
2+
--> $DIR/arg-error-issue-121425.rs:4:1
33
|
44
LL | #[repr(align(N))]
5-
| ^
5+
| ^^^^^^^^^^^^^-^^^
6+
| |
7+
| expected an integer literal here
8+
|
9+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
610

7-
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
11+
error[E0589]: invalid alignment value: not an unsuffixed integer
812
--> $DIR/arg-error-issue-121425.rs:8:14
913
|
1014
LL | #[repr(align('a'))]
1115
| ^^^
1216

13-
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
17+
error[E0589]: invalid alignment value: not an unsuffixed integer
1418
--> $DIR/arg-error-issue-121425.rs:12:14
1519
|
1620
LL | #[repr(align("str"))]
1721
| ^^^^^
1822

19-
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
20-
--> $DIR/arg-error-issue-121425.rs:16:8
23+
error[E0805]: malformed `repr` attribute input
24+
--> $DIR/arg-error-issue-121425.rs:16:1
2125
|
2226
LL | #[repr(align())]
23-
| ^^^^^^^
27+
| ^^^^^^^^^^^^--^^
28+
| |
29+
| expected an argument here
30+
|
31+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
2432

25-
error[E0552]: incorrect `repr(packed)` attribute format: `packed` expects a literal integer as argument
26-
--> $DIR/arg-error-issue-121425.rs:21:15
33+
error[E0539]: malformed `repr` attribute input
34+
--> $DIR/arg-error-issue-121425.rs:21:1
2735
|
2836
LL | #[repr(packed(P))]
29-
| ^
37+
| ^^^^^^^^^^^^^^-^^^
38+
| |
39+
| expected an integer literal here
40+
|
41+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
3042

31-
error[E0552]: incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
32-
--> $DIR/arg-error-issue-121425.rs:25:8
43+
error[E0805]: malformed `repr` attribute input
44+
--> $DIR/arg-error-issue-121425.rs:25:1
3345
|
3446
LL | #[repr(packed())]
35-
| ^^^^^^^^
47+
| ^^^^^^^^^^^^^--^^
48+
| |
49+
| expected an argument here
50+
|
51+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
3652

3753
error: aborting due to 6 previous errors
3854

39-
Some errors have detailed explanations: E0552, E0589, E0693.
40-
For more information about an error, try `rustc --explain E0552`.
55+
Some errors have detailed explanations: E0539, E0589, E0805.
56+
For more information about an error, try `rustc --explain E0539`.

tests/ui/attributes/malformed-reprs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// This is a regression test for https://github.com/rust-lang/rust/issues/143479
99
#[repr(align(0))]
10-
//~^ ERROR invalid `repr(align)` attribute: not a power of two
10+
//~^ ERROR not a power of two
1111
//~| ERROR unsupported representation for zero-variant enum [E0084]
1212
enum Foo {}
1313

tests/ui/attributes/malformed-reprs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL - #![repr]
2121
LL + #[repr]
2222
|
2323

24-
error[E0589]: invalid `repr(align)` attribute: not a power of two
24+
error[E0589]: invalid alignment value: not a power of two
2525
--> $DIR/malformed-reprs.rs:9:14
2626
|
2727
LL | #[repr(align(0))]

tests/ui/repr/malformed-repr-hints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
//@ compile-flags: -Zdeduplicate-diagnostics=yes
55

66
#[repr(packed())]
7-
//~^ ERROR: incorrect `repr(packed)` attribute format
7+
//~^ ERROR: malformed `repr` attribute input
88
struct S1;
99

1010
#[repr(align)]
1111
//~^ ERROR: malformed `repr` attribute input
1212
struct S2;
1313

1414
#[repr(align(2, 4))]
15-
//~^ ERROR: incorrect `repr(align)` attribute format
15+
//~^ ERROR: malformed `repr` attribute input
1616
struct S3;
1717

1818
#[repr(align())]
19-
//~^ ERROR: incorrect `repr(align)` attribute format
19+
//~^ ERROR: malformed `repr` attribute input
2020
struct S4;
2121

2222
// Regression test for issue #118334:

tests/ui/repr/malformed-repr-hints.stderr

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
error[E0552]: incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
2-
--> $DIR/malformed-repr-hints.rs:6:8
1+
error[E0805]: malformed `repr` attribute input
2+
--> $DIR/malformed-repr-hints.rs:6:1
33
|
44
LL | #[repr(packed())]
5-
| ^^^^^^^^
5+
| ^^^^^^^^^^^^^--^^
6+
| |
7+
| expected an argument here
8+
|
9+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
610

711
error[E0539]: malformed `repr` attribute input
812
--> $DIR/malformed-repr-hints.rs:10:1
@@ -14,17 +18,25 @@ LL | #[repr(align)]
1418
|
1519
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
1620

17-
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
18-
--> $DIR/malformed-repr-hints.rs:14:8
21+
error[E0805]: malformed `repr` attribute input
22+
--> $DIR/malformed-repr-hints.rs:14:1
1923
|
2024
LL | #[repr(align(2, 4))]
21-
| ^^^^^^^^^^^
25+
| ^^^^^^^^^^^^------^^
26+
| |
27+
| expected a single argument here
28+
|
29+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
2230

23-
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
24-
--> $DIR/malformed-repr-hints.rs:18:8
31+
error[E0805]: malformed `repr` attribute input
32+
--> $DIR/malformed-repr-hints.rs:18:1
2533
|
2634
LL | #[repr(align())]
27-
| ^^^^^^^
35+
| ^^^^^^^^^^^^--^^
36+
| |
37+
| expected an argument here
38+
|
39+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
2840

2941
error[E0565]: malformed `repr` attribute input
3042
--> $DIR/malformed-repr-hints.rs:23:1
@@ -88,5 +100,5 @@ LL | #[repr(i64 = 2)]
88100

89101
error: aborting due to 10 previous errors
90102

91-
Some errors have detailed explanations: E0539, E0552, E0565, E0693.
103+
Some errors have detailed explanations: E0539, E0565, E0805.
92104
For more information about an error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)