Skip to content

Commit 8f2fc9d

Browse files
committed
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
2 parents 3c97cbe + f2d8a00 commit 8f2fc9d

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/librustc/traits/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,11 @@ pub fn get_vtable_methods<'a, 'tcx>(
628628
|_, _| tcx.mk_region(ty::ReErased),
629629
|def, _| trait_ref.substs().type_for_def(def));
630630

631+
// the trait type may have higher-ranked lifetimes in it;
632+
// so erase them if they appear, so that we get the type
633+
// at some particular call site
634+
let substs = tcx.erase_late_bound_regions_and_normalize(&ty::Binder(substs));
635+
631636
// It's possible that the method relies on where clauses that
632637
// do not hold for this particular set of type parameters.
633638
// Note that this method could then never be called, so we

src/librustc_trans/collector.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1090,13 +1090,16 @@ fn create_trans_items_for_vtable_methods<'a, 'tcx>(scx: &SharedCrateContext<'a,
10901090
trait_ty: ty::Ty<'tcx>,
10911091
impl_ty: ty::Ty<'tcx>,
10921092
output: &mut Vec<TransItem<'tcx>>) {
1093-
assert!(!trait_ty.needs_subst() && !impl_ty.needs_subst());
1093+
assert!(!trait_ty.needs_subst() && !trait_ty.has_escaping_regions() &&
1094+
!impl_ty.needs_subst() && !impl_ty.has_escaping_regions());
10941095

10951096
if let ty::TyDynamic(ref trait_ty, ..) = trait_ty.sty {
10961097
if let Some(principal) = trait_ty.principal() {
10971098
let poly_trait_ref = principal.with_self_ty(scx.tcx(), impl_ty);
10981099
let param_substs = scx.tcx().intern_substs(&[]);
10991100

1101+
assert!(!poly_trait_ref.has_escaping_regions());
1102+
11001103
// Walk all methods of the trait, including those of its supertraits
11011104
let methods = traits::get_vtable_methods(scx.tcx(), poly_trait_ref);
11021105
let methods = methods.filter_map(|method| method)

src/test/run-pass/issue-39292.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for issue #39292. The object vtable was being
12+
// incorrectly left with a null pointer.
13+
14+
trait Foo<T> {
15+
fn print<'a>(&'a self) where T: 'a { println!("foo"); }
16+
}
17+
18+
impl<'a> Foo<&'a ()> for () { }
19+
20+
trait Bar: for<'a> Foo<&'a ()> { }
21+
22+
impl Bar for () {}
23+
24+
fn main() {
25+
(&() as &Bar).print(); // Segfault
26+
}

0 commit comments

Comments
 (0)