Skip to content

Commit 7971a47

Browse files
committed
Added feature gate, updated error messages and tests.
1 parent 4358e35 commit 7971a47

File tree

11 files changed

+53
-8
lines changed

11 files changed

+53
-8
lines changed

src/doc/unstable-book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [proc_macro](language-features/proc-macro.md)
7373
- [quote](language-features/quote.md)
7474
- [relaxed_adts](language-features/relaxed-adts.md)
75+
- [repr_align](language-features/repr-align.md)
7576
- [repr_simd](language-features/repr-simd.md)
7677
- [rustc_attrs](language-features/rustc-attrs.md)
7778
- [rustc_diagnostic_macros](language-features/rustc-diagnostic-macros.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `repr_align`
2+
3+
The tracking issue for this feature is: [#33626]
4+
5+
[#33626]: https://github.com/rust-lang/rust/issues/33626
6+
7+
------------------------
8+
9+
10+
11+

src/libsyntax/attr.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -972,19 +972,24 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
972972
} else if let Some((name, value)) = item.name_value_literal() {
973973
if name == "align" {
974974
recognised = true;
975-
let mut valid_align = false;
975+
let mut align_error = None;
976976
if let ast::LitKind::Int(align, ast::LitIntType::Unsuffixed) = value.node {
977977
if align.is_power_of_two() {
978978
// rustc::ty::layout::Align restricts align to <= 32768
979979
if align <= 32768 {
980980
acc.push(ReprAlign(align as u16));
981-
valid_align = true;
981+
} else {
982+
align_error = Some("larger than 32768");
982983
}
984+
} else {
985+
align_error = Some("not a power of two");
983986
}
987+
} else {
988+
align_error = Some("not an unsuffixed integer");
984989
}
985-
if !valid_align {
990+
if let Some(align_error) = align_error {
986991
span_err!(diagnostic, item.span, E0589,
987-
"align representation must be a u16 power of two");
992+
"invalid `repr(align)` attribute: {}", align_error);
988993
}
989994
}
990995
}

src/libsyntax/diagnostic_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,5 @@ register_diagnostics! {
292292
E0556, // malformed feature, expected just one word
293293
E0557, // feature has been removed
294294
E0584, // file for module `..` found at both .. and ..
295-
E0589, // align representation must be a u16 power of two
295+
E0589, // invalid `repr(align)` attribute
296296
}

src/libsyntax/feature_gate.rs

+8
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ declare_features! (
338338
// Allows the `catch {...}` expression
339339
(active, catch_expr, "1.17.0", Some(31436)),
340340

341+
// Allows `repr(align(u16))` struct attribute (RFC 1358)
342+
(active, repr_align, "1.17.0", Some(33626)),
343+
341344
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
342345
(active, rvalue_static_promotion, "1.15.1", Some(38865)),
343346

@@ -1189,6 +1192,11 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
11891192
and possibly buggy");
11901193

11911194
}
1195+
if item.check_name("align") {
1196+
gate_feature_post!(&self, repr_align, i.span,
1197+
"the struct `#[repr(align(u16))]` attribute \
1198+
is experimental");
1199+
}
11921200
}
11931201
}
11941202
}

src/test/compile-fail/conflicting-repr-hints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#![allow(dead_code)]
1212
#![feature(attr_literals)]
13+
#![feature(repr_align)]
1314

1415
#[repr(C)]
1516
enum A { A }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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+
#![feature(attr_literals)]
11+
12+
#[repr(align(64))]
13+
struct Foo(u64, u64); //~ error: the struct `#[repr(align(u16))]` attribute is experimental
14+
15+
fn main() {}

src/test/compile-fail/repr-align.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
// except according to those terms.
1010
#![allow(dead_code)]
1111
#![feature(attr_literals)]
12+
#![feature(repr_align)]
1213

13-
#[repr(align(16.0))] //~ ERROR: align representation must be a u16 power of two
14+
#[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
1415
struct A(i32);
1516

16-
#[repr(align(15))] //~ ERROR: align representation must be a u16 power of two
17+
#[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
1718
struct B(i32);
1819

19-
#[repr(align(65536))] //~ ERROR: align representation must be a u16 power of tw
20+
#[repr(align(65536))] //~ ERROR: invalid `repr(align)` attribute: larger than 32768
2021
struct C(i32);
2122

2223
fn main() {}

src/test/compile-fail/repr-packed-contains-align.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010
#![feature(attr_literals)]
11+
#![feature(repr_align)]
1112
#![allow(dead_code)]
1213

1314
#[repr(align(16))]

src/test/run-pass/align-struct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010
#![feature(attr_literals)]
11+
#![feature(repr_align)]
1112

1213
use std::mem;
1314

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

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// aligned (while on most it is 8-byte aligned) and so the resulting
1919
// padding and overall computed sizes can be quite different.
2020
#![feature(attr_literals)]
21+
#![feature(repr_align)]
2122
#![allow(dead_code)]
2223

2324
#[repr(align(16))]

0 commit comments

Comments
 (0)