Skip to content

Commit

Permalink
Remove literal constraint
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil B N <[email protected]>
  • Loading branch information
NBNARADHYA committed Feb 11, 2022
1 parent 44ce9d8 commit e4aaf76
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 44 deletions.
23 changes: 0 additions & 23 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -47,7 +46,6 @@ pub fn linearize(c_group: &ConstraintGroup) -> Vec<Constraint> {
mutable,
signer,
has_one,
literal,
raw,
owner,
rent_exempt,
Expand Down Expand Up @@ -80,7 +78,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 @@ -110,7 +107,6 @@ fn generate_constraint(f: &Field, c: &Constraint) -> proc_macro2::TokenStream {
Constraint::Mut(c) => generate_constraint_mut(f, c),
Constraint::HasOne(c) => generate_constraint_has_one(f, c),
Constraint::Signer(c) => generate_constraint_signer(f, c),
Constraint::Literal(c) => generate_constraint_literal(c),
Constraint::Raw(c) => generate_constraint_raw(c),
Constraint::Owner(c) => generate_constraint_owner(f, c),
Constraint::RentExempt(c) => generate_constraint_rent_exempt(f, c),
Expand All @@ -126,7 +122,6 @@ fn generate_constraint(f: &Field, c: &Constraint) -> proc_macro2::TokenStream {
fn generate_constraint_composite(_f: &CompositeField, c: &Constraint) -> proc_macro2::TokenStream {
match c {
Constraint::Raw(c) => generate_constraint_raw(c),
Constraint::Literal(c) => generate_constraint_literal(c),
_ => panic!("Invariant violation"),
}
}
Expand Down Expand Up @@ -219,24 +214,6 @@ pub fn generate_constraint_signer(f: &Field, c: &ConstraintSigner) -> proc_macro
}
}

pub fn generate_constraint_literal(c: &ConstraintLiteral) -> proc_macro2::TokenStream {
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::__private::ErrorCode::Deprecated.into());
}
}
}

pub fn generate_constraint_raw(c: &ConstraintRaw) -> proc_macro2::TokenStream {
let raw = &c.raw;
let error = generate_custom_error(&c.error, quote! { ConstraintRaw });
Expand Down
8 changes: 0 additions & 8 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ pub struct ConstraintGroup {
executable: Option<ConstraintExecutable>,
state: Option<ConstraintState>,
has_one: Vec<ConstraintHasOne>,
literal: Vec<ConstraintLiteral>,
raw: Vec<ConstraintRaw>,
close: Option<ConstraintClose>,
address: Option<ConstraintAddress>,
Expand Down Expand Up @@ -608,7 +607,6 @@ pub enum Constraint {
Mut(ConstraintMut),
Signer(ConstraintSigner),
HasOne(ConstraintHasOne),
Literal(ConstraintLiteral),
Raw(ConstraintRaw),
Owner(ConstraintOwner),
RentExempt(ConstraintRentExempt),
Expand All @@ -629,7 +627,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 @@ -684,11 +681,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
15 changes: 2 additions & 13 deletions lang/syn/src/parser/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ pub fn is_instruction(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);
// Using a literal constraint is deprecated.
if is_lit {
let lit: LitStr = stream.parse()?;
let c = ConstraintToken::Literal(Context::new(lit.span(), ConstraintLiteral { lit }));
return Ok(c);
return Err(anchor_lang::__private::ErrorCode::Deprecated.into());
}

let ident = stream.call(Ident::parse_any)?;
Expand Down Expand Up @@ -316,7 +315,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 @@ -347,7 +345,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 @@ -525,7 +522,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
mutable,
signer,
has_one,
literal,
raw,
owner,
rent_exempt,
Expand Down Expand Up @@ -633,7 +629,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 @@ -653,7 +648,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 @@ -920,11 +914,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

0 comments on commit e4aaf76

Please sign in to comment.