Skip to content

Commit

Permalink
Add regression test for issue 1718
Browse files Browse the repository at this point in the history
    error[E0502]: cannot borrow `generics.params` as mutable because it is also borrowed as immutable
      --> tests/test_iterators.rs:81:25
       |
    77 |       let _ = generics
       |               --------
       |               |
       |  _____________immutable borrow occurs here
       | |
    78 | |         .lifetimes()
       | |____________________- a temporary with access to the immutable borrow is created here ...
    ...
    81 |           .unwrap_or_else(|| {
       |                           ^^ mutable borrow occurs here
    82 |               let lifetime: Lifetime = parse_quote!('a);
    83 |               generics.params.insert(
       |               --------------- second borrow occurs due to use of `generics.params` in closure
    ...
    88 |           });
       |             - ... and the immutable borrow might be used here, when that temporary is dropped and runs the destructor for type `impl Iterator<Item = &LifetimeParam>`
  • Loading branch information
dtolnay committed Aug 11, 2024
1 parent 857942e commit 7dc05a5
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tests/test_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::map_unwrap_or, clippy::uninlined_format_args)]

use syn::punctuated::{Pair, Punctuated};
use syn::Token;
use syn::{parse_quote, GenericParam, Generics, Lifetime, LifetimeParam, Token};

#[macro_use]
mod macros;
Expand Down Expand Up @@ -68,3 +68,22 @@ fn may_dangle() {
}
}
}

// Regression test for https://github.com/dtolnay/syn/issues/1718
#[test]
fn no_opaque_drop() {
let mut generics = Generics::default();

let _ = generics
.lifetimes()
.next()
.map(|param| param.lifetime.clone())
.unwrap_or_else(|| {
let lifetime: Lifetime = parse_quote!('a);
generics.params.insert(
0,
GenericParam::Lifetime(LifetimeParam::new(lifetime.clone())),
);
lifetime
});
}

0 comments on commit 7dc05a5

Please sign in to comment.