Improve derived discriminant testing.
Currently the generated code for methods like `eq`, `ne`, and `partial_cmp` includes stuff like this: ``` let __self_vi = ::core::intrinsics::discriminant_value(&*self); let __arg_1_vi = ::core::intrinsics::discriminant_value(&*other); if true && __self_vi == __arg_1_vi { ... } ``` This commit removes the unnecessary `true &&`, and makes the generating code a little easier to read in the process. It also fixes some errors in comments.
This commit is contained in:
parent
ea74997c41
commit
00207ead61
2 changed files with 27 additions and 32 deletions
|
@ -1392,37 +1392,32 @@ impl<'a> MethodDef<'a> {
|
|||
//
|
||||
// i.e., for `enum E<T> { A, B(1), C(T, T) }`, and a deriving
|
||||
// with three Self args, builds three statements:
|
||||
//
|
||||
// ```
|
||||
// let __self0_vi = std::intrinsics::discriminant_value(&self);
|
||||
// let __self1_vi = std::intrinsics::discriminant_value(&arg1);
|
||||
// let __self2_vi = std::intrinsics::discriminant_value(&arg2);
|
||||
// let __self_vi = std::intrinsics::discriminant_value(&self);
|
||||
// let __arg_1_vi = std::intrinsics::discriminant_value(&arg1);
|
||||
// let __arg_2_vi = std::intrinsics::discriminant_value(&arg2);
|
||||
// ```
|
||||
let mut index_let_stmts: Vec<ast::Stmt> = Vec::with_capacity(vi_idents.len() + 1);
|
||||
|
||||
// We also build an expression which checks whether all discriminants are equal
|
||||
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
|
||||
// We also build an expression which checks whether all discriminants are equal:
|
||||
// `__self_vi == __arg_1_vi && __self_vi == __arg_2_vi && ...`
|
||||
let mut discriminant_test = cx.expr_bool(span, true);
|
||||
|
||||
let mut first_ident = None;
|
||||
for (&ident, self_arg) in iter::zip(&vi_idents, &self_args) {
|
||||
for (i, (&ident, self_arg)) in iter::zip(&vi_idents, &self_args).enumerate() {
|
||||
let self_addr = cx.expr_addr_of(span, self_arg.clone());
|
||||
let variant_value =
|
||||
deriving::call_intrinsic(cx, span, sym::discriminant_value, vec![self_addr]);
|
||||
let let_stmt = cx.stmt_let(span, false, ident, variant_value);
|
||||
index_let_stmts.push(let_stmt);
|
||||
|
||||
match first_ident {
|
||||
Some(first) => {
|
||||
let first_expr = cx.expr_ident(span, first);
|
||||
let id = cx.expr_ident(span, ident);
|
||||
let test = cx.expr_binary(span, BinOpKind::Eq, first_expr, id);
|
||||
discriminant_test =
|
||||
cx.expr_binary(span, BinOpKind::And, discriminant_test, test)
|
||||
}
|
||||
None => {
|
||||
first_ident = Some(ident);
|
||||
}
|
||||
if i > 0 {
|
||||
let id0 = cx.expr_ident(span, vi_idents[0]);
|
||||
let id = cx.expr_ident(span, ident);
|
||||
let test = cx.expr_binary(span, BinOpKind::Eq, id0, id);
|
||||
discriminant_test = if i == 1 {
|
||||
test
|
||||
} else {
|
||||
cx.expr_binary(span, BinOpKind::And, discriminant_test, test)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1453,7 +1448,7 @@ impl<'a> MethodDef<'a> {
|
|||
// }
|
||||
// }
|
||||
// else {
|
||||
// <delegated expression referring to __self0_vi, et al.>
|
||||
// <delegated expression referring to __self_vi, et al.>
|
||||
// }
|
||||
let all_match = cx.expr_match(span, match_arg, match_arms);
|
||||
let arm_expr = cx.expr_if(span, discriminant_test, all_match, Some(arm_expr));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue