I have problem of borrowing accounts mutably from remaining accounts. Not exactly how to use AccountInfo, as error says that accounts does not live long enough. I am using Anchor 0.29, Solana-Program 0.17.4. Previously it was working, after upgrade AccountInfo started falling with lifetime issue. Tried a bunch of stuff even tried to use std::mem::replace accounts, but nothing helped so far. This is the function I am trying to fix, I know it does not do anything meaningful yet, but I am trying to fix lifetime issue here:
fn swap_spl<'info>(
authority: &AccountInfo<'info>,
remaining_accounts: &mut Iter<AccountInfo<'info>>,
scratch: &mut Scratch,
) -> Result<AccountInfo<'info>> {
let accounts: Vec<AccountInfo<'info>> = remaining_accounts.take(7).cloned().collect::<Vec<_>>();
SplSwapInfo::try_accounts(
// Will be validated by the spl swap registry
&Pubkey::default(),
&mut &accounts[..],
&[],
&mut SplSwapInfoBumps::default(),
&mut scratch.reallocs,
)?;
Ok(authority.to_account_info())
}
#[derive(Accounts)]
pub struct SplSwapInfo<'info> {
/// CHECK:
pub swap_pool: AccountInfo<'info>,
/// CHECK:
pub authority: AccountInfo<'info>,
/// CHECK:
#[account(mut)]
pub vault_a: AccountInfo<'info>,
/// CHECK:
#[account(mut)]
pub vault_b: AccountInfo<'info>,
/// CHECK:
#[account(mut)]
pub token_mint: AccountInfo<'info>,
/// CHECK:
#[account(mut)]
pub fee_account: AccountInfo<'info>,
/// The address of the swap program
/// CHECK:
pub swap_program: AccountInfo<'info>,
}
- I tried using 'a:'info with &'a AccountInfo<'info> and even wise versa, does not help.
- Tried std::mem::replace accounts, does not help.
- Tried removing 'info lifetime at all, does not help.
The error itself is:
error[E0597]: `accounts` does not live long enough
|
| fn swap_spl<'info>(
| ----- lifetime `'info` defined here
...
| let accounts: Vec<AccountInfo<'info>> = remaining_accounts.take(7).cloned().collect::<Vec<_>>();
| -------- ----------------------- type annotation requires that `accounts` is borrowed for `'info`
| |
| binding `accounts` declared here
...
| &mut &accounts[..],
| ^^^^^^^^ borrowed value does not live long enough
...
| }
| - `accounts` dropped here while still borrowed
instructions::spl_token_swap::SplSwapInfo fn try_accounts(__program_id: &anchor_lang::solana_program::pubkey::Pubkey, __accounts: &mut &'info [anchor_lang::solana_program::account_info::AccountInfo<'info>], __ix_data: &[u8], __bumps: &mut SplSwapInfoBumps, __reallocs: &mut std::collections::BTreeSet<anchor_lang::solana_program::pubkey::Pubkey>) -> anchor_lang::Result<Self>
Lifetime of the __accounts. I can not change signature of the Accounts::try_accounts, so I don't know the fix.fn mock<'info>(acc: &'info [AccountInfo<'info>]) { for item in acc { println!("mock {:?}", item); } }
which actually fails, but if you will make it like:fn mock<'info>(acc: &[AccountInfo<'info>]) { for item in acc { println!("mock {:?}", item); } }
then everything works. So extra 'info lifetime breaks it.