about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2018-05-10 12:06:51 +0300
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2018-05-16 14:11:01 +0300
commitd79dee0b62fb33bcb2febac60ced71f28dccc602 (patch)
tree3ae271316e0678a5b01b09a6a3bbe806cf0fcb6b /src
parentb9af400a4659f722843ac96c251ec52f87998dd2 (diff)
downloadrust-d79dee0b62fb33bcb2febac60ced71f28dccc602.tar.gz
rust-d79dee0b62fb33bcb2febac60ced71f28dccc602.zip
rustc_mir: also promote interior borrows, not just whole temps.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/transform/qualify_consts.rs15
-rw-r--r--src/test/run-pass/issue-49955.rs30
2 files changed, 43 insertions, 2 deletions
diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs
index 28ab3d6a857..544f122f25a 100644
--- a/src/librustc_mir/transform/qualify_consts.rs
+++ b/src/librustc_mir/transform/qualify_consts.rs
@@ -737,10 +737,21 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
                 // We might have a candidate for promotion.
                 let candidate = Candidate::Ref(location);
                 if self.can_promote() {
-                    // We can only promote direct borrows of temps.
+                    // We can only promote interior borrows of non-drop temps.
+                    let mut place = place;
+                    while let Place::Projection(ref proj) = *place {
+                        if proj.elem == ProjectionElem::Deref {
+                            break;
+                        }
+                        place = &proj.base;
+                    }
                     if let Place::Local(local) = *place {
                         if self.mir.local_kind(local) == LocalKind::Temp {
-                            self.promotion_candidates.push(candidate);
+                            if let Some(qualif) = self.temp_qualif[local] {
+                                if !qualif.intersects(Qualif::NEEDS_DROP) {
+                                    self.promotion_candidates.push(candidate);
+                                }
+                            }
                         }
                     }
                 }
diff --git a/src/test/run-pass/issue-49955.rs b/src/test/run-pass/issue-49955.rs
new file mode 100644
index 00000000000..2d36806ef4f
--- /dev/null
+++ b/src/test/run-pass/issue-49955.rs
@@ -0,0 +1,30 @@
+// Copyright 2018 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: -Z borrowck=compare
+
+const ALL_THE_NUMS: [u32; 1] = [
+    1
+];
+
+#[inline(never)]
+fn array(i: usize) -> &'static u32 {
+    return &ALL_THE_NUMS[i];
+}
+
+#[inline(never)]
+fn tuple_field() -> &'static u32 {
+    &(42,).0
+}
+
+fn main() {
+    assert_eq!(tuple_field().to_string(), "42");
+    // assert_eq!(array(0).to_string(), "1");
+}