Skip to content

Commit bfb09ee

Browse files
committed
Merge pull request #4164 from brson/deriving
Fix deriving for single-variant enums
2 parents 6e38e33 + 7d556e1 commit bfb09ee

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

Diff for: src/libsyntax/ext/deriving.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -698,26 +698,30 @@ fn expand_deriving_eq_enum_method(cx: ext_ctxt,
698698
};
699699
other_arms.push(move matching_arm);
700700

701-
// Create the nonmatching pattern.
702-
let nonmatching_pat = @{
703-
id: cx.next_id(),
704-
node: pat_wild,
705-
span: span
706-
};
707-
708-
// Create the nonmatching pattern body.
709-
let nonmatching_expr = build::mk_bool(cx, span, !is_eq);
710-
let nonmatching_body_block = build::mk_simple_block(cx,
711-
span,
712-
nonmatching_expr);
713-
714-
// Create the nonmatching arm.
715-
let nonmatching_arm = {
716-
pats: ~[ nonmatching_pat ],
717-
guard: None,
718-
body: move nonmatching_body_block
719-
};
720-
other_arms.push(move nonmatching_arm);
701+
// Maybe generate a non-matching case. If there is only one
702+
// variant then there will always be a match.
703+
if enum_definition.variants.len() > 1 {
704+
// Create the nonmatching pattern.
705+
let nonmatching_pat = @{
706+
id: cx.next_id(),
707+
node: pat_wild,
708+
span: span
709+
};
710+
711+
// Create the nonmatching pattern body.
712+
let nonmatching_expr = build::mk_bool(cx, span, !is_eq);
713+
let nonmatching_body_block = build::mk_simple_block(cx,
714+
span,
715+
nonmatching_expr);
716+
717+
// Create the nonmatching arm.
718+
let nonmatching_arm = {
719+
pats: ~[ nonmatching_pat ],
720+
guard: None,
721+
body: move nonmatching_body_block
722+
};
723+
other_arms.push(move nonmatching_arm);
724+
}
721725

722726
// Create the self pattern.
723727
let self_pat = create_enum_variant_pattern(cx,

Diff for: src/test/run-pass/deriving-enum-single-variant.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type task_id = int;
2+
3+
#[deriving_eq]
4+
pub enum Task {
5+
TaskHandle(task_id)
6+
}
7+
8+
fn main() { }

0 commit comments

Comments
 (0)