Skip to content

Commit f7cb53e

Browse files
committed
Auto merge of rust-lang#121900 - chenyukang:yukang-fix-121425-repr-pack-error, r=compiler-errors
Fix misleading message in struct repr alignment and packed Fixes rust-lang#121425 By the way, fix the spans for the argument in the second commit.
2 parents 8b1af4c + 53dba7f commit f7cb53e

File tree

9 files changed

+160
-49
lines changed

9 files changed

+160
-49
lines changed

compiler/rustc_attr/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ attr_incorrect_meta_item =
2727
attr_incorrect_repr_format_align_one_arg =
2828
incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
2929
30+
attr_incorrect_repr_format_expect_literal_integer =
31+
incorrect `repr(align)` attribute format: `align` expects a literal integer as argument
32+
3033
attr_incorrect_repr_format_generic =
3134
incorrect `repr({$repr_arg})` attribute format
3235
.suggestion = use parentheses instead
3336
37+
attr_incorrect_repr_format_packed_expect_integer =
38+
incorrect `repr(packed)` attribute format: `packed` expects a literal integer as argument
39+
3440
attr_incorrect_repr_format_packed_one_or_zero_arg =
3541
incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
3642

compiler/rustc_attr/src/builtin.rs

+37-14
Original file line numberDiff line numberDiff line change
@@ -984,17 +984,24 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
984984
}
985985
} else if let Some((name, value)) = item.name_value_literal() {
986986
let mut literal_error = None;
987+
let mut err_span = item.span();
987988
if name == sym::align {
988989
recognised = true;
989990
match parse_alignment(&value.kind) {
990991
Ok(literal) => acc.push(ReprAlign(literal)),
991-
Err(message) => literal_error = Some(message),
992+
Err(message) => {
993+
err_span = value.span;
994+
literal_error = Some(message)
995+
}
992996
};
993997
} else if name == sym::packed {
994998
recognised = true;
995999
match parse_alignment(&value.kind) {
9961000
Ok(literal) => acc.push(ReprPacked(literal)),
997-
Err(message) => literal_error = Some(message),
1001+
Err(message) => {
1002+
err_span = value.span;
1003+
literal_error = Some(message)
1004+
}
9981005
};
9991006
} else if matches!(name, sym::Rust | sym::C | sym::simd | sym::transparent)
10001007
|| int_type_of_word(name).is_some()
@@ -1007,7 +1014,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10071014
}
10081015
if let Some(literal_error) = literal_error {
10091016
sess.dcx().emit_err(session_diagnostics::InvalidReprGeneric {
1010-
span: item.span(),
1017+
span: err_span,
10111018
repr_arg: name.to_ident_string(),
10121019
error_part: literal_error,
10131020
});
@@ -1039,21 +1046,37 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10391046
});
10401047
}
10411048
}
1042-
MetaItemKind::List(_) => {
1049+
MetaItemKind::List(nested_items) => {
10431050
if meta_item.has_name(sym::align) {
10441051
recognised = true;
1045-
sess.dcx().emit_err(
1046-
session_diagnostics::IncorrectReprFormatAlignOneArg {
1047-
span: meta_item.span,
1048-
},
1049-
);
1052+
if nested_items.len() == 1 {
1053+
sess.dcx().emit_err(
1054+
session_diagnostics::IncorrectReprFormatExpectInteger {
1055+
span: nested_items[0].span(),
1056+
},
1057+
);
1058+
} else {
1059+
sess.dcx().emit_err(
1060+
session_diagnostics::IncorrectReprFormatAlignOneArg {
1061+
span: meta_item.span,
1062+
},
1063+
);
1064+
}
10501065
} else if meta_item.has_name(sym::packed) {
10511066
recognised = true;
1052-
sess.dcx().emit_err(
1053-
session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
1054-
span: meta_item.span,
1055-
},
1056-
);
1067+
if nested_items.len() == 1 {
1068+
sess.dcx().emit_err(
1069+
session_diagnostics::IncorrectReprFormatPackedExpectInteger {
1070+
span: nested_items[0].span(),
1071+
},
1072+
);
1073+
} else {
1074+
sess.dcx().emit_err(
1075+
session_diagnostics::IncorrectReprFormatPackedOneOrZeroArg {
1076+
span: meta_item.span,
1077+
},
1078+
);
1079+
}
10571080
} else if matches!(
10581081
meta_item.name_or_empty(),
10591082
sym::Rust | sym::C | sym::simd | sym::transparent

compiler/rustc_attr/src/session_diagnostics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ pub(crate) struct IncorrectReprFormatPackedOneOrZeroArg {
170170
#[primary_span]
171171
pub span: Span,
172172
}
173+
#[derive(Diagnostic)]
174+
#[diag(attr_incorrect_repr_format_packed_expect_integer, code = E0552)]
175+
pub(crate) struct IncorrectReprFormatPackedExpectInteger {
176+
#[primary_span]
177+
pub span: Span,
178+
}
173179

174180
#[derive(Diagnostic)]
175181
#[diag(attr_invalid_repr_hint_no_paren, code = E0552)]
@@ -252,6 +258,13 @@ pub(crate) struct IncorrectReprFormatAlignOneArg {
252258
pub span: Span,
253259
}
254260

261+
#[derive(Diagnostic)]
262+
#[diag(attr_incorrect_repr_format_expect_literal_integer, code = E0693)]
263+
pub(crate) struct IncorrectReprFormatExpectInteger {
264+
#[primary_span]
265+
pub span: Span,
266+
}
267+
255268
#[derive(Diagnostic)]
256269
#[diag(attr_incorrect_repr_format_generic, code = E0693)]
257270
pub(crate) struct IncorrectReprFormatGeneric<'a> {

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
441441
.map_err(|msg| {
442442
struct_span_code_err!(
443443
tcx.dcx(),
444-
attr.span,
444+
literal.span,
445445
E0589,
446446
"invalid `repr(align)` attribute: {}",
447447
msg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ compile-flags: -Zdeduplicate-diagnostics=yes
2+
3+
const N: usize = 8;
4+
#[repr(align(N))]
5+
//~^ ERROR: incorrect `repr(align)` attribute format
6+
struct T;
7+
8+
#[repr(align('a'))]
9+
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
10+
struct H;
11+
12+
#[repr(align("str"))]
13+
//~^ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer [E0589]
14+
struct L;
15+
16+
#[repr(align())]
17+
//~^ ERROR: attribute format: `align` takes exactly one argument in parentheses
18+
struct X;
19+
20+
const P: usize = 8;
21+
#[repr(packed(P))]
22+
//~^ ERROR: attribute format: `packed` expects a literal integer as argument
23+
struct A;
24+
25+
#[repr(packed())]
26+
//~^ ERROR: attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
27+
struct B;
28+
29+
#[repr(packed)]
30+
struct C;
31+
32+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0693]: incorrect `repr(align)` attribute format: `align` expects a literal integer as argument
2+
--> $DIR/arg-error-issue-121425.rs:4:14
3+
|
4+
LL | #[repr(align(N))]
5+
| ^
6+
7+
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
8+
--> $DIR/arg-error-issue-121425.rs:8:14
9+
|
10+
LL | #[repr(align('a'))]
11+
| ^^^
12+
13+
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
14+
--> $DIR/arg-error-issue-121425.rs:12:14
15+
|
16+
LL | #[repr(align("str"))]
17+
| ^^^^^
18+
19+
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
20+
--> $DIR/arg-error-issue-121425.rs:16:8
21+
|
22+
LL | #[repr(align())]
23+
| ^^^^^^^
24+
25+
error[E0552]: incorrect `repr(packed)` attribute format: `packed` expects a literal integer as argument
26+
--> $DIR/arg-error-issue-121425.rs:21:15
27+
|
28+
LL | #[repr(packed(P))]
29+
| ^
30+
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
33+
|
34+
LL | #[repr(packed())]
35+
| ^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+
39+
Some errors have detailed explanations: E0552, E0589, E0693.
40+
For more information about an error, try `rustc --explain E0552`.

tests/ui/attributes/nonterminal-expansion.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
//@ compile-flags: -Zdeduplicate-diagnostics=yes
2+
13
// Macros were previously expanded in `Expr` nonterminal tokens, now they are not.
24

35
macro_rules! pass_nonterminal {
46
($n:expr) => {
57
#[repr(align($n))]
68
//~^ ERROR expected unsuffixed literal or identifier, found `n!()`
7-
//~| ERROR incorrect `repr(align)` attribute format
89
struct S;
910
};
1011
}
@@ -14,5 +15,6 @@ macro_rules! n {
1415
}
1516

1617
pass_nonterminal!(n!());
18+
//~^ ERROR incorrect `repr(align)` attribute format: `align` expects a literal integer as argument [E0693]
1719

1820
fn main() {}

tests/ui/attributes/nonterminal-expansion.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected unsuffixed literal or identifier, found `n!()`
2-
--> $DIR/nonterminal-expansion.rs:5:22
2+
--> $DIR/nonterminal-expansion.rs:7:22
33
|
44
LL | #[repr(align($n))]
55
| ^^
@@ -9,16 +9,11 @@ LL | pass_nonterminal!(n!());
99
|
1010
= note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
13-
--> $DIR/nonterminal-expansion.rs:5:16
12+
error[E0693]: incorrect `repr(align)` attribute format: `align` expects a literal integer as argument
13+
--> $DIR/nonterminal-expansion.rs:17:19
1414
|
15-
LL | #[repr(align($n))]
16-
| ^^^^^^^^^
17-
...
1815
LL | pass_nonterminal!(n!());
19-
| ----------------------- in this macro invocation
20-
|
21-
= note: this error originates in the macro `pass_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
| ^
2217

2318
error: aborting due to 2 previous errors
2419

tests/ui/repr/repr-align.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
11
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
2-
--> $DIR/repr-align.rs:3:8
2+
--> $DIR/repr-align.rs:3:14
33
|
44
LL | #[repr(align(16.0))]
5-
| ^^^^^^^^^^^
5+
| ^^^^
66

77
error[E0589]: invalid `repr(align)` attribute: not a power of two
8-
--> $DIR/repr-align.rs:7:8
8+
--> $DIR/repr-align.rs:7:14
99
|
1010
LL | #[repr(align(15))]
11-
| ^^^^^^^^^
11+
| ^^
1212

1313
error[E0589]: invalid `repr(align)` attribute: larger than 2^29
14-
--> $DIR/repr-align.rs:11:8
14+
--> $DIR/repr-align.rs:11:14
1515
|
1616
LL | #[repr(align(4294967296))]
17-
| ^^^^^^^^^^^^^^^^^
17+
| ^^^^^^^^^^
1818

1919
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
20-
--> $DIR/repr-align.rs:18:8
20+
--> $DIR/repr-align.rs:18:14
2121
|
2222
LL | #[repr(align(16.0))]
23-
| ^^^^^^^^^^^
23+
| ^^^^
2424

2525
error[E0589]: invalid `repr(align)` attribute: not a power of two
26-
--> $DIR/repr-align.rs:22:8
26+
--> $DIR/repr-align.rs:22:14
2727
|
2828
LL | #[repr(align(15))]
29-
| ^^^^^^^^^
29+
| ^^
3030

3131
error[E0589]: invalid `repr(align)` attribute: larger than 2^29
32-
--> $DIR/repr-align.rs:26:8
32+
--> $DIR/repr-align.rs:26:14
3333
|
3434
LL | #[repr(align(4294967296))]
35-
| ^^^^^^^^^^^^^^^^^
35+
| ^^^^^^^^^^
3636

3737
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
38-
--> $DIR/repr-align.rs:3:8
38+
--> $DIR/repr-align.rs:3:14
3939
|
4040
LL | #[repr(align(16.0))]
41-
| ^^^^^^^^^^^
41+
| ^^^^
4242
|
4343
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4444

4545
error[E0589]: invalid `repr(align)` attribute: not a power of two
46-
--> $DIR/repr-align.rs:7:8
46+
--> $DIR/repr-align.rs:7:14
4747
|
4848
LL | #[repr(align(15))]
49-
| ^^^^^^^^^
49+
| ^^
5050
|
5151
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
5252

5353
error[E0589]: invalid `repr(align)` attribute: larger than 2^29
54-
--> $DIR/repr-align.rs:11:8
54+
--> $DIR/repr-align.rs:11:14
5555
|
5656
LL | #[repr(align(4294967296))]
57-
| ^^^^^^^^^^^^^^^^^
57+
| ^^^^^^^^^^
5858
|
5959
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
6060

6161
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
62-
--> $DIR/repr-align.rs:18:8
62+
--> $DIR/repr-align.rs:18:14
6363
|
6464
LL | #[repr(align(16.0))]
65-
| ^^^^^^^^^^^
65+
| ^^^^
6666
|
6767
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
6868

6969
error[E0589]: invalid `repr(align)` attribute: not a power of two
70-
--> $DIR/repr-align.rs:22:8
70+
--> $DIR/repr-align.rs:22:14
7171
|
7272
LL | #[repr(align(15))]
73-
| ^^^^^^^^^
73+
| ^^
7474
|
7575
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
7676

7777
error[E0589]: invalid `repr(align)` attribute: larger than 2^29
78-
--> $DIR/repr-align.rs:26:8
78+
--> $DIR/repr-align.rs:26:14
7979
|
8080
LL | #[repr(align(4294967296))]
81-
| ^^^^^^^^^^^^^^^^^
81+
| ^^^^^^^^^^
8282
|
8383
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
8484

0 commit comments

Comments
 (0)