]> git.lizzy.rs Git - rust.git/commitdiff
mark user-defined derefs as non-constant
authorAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Tue, 7 Jul 2015 15:45:52 +0000 (18:45 +0300)
committerAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Tue, 7 Jul 2015 15:48:28 +0000 (18:48 +0300)
Fixes #25901

src/librustc/diagnostics.rs
src/librustc/middle/check_const.rs
src/test/compile-fail/issue-25901.rs [new file with mode: 0644]

index eb504d03209f60216eb5ddcaf9be322aae916dd6..c329f2fb012db00519bc9b9e8452ce697c512cbe 100644 (file)
@@ -1237,5 +1237,6 @@ fn foo<'a>(arg: &Box<SomeTrait+'a>) { ... }
     E0314, // closure outlives stack frame
     E0315, // cannot invoke closure outside of its lifetime
     E0316, // nested quantification of lifetimes
-    E0370  // discriminant overflow
+    E0370, // discriminant overflow
+    E0400  // overloaded derefs are not allowed in constants
 }
index b5c78340d022d19aa9de7adae205244cfa343c27..59f91a50f74212e92c9422a070f2550429ca3610 100644 (file)
@@ -405,6 +405,7 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
 
         let node_ty = self.tcx.node_id_to_type(ex.id);
         check_expr(self, ex, node_ty);
+        check_adjustments(self, ex);
 
         // Special-case some expressions to avoid certain flags bubbling up.
         match ex.node {
@@ -777,6 +778,25 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
     }
 }
 
+/// Check the adjustments of an expression
+fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &ast::Expr) {
+    match v.tcx.tables.borrow().adjustments.get(&e.id) {
+        None | Some(&ty::AdjustReifyFnPointer) | Some(&ty::AdjustUnsafeFnPointer) => {}
+        Some(&ty::AdjustDerefRef(ty::AutoDerefRef { autoderefs, .. })) => {
+            if (0..autoderefs as u32).any(|autoderef| {
+                    v.tcx.is_overloaded_autoderef(e.id, autoderef)
+            }) {
+                v.add_qualif(ConstQualif::NOT_CONST);
+                if v.mode != Mode::Var {
+                    span_err!(v.tcx.sess, e.span, E0400,
+                              "user-defined dereference operators are not allowed in {}s",
+                              v.msg());
+                }
+            }
+        }
+    }
+}
+
 pub fn check_crate(tcx: &ty::ctxt) {
     visit::walk_crate(&mut CheckCrateVisitor {
         tcx: tcx,
diff --git a/src/test/compile-fail/issue-25901.rs b/src/test/compile-fail/issue-25901.rs
new file mode 100644 (file)
index 0000000..3254f0b
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2015 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.
+
+struct A;
+struct B;
+
+static S: &'static B = &A; //~ ERROR user-defined dereference operators
+
+use std::ops::Deref;
+
+impl Deref for A {
+    type Target = B;
+    fn deref(&self)->&B { static B_: B = B; &B_ }
+}
+
+fn main(){}