Skip to content

Commit 92df8a9

Browse files
committed
Add E0517, E0518 for repr() errors
1 parent 2228bac commit 92df8a9

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
@@ -2039,6 +2039,79 @@ It is not possible to use stability attributes outside of the standard library.
20392039
Also, for now, it is not possible to write deprecation messages either.
20402040
"##,
20412041

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

20442117

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)