lang: Add deprecation warning for constraint (#429)

This commit is contained in:
Kirill Fomichev 2021-06-27 19:50:54 +03:00 committed by GitHub
parent e5b3f0aa06
commit e14fec0c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -15,6 +15,7 @@ anchor-debug = []
[dependencies]
proc-macro2 = "1.0"
proc-macro2-diagnostics = "0.9"
quote = "1.0"
syn = { version = "1.0.60", features = ["full", "extra-traits", "parsing"] }
anyhow = "1.0.32"

View File

@ -4,6 +4,7 @@ use crate::{
ConstraintOwner, ConstraintRaw, ConstraintRentExempt, ConstraintSeedsGroup, ConstraintSigner,
ConstraintState, Field, Ty,
};
use proc_macro2_diagnostics::SpanDiagnosticExt;
use quote::quote;
use syn::LitInt;
@ -192,8 +193,13 @@ pub fn generate_constraint_signer(f: &Field, _c: &ConstraintSigner) -> proc_macr
pub fn generate_constraint_literal(c: &ConstraintLiteral) -> proc_macro2::TokenStream {
let lit: proc_macro2::TokenStream = {
let lit = &c.lit;
let lit_ts: proc_macro2::TokenStream = quote! {#lit};
lit_ts.to_string().replace("\"", "").parse().unwrap()
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) {