From e010c3fafeddd236cb48adecd50620bb27d9e55f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 9 May 2013 17:20:31 -0400 Subject: [PATCH] test: Add two tests for issue #6272. --- ...-autoborrowvec-combined-fail-issue-6272.rs | 21 ++++++++++ ...f-and-autoborrowvec-combined-issue-6272.rs | 41 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/test/run-fail/borrowck-wg-autoderef-and-autoborrowvec-combined-fail-issue-6272.rs create mode 100644 src/test/run-pass/borrowck-wg-autoderef-and-autoborrowvec-combined-issue-6272.rs diff --git a/src/test/run-fail/borrowck-wg-autoderef-and-autoborrowvec-combined-fail-issue-6272.rs b/src/test/run-fail/borrowck-wg-autoderef-and-autoborrowvec-combined-fail-issue-6272.rs new file mode 100644 index 0000000000000..2d3e7dd616d33 --- /dev/null +++ b/src/test/run-fail/borrowck-wg-autoderef-and-autoborrowvec-combined-fail-issue-6272.rs @@ -0,0 +1,21 @@ +// error-pattern:borrowed + +// Issue #6272. Tests that freezing correctly accounts for all the +// implicit derefs that can occur and freezes the innermost box. See +// the companion test +// +// run-pass/borrowck-wg-autoderef-and-autoborrowvec-combined-issue-6272.rs +// +// for a detailed explanation of what is going on here. + +fn main() { + let a = @mut [3i]; + let b = @mut [a]; + let c = @mut b; + + // this should freeze `a` only + let x: &mut [int] = c[0]; + + // hence this should fail + a[0] = a[0]; +} diff --git a/src/test/run-pass/borrowck-wg-autoderef-and-autoborrowvec-combined-issue-6272.rs b/src/test/run-pass/borrowck-wg-autoderef-and-autoborrowvec-combined-issue-6272.rs new file mode 100644 index 0000000000000..056397f55ff26 --- /dev/null +++ b/src/test/run-pass/borrowck-wg-autoderef-and-autoborrowvec-combined-issue-6272.rs @@ -0,0 +1,41 @@ +// Issue #6272. Tests that freezing correctly accounts for all the +// implicit derefs that can occur. +// +// In this particular case, the expression: +// +// let x: &mut [int] = c[0]; +// +// is seen by borrowck as this sequence of derefs +// and pointer offsets: +// +// &*((**c)[0]) +// +// or, written using `x.*` for `*x` (so that everything +// is a postfix operation): +// +// &c.*.*.[0].* +// ^ ^ +// | | +// b a +// +// Here I also indicated where the evaluation yields the boxes `a` and +// `b`. It is important then that we only freeze the innermost box +// (`a`), and not the other ones (`b`, `c`). +// +// Also see the companion test: +// +// run-fail/borrowck-wg-autoderef-and-autoborrowvec-combined-fail-issue-6272.rs + + +fn main() { + let a = @mut [3i]; + let b = @mut [a]; + let c = @mut b; + + // this should freeze `a` only + let _x: &mut [int] = c[0]; + + // hence these writes should not fail: + b[0] = b[0]; + c[0] = c[0]; +}