Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: Parse #[account] attribute arguments with syn #3140

Conversation

acheroncrypto
Copy link
Collaborator

Problem

#[account] macro arguments are parsed by converting the tokens to string and interpreting:

let mut namespace = "".to_string();
let mut is_zero_copy = false;
let mut unsafe_bytemuck = false;
let args_str = args.to_string();
let args: Vec<&str> = args_str.split(',').collect();
if args.len() > 2 {
panic!("Only two args are allowed to the account attribute.")
}
for arg in args {
let ns = arg
.to_string()
.replace('\"', "")
.chars()
.filter(|c| !c.is_whitespace())
.collect();
if ns == "zero_copy" {
is_zero_copy = true;
unsafe_bytemuck = false;
} else if ns == "zero_copy(unsafe)" {
is_zero_copy = true;
unsafe_bytemuck = true;
} else {
namespace = ns;
}
}

This makes adding named arguments e.g. discriminator = <EXPR> challenging.

Furthermore, any argument other than zero_copy and zero_copy(unsafe) is interpreted as custom namespace, even when the argument is not a string.

Summary of changes

  • Parse the arguments using syn
  • Unknown non-string arguments (e.g. #[account(my_namespace)]) are no longer interpreted as custom namespaces, and they return an error instead

Note: This PR is part of a greater effort explained in #3097.

Copy link

vercel bot commented Aug 1, 2024

@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto mentioned this pull request Aug 1, 2024
26 tasks
@acheroncrypto acheroncrypto merged commit 9fa2509 into coral-xyz:master Aug 1, 2024
49 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant