Skip to content

Commit

Permalink
Add regression test for issue 1246
Browse files Browse the repository at this point in the history
    error[E0505]: cannot move out of `p` because it is borrowed
      --> tests/test_iterators.rs:56:18
       |
    54 |     for element in &p {
       |                    --
       |                    |
       |                    borrow of `p` occurs here
       |                    a temporary with access to the borrow is created here ...
    55 |         if *element == 2 {
    56 |             drop(p);
       |                  ^ move out of `p` occurs here
    ...
    59 |     }
       |     - ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `syn::punctuated::Iter<'_, i32>`

    error[E0505]: cannot move out of `p` because it is borrowed
      --> tests/test_iterators.rs:64:18
       |
    62 |     for element in &mut p {
       |                    ------
       |                    |
       |                    borrow of `p` occurs here
       |                    a temporary with access to the borrow is created here ...
    63 |         if *element == 2 {
    64 |             drop(p);
       |                  ^ move out of `p` occurs here
    ...
    67 |     }
       |     - ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `syn::punctuated::IterMut<'_, i32>`
  • Loading branch information
dtolnay committed Dec 1, 2022
1 parent 17f9a5c commit 3eaa443
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/test_iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,22 @@ fn iter() {
assert_eq!(p.iter_mut().next_back(), Some(&mut 4));
assert_eq!(p.into_iter().next_back(), Some(4));
}

#[test]
fn may_dangle() {
let p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
for element in &p {
if *element == 2 {
drop(p);
break;
}
}

let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
for element in &mut p {
if *element == 2 {
drop(p);
break;
}
}
}

0 comments on commit 3eaa443

Please sign in to comment.