Browse Source

lang: Add deprecation warning for constraint (#429)

Kirill Fomichev 4 years ago
parent
commit
e14fec0c46
2 changed files with 9 additions and 2 deletions
  1. 1 0
      lang/syn/Cargo.toml
  2. 8 2
      lang/syn/src/codegen/accounts/constraints.rs

+ 1 - 0
lang/syn/Cargo.toml

@@ -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"

+ 8 - 2
lang/syn/src/codegen/accounts/constraints.rs

@@ -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) {