Skip to content

Allow #[derive()] to generate unsafe methods #25524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
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)
}))
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
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)
}))
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ext/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
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)
}))
Expand All @@ -60,6 +61,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
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)
}))
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
Expand Down
11 changes: 10 additions & 1 deletion src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}

Expand Down Expand Up @@ -859,6 +862,12 @@ impl<'a> MethodDef<'a> {
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,
Expand All @@ -870,7 +879,7 @@ impl<'a> MethodDef<'a> {
generics: fn_generics,
abi: abi,
explicit_self: explicit_self,
unsafety: ast::Unsafety::Normal,
unsafety: unsafety,
decl: fn_decl
}, body_block)
})
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ext/deriving/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})),
Expand All @@ -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)
})),
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/deriving/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
Expand Down
1 change: 1 addition & 0 deletions src/test/auxiliary/custom_derive_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/test/auxiliary/custom_derive_plugin_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
},
],
Expand Down