]> git.lizzy.rs Git - rust.git/commitdiff
Don't allow single-variant enums to be dereferenced. #6246
authorBrian Anderson <banderson@mozilla.com>
Thu, 31 Oct 2013 22:29:15 +0000 (15:29 -0700)
committerBrian Anderson <banderson@mozilla.com>
Sat, 4 Jan 2014 21:17:31 +0000 (13:17 -0800)
I'm not sure if this was even intentional at this point.

src/librustc/middle/privacy.rs
src/librustc/middle/trans/datum.rs
src/librustc/middle/ty.rs
src/librustc/middle/typeck/check/mod.rs
src/test/compile-fail/issue-818.rs [deleted file]
src/test/run-pass/explicit-self-generic.rs

index 435bdcc260747da1a660a48603358eaa5d983ae1..0fdc071dfe30b828dac75ee2e10e0503afd17aa4 100644 (file)
@@ -540,19 +540,6 @@ fn ensure_public(&self, span: Span, to_check: ast::DefId,
         return false;
     }
 
-    // Checks that a dereference of a univariant enum can occur.
-    fn check_variant(&self, span: Span, enum_id: ast::DefId) {
-        let variant_info = ty::enum_variants(self.tcx, enum_id)[0];
-
-        match self.def_privacy(variant_info.id) {
-            Allowable => {}
-            ExternallyDenied | DisallowedBy(..) => {
-                self.tcx.sess.span_err(span, "can only dereference enums \
-                                              with a single, public variant");
-            }
-        }
-    }
-
     // Checks that a field is in scope.
     // FIXME #6993: change type (and name) from Ident to Name
     fn check_field(&mut self, span: Span, id: ast::DefId, ident: ast::Ident) {
@@ -713,18 +700,6 @@ fn visit_expr(&mut self, expr: @ast::Expr, _: ()) {
                                                             struct type?!"),
                 }
             }
-            ast::ExprUnary(_, ast::UnDeref, operand) => {
-                // In *e, we need to check that if e's type is an
-                // enum type t, then t's first variant is public or
-                // privileged. (We can assume it has only one variant
-                // since typeck already happened.)
-                match ty::get(ty::expr_ty(self.tcx, operand)).sty {
-                    ty::ty_enum(id, _) => {
-                        self.check_variant(expr.span, id);
-                    }
-                    _ => { /* No check needed */ }
-                }
-            }
             _ => {}
         }
 
index f57f31a56f458aa90269dd6aa035487895ca2cf6..f547e370bb139445b674e83a8b98103b45c7b728 100644 (file)
@@ -628,37 +628,6 @@ pub fn try_deref(&self,
             ty::ty_rptr(_, mt) => {
                 return (Some(deref_ptr(bcx, self, mt.ty)), bcx);
             }
-            ty::ty_enum(did, ref substs) => {
-                // Check whether this enum is a newtype enum:
-                let variants = ty::enum_variants(ccx.tcx, did);
-                if (*variants).len() != 1 || variants[0].args.len() != 1 {
-                    return (None, bcx);
-                }
-
-                let repr = adt::represent_type(ccx, self.ty);
-                let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]);
-                return match self.mode {
-                    ByRef(_) => {
-                        // Recast lv.val as a pointer to the newtype
-                        // rather than a ptr to the enum type.
-                        (
-                            Some(Datum {
-                                val: adt::trans_field_ptr(bcx, repr, self.val,
-                                                    0, 0),
-                                ty: ty,
-                                mode: ByRef(ZeroMem)
-                            }),
-                            bcx
-                        )
-                    }
-                    ByValue => {
-                        // Actually, this case cannot happen right
-                        // now, because enums are never immediate.
-                        assert!(type_is_immediate(bcx.ccx(), ty));
-                        (Some(Datum {ty: ty, ..*self}), bcx)
-                    }
-                };
-            }
             ty::ty_struct(did, ref substs) => {
                 // Check whether this struct is a newtype struct.
                 let fields = ty::struct_fields(ccx.tcx, did, substs);
index 3ae29eade7778a69aa14bc870fc7f27a8c1ceae0..6a2fd3c7f5f94d84e71ff87bbc813da428c72cad 100644 (file)
@@ -2631,16 +2631,6 @@ pub fn deref_sty(cx: ctxt, sty: &sty, explicit: bool) -> Option<mt> {
         Some(mt)
       }
 
-      ty_enum(did, ref substs) => {
-        let variants = enum_variants(cx, did);
-        if (*variants).len() == 1u && variants[0].args.len() == 1u {
-            let v_t = subst(cx, substs, variants[0].args[0]);
-            Some(mt {ty: v_t, mutbl: ast::MutImmutable})
-        } else {
-            None
-        }
-      }
-
       ty_struct(did, ref substs) => {
         let fields = struct_fields(cx, did, substs);
         if fields.len() == 1 && fields[0].ident ==
index 088104e84ef4184cbe88c9e30249edb2c313aa4f..defd0893daafa1f12bb72195538fd55bf3280825 100644 (file)
@@ -2765,12 +2765,6 @@ fn check_struct_enum_variant(fcx: @FnCtxt,
                         }
                         None => {
                             match *sty {
-                                ty::ty_enum(..) => {
-                                    tcx.sess.span_err(
-                                        expr.span,
-                                        "can only dereference enums with a single variant which \
-                                         has a single argument");
-                                }
                                 ty::ty_struct(..) => {
                                     tcx.sess.span_err(
                                         expr.span,
diff --git a/src/test/compile-fail/issue-818.rs b/src/test/compile-fail/issue-818.rs
deleted file mode 100644 (file)
index df08652..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-mod ctr {
-
-    pub enum ctr { priv mkCtr(int) }
-
-    pub fn new(i: int) -> ctr { mkCtr(i) }
-    pub fn inc(c: ctr) -> ctr { mkCtr(*c + 1) }
-}
-
-
-fn main() {
-    let c = ctr::new(42);
-    let c2 = ctr::inc(c);
-    assert!(*c2 == 5); //~ ERROR can only dereference enums with a single, public variant
-}
index 023381949a3577f1316a1e4ce9b3574f0b94b868..fb533a3de585814d2feebd475560b8f505902e80 100644 (file)
@@ -32,7 +32,9 @@ fn linear_map<K,V>() -> HashMap<K,V> {
 
 impl<K,V> HashMap<K,V> {
     pub fn len(&mut self) -> uint {
-        self.size
+        match *self {
+            HashMap_(l) => l.size
+        }
     }
 }