about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-02-18 12:17:06 +0000
committerbors <bors@rust-lang.org>2017-02-18 12:17:06 +0000
commit8f2fc9db15d41285619138900c6f65686bd32a2a (patch)
treee0d56fbc406bc04f42a4d04445bcb579bbd5ddf6
parent3c97cbe4c29271bccaf29204310aa0c2779ab8f4 (diff)
parentf2d8a0019188d4db19afc22a3fd8e3df28b28da9 (diff)
downloadrust-8f2fc9db15d41285619138900c6f65686bd32a2a.tar.gz
rust-8f2fc9db15d41285619138900c6f65686bd32a2a.zip
Auto merge of #39887 - nikomatsakis:issue-39292, r=arielb1
erase late bound regions in `get_vtable_methods()`

Higher-ranked object types can otherwise cause late-bound regions to
sneak into the substs, leading to the false conclusion that some method
is unreachable.

r? @arielb1, who wrote the heart of this patch anyhow

Fixes #39292
-rw-r--r--src/librustc/traits/mod.rs5
-rw-r--r--src/librustc_trans/collector.rs5
-rw-r--r--src/test/run-pass/issue-39292.rs26
3 files changed, 35 insertions, 1 deletions
diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs
index 4893e240911..58ab713ef27 100644
--- a/src/librustc/traits/mod.rs
+++ b/src/librustc/traits/mod.rs
@@ -628,6 +628,11 @@ pub fn get_vtable_methods<'a, 'tcx>(
                                           |_, _| tcx.mk_region(ty::ReErased),
                                           |def, _| trait_ref.substs().type_for_def(def));
 
+            // the trait type may have higher-ranked lifetimes in it;
+            // so erase them if they appear, so that we get the type
+            // at some particular call site
+            let substs = tcx.erase_late_bound_regions_and_normalize(&ty::Binder(substs));
+
             // It's possible that the method relies on where clauses that
             // do not hold for this particular set of type parameters.
             // Note that this method could then never be called, so we
diff --git a/src/librustc_trans/collector.rs b/src/librustc_trans/collector.rs
index 89f5c00e9c1..b5f948442b7 100644
--- a/src/librustc_trans/collector.rs
+++ b/src/librustc_trans/collector.rs
@@ -1090,13 +1090,16 @@ fn create_trans_items_for_vtable_methods<'a, 'tcx>(scx: &SharedCrateContext<'a,
                                                    trait_ty: ty::Ty<'tcx>,
                                                    impl_ty: ty::Ty<'tcx>,
                                                    output: &mut Vec<TransItem<'tcx>>) {
-    assert!(!trait_ty.needs_subst() && !impl_ty.needs_subst());
+    assert!(!trait_ty.needs_subst() && !trait_ty.has_escaping_regions() &&
+            !impl_ty.needs_subst() && !impl_ty.has_escaping_regions());
 
     if let ty::TyDynamic(ref trait_ty, ..) = trait_ty.sty {
         if let Some(principal) = trait_ty.principal() {
             let poly_trait_ref = principal.with_self_ty(scx.tcx(), impl_ty);
             let param_substs = scx.tcx().intern_substs(&[]);
 
+            assert!(!poly_trait_ref.has_escaping_regions());
+
             // Walk all methods of the trait, including those of its supertraits
             let methods = traits::get_vtable_methods(scx.tcx(), poly_trait_ref);
             let methods = methods.filter_map(|method| method)
diff --git a/src/test/run-pass/issue-39292.rs b/src/test/run-pass/issue-39292.rs
new file mode 100644
index 00000000000..dc2b21f3470
--- /dev/null
+++ b/src/test/run-pass/issue-39292.rs
@@ -0,0 +1,26 @@
+// Copyright 2016 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.
+
+// Regression test for issue #39292. The object vtable was being
+// incorrectly left with a null pointer.
+
+trait Foo<T> {
+    fn print<'a>(&'a self) where T: 'a { println!("foo"); }
+}
+
+impl<'a> Foo<&'a ()> for () { }
+
+trait Bar: for<'a> Foo<&'a ()> { }
+
+impl Bar for () {}
+
+fn main() {
+    (&() as &Bar).print(); // Segfault
+}