Skip to content

Commit 9421141

Browse files
committed
Auto merge of #44516 - gaurikholkar:fns, r=arielb1
Extend E0623 for fn items This fixes #44516 The below example now gives ``` error[E0623]: lifetime mismatch --> gg.rs:3:10 | 2 | fn foo(x:fn(&u8, &u8), y: Vec<&u8>, z: &u8) { | --- --- these two types are declared with different lifetimes... 3 | y.push(z); | ^ ...but data from `z` flows into `y` here error: aborting due to previous error ``` r? @nikomatsakis cc @arielb1
2 parents 539f208 + e71eef1 commit 9421141

11 files changed

+151
-2
lines changed

fn.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
fn foo(x: fn(&u8, &u8), y: Vec<&u8>, z: &u8) {
3+
// Debruijn 1 1 1 1
4+
// Anon-Index 0 1 0 1
5+
// ------
6+
// debruijn indices are shifted by 1 in here
7+
y.push(z); // index will be zero or one
8+
}

src/librustc/infer/error_reporting/different_lifetimes.rs

+19-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> {
@@ -204,6 +206,21 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
204206

205207
fn visit_ty(&mut self, arg: &'gcx hir::Ty) {
206208
match arg.node {
209+
hir::TyBareFn(_) => {
210+
self.depth += 1;
211+
intravisit::walk_ty(self, arg);
212+
self.depth -= 1;
213+
return;
214+
}
215+
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+
207224
hir::TyRptr(ref lifetime, _) => {
208225
// the lifetime of the TyRptr
209226
let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
@@ -217,7 +234,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
217234
debruijn_index.depth,
218235
anon_index,
219236
br_index);
220-
if debruijn_index.depth == 1 && anon_index == br_index {
237+
if debruijn_index.depth == self.depth && anon_index == br_index {
221238
self.found_type = Some(arg);
222239
return; // we can stop visiting now
223240
}
@@ -246,7 +263,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
246263
debug!("self.infcx.tcx.hir.local_def_id(id)={:?}",
247264
self.infcx.tcx.hir.local_def_id(id));
248265
debug!("def_id={:?}", def_id);
249-
if debruijn_index.depth == 1 &&
266+
if debruijn_index.depth == self.depth &&
250267
self.infcx.tcx.hir.local_def_id(id) == def_id {
251268
self.found_type = Some(arg);
252269
return; // we can stop visiting now
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0623]: lifetime mismatch
2+
--> $DIR/ex3-both-anon-regions-4.rs:12:13
3+
|
4+
11 | fn foo(z: &mut Vec<(&u8,&u8)>, (x, y): (&u8, &u8)) {
5+
| --- --- these references are declared with different lifetimes...
6+
12 | z.push((x,y));
7+
| ^ ...but data flows into `z` here
8+
9+
error[E0623]: lifetime mismatch
10+
--> $DIR/ex3-both-anon-regions-4.rs:12:15
11+
|
12+
11 | fn foo(z: &mut Vec<(&u8,&u8)>, (x, y): (&u8, &u8)) {
13+
| --- --- these references are declared with different lifetimes...
14+
12 | z.push((x,y));
15+
| ^ ...but data flows into `z` here
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
struct Ref<'a, 'b> {
11+
a: &'a u32,
12+
b: &'b u32,
13+
}
14+
15+
fn foo(mut x: Ref) {
16+
x.a = x.b;
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0623]: lifetime mismatch
2+
--> $DIR/ex3-both-anon-regions-both-are-structs-4.rs:16:11
3+
|
4+
15 | fn foo(mut x: Ref) {
5+
| ---
6+
| |
7+
| this type was declared with multiple lifetimes...
8+
16 | x.a = x.b;
9+
| ^^^ ...but data with one lifetime flows into the other here
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
11+
struct Ref<'a, 'b> { a: &'a u32, b: &'b u32 }
12+
13+
fn foo(mut y: Ref, x: &u32) {
14+
y.b = x;
15+
}
16+
17+
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-one-is-struct-4.rs:14:11
3+
|
4+
13 | fn foo(mut y: Ref, x: &u32) {
5+
| --- ---- these two types are declared with different lifetimes...
6+
14 | y.b = x;
7+
| ^ ...but data from `x` flows into `y` here
8+
9+
error: aborting due to previous error
10+
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: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-fn-items.rs:11:10
3+
|
4+
10 | fn foo(x: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+
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)