Skip to content

Commit 18cef3f

Browse files
committed
Don't allow single-variant enums to be dereferenced. #6246
I'm not sure if this was even intentional at this point.
1 parent 8990708 commit 18cef3f

File tree

6 files changed

+3
-97
lines changed

6 files changed

+3
-97
lines changed

src/librustc/middle/privacy.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -540,19 +540,6 @@ impl<'a> PrivacyVisitor<'a> {
540540
return false;
541541
}
542542

543-
// Checks that a dereference of a univariant enum can occur.
544-
fn check_variant(&self, span: Span, enum_id: ast::DefId) {
545-
let variant_info = ty::enum_variants(self.tcx, enum_id)[0];
546-
547-
match self.def_privacy(variant_info.id) {
548-
Allowable => {}
549-
ExternallyDenied | DisallowedBy(..) => {
550-
self.tcx.sess.span_err(span, "can only dereference enums \
551-
with a single, public variant");
552-
}
553-
}
554-
}
555-
556543
// Checks that a field is in scope.
557544
// FIXME #6993: change type (and name) from Ident to Name
558545
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident) {
@@ -713,18 +700,6 @@ impl<'a> Visitor<()> for PrivacyVisitor<'a> {
713700
struct type?!"),
714701
}
715702
}
716-
ast::ExprUnary(_, ast::UnDeref, operand) => {
717-
// In *e, we need to check that if e's type is an
718-
// enum type t, then t's first variant is public or
719-
// privileged. (We can assume it has only one variant
720-
// since typeck already happened.)
721-
match ty::get(ty::expr_ty(self.tcx, operand)).sty {
722-
ty::ty_enum(id, _) => {
723-
self.check_variant(expr.span, id);
724-
}
725-
_ => { /* No check needed */ }
726-
}
727-
}
728703
_ => {}
729704
}
730705

src/librustc/middle/trans/datum.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -628,37 +628,6 @@ impl Datum {
628628
ty::ty_rptr(_, mt) => {
629629
return (Some(deref_ptr(bcx, self, mt.ty)), bcx);
630630
}
631-
ty::ty_enum(did, ref substs) => {
632-
// Check whether this enum is a newtype enum:
633-
let variants = ty::enum_variants(ccx.tcx, did);
634-
if (*variants).len() != 1 || variants[0].args.len() != 1 {
635-
return (None, bcx);
636-
}
637-
638-
let repr = adt::represent_type(ccx, self.ty);
639-
let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]);
640-
return match self.mode {
641-
ByRef(_) => {
642-
// Recast lv.val as a pointer to the newtype
643-
// rather than a ptr to the enum type.
644-
(
645-
Some(Datum {
646-
val: adt::trans_field_ptr(bcx, repr, self.val,
647-
0, 0),
648-
ty: ty,
649-
mode: ByRef(ZeroMem)
650-
}),
651-
bcx
652-
)
653-
}
654-
ByValue => {
655-
// Actually, this case cannot happen right
656-
// now, because enums are never immediate.
657-
assert!(type_is_immediate(bcx.ccx(), ty));
658-
(Some(Datum {ty: ty, ..*self}), bcx)
659-
}
660-
};
661-
}
662631
ty::ty_struct(did, ref substs) => {
663632
// Check whether this struct is a newtype struct.
664633
let fields = ty::struct_fields(ccx.tcx, did, substs);

src/librustc/middle/ty.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,16 +2631,6 @@ pub fn deref_sty(cx: ctxt, sty: &sty, explicit: bool) -> Option<mt> {
26312631
Some(mt)
26322632
}
26332633

2634-
ty_enum(did, ref substs) => {
2635-
let variants = enum_variants(cx, did);
2636-
if (*variants).len() == 1u && variants[0].args.len() == 1u {
2637-
let v_t = subst(cx, substs, variants[0].args[0]);
2638-
Some(mt {ty: v_t, mutbl: ast::MutImmutable})
2639-
} else {
2640-
None
2641-
}
2642-
}
2643-
26442634
ty_struct(did, ref substs) => {
26452635
let fields = struct_fields(cx, did, substs);
26462636
if fields.len() == 1 && fields[0].ident ==

src/librustc/middle/typeck/check/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,12 +2765,6 @@ pub fn check_expr_with_unifier(fcx: @FnCtxt,
27652765
}
27662766
None => {
27672767
match *sty {
2768-
ty::ty_enum(..) => {
2769-
tcx.sess.span_err(
2770-
expr.span,
2771-
"can only dereference enums with a single variant which \
2772-
has a single argument");
2773-
}
27742768
ty::ty_struct(..) => {
27752769
tcx.sess.span_err(
27762770
expr.span,

src/test/compile-fail/issue-818.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/test/run-pass/explicit-self-generic.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ fn linear_map<K,V>() -> HashMap<K,V> {
3232

3333
impl<K,V> HashMap<K,V> {
3434
pub fn len(&mut self) -> uint {
35-
self.size
35+
match *self {
36+
HashMap_(l) => l.size
37+
}
3638
}
3739
}
3840

0 commit comments

Comments
 (0)