Skip to content

Commit e24fffe

Browse files
committed
Auto merge of #29716 - Manishearth:repr-error, r=Gankro
r? @gankro
2 parents f046532 + 92df8a9 commit e24fffe

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

src/librustc/diagnostics.rs

+73
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,79 @@ It is not possible to use stability attributes outside of the standard library.
20422042
Also, for now, it is not possible to write deprecation messages either.
20432043
"##,
20442044

2045+
E0517: r##"
2046+
This error indicates that a `#[repr(..)]` attribute was placed on an unsupported
2047+
item.
2048+
2049+
Examples of erroneous code:
2050+
2051+
```
2052+
#[repr(C)]
2053+
type Foo = u8;
2054+
2055+
#[repr(packed)]
2056+
enum Foo {Bar, Baz}
2057+
2058+
#[repr(u8)]
2059+
struct Foo {bar: bool, baz: bool}
2060+
2061+
#[repr(C)]
2062+
impl Foo {
2063+
...
2064+
}
2065+
```
2066+
2067+
- The `#[repr(C)]` attribute can only be placed on structs and enums
2068+
- The `#[repr(packed)]` and `#[repr(simd)]` attributes only work on structs
2069+
- The `#[repr(u8)]`, `#[repr(i16)]`, etc attributes only work on enums
2070+
2071+
These attributes do not work on typedefs, since typedefs are just aliases.
2072+
2073+
Representations like `#[repr(u8)]`, `#[repr(i64)]` are for selecting the
2074+
discriminant size for C-like enums (when there is no associated data, e.g. `enum
2075+
Color {Red, Blue, Green}`), effectively setting the size of the enum to the size
2076+
of the provided type. Such an enum can be cast to a value of the same type as
2077+
well. In short, `#[repr(u8)]` makes the enum behave like an integer with a
2078+
constrained set of allowed values.
2079+
2080+
Only C-like enums can be cast to numerical primitives, so this attribute will
2081+
not apply to structs.
2082+
2083+
`#[repr(packed)]` reduces padding to make the struct size smaller. The
2084+
representation of enums isn't strictly defined in Rust, and this attribute won't
2085+
work on enums.
2086+
2087+
`#[repr(simd)]` will give a struct consisting of a homogenous series of machine
2088+
types (i.e. `u8`, `i32`, etc) a representation that permits vectorization via
2089+
SIMD. This doesn't make much sense for enums since they don't consist of a
2090+
single list of data.
2091+
"##,
2092+
2093+
E0518: r##"
2094+
This error indicates that an `#[inline(..)]` attribute was incorrectly placed on
2095+
something other than a function or method.
2096+
2097+
Examples of erroneous code:
2098+
2099+
```
2100+
#[inline(always)]
2101+
struct Foo;
2102+
2103+
#[inline(never)]
2104+
impl Foo {
2105+
...
2106+
}
2107+
```
2108+
2109+
`#[inline]` hints the compiler whether or not to attempt to inline a method or
2110+
function. By default, the compiler does a pretty good job of figuring this out
2111+
itself, but if you feel the need for annotations, `#[inline(always)]` and
2112+
`#[inline(never)]` can override or force the compiler's decision.
2113+
2114+
If you wish to apply this attribute to all methods in an impl, manually annotate
2115+
each method; it is not possible to annotate the entire impl with an `#[inline]`
2116+
attribute.
2117+
"##,
20452118
}
20462119

20472120

src/librustc/front/check_attr.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ struct CheckAttrVisitor<'a> {
4141
impl<'a> CheckAttrVisitor<'a> {
4242
fn check_inline(&self, attr: &ast::Attribute, target: Target) {
4343
if target != Target::Fn {
44-
self.sess.span_err(
45-
attr.span,
46-
"attribute should be applied to function");
44+
span_err!(self.sess, attr.span, E0518, "attribute should be applied to function");
4745
}
4846
}
4947

@@ -56,33 +54,34 @@ impl<'a> CheckAttrVisitor<'a> {
5654
};
5755
for word in words {
5856
let word: &str = &word.name();
59-
match word {
57+
let message = match word {
6058
"C" => {
6159
if target != Target::Struct && target != Target::Enum {
62-
self.sess.span_err(
63-
attr.span,
64-
"attribute should be applied to struct or enum");
60+
"attribute should be applied to struct or enum"
61+
} else {
62+
continue
6563
}
6664
}
6765
"packed" |
6866
"simd" => {
6967
if target != Target::Struct {
70-
self.sess.span_err(
71-
attr.span,
72-
"attribute should be applied to struct");
68+
"attribute should be applied to struct"
69+
} else {
70+
continue
7371
}
7472
}
7573
"i8" | "u8" | "i16" | "u16" |
7674
"i32" | "u32" | "i64" | "u64" |
7775
"isize" | "usize" => {
7876
if target != Target::Enum {
79-
self.sess.span_err(
80-
attr.span,
81-
"attribute should be applied to enum");
77+
"attribute should be applied to enum"
78+
} else {
79+
continue
8280
}
8381
}
84-
_ => (),
85-
}
82+
_ => continue,
83+
};
84+
span_err!(self.sess, attr.span, E0517, "{}", message);
8685
}
8786
}
8887

0 commit comments

Comments
 (0)