Skip to content

Commit 6e3cdce

Browse files
committed
Adding changes for trait objects
1 parent 93529b4 commit 6e3cdce

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/librustc/infer/error_reporting/different_lifetimes.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
173173
hir_map: &self.tcx.hir,
174174
bound_region: *br,
175175
found_type: None,
176+
depth: 1,
176177
};
177178
nested_visitor.visit_ty(arg);
178179
nested_visitor.found_type
@@ -195,6 +196,7 @@ struct FindNestedTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
195196
// The type where the anonymous lifetime appears
196197
// for e.g. Vec<`&u8`> and <`&u8`>
197198
found_type: Option<&'gcx hir::Ty>,
199+
depth: u32,
198200
}
199201

200202
impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
@@ -211,6 +213,14 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
211213
return;
212214
}
213215

216+
hir::TyTraitObject(ref bounds, _) => {
217+
for bound in bounds {
218+
self.depth += 1;
219+
self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
220+
self.depth -= 1;
221+
}
222+
}
223+
214224
hir::TyRptr(ref lifetime, _) => {
215225
// the lifetime of the TyRptr
216226
let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
@@ -224,7 +234,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
224234
debruijn_index.depth,
225235
anon_index,
226236
br_index);
227-
if debruijn_index.depth == 1 && anon_index == br_index {
237+
if debruijn_index.depth == self.depth && anon_index == br_index {
228238
self.found_type = Some(arg);
229239
return; // we can stop visiting now
230240
}
@@ -253,7 +263,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
253263
debug!("self.infcx.tcx.hir.local_def_id(id)={:?}",
254264
self.infcx.tcx.hir.local_def_id(id));
255265
debug!("def_id={:?}", def_id);
256-
if debruijn_index.depth == 1 &&
266+
if debruijn_index.depth == self.depth &&
257267
self.infcx.tcx.hir.local_def_id(id) == def_id {
258268
self.found_type = Some(arg);
259269
return; // we can stop visiting now
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 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+
fn foo(x:Box<Fn(&u8, &u8)> , y: Vec<&u8>, z: &u8) {
11+
y.push(z);
12+
}
13+
14+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0623]: lifetime mismatch
2+
--> $DIR/ex3-both-anon-regions-using-trait-objects.rs:11:10
3+
|
4+
10 | fn foo(x:Box<Fn(&u8, &u8)> , y: Vec<&u8>, z: &u8) {
5+
| --- --- these two types are declared with different lifetimes...
6+
11 | y.push(z);
7+
| ^ ...but data from `z` flows into `y` here
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)