Skip to content

Commit

Permalink
lang: custom error for address
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Oct 22, 2021
1 parent d91c187 commit 6047550
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ incremented for features.
* lang: Add `mint::freeze_authority` keyword for mint initialization within `#[derive(Accounts)]` ([#835](https://github.com/project-serum/anchor/pull/835)).
* lang: Add `AccountLoader` type for `zero_copy` accounts with support for CPI ([#792](https://github.com/project-serum/anchor/pull/792)).
* lang: Add `#[account(init_if_needed)]` keyword for allowing one to invoke the same instruction even if the account was created already ([#906](https://github.com/project-serum/anchor/pull/906)).
* lang: Add custom errors support for `signer`, `mut`, `has_one` and raw constraints ([#905](https://github.com/project-serum/anchor/pull/905), [#913](https://github.com/project-serum/anchor/pull/913)).
* lang: Add custom errors support for `signer`, `mut`, `has_one`, raw constraints and `address` ([#905](https://github.com/project-serum/anchor/pull/905), [#913](https://github.com/project-serum/anchor/pull/913)).

### Breaking

Expand Down
2 changes: 1 addition & 1 deletion lang/derive/accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use syn::parse_macro_input;
/// | `#[account(executable)]` | On `AccountInfo` structs | Checks the given account is an executable program. |
/// | `#[account(state = <target>)]` | On `CpiState` structs | Checks the given state is the canonical state account for the target program. |
/// | `#[account(owner = <target>)]` | On `CpiState`, `CpiAccount`, and `AccountInfo` | Checks the account owner matches the target. |
/// | `#[account(address = <pubkey>)]` | On `AccountInfo` and `Account` | Checks the account key matches the pubkey. |
/// | `#[account(address = <pubkey>)]`<br><br>`#[account(address = <pubkey> @ <custom_error>)]` | On `AccountInfo` and `Account` | Checks the account key matches the pubkey. Custom errors are supported via `@`. |
// TODO: How do we make the markdown render correctly without putting everything
// on absurdly long lines?
#[proc_macro_derive(Accounts, attributes(account, instruction))]
Expand Down
3 changes: 2 additions & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ fn generate_constraint_composite(_f: &CompositeField, c: &Constraint) -> proc_ma
fn generate_constraint_address(f: &Field, c: &ConstraintAddress) -> proc_macro2::TokenStream {
let field = &f.ident;
let addr = &c.address;
let error = generate_custom_error(&c.error, quote! { ConstraintAddress });
quote! {
if #field.to_account_info().key != &#addr {
return Err(anchor_lang::__private::ErrorCode::ConstraintAddress.into());
return Err(#error);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ pub struct ConstraintOwner {
#[derive(Debug, Clone)]
pub struct ConstraintAddress {
pub address: Expr,
pub error: Option<Expr>,
}

#[derive(Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
span,
ConstraintAddress {
address: stream.parse()?,
error: parse_optional_custom_error(&stream)?,
},
)),
_ => return Err(ParseError::new(ident.span(), "Invalid attribute")),
Expand Down

0 comments on commit 6047550

Please sign in to comment.