Skip to content

Commit 219023e

Browse files
authored
lang: Deprecate AccountInfo usage with compile-time warning in Accounts macro (solana-foundation#3854)
* Deprecate AccountInfo usage with compile-time warning in Accounts macro * cargo fmt
1 parent 2ab5b9e commit 219023e

25 files changed

+62
-3
lines changed

lang/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ pub use anchor_attribute_program::interface;
173173

174174
pub type Result<T> = std::result::Result<T, error::Error>;
175175

176+
// Deprecated message for AccountInfo usage in Accounts struct
177+
#[deprecated(
178+
note = "Use `UncheckedAccount` instead of `AccountInfo` for safer unchecked accounts."
179+
)]
180+
pub fn deprecated_account_info_usage() {}
181+
176182
/// A data structure of validated accounts that can be deserialized from the
177183
/// input to a Solana program. Implementations of this trait should perform any
178184
/// and all requisite constraint checks on accounts to ensure the accounts

lang/src/system_program.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(deprecated)]
13
use crate::prelude::*;
24
use crate::solana_program::pubkey::Pubkey;
35

lang/src/vec.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(deprecated)]
13
use crate::solana_program::account_info::AccountInfo;
24
use crate::solana_program::instruction::AccountMeta;
35
use crate::solana_program::pubkey::Pubkey;

lang/syn/src/codegen/accounts/try_accounts.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::codegen::accounts::{bumps, constraints, generics, ParsedGenerics};
2-
use crate::{AccountField, AccountsStruct};
3-
use quote::quote;
2+
use crate::{AccountField, AccountsStruct, Ty};
3+
use quote::{quote, quote_spanned};
44
use syn::Expr;
55

66
// Generates the `Accounts` trait implementation.
@@ -68,11 +68,21 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
6868
} else {
6969
let name = f.ident.to_string();
7070
let typed_name = f.typed_ident();
71+
72+
// Generate the deprecation call if it is an AccountInfo
73+
let warning = if matches!(f.ty, Ty::AccountInfo) {
74+
quote_spanned! { f.ty_span =>
75+
::anchor_lang::deprecated_account_info_usage();
76+
}
77+
} else {
78+
quote! {}
79+
};
7180
quote! {
7281
#[cfg(feature = "anchor-debug")]
7382
::solana_program::log::sol_log(stringify!(#typed_name));
7483
let #typed_name = anchor_lang::Accounts::try_accounts(__program_id, __accounts, __ix_data, __bumps, __reallocs)
7584
.map_err(|e| e.with_account_name(#name))?;
85+
#warning
7686
}
7787
}
7888
}

lang/syn/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ pub struct Field {
282282
pub constraints: ConstraintGroup,
283283
pub ty: Ty,
284284
pub is_optional: bool,
285+
pub ty_span: Span,
285286
/// IDL Doc comment
286287
pub docs: Option<Vec<String>>,
287288
}

lang/syn/src/parser/accounts/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub fn parse_account_field(f: &syn::Field) -> ParseResult<AccountField> {
315315
ty,
316316
is_optional,
317317
constraints: account_constraints,
318+
ty_span: f.ty.span(),
318319
docs,
319320
})
320321
}

lang/tests/generics_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![allow(dead_code)]
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(dead_code, deprecated)]
23

34
use anchor_lang::prelude::borsh::maybestd::io::Write;
45
use anchor_lang::prelude::*;

spl/src/associated_token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(deprecated)]
13
use anchor_lang::solana_program::account_info::AccountInfo;
24
use anchor_lang::solana_program::pubkey::Pubkey;
35
use anchor_lang::Result;

spl/src/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(deprecated)]
13
use anchor_lang::solana_program::account_info::AccountInfo;
24
use anchor_lang::solana_program::program_pack::Pack;
35
use anchor_lang::solana_program::pubkey::Pubkey;

spl/src/token_2022.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Avoiding AccountInfo deprecated msg in anchor context
2+
#![allow(deprecated)]
13
use anchor_lang::solana_program::account_info::AccountInfo;
24
use anchor_lang::solana_program::pubkey::Pubkey;
35
use anchor_lang::Result;

0 commit comments

Comments
 (0)