Skip to content

Commit 774a6a3

Browse files
committed
Auto merge of #50317 - varkor:repr-align-assign, r=nagisa
Improve error message for #[repr(align=x)] Before: ``` error[E0552]: unrecognized representation hint --> src/main.rs:1:8 | 1 | #[repr(align="8")] | ^^^^^^^^^ ``` After: ``` error[E0693]: incorrect `repr(align)` attribute format --> src/main.rs:1:8 | 2 | #[repr(align="8")] | ^^^^^^^^^ help: use parentheses instead: `align(8)` ``` Fixes #50314.
2 parents 79252ff + 35fe299 commit 774a6a3

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/libsyntax/attr.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,30 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
10451045
span_err!(diagnostic, item.span, E0589,
10461046
"invalid `repr(align)` attribute: {}", literal_error);
10471047
}
1048+
} else {
1049+
if let Some(meta_item) = item.meta_item() {
1050+
if meta_item.ident.name == "align" {
1051+
if let MetaItemKind::NameValue(ref value) = meta_item.node {
1052+
recognised = true;
1053+
let mut err = struct_span_err!(diagnostic, item.span, E0693,
1054+
"incorrect `repr(align)` attribute format");
1055+
match value.node {
1056+
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
1057+
err.span_suggestion(item.span,
1058+
"use parentheses instead",
1059+
format!("align({})", int));
1060+
}
1061+
ast::LitKind::Str(s, _) => {
1062+
err.span_suggestion(item.span,
1063+
"use parentheses instead",
1064+
format!("align({})", s));
1065+
}
1066+
_ => {}
1067+
}
1068+
err.emit();
1069+
}
1070+
}
1071+
}
10481072
}
10491073
if !recognised {
10501074
// Not a word we recognize

src/libsyntax/diagnostic_list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,5 @@ register_diagnostics! {
324324
E0589, // invalid `repr(align)` attribute
325325
E0629, // missing 'feature' (rustc_const_unstable)
326326
E0630, // rustc_const_unstable attribute must be paired with stable/unstable attribute
327+
E0693, // incorrect `repr(align)` attribute format
327328
}

src/test/ui/repr-align-assign.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[repr(align=8)] //~ ERROR incorrect `repr(align)` attribute format
12+
struct A(u64);
13+
14+
#[repr(align="8")] //~ ERROR incorrect `repr(align)` attribute format
15+
struct B(u64);
16+
17+
fn main() {}

src/test/ui/repr-align-assign.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0693]: incorrect `repr(align)` attribute format
2+
--> $DIR/repr-align-assign.rs:11:8
3+
|
4+
LL | #[repr(align=8)] //~ ERROR incorrect `repr(align)` attribute format
5+
| ^^^^^^^ help: use parentheses instead: `align(8)`
6+
7+
error[E0693]: incorrect `repr(align)` attribute format
8+
--> $DIR/repr-align-assign.rs:14:8
9+
|
10+
LL | #[repr(align="8")] //~ ERROR incorrect `repr(align)` attribute format
11+
| ^^^^^^^^^ help: use parentheses instead: `align(8)`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0693`.

0 commit comments

Comments
 (0)