Skip to content

Commit

Permalink
lang: remove ConstraintLiteral (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen authored Feb 6, 2023
1 parent c76641f commit eef9888
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The minor version will be incremented upon a breaking change and the patch versi

- lang: Remove deprecated account types: `CpiAccount`, `Loader` and `ProgramAccount` ([#2375](https://github.com/coral-xyz/anchor/pull/2375)).
- lang: Remove `state` and `interface` attributes ([#2285](https://github.com/coral-xyz/anchor/pull/2285)).
- lang: Remove depecrated literal constraint which has been replaced by `#[account(constraint = {})]` ([#2379](https://github.com/coral-xyz/anchor/pull/2379)).
- lang: `account(zero_copy)` and `zero_copy` attributes now derive the `bytemuck::Pod` and `bytemuck::Zeroable` traits instead of using `unsafe impl` ([#2330](https://github.com/coral-xyz/anchor/pull/2330)). This imposes useful restrictions on the type, like not having padding bytes and all fields being `Pod` themselves. See [bytemuck::Pod](https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html) for details. This change requires adding `bytemuck = { version = "1.4.0", features = ["derive", "min_const_generics"]}` to your `cargo.toml`. Legacy applications can still use `#[account(zero_copy(unsafe))]` and `#[zero_copy(unsafe)]` for the old behavior.
- ts: Remove `createProgramAddressSync`, `findProgramAddressSync` (now available in `@solana/web3.js`) and update `associatedAddress` to be synchronous ([#2357](https://github.com/coral-xyz/anchor/pull/2357)).

Expand Down
20 changes: 0 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lang/syn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ seeds = []

[dependencies]
proc-macro2 = { version = "1.0", features=["span-locations"]}
proc-macro2-diagnostics = "0.9"
quote = "1.0"
syn = { version = "1.0.60", features = ["full", "extra-traits", "parsing"] }
anyhow = "1.0.32"
Expand Down
27 changes: 0 additions & 27 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2_diagnostics::SpanDiagnosticExt;
use quote::quote;
use std::collections::HashSet;
use syn::Expr;
Expand Down Expand Up @@ -59,7 +58,6 @@ pub fn generate_composite(f: &CompositeField) -> proc_macro2::TokenStream {
.iter()
.filter_map(|c| match c {
Constraint::Raw(_) => Some(c),
Constraint::Literal(_) => Some(c),
_ => panic!("Invariant violation: composite constraints can only be raw or literals"),
})
.map(|c| generate_constraint_composite(f, c))
Expand All @@ -78,7 +76,6 @@ pub fn linearize(c_group: &ConstraintGroup) -> Vec<Constraint> {
mutable,
signer,
has_one,
literal,
raw,
owner,
rent_exempt,
Expand Down Expand Up @@ -116,7 +113,6 @@ pub fn linearize(c_group: &ConstraintGroup) -> Vec<Constraint> {
constraints.push(Constraint::Signer(c));
}
constraints.append(&mut has_one.into_iter().map(Constraint::HasOne).collect());
constraints.append(&mut literal.into_iter().map(Constraint::Literal).collect());
constraints.append(&mut raw.into_iter().map(Constraint::Raw).collect());
if let Some(c) = owner {
constraints.push(Constraint::Owner(c));
Expand Down Expand Up @@ -153,7 +149,6 @@ fn generate_constraint(
Constraint::Mut(c) => generate_constraint_mut(f, c),
Constraint::HasOne(c) => generate_constraint_has_one(f, c, accs),
Constraint::Signer(c) => generate_constraint_signer(f, c),
Constraint::Literal(c) => generate_constraint_literal(&f.ident, c),
Constraint::Raw(c) => generate_constraint_raw(&f.ident, c),
Constraint::Owner(c) => generate_constraint_owner(f, c),
Constraint::RentExempt(c) => generate_constraint_rent_exempt(f, c),
Expand All @@ -171,7 +166,6 @@ fn generate_constraint(
fn generate_constraint_composite(f: &CompositeField, c: &Constraint) -> proc_macro2::TokenStream {
match c {
Constraint::Raw(c) => generate_constraint_raw(&f.ident, c),
Constraint::Literal(c) => generate_constraint_literal(&f.ident, c),
_ => panic!("Invariant violation"),
}
}
Expand Down Expand Up @@ -301,27 +295,6 @@ pub fn generate_constraint_signer(f: &Field, c: &ConstraintSigner) -> proc_macro
}
}

pub fn generate_constraint_literal(
ident: &Ident,
c: &ConstraintLiteral,
) -> proc_macro2::TokenStream {
let name_str = ident.to_string();
let lit: proc_macro2::TokenStream = {
let lit = &c.lit;
let constraint = lit.value().replace('\"', "");
let message = format!(
"Deprecated. Should be used with constraint: #[account(constraint = {constraint})]",
);
lit.span().warning(message).emit_as_item_tokens();
constraint.parse().unwrap()
};
quote! {
if !(#lit) {
return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::Deprecated).with_account_name(#name_str));
}
}
}

pub fn generate_constraint_raw(ident: &Ident, c: &ConstraintRaw) -> proc_macro2::TokenStream {
let raw = &c.raw;
let error = generate_custom_error(ident, &c.error, quote! { ConstraintRaw }, &None);
Expand Down
12 changes: 2 additions & 10 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{
Expr, Generics, Ident, ItemEnum, ItemFn, ItemMod, ItemStruct, LitInt, LitStr, PatType, Token,
Type, TypePath,
Expr, Generics, Ident, ItemEnum, ItemFn, ItemMod, ItemStruct, LitInt, PatType, Token, Type,
TypePath,
};

pub mod codegen;
Expand Down Expand Up @@ -557,7 +557,6 @@ pub struct ConstraintGroup {
pub seeds: Option<ConstraintSeedsGroup>,
pub executable: Option<ConstraintExecutable>,
pub has_one: Vec<ConstraintHasOne>,
pub literal: Vec<ConstraintLiteral>,
pub raw: Vec<ConstraintRaw>,
pub close: Option<ConstraintClose>,
pub address: Option<ConstraintAddress>,
Expand Down Expand Up @@ -596,7 +595,6 @@ pub enum Constraint {
Mut(ConstraintMut),
Signer(ConstraintSigner),
HasOne(ConstraintHasOne),
Literal(ConstraintLiteral),
Raw(ConstraintRaw),
Owner(ConstraintOwner),
RentExempt(ConstraintRentExempt),
Expand All @@ -619,7 +617,6 @@ pub enum ConstraintToken {
Mut(Context<ConstraintMut>),
Signer(Context<ConstraintSigner>),
HasOne(Context<ConstraintHasOne>),
Literal(Context<ConstraintLiteral>),
Raw(Context<ConstraintRaw>),
Owner(Context<ConstraintOwner>),
RentExempt(Context<ConstraintRentExempt>),
Expand Down Expand Up @@ -698,11 +695,6 @@ pub struct ConstraintHasOne {
pub error: Option<Expr>,
}

#[derive(Debug, Clone)]
pub struct ConstraintLiteral {
pub lit: LitStr,
}

#[derive(Debug, Clone)]
pub struct ConstraintRaw {
pub raw: Expr,
Expand Down
19 changes: 1 addition & 18 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::parse::{Error as ParseError, Parse, ParseStream, Result as ParseResult}
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{bracketed, Expr, Ident, LitStr, Token};
use syn::{bracketed, Expr, Ident, Token};

pub fn parse(f: &syn::Field, f_ty: Option<&Ty>) -> ParseResult<ConstraintGroup> {
let mut constraints = ConstraintGroupBuilder::new(f_ty);
Expand All @@ -26,13 +26,6 @@ pub fn is_account(attr: &&syn::Attribute) -> bool {

// Parses a single constraint from a parse stream for `#[account(<STREAM>)]`.
pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
let is_lit = stream.peek(LitStr);
if is_lit {
let lit: LitStr = stream.parse()?;
let c = ConstraintToken::Literal(Context::new(lit.span(), ConstraintLiteral { lit }));
return Ok(c);
}

let ident = stream.call(Ident::parse_any)?;
let kw = ident.to_string();

Expand Down Expand Up @@ -328,7 +321,6 @@ pub struct ConstraintGroupBuilder<'ty> {
pub mutable: Option<Context<ConstraintMut>>,
pub signer: Option<Context<ConstraintSigner>>,
pub has_one: Vec<Context<ConstraintHasOne>>,
pub literal: Vec<Context<ConstraintLiteral>>,
pub raw: Vec<Context<ConstraintRaw>>,
pub owner: Option<Context<ConstraintOwner>>,
pub rent_exempt: Option<Context<ConstraintRentExempt>>,
Expand Down Expand Up @@ -361,7 +353,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
mutable: None,
signer: None,
has_one: Vec::new(),
literal: Vec::new(),
raw: Vec::new(),
owner: None,
rent_exempt: None,
Expand Down Expand Up @@ -561,7 +552,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
mutable,
signer,
has_one,
literal,
raw,
owner,
rent_exempt,
Expand Down Expand Up @@ -710,7 +700,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
mutable: into_inner!(mutable),
signer: into_inner!(signer),
has_one: into_inner_vec!(has_one),
literal: into_inner_vec!(literal),
raw: into_inner_vec!(raw),
owner: into_inner!(owner),
rent_exempt: into_inner!(rent_exempt),
Expand All @@ -731,7 +720,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
ConstraintToken::Mut(c) => self.add_mut(c),
ConstraintToken::Signer(c) => self.add_signer(c),
ConstraintToken::HasOne(c) => self.add_has_one(c),
ConstraintToken::Literal(c) => self.add_literal(c),
ConstraintToken::Raw(c) => self.add_raw(c),
ConstraintToken::Owner(c) => self.add_owner(c),
ConstraintToken::RentExempt(c) => self.add_rent_exempt(c),
Expand Down Expand Up @@ -1060,11 +1048,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
Ok(())
}

fn add_literal(&mut self, c: Context<ConstraintLiteral>) -> ParseResult<()> {
self.literal.push(c);
Ok(())
}

fn add_raw(&mut self, c: Context<ConstraintRaw>) -> ParseResult<()> {
self.raw.push(c);
Ok(())
Expand Down

1 comment on commit eef9888

@vercel
Copy link

@vercel vercel bot commented on eef9888 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-200ms.vercel.app
anchor-lang.com
anchor-docs-git-master-200ms.vercel.app
www.anchor-lang.com

Please sign in to comment.