From 3b4ed18d1c0cd0b33606cf2baeaaa64c67e587da Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 6 Aug 2018 12:17:39 +0200 Subject: [PATCH] add test case showing how previous attempt was unsound --- src/test/ui/borrowck/issue-52713-bug.rs | 29 +++++++++++++++++++++ src/test/ui/borrowck/issue-52713-bug.stderr | 14 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/test/ui/borrowck/issue-52713-bug.rs create mode 100644 src/test/ui/borrowck/issue-52713-bug.stderr diff --git a/src/test/ui/borrowck/issue-52713-bug.rs b/src/test/ui/borrowck/issue-52713-bug.rs new file mode 100644 index 0000000000000..4ecba64b27112 --- /dev/null +++ b/src/test/ui/borrowck/issue-52713-bug.rs @@ -0,0 +1,29 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Regression test for a bug in #52713: this was an optimization for +// computing liveness that wound up accidentally causing the program +// below to be accepted. + +#![feature(nll)] + +fn foo<'a>(x: &'a mut u32) -> u32 { + let mut x = 22; + let y = &x; + if false { + return x; + } + + x += 1; //~ ERROR + println!("{}", y); + return 0; +} + +fn main() { } diff --git a/src/test/ui/borrowck/issue-52713-bug.stderr b/src/test/ui/borrowck/issue-52713-bug.stderr new file mode 100644 index 0000000000000..7d9b57400dda3 --- /dev/null +++ b/src/test/ui/borrowck/issue-52713-bug.stderr @@ -0,0 +1,14 @@ +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/issue-52713-bug.rs:24:5 + | +LL | let y = &x; + | -- borrow of `x` occurs here +... +LL | x += 1; //~ ERROR + | ^^^^^^ assignment to borrowed `x` occurs here +LL | println!("{}", y); + | - borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0506`.