Skip to content

File tree

7 files changed

+82
-17
lines changed

7 files changed

+82
-17
lines changed
 

‎src/libsyntax_ext/asm.rs

-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ impl State {
4545
}
4646
}
4747

48-
macro_rules! span_err_if_not_stage0 {
49-
($cx:expr, $sp:expr, $code:ident, $text:tt) => {
50-
#[cfg(not(stage0))] {
51-
span_err!($cx, $sp, $code, $text)
52-
}
53-
#[cfg(stage0)] {
54-
$cx.span_err($sp, $text)
55-
}
56-
}
57-
}
58-
5948
const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
6049

6150
pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,

‎src/libsyntax_ext/deriving/default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
7676
}
7777
}
7878
StaticEnum(..) => {
79-
cx.span_err(trait_span,
80-
"`Default` cannot be derived for enums, only structs");
79+
span_err_if_not_stage0!(cx, trait_span, E0665,
80+
"`Default` cannot be derived for enums, only structs");
8181
// let compilation continue
8282
cx.expr_usize(trait_span, 0)
8383
}

‎src/libsyntax_ext/deriving/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ use syntax::ptr::P;
1919
use syntax::symbol::Symbol;
2020
use syntax_pos::Span;
2121

22+
macro_rules! span_err_if_not_stage0 {
23+
($cx:expr, $sp:expr, $code:ident, $text:tt) => {
24+
#[cfg(not(stage0))] {
25+
span_err!($cx, $sp, $code, $text)
26+
}
27+
#[cfg(stage0)] {
28+
$cx.span_err($sp, $text)
29+
}
30+
}
31+
}
32+
2233
macro path_local($x:ident) {
2334
generic::ty::Path::new_local(stringify!($x))
2435
}

‎src/libsyntax_ext/diagnostics.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ A clobber was surrounded by braces in the `asm` macro.
8282
8383
Erroneous code example:
8484
85-
```compile_fail,E0663
85+
```compile_fail,E0664
8686
asm!("mov $$0x200, %eax"
8787
:
8888
:
@@ -94,4 +94,38 @@ Considering that this would be a long explanation, we instead recommend you to
9494
take a look at the unstable book:
9595
https://doc.rust-lang.org/unstable-book/language-features/asm.html
9696
"##,
97+
98+
E0665: r##"
99+
The `Default` trait was derived on an enum.
100+
101+
Erroneous code example:
102+
103+
```compile_fail,E0665
104+
#[derive(Default)]
105+
enum Food {
106+
Sweet,
107+
Salty,
108+
}
109+
```
110+
111+
The `Default` cannot be derived on an enum for the simple reason that the
112+
compiler doesn't know which value to pick by default whereas it can for a
113+
struct as long as all its fields implement the `Default` trait as well.
114+
115+
If you still want to implement `Default` on your enum, you'll have to do it "by
116+
hand":
117+
118+
```
119+
enum Food {
120+
Sweet,
121+
Salty,
122+
}
123+
124+
impl Default for Food {
125+
fn default() -> Food {
126+
Food::Sweet
127+
}
128+
}
129+
```
130+
"##,
97131
}

‎src/libsyntax_ext/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ extern crate rustc_target;
3232
#[cfg(not(stage0))]
3333
mod diagnostics;
3434

35-
mod assert;
35+
#[macro_use]
36+
// for custom_derive
37+
pub mod deriving;
38+
3639
mod asm;
40+
mod assert;
3741
mod cfg;
3842
mod compile_error;
3943
mod concat;
@@ -47,8 +51,6 @@ mod trace_macros;
4751

4852
pub mod proc_macro_registrar;
4953

50-
// for custom_derive
51-
pub mod deriving;
5254

5355
pub mod proc_macro_impl;
5456

‎src/test/ui/E0665.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// ignore-stage1
12+
13+
#[derive(Default)] //~ ERROR E0665
14+
enum Food {
15+
Sweet,
16+
Salty,
17+
}
18+
19+
fn main() {
20+
}

‎src/test/ui/E0665.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0665]: `Default` cannot be derived for enums, only structs
2+
--> $DIR/E0665.rs:13:10
3+
|
4+
LL | #[derive(Default)] //~ ERROR E0665
5+
| ^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0665`.

0 commit comments

Comments
 (0)
Please sign in to comment.