From f676547c9788b919762bfb379b1e26ebd94d3dc9 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 19 Jul 2012 10:10:22 -0700 Subject: [PATCH] Fix intersection of two region params in infer, cc #2962 --- src/rustc/middle/typeck/infer.rs | 4 ++-- src/test/compile-fail/regions-infer-call-3.rs | 14 ++++++++++++++ src/test/run-pass/regions-infer-call-2.rs | 13 +++++++++++++ src/test/run-pass/regions-infer-call.rs | 9 +++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/regions-infer-call-3.rs create mode 100644 src/test/run-pass/regions-infer-call-2.rs create mode 100644 src/test/run-pass/regions-infer-call.rs diff --git a/src/rustc/middle/typeck/infer.rs b/src/rustc/middle/typeck/infer.rs index 2bc018395639..79df5975307d 100644 --- a/src/rustc/middle/typeck/infer.rs +++ b/src/rustc/middle/typeck/infer.rs @@ -2371,7 +2371,8 @@ fn regions(a: ty::region, b: ty::region) -> cres { } } - (ty::re_scope(a_id), ty::re_scope(b_id)) { + (ty::re_scope(a_id), ty::re_scope(b_id)) | + (ty::re_free(a_id, _), ty::re_free(b_id, _)) { // We want to generate a region that is contained by both of // these: so, if one of these scopes is a subscope of the // other, return it. Otherwise fail. @@ -2385,7 +2386,6 @@ fn regions(a: ty::region, b: ty::region) -> cres { // For these types, we cannot define any additional // relationship: - (ty::re_free(_, _), ty::re_free(_, _)) | (ty::re_bound(_), ty::re_bound(_)) | (ty::re_bound(_), ty::re_free(_, _)) | (ty::re_bound(_), ty::re_scope(_)) | diff --git a/src/test/compile-fail/regions-infer-call-3.rs b/src/test/compile-fail/regions-infer-call-3.rs new file mode 100644 index 000000000000..4a2be378d425 --- /dev/null +++ b/src/test/compile-fail/regions-infer-call-3.rs @@ -0,0 +1,14 @@ +fn select(x: &int, y: &int) -> &int { x } + +fn with(f: fn(x: &int) -> T) -> T { + f(&20) +} + +fn manip(x: &a/int) -> int { + let z = do with |y| { select(x, y) }; + //~^ ERROR reference is not valid outside of its lifetime + *z +} + +fn main() { +} \ No newline at end of file diff --git a/src/test/run-pass/regions-infer-call-2.rs b/src/test/run-pass/regions-infer-call-2.rs new file mode 100644 index 000000000000..9e4000f05993 --- /dev/null +++ b/src/test/run-pass/regions-infer-call-2.rs @@ -0,0 +1,13 @@ +fn takes_two(x: &int, y: &int) -> int { *x + *y } + +fn with(f: fn(x: &int) -> T) -> T { + f(&20) +} + +fn has_one(x: &a/int) -> int { + do with |y| { takes_two(x, y) } +} + +fn main() { + assert has_one(&2) == 22; +} \ No newline at end of file diff --git a/src/test/run-pass/regions-infer-call.rs b/src/test/run-pass/regions-infer-call.rs new file mode 100644 index 000000000000..f3f14d0250c6 --- /dev/null +++ b/src/test/run-pass/regions-infer-call.rs @@ -0,0 +1,9 @@ +fn takes_two(x: &int, y: &int) -> int { *x + *y } + +fn has_two(x: &a/int, y: &b/int) -> int { + takes_two(x, y) +} + +fn main() { + assert has_two(&20, &2) == 22; +} \ No newline at end of file