coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.59k stars 1.32k forks source link

Build error when using `address` constraint with anchor V0.30.0 #2912

Closed 0x777A closed 3 months ago

0x777A commented 5 months ago

When using anchor build to compile the program,the fields limited by the address constraint cannot be recognized, but when compiled directly using cargo build-bpf, the compilation can be successful. example:


declare_id!("2CuWx82vA1HdJ2tJQqd7E3LLRb4NsqknAGpJUX7ZUA7u");

#[program]
pub mod example {
    use super::*;

    pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    pub owner: Signer<'info>,

    #[account(mut)]
    pub pool_state: Account<'info, PoolState>,

    /// CHECK:
    #[account(
        address = pool_state.token_vault
    )]
    pub vault: UncheckedAccount<'info>,
}

#[account()]
#[derive(Default, Debug)]
pub struct PoolState {
    pub token_vault: Pubkey,
}

Here is the error

error[E0425]: cannot find value `pool_state` in this scope
  --> programs/example/src/lib.rs:23:19
   |
23 |         address = pool_state.token_vault
   |                   ^^^^^^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `example` (lib test) due to 1 previous error
Error: Building IDL failed
acheroncrypto commented 5 months ago

Looks like we don't have a test case for this specific usage.

You can use one of the following workarounds until we support this usage:

jacksondoherty commented 4 months ago

+1. Just upgraded from 0.29.0 and 0.30.0 and all my address = constraints are giving me this error. Perhaps Rust docs should be updated to remove this feature? https://docs.rs/anchor-lang/latest/anchor_lang/derive.Accounts.html

Happy to submit a PR for that if you point me where. Found it, will submit one