Skip to content

Commit 49ef569

Browse files
committed
Don't allow single-variant enums to be dereferenced. rust-lang#6246
I'm not sure if this was even intentional at this point.
1 parent 22eb11c commit 49ef569

File tree

7 files changed

+12
-99
lines changed

7 files changed

+12
-99
lines changed

src/librustc/middle/privacy.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -414,19 +414,6 @@ impl<'self> PrivacyVisitor<'self> {
414414
return false;
415415
}
416416

417-
// Checks that a dereference of a univariant enum can occur.
418-
fn check_variant(&self, span: Span, enum_id: ast::DefId) {
419-
let variant_info = ty::enum_variants(self.tcx, enum_id)[0];
420-
421-
match self.def_privacy(variant_info.id) {
422-
Allowable => {}
423-
ExternallyDenied | DisallowedBy(*) => {
424-
self.tcx.sess.span_err(span, "can only dereference enums \
425-
with a single, public variant");
426-
}
427-
}
428-
}
429-
430417
// Checks that a field is in scope.
431418
// FIXME #6993: change type (and name) from Ident to Name
432419
fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident) {
@@ -739,18 +726,6 @@ impl<'self> Visitor<()> for PrivacyVisitor<'self> {
739726
struct type?!"),
740727
}
741728
}
742-
ast::ExprUnary(_, ast::UnDeref, operand) => {
743-
// In *e, we need to check that if e's type is an
744-
// enum type t, then t's first variant is public or
745-
// privileged. (We can assume it has only one variant
746-
// since typeck already happened.)
747-
match ty::get(ty::expr_ty(self.tcx, operand)).sty {
748-
ty::ty_enum(id, _) => {
749-
self.check_variant(expr.span, id);
750-
}
751-
_ => { /* No check needed */ }
752-
}
753-
}
754729
_ => {}
755730
}
756731

src/librustc/middle/resolve.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,10 @@ impl Resolver {
12751275
} if path.segments.len() == 1 => {
12761276
let name = path_to_ident(path);
12771277

1278-
let new_parent = match parent.children.find(&name.name) {
1278+
let ModuleReducedGraphParent(@Module {
1279+
children: ref children, _
1280+
}) = parent;
1281+
let new_parent = match children.find(&name.name) {
12791282
// It already exists
12801283
Some(&child) if child.get_module_if_available()
12811284
.is_some() &&
@@ -1542,7 +1545,11 @@ impl Resolver {
15421545
false,
15431546
true);
15441547

1545-
parent.external_module_children.insert(
1548+
let ModuleReducedGraphParent(@Module {
1549+
external_module_children: ref external_module_children, _
1550+
}) = parent;
1551+
1552+
external_module_children.insert(
15461553
name.name,
15471554
external_module);
15481555

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
@@ -2675,16 +2675,6 @@ pub fn deref_sty(cx: ctxt, sty: &sty, explicit: bool) -> Option<mt> {
26752675
Some(mt)
26762676
}
26772677

2678-
ty_enum(did, ref substs) => {
2679-
let variants = enum_variants(cx, did);
2680-
if (*variants).len() == 1u && variants[0].args.len() == 1u {
2681-
let v_t = subst(cx, substs, variants[0].args[0]);
2682-
Some(mt {ty: v_t, mutbl: ast::MutImmutable})
2683-
} else {
2684-
None
2685-
}
2686-
}
2687-
26882678
ty_struct(did, ref substs) => {
26892679
let fields = struct_fields(cx, did, substs);
26902680
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
@@ -2475,12 +2475,6 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
24752475
}
24762476
None => {
24772477
match *sty {
2478-
ty::ty_enum(*) => {
2479-
tcx.sess.span_err(
2480-
expr.span,
2481-
"can only dereference enums with a single variant which \
2482-
has a single argument");
2483-
}
24842478
ty::ty_struct(*) => {
24852479
tcx.sess.span_err(
24862480
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)