]> git.lizzy.rs Git - rust.git/commitdiff
Allow #[derive()] to generate unsafe methods
authorManish Goregaokar <manishsmail@gmail.com>
Sun, 17 May 2015 05:58:19 +0000 (11:28 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Sun, 17 May 2015 09:26:13 +0000 (14:56 +0530)
14 files changed:
src/libsyntax/ext/deriving/clone.rs
src/libsyntax/ext/deriving/cmp/eq.rs
src/libsyntax/ext/deriving/cmp/ord.rs
src/libsyntax/ext/deriving/cmp/partial_eq.rs
src/libsyntax/ext/deriving/cmp/partial_ord.rs
src/libsyntax/ext/deriving/decodable.rs
src/libsyntax/ext/deriving/default.rs
src/libsyntax/ext/deriving/encodable.rs
src/libsyntax/ext/deriving/generic/mod.rs
src/libsyntax/ext/deriving/hash.rs
src/libsyntax/ext/deriving/primitive.rs
src/libsyntax/ext/deriving/show.rs
src/test/auxiliary/custom_derive_plugin.rs
src/test/auxiliary/custom_derive_plugin_attr.rs

index 0d8fb471429377e3878a9fd52b038b0a5d9ae462..d99156bfa45037d514e7e78e8228fca0ba0aaecb 100644 (file)
@@ -39,6 +39,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
                 args: Vec::new(),
                 ret_ty: Self_,
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|c, s, sub| {
                     cs_clone("Clone", c, s, sub)
                 })),
index bd7c7d85a375f41bf6cec262201bbab88ac874af..2a58ba2fc5419845451b4e88fcf5743b9da0b4f5 100644 (file)
@@ -59,6 +59,7 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<
                 args: vec!(),
                 ret_ty: nil_ty(),
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     cs_total_eq_assert(a, b, c)
                 }))
index b4caf0ec26e277de9a25ec9abc371167446422b5..af36428437cb84fe9f8215243ade58db167d3a79 100644 (file)
@@ -40,6 +40,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
                 args: vec!(borrowed_self()),
                 ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     cs_cmp(a, b, c)
                 })),
index 5d744334745d32e9c17bb6394a15df3d19b33623..b330c1de180afe6d5f2107d2f9a57c2da2738281 100644 (file)
@@ -71,6 +71,7 @@ macro_rules! md {
                 args: vec!(borrowed_self()),
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     $f(a, b, c)
                 }))
index 3ef73f6556e3015e767e331807708bd1e6180a5e..557cce633465eb868bea754105c0a4b894fcf5d7 100644 (file)
@@ -37,6 +37,7 @@ macro_rules! md {
                 args: vec!(borrowed_self()),
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
                     cs_op($op, $equal, cx, span, substr)
                 }))
@@ -60,6 +61,7 @@ macro_rules! md {
         args: vec![borrowed_self()],
         ret_ty: ret_ty,
         attributes: attrs,
+        is_unsafe: false,
         combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
             cs_partial_cmp(cx, span, substr)
         }))
index 75c9bc42ea2bfc6e261af3c138620bb9af5a6c35..d21373c4a0b68f25643b49cf2312ced77886df20 100644 (file)
@@ -79,6 +79,7 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
                     true
                 )),
                 attributes: Vec::new(),
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     decodable_substructure(a, b, c, krate)
                 })),
index e9984c84e322fad9b5587f576819428b7b7919a7..84b91a252a638f3943fb0ee16507b7d9cca7d389 100644 (file)
@@ -39,6 +39,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt,
                 args: Vec::new(),
                 ret_ty: Self_,
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     default_substructure(a, b, c)
                 }))
index 4430c58700af365be6da580d708e478814911f43..08037c0308ddc2cac9c61942e8219d3b843cb263 100644 (file)
@@ -155,6 +155,7 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
                     true
                 )),
                 attributes: Vec::new(),
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     encodable_substructure(a, b, c)
                 })),
index 1525f1a822bec5436160b91abb9f9f078383b179..b9968ca96088ff5b122ab2f4a083b991b71a5948 100644 (file)
@@ -253,6 +253,9 @@ pub struct MethodDef<'a> {
 
     pub attributes: Vec<ast::Attribute>,
 
+    // Is it an `unsafe fn`?
+    pub is_unsafe: bool,
+
     pub combine_substructure: RefCell<CombineSubstructureFunc<'a>>,
 }
 
@@ -859,6 +862,12 @@ fn create_method(&self,
         let fn_decl = cx.fn_decl(args, ret_type);
         let body_block = cx.block_expr(body);
 
+        let unsafety = if self.is_unsafe {
+            ast::Unsafety::Unsafe
+        } else {
+            ast::Unsafety::Normal
+        };
+
         // Create the method.
         P(ast::ImplItem {
             id: ast::DUMMY_NODE_ID,
@@ -870,7 +879,7 @@ fn create_method(&self,
                 generics: fn_generics,
                 abi: abi,
                 explicit_self: explicit_self,
-                unsafety: ast::Unsafety::Normal,
+                unsafety: unsafety,
                 decl: fn_decl
             }, body_block)
         })
index 698e788d65f00d8827cea539f72d2cf453376877..04387bd4b3f6028b1ecc6501979139be60b3ecb6 100644 (file)
@@ -44,6 +44,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
                 args: vec!(Ptr(Box::new(Literal(arg)), Borrowed(None, MutMutable))),
                 ret_ty: nil_ty(),
                 attributes: vec![],
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     hash_substructure(a, b, c)
                 }))
index 4fe9aefa1a4dfe51a5080b51e47a7bb76a266379..4aabcf04e417058b757246587fc8d18b7486eafe 100644 (file)
@@ -44,6 +44,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
                                            true)),
                 // #[inline] liable to cause code-bloat
                 attributes: attrs.clone(),
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|c, s, sub| {
                     cs_from("i64", c, s, sub)
                 })),
@@ -59,6 +60,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt,
                                            true)),
                 // #[inline] liable to cause code-bloat
                 attributes: attrs,
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|c, s, sub| {
                     cs_from("u64", c, s, sub)
                 })),
index dc634df2073a48b031be43f86dda072d8f2473b5..518bd161c1efb6eef38b4dccfee34ca9251294b1 100644 (file)
@@ -42,6 +42,7 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt,
                 args: vec!(fmtr),
                 ret_ty: Literal(path_std!(cx, core::fmt::Result)),
                 attributes: Vec::new(),
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| {
                     show_substructure(a, b, c)
                 }))
index 0edbeb2d02ca4ccdb917b30b22d151d9f3229a68..b130c42dcd718aa4972ddb39d75b86f7e2634b26 100644 (file)
@@ -54,6 +54,7 @@ fn expand(cx: &mut ExtCtxt,
                 args: vec![],
                 ret_ty: Literal(Path::new_local("isize")),
                 attributes: vec![],
+                is_unsafe: false,
                 combine_substructure: combine_substructure(box |cx, span, substr| {
                     let zero = cx.expr_isize(span, 0);
                     cs_fold(false,
index 92963b2812dd8ef49bfb9dc617207869e08e3667..b056d8b565410262cc1834c4041c0ed1a88ebbb4 100644 (file)
@@ -56,6 +56,7 @@ fn expand(cx: &mut ExtCtxt,
                 args: vec![],
                 ret_ty: Literal(Path::new_local("isize")),
                 attributes: vec![],
+                is_unsafe: false,
                 combine_substructure: combine_substructure(Box::new(totalsum_substructure)),
             },
         ],