Skip to content

Commit ad61ff4

Browse files
author
Keegan McAllister
committedNov 18, 2014
deriving: error out when used on a non-type
Besides being more helpful, this gives us the flexibility to later define a meaning for something like #[deriving(...)] mod bar { ... }
1 parent 09e2ad1 commit ad61ff4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
 

‎src/libsyntax/ext/deriving/generic/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub fn combine_substructure<'a>(f: CombineSubstructureFunc<'a>)
335335
impl<'a> TraitDef<'a> {
336336
pub fn expand(&self,
337337
cx: &mut ExtCtxt,
338-
_mitem: &ast::MetaItem,
338+
mitem: &ast::MetaItem,
339339
item: &ast::Item,
340340
push: |P<ast::Item>|) {
341341
let newitem = match item.node {
@@ -351,7 +351,10 @@ impl<'a> TraitDef<'a> {
351351
item.ident,
352352
generics)
353353
}
354-
_ => return
354+
_ => {
355+
cx.span_err(mitem.span, "`deriving` may only be applied to structs and enums");
356+
return;
357+
}
355358
};
356359
// Keep the lint attributes of the previous item to control how the
357360
// generated implementations are linted
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2014 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+
#![allow(dead_code)]
12+
13+
struct S;
14+
15+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
16+
trait T { }
17+
18+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
19+
impl S { }
20+
21+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
22+
impl T for S { }
23+
24+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
25+
static s: uint = 0u;
26+
27+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
28+
const c: uint = 0u;
29+
30+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
31+
mod m { }
32+
33+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
34+
extern "C" { }
35+
36+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
37+
type A = uint;
38+
39+
#[deriving(PartialEq)] //~ ERROR: `deriving` may only be applied to structs and enums
40+
fn main() { }

0 commit comments

Comments
 (0)
Please sign in to comment.