self: now reject capitalized entrypoint annotation

This commit is contained in:
Lesenechal Remi 2020-03-09 11:22:56 +01:00
parent 7717ac926e
commit 25f004844a
7 changed files with 32 additions and 36 deletions

View File

@ -324,8 +324,8 @@ let main = (p : unit) : address => Tezos.self_address;
## Self ## Self
Typecast the currently running contract with an entrypoint annotation. Typecast the currently running contract with an entrypoint annotation.
If your are using entrypoints: use "%Bar" for constructor Bar If your are using entrypoints: use "%bar" for constructor Bar
If you are not using entrypoints: use "%Default" If you are not using entrypoints: use "%default"
<Syntax syntax="pascaligo"> <Syntax syntax="pascaligo">

View File

@ -1322,7 +1322,7 @@ let%expect_test _ =
run_ligo_bad [ "compile-contract" ; bad_contract "self_bad_entrypoint_format.ligo" ; "main" ] ; run_ligo_bad [ "compile-contract" ; bad_contract "self_bad_entrypoint_format.ligo" ; "main" ] ;
[%expect {| [%expect {|
ligo: in file "self_bad_entrypoint_format.ligo", line 8, characters 52-58. bad entrypoint format: entrypoint "Toto" is badly formatted. We expect "%Bar" for entrypoint Bar and "%Default" when no entrypoint used {"location":"in file \"self_bad_entrypoint_format.ligo\", line 8, characters 52-58","hint":"try %Toto"} ligo: in file "self_bad_entrypoint_format.ligo", line 8, characters 52-58. bad entrypoint format: entrypoint "Toto" is badly formatted. We expect "%bar" for entrypoint Bar and "%default" when no entrypoint used {"location":"in file \"self_bad_entrypoint_format.ligo\", line 8, characters 52-58"}
If you're not sure how to fix this error, you can If you're not sure how to fix this error, you can

View File

@ -7,17 +7,6 @@ type contract_pass_data = {
} }
module Errors = struct module Errors = struct
let corner_case loc message =
let title () = "corner case" in
let content () = "we don't have a good error message for this case. we are
striving find ways to better report them and find the use-cases that generate
them. please report this to the developers." in
let data = [
("location" , fun () -> loc) ;
("message" , fun () -> message) ;
] in
error ~data title content
let bad_self_type expected got loc () = let bad_self_type expected got loc () =
let title = thunk "bad self type" in let title = thunk "bad self type" in
let message () = Format.asprintf "expected %a but got %a" Ast_typed.PP.type_expression expected Ast_typed.PP.type_expression got in let message () = Format.asprintf "expected %a but got %a" Ast_typed.PP.type_expression expected Ast_typed.PP.type_expression got in
@ -28,10 +17,25 @@ them. please report this to the developers." in
let bad_format_entrypoint_ann ep loc () = let bad_format_entrypoint_ann ep loc () =
let title = thunk "bad entrypoint format" in let title = thunk "bad entrypoint format" in
let message () = Format.asprintf "entrypoint \"%s\" is badly formatted. We expect \"%%Bar\" for entrypoint Bar and \"%%Default\" when no entrypoint used" ep in let message () = Format.asprintf "entrypoint \"%s\" is badly formatted. We expect \"%%bar\" for entrypoint Bar and \"%%default\" when no entrypoint used" ep in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
let entrypoint_annotation_not_literal loc () =
let title = thunk "entrypoint annotation must be a string literal" in
let message () = Format.asprintf "" in
let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
] in
error ~data title message ()
let unmatched_entrypoint loc () =
let title = thunk "No constructor matches the entrypoint annotation" in
let message () = Format.asprintf "" in
let data = [ let data = [
("location" , fun () -> Format.asprintf "%a" Location.pp loc) ; ("location" , fun () -> Format.asprintf "%a" Location.pp loc) ;
("hint" , fun () -> Format.asprintf "try %%%s" ep)
] in ] in
error ~data title message () error ~data title message ()
@ -39,7 +43,10 @@ end
let check_entrypoint_annotation_format ep exp = let check_entrypoint_annotation_format ep exp =
match String.split_on_char '%' ep with match String.split_on_char '%' ep with
| [ "" ; ep'] -> ok @@ ep' | [ "" ; ep'] ->
let cap = String.capitalize_ascii ep' in
if String.equal cap ep' then fail @@ Errors.bad_format_entrypoint_ann ep exp.location
else ok cap
| _ -> fail @@ Errors.bad_format_entrypoint_ann ep exp.location | _ -> fail @@ Errors.bad_format_entrypoint_ann ep exp.location
@ -53,24 +60,13 @@ let self_typing : contract_pass_data -> expression -> (bool * contract_pass_data
| E_constant {cons_name=C_SELF ; arguments=[entrypoint_exp]}, {type_content = T_operator (TC_contract t) ; type_meta=_} -> | E_constant {cons_name=C_SELF ; arguments=[entrypoint_exp]}, {type_content = T_operator (TC_contract t) ; type_meta=_} ->
let%bind entrypoint = match entrypoint_exp.expression_content with let%bind entrypoint = match entrypoint_exp.expression_content with
| E_literal (Literal_string ep) -> check_entrypoint_annotation_format ep entrypoint_exp | E_literal (Literal_string ep) -> check_entrypoint_annotation_format ep entrypoint_exp
| E_variable v -> ( | _ -> fail @@ Errors.entrypoint_annotation_not_literal entrypoint_exp.location in
match Ast_typed.Environment.get_opt v e.environment with
| Some {type_value = _; source_environment = _ ; definition = ED_declaration {expr ; free_variables = _}} -> (
match expr.expression_content with
| E_literal (Literal_string ep) -> check_entrypoint_annotation_format ep entrypoint_exp
| _ -> fail @@ Errors.corner_case __LOC__ "SELF argument do not resolve to a string"
)
| _ -> fail @@ Errors.corner_case __LOC__ "SELD argument not found in the environment"
)
| _ -> fail @@ Errors.corner_case __LOC__ "SELF argument is not a string or a variable" in
let%bind entrypoint_t = match dat.contract_type.parameter.type_content with let%bind entrypoint_t = match dat.contract_type.parameter.type_content with
| T_sum cmap -> trace_option (simple_error "No constructor matches the entrypoint annotation") | T_sum cmap -> trace_option (Errors.unmatched_entrypoint entrypoint_exp.location)
@@ Stage_common.Types.CMap.find_opt (Constructor entrypoint) cmap @@ Stage_common.Types.CMap.find_opt (Constructor entrypoint) cmap
| t -> ok {dat.contract_type.parameter with type_content = t} in | t -> ok {dat.contract_type.parameter with type_content = t} in
let%bind () = let%bind () =
trace_strong (bad_self_err ()) @@ trace_strong (bad_self_err ()) @@
Ast_typed.assert_type_expression_eq (entrypoint_t , t) in Ast_typed.assert_type_expression_eq (entrypoint_t , t) in
ok (true, dat, e) ok (true, dat, e)
| E_constant {cons_name=C_SELF ; arguments=[_]}, {type_content=_ ; type_meta=_} ->
fail (bad_self_err ())
| _ -> ok (true,dat,e) | _ -> ok (true,dat,e)

View File

@ -5,6 +5,6 @@ type return is list (operation) * storage
function main (const p : parameter; const s : storage) : return is function main (const p : parameter; const s : storage) : return is
block { block {
const self_contract: contract(int) = Tezos.self ("%Default"); const self_contract: contract(int) = Tezos.self ("%default");
} }
with ((nil: list(operation)), s) with ((nil: list(operation)), s)

View File

@ -5,6 +5,6 @@ type return is list (operation) * storage
function main (const p : parameter; const s : storage) : return is function main (const p : parameter; const s : storage) : return is
block { block {
const self_contract: contract(parameter) = Tezos.self("%Default") ; const self_contract: contract(parameter) = Tezos.self("%default") ;
} }
with ((nil: list(operation)), s) with ((nil: list(operation)), s)

View File

@ -5,8 +5,8 @@ type return is list (operation) * storage
function main (const p : parameter; const s : storage) : return is function main (const p : parameter; const s : storage) : return is
block { block {
const v : string = "%Toto" ; // const v : string = "%toto" ;
const self_contract: contract(int) = Tezos.self(v) ; const self_contract: contract(int) = Tezos.self("%toto") ;
const op : operation = Tezos.transaction (2, 300tz, self_contract) ; const op : operation = Tezos.transaction (2, 300tz, self_contract) ;
} }
with (list [op], s) with (list [op], s)

View File

@ -5,7 +5,7 @@ type return is list (operation) * storage
function main (const p : parameter; const s : storage) : return is function main (const p : parameter; const s : storage) : return is
block { block {
const self_contract: contract(int) = Tezos.self("%Default") ; const self_contract: contract(int) = Tezos.self("%default") ;
const op : operation = Tezos.transaction (2, 300tz, self_contract) ; const op : operation = Tezos.transaction (2, 300tz, self_contract) ;
} }
with (list [op], s) with (list [op], s)