Skip to content

Commit 19c5b6c

Browse files
committed
fix(idl): Respect offset = ... in IDL generation for custom errors
1 parent 292b095 commit 19c5b6c

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

lang/syn/src/idl/error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ pub fn gen_idl_print_fn_error(error: &Error) -> TokenStream {
1212
"__anchor_private_print_idl_error_{}",
1313
error.ident.to_string().to_snake_case()
1414
);
15+
let offset = match &error.args {
16+
Some(args) => {
17+
let offset = &args.offset;
18+
quote! { #offset }
19+
}
20+
None => quote! { ::anchor_lang::error::ERROR_CODE_OFFSET },
21+
};
1522

1623
let error_codes = error
1724
.codes
@@ -26,7 +33,7 @@ pub fn gen_idl_print_fn_error(error: &Error) -> TokenStream {
2633

2734
quote! {
2835
#idl::IdlErrorCode {
29-
code: anchor_lang::error::ERROR_CODE_OFFSET + #id,
36+
code: #offset + #id,
3037
name: #name.into(),
3138
msg: #msg,
3239
}

lang/syn/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,8 @@ impl Parse for ErrorArgs {
680680
return Err(ParseError::new(offset_span, "expected keyword offset"));
681681
}
682682
stream.parse::<Token![=]>()?;
683-
Ok(ErrorArgs {
684-
offset: stream.parse()?,
685-
})
683+
let offset: LitInt = stream.parse()?;
684+
Ok(ErrorArgs { offset })
686685
}
687686
}
688687

tests/idl/programs/idl/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,14 @@ pub struct Initialize2<'info> {
300300
#[derive(Accounts)]
301301
pub struct CauseError {}
302302

303-
#[error_code]
303+
#[error_code(offset = 500_000)]
304304
pub enum ErrorCode {
305305
#[msg("Example error.")]
306306
SomeError,
307307
#[msg("Another error.")]
308308
OtherError,
309309
ErrorWithoutMsg,
310+
WithDiscrim = 500,
310311
}
311312

312313
mod some_other_module {

0 commit comments

Comments
 (0)