From 5b63841d91c3de96aac8bc79df1fc22b47ed993e Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sun, 17 May 2015 11:28:19 +0530 Subject: [PATCH] Allow #[derive()] to generate unsafe methods --- src/libsyntax/ext/deriving/clone.rs | 1 + src/libsyntax/ext/deriving/cmp/eq.rs | 1 + src/libsyntax/ext/deriving/cmp/ord.rs | 1 + src/libsyntax/ext/deriving/cmp/partial_eq.rs | 1 + src/libsyntax/ext/deriving/cmp/partial_ord.rs | 2 ++ src/libsyntax/ext/deriving/decodable.rs | 1 + src/libsyntax/ext/deriving/default.rs | 1 + src/libsyntax/ext/deriving/encodable.rs | 1 + src/libsyntax/ext/deriving/generic/mod.rs | 11 ++++++++++- src/libsyntax/ext/deriving/hash.rs | 1 + src/libsyntax/ext/deriving/primitive.rs | 2 ++ src/libsyntax/ext/deriving/show.rs | 1 + src/test/auxiliary/custom_derive_plugin.rs | 1 + src/test/auxiliary/custom_derive_plugin_attr.rs | 1 + 14 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index 0d8fb471429..d99156bfa45 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -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) })), diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index bd7c7d85a37..2a58ba2fc54 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -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) })) diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index b4caf0ec26e..af36428437c 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -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) })), diff --git a/src/libsyntax/ext/deriving/cmp/partial_eq.rs b/src/libsyntax/ext/deriving/cmp/partial_eq.rs index 5d744334745..b330c1de180 100644 --- a/src/libsyntax/ext/deriving/cmp/partial_eq.rs +++ b/src/libsyntax/ext/deriving/cmp/partial_eq.rs @@ -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) })) diff --git a/src/libsyntax/ext/deriving/cmp/partial_ord.rs b/src/libsyntax/ext/deriving/cmp/partial_ord.rs index 3ef73f6556e..557cce63346 100644 --- a/src/libsyntax/ext/deriving/cmp/partial_ord.rs +++ b/src/libsyntax/ext/deriving/cmp/partial_ord.rs @@ -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) })) diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 75c9bc42ea2..d21373c4a0b 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -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) })), diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs index e9984c84e32..84b91a252a6 100644 --- a/src/libsyntax/ext/deriving/default.rs +++ b/src/libsyntax/ext/deriving/default.rs @@ -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) })) diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 4430c58700a..08037c0308d 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -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) })), diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 1525f1a822b..b9968ca9608 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -253,6 +253,9 @@ pub struct MethodDef<'a> { pub attributes: Vec, + // Is it an `unsafe fn`? + pub is_unsafe: bool, + pub combine_substructure: RefCell>, } @@ -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) }) diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index 698e788d65f..04387bd4b3f 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -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) })) diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs index 4fe9aefa1a4..4aabcf04e41 100644 --- a/src/libsyntax/ext/deriving/primitive.rs +++ b/src/libsyntax/ext/deriving/primitive.rs @@ -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) })), diff --git a/src/libsyntax/ext/deriving/show.rs b/src/libsyntax/ext/deriving/show.rs index dc634df2073..518bd161c1e 100644 --- a/src/libsyntax/ext/deriving/show.rs +++ b/src/libsyntax/ext/deriving/show.rs @@ -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) })) diff --git a/src/test/auxiliary/custom_derive_plugin.rs b/src/test/auxiliary/custom_derive_plugin.rs index 0edbeb2d02c..b130c42dcd7 100644 --- a/src/test/auxiliary/custom_derive_plugin.rs +++ b/src/test/auxiliary/custom_derive_plugin.rs @@ -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, diff --git a/src/test/auxiliary/custom_derive_plugin_attr.rs b/src/test/auxiliary/custom_derive_plugin_attr.rs index 92963b2812d..b056d8b5654 100644 --- a/src/test/auxiliary/custom_derive_plugin_attr.rs +++ b/src/test/auxiliary/custom_derive_plugin_attr.rs @@ -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)), }, ],