From 7868b8fffbe67b499f7fffcb668284968547915c Mon Sep 17 00:00:00 2001 From: Alex Gurganus Date: Tue, 5 Feb 2019 10:21:05 -0600 Subject: [PATCH 1/3] Fix borrow so it fails in 2018 edition Fixes #1141 --- src/scope/borrow.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/scope/borrow.md b/src/scope/borrow.md index e38b717b89..2784508e50 100644 --- a/src/scope/borrow.md +++ b/src/scope/borrow.md @@ -30,14 +30,13 @@ fn main() { borrow_i32(&stacked_i32); { - // Take a reference to the data contained inside the box - let _ref_to_i32: &i32 = &boxed_i32; - // Error! - // Can't destroy `boxed_i32` while the inner value is borrowed. + // Can't destroy `boxed_i32` while the inner value is borrowed later in scope. eat_box_i32(boxed_i32); // FIXME ^ Comment out this line + // Take a reference to the data contained inside the box + let _ref_to_i32: &i32 = &boxed_i32; // `_ref_to_i32` goes out of scope and is no longer borrowed. } From d960310aa88cc7c6a055814c476b839afa5af4e5 Mon Sep 17 00:00:00 2001 From: Alex Gurganus Date: Tue, 19 Feb 2019 23:56:10 -0600 Subject: [PATCH 2/3] Only trigger E505. A value is referenced. The underlying value is then destroyed. The reference is prevented from being used after the underlying value is destroyed. --- src/scope/borrow.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/scope/borrow.md b/src/scope/borrow.md index 2784508e50..51407f147c 100644 --- a/src/scope/borrow.md +++ b/src/scope/borrow.md @@ -29,18 +29,14 @@ fn main() { borrow_i32(&boxed_i32); borrow_i32(&stacked_i32); - { - // Error! - // Can't destroy `boxed_i32` while the inner value is borrowed later in scope. - eat_box_i32(boxed_i32); - // FIXME ^ Comment out this line - - // Take a reference to the data contained inside the box - let _ref_to_i32: &i32 = &boxed_i32; - // `_ref_to_i32` goes out of scope and is no longer borrowed. - } - - // `boxed_i32` can now give up ownership to `eat_box` and be destroyed + // Take a reference to the data contained inside the box + let _ref_to_i32: &i32 = &boxed_i32; + + // Can't destroy `boxed_i32` while the inner value is borrowed later in scope. eat_box_i32(boxed_i32); + // FIXME ^ Comment out this line + + // Attempt to borrow `_ref_to_i32` after inner value is destroyed + borrow_i32(_ref_to_i32); } ``` From 4e85201c4ed990b571146e9e2d0e4f7cf35fb08a Mon Sep 17 00:00:00 2001 From: Alex Gurganus Date: Wed, 20 Feb 2019 01:15:18 -0600 Subject: [PATCH 3/3] Emit both E0385 and E0505. Work with 2015/2018 edition --- src/scope/borrow.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/scope/borrow.md b/src/scope/borrow.md index 51407f147c..72c70e736a 100644 --- a/src/scope/borrow.md +++ b/src/scope/borrow.md @@ -29,14 +29,21 @@ fn main() { borrow_i32(&boxed_i32); borrow_i32(&stacked_i32); - // Take a reference to the data contained inside the box - let _ref_to_i32: &i32 = &boxed_i32; + { + // Take a reference to the data contained inside the box + let _ref_to_i32: &i32 = &boxed_i32; - // Can't destroy `boxed_i32` while the inner value is borrowed later in scope. - eat_box_i32(boxed_i32); - // FIXME ^ Comment out this line + // Error! + // Can't destroy `boxed_i32` while the inner value is borrowed later in scope. + eat_box_i32(boxed_i32); + // FIXME ^ Comment out this line + + // Attempt to borrow `_ref_to_i32` after inner value is destroyed + borrow_i32(_ref_to_i32); + // `_ref_to_i32` goes out of scope and is no longer borrowed. + } - // Attempt to borrow `_ref_to_i32` after inner value is destroyed - borrow_i32(_ref_to_i32); + // `boxed_i32` can now give up ownership to `eat_box` and be destroyed + eat_box_i32(boxed_i32); } ```