]> git.lizzy.rs Git - rust.git/commitdiff
Avoid deref/ref cycles for no-op coercions between unsafe pointers
authorBjörn Steinbrink <bsteinbr@gmail.com>
Tue, 16 Jun 2015 08:53:06 +0000 (10:53 +0200)
committerBjörn Steinbrink <bsteinbr@gmail.com>
Tue, 16 Jun 2015 12:10:52 +0000 (14:10 +0200)
Unlike coercing from reference to unsafe pointer, coercing between two
unsafe pointers doesn't need an AutoDerefRef, because there is no region
that regionck would need to know about.

In unoptimized libcore, this reduces the number of "auto_deref" allocas
from 174 to 4.

src/librustc_typeck/check/coercion.rs
src/test/codegen/coercions.rs [new file with mode: 0644]
src/test/compile-fail/issue-16538.rs

index 91b31bd0bc9572deb31c3595bd801bca96a1ea07..2c332b65a48d4015c3c144e1ee675d89e392e523 100644 (file)
@@ -403,8 +403,9 @@ fn coerce_unsafe_ptr(&self,
                a.repr(self.tcx()),
                b.repr(self.tcx()));
 
-        let mt_a = match a.sty {
-            ty::TyRef(_, mt) | ty::TyRawPtr(mt) => mt,
+        let (is_ref, mt_a) = match a.sty {
+            ty::TyRef(_, mt) => (true, mt),
+            ty::TyRawPtr(mt) => (false, mt),
             _ => {
                 return self.subtype(a, b);
             }
@@ -418,11 +419,15 @@ fn coerce_unsafe_ptr(&self,
         // Although references and unsafe ptrs have the same
         // representation, we still register an AutoDerefRef so that
         // regionck knows that the region for `a` must be valid here.
-        Ok(Some(AdjustDerefRef(AutoDerefRef {
-            autoderefs: 1,
-            autoref: Some(ty::AutoUnsafe(mutbl_b)),
-            unsize: None
-        })))
+        if is_ref {
+            Ok(Some(AdjustDerefRef(AutoDerefRef {
+                autoderefs: 1,
+                autoref: Some(ty::AutoUnsafe(mutbl_b)),
+                unsize: None
+            })))
+        } else {
+            Ok(None)
+        }
     }
 }
 
diff --git a/src/test/codegen/coercions.rs b/src/test/codegen/coercions.rs
new file mode 100644 (file)
index 0000000..2a136d7
--- /dev/null
@@ -0,0 +1,27 @@
+// 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.
+
+// compile-flags: -C no-prepopulate-passes
+
+static X: i32 = 5;
+
+// CHECK-LABEL: @raw_ptr_to_raw_ptr_noop
+// CHECK-NOT: alloca
+#[no_mangle]
+pub fn raw_ptr_to_raw_ptr_noop() -> *const i32{
+    &X as *const i32
+}
+
+// CHECK-LABEL: @reference_to_raw_ptr_noop
+// CHECK-NOT: alloca
+#[no_mangle]
+pub fn reference_to_raw_ptr_noop() -> *const i32 {
+    &X
+}
index 2b53e92d9bce6b63b3a510bfed7e096c146e1f93..6f627bfe704a85d50329de907925fa8930ba68e1 100644 (file)
@@ -20,6 +20,7 @@ fn foo(value: *const X) -> *const X {
 
 static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
 //~^ ERROR the trait `core::marker::Sync` is not implemented for the type
+//~| ERROR cannot refer to other statics by value, use the address-of operator or a constant instead
 //~| ERROR E0015
 
 fn main() {}