Skip to content

Commit a037073

Browse files
committed
deriving: factor out discriminant_value construction
1 parent e084a84 commit a037073

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

src/libsyntax_ext/deriving/generic/mod.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ use syntax::ptr::P;
209209

210210
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
211211

212+
use deriving;
213+
212214
pub mod ty;
213215

214216
pub struct TraitDef<'a> {
@@ -381,22 +383,6 @@ fn find_type_parameters(ty: &ast::Ty, ty_param_names: &[ast::Name]) -> Vec<P<ast
381383
visitor.types
382384
}
383385

384-
/// Replacement for expr_unreachable which generates intrinsics::unreachable()
385-
/// instead of unreachable!()
386-
fn expr_unreachable_intrinsic(cx: &ExtCtxt, sp: Span) -> P<Expr> {
387-
let path = cx.std_path(&["intrinsics", "unreachable"]);
388-
let call = cx.expr_call_global(
389-
sp, path, vec![]);
390-
let unreachable = cx.expr_block(P(ast::Block {
391-
stmts: vec![],
392-
expr: Some(call),
393-
id: ast::DUMMY_NODE_ID,
394-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
395-
span: sp }));
396-
397-
unreachable
398-
}
399-
400386
impl<'a> TraitDef<'a> {
401387
pub fn expand(&self,
402388
cx: &mut ExtCtxt,
@@ -1277,15 +1263,11 @@ impl<'a> MethodDef<'a> {
12771263

12781264
let mut first_ident = None;
12791265
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
1280-
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
1281-
let call = cx.expr_call_global(
1282-
sp, path, vec![cx.expr_addr_of(sp, self_arg.clone())]);
1283-
let variant_value = cx.expr_block(P(ast::Block {
1284-
stmts: vec![],
1285-
expr: Some(call),
1286-
id: ast::DUMMY_NODE_ID,
1287-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
1288-
span: sp }));
1266+
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
1267+
let variant_value = deriving::call_intrinsic(cx,
1268+
sp,
1269+
"discriminant_value",
1270+
vec![self_addr]);
12891271

12901272
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name));
12911273
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
@@ -1313,7 +1295,9 @@ impl<'a> MethodDef<'a> {
13131295
//Since we know that all the arguments will match if we reach the match expression we
13141296
//add the unreachable intrinsics as the result of the catch all which should help llvm
13151297
//in optimizing it
1316-
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], expr_unreachable_intrinsic(cx, sp)));
1298+
match_arms.push(cx.arm(sp,
1299+
vec![cx.pat_wild(sp)],
1300+
deriving::call_intrinsic(cx, sp, "unreachable", vec![])));
13171301

13181302
// Final wrinkle: the self_args are expressions that deref
13191303
// down to desired l-values, but we cannot actually deref
@@ -1389,7 +1373,7 @@ impl<'a> MethodDef<'a> {
13891373
// derive Debug on such a type could here generate code
13901374
// that needs the feature gate enabled.)
13911375

1392-
expr_unreachable_intrinsic(cx, sp)
1376+
deriving::call_intrinsic(cx, sp, "unreachable", vec![])
13931377
}
13941378
else {
13951379

src/libsyntax_ext/deriving/hash.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use deriving;
1212
use deriving::generic::*;
1313
use deriving::generic::ty::*;
1414

15-
use syntax::ast::{self, MetaItem, Expr, Mutability};
15+
use syntax::ast::{MetaItem, Expr, Mutability};
1616
use syntax::codemap::Span;
1717
use syntax::ext::base::{ExtCtxt, Annotatable};
1818
use syntax::ext::build::AstBuilder;
@@ -82,15 +82,10 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
8282
let fields = match *substr.fields {
8383
Struct(_, ref fs) => fs,
8484
EnumMatching(_, _, ref fs) => {
85-
let path = cx.std_path(&["intrinsics", "discriminant_value"]);
86-
let call = cx.expr_call_global(
87-
trait_span, path, vec![cx.expr_self(trait_span)]);
88-
let variant_value = cx.expr_block(P(ast::Block {
89-
stmts: vec![],
90-
expr: Some(call),
91-
id: ast::DUMMY_NODE_ID,
92-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
93-
span: trait_span }));
85+
let variant_value = deriving::call_intrinsic(cx,
86+
trait_span,
87+
"discriminant_value",
88+
vec![cx.expr_self(trait_span)]);
9489

9590
stmts.push(call_hash(trait_span, variant_value));
9691

src/libsyntax_ext/deriving/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use syntax::ext::build::AstBuilder;
1818
use syntax::feature_gate;
1919
use syntax::codemap::Span;
2020
use syntax::parse::token::{intern, intern_and_get_ident};
21+
use syntax::ptr::P;
2122

2223
macro_rules! pathvec {
2324
($($x:ident)::+) => (
@@ -218,3 +219,19 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String {
218219
typaram
219220
}
220221

222+
/// Constructs an expression that calls an intrinsic
223+
fn call_intrinsic(cx: &ExtCtxt,
224+
span: Span,
225+
intrinsic: &str,
226+
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
227+
let path = cx.std_path(&["intrinsics", intrinsic]);
228+
let call = cx.expr_call_global(span, path, args);
229+
230+
cx.expr_block(P(ast::Block {
231+
stmts: vec![],
232+
expr: Some(call),
233+
id: ast::DUMMY_NODE_ID,
234+
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
235+
span: span }))
236+
}
237+

0 commit comments

Comments
 (0)