From 86de9d27e7d2095b88cc084ea2fb904f5a525173 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Wed, 1 Apr 2020 18:08:31 +0200 Subject: [PATCH] Add errors for properly handling `let x = _` cases. --- src/passes/1-parser/reasonligo.ml | 14 ++++++++++++++ src/passes/1-parser/reasonligo/Parser.mly | 17 ++++++++++++++++- src/passes/1-parser/reasonligo/ParserMain.ml | 7 ++++++- src/passes/1-parser/reasonligo/SyntaxError.ml | 1 + src/passes/1-parser/reasonligo/SyntaxError.mli | 1 + 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/passes/1-parser/reasonligo.ml b/src/passes/1-parser/reasonligo.ml index 5254018e8..ee59d073c 100644 --- a/src/passes/1-parser/reasonligo.ml +++ b/src/passes/1-parser/reasonligo.ml @@ -68,6 +68,18 @@ module Errors = ("location", fun () -> Format.asprintf "%a" Location.pp_lift @@ expression_loc)] in error ~data title message + + let invalid_wild (expr: AST.expr) = + let title () = "" in + let message () = + "It looks you are using a wild pattern where it cannot be used." + in + let expression_loc = AST.expr_to_region expr in + let data = [ + ("location", + fun () -> Format.asprintf "%a" Location.pp_lift @@ expression_loc)] + in error ~data title message + end let parse (module IO : IO) parser = @@ -127,6 +139,8 @@ let parse (module IO : IO) parser = | exception SyntaxError.Error (SyntaxError.WrongFunctionArguments expr) -> Trace.fail @@ Errors.wrong_function_arguments expr + | exception SyntaxError.Error (SyntaxError.InvalidWild expr) -> + Trace.fail @@ Errors.wrong_function_arguments expr let parse_file (source: string) = let module IO = diff --git a/src/passes/1-parser/reasonligo/Parser.mly b/src/passes/1-parser/reasonligo/Parser.mly index d50379b00..1c6078355 100644 --- a/src/passes/1-parser/reasonligo/Parser.mly +++ b/src/passes/1-parser/reasonligo/Parser.mly @@ -40,6 +40,13 @@ let rec curry hd = function in TFun {value; region} | [] -> hd +let wild_error e = + match e with + | EVar { value = "_"; _} as e -> + let open! SyntaxError in + raise (Error (InvalidWild e)) + | _ -> () + (* END HEADER *) %} @@ -262,24 +269,30 @@ let_declaration: let_binding: "" type_annotation? "=" expr { + wild_error $4; Scoping.check_reserved_name $1; {binders = PVar $1, []; lhs_type=$2; eq=$3; let_rhs=$4} } | "_" type_annotation? "=" expr { + wild_error $4; {binders = PWild $1, []; lhs_type=$2; eq=$3; let_rhs=$4} } | unit type_annotation? "=" expr { + wild_error $4; {binders = PUnit $1, []; lhs_type=$2; eq=$3; let_rhs=$4} } | record_pattern type_annotation? "=" expr { + wild_error $4; Scoping.check_pattern (PRecord $1); {binders = PRecord $1, []; lhs_type=$2; eq=$3; let_rhs=$4} } | par(closed_irrefutable) type_annotation? "=" expr { + wild_error $4; Scoping.check_pattern $1.value.inside; {binders = PPar $1, []; lhs_type=$2; eq=$3; let_rhs=$4} } | tuple(sub_irrefutable) type_annotation? "=" expr { + wild_error $4; Utils.nsepseq_iter Scoping.check_pattern $1; let hd, tl = $1 in let start = pattern_to_region hd in @@ -408,7 +421,9 @@ expr: base_cond__open(expr) | switch_expr(base_cond) { $1 } base_cond__open(x): - base_expr(x) | conditional(expr_with_let_expr) { $1 } + base_expr(x) | conditional(expr_with_let_expr) { + wild_error $1; + $1 } base_cond: base_cond__open(base_cond) { $1 } diff --git a/src/passes/1-parser/reasonligo/ParserMain.ml b/src/passes/1-parser/reasonligo/ParserMain.ml index 6d27665a2..c84c6bdc4 100644 --- a/src/passes/1-parser/reasonligo/ParserMain.ml +++ b/src/passes/1-parser/reasonligo/ParserMain.ml @@ -48,7 +48,12 @@ let parse parser : ('a, string Region.reg) Stdlib.result = in Stdlib.Error Region.{value=error; region} (* Scoping errors *) - + | SyntaxError.Error (SyntaxError.InvalidWild expr) -> + let msg = "It looks you are using a wild pattern where it cannot be used.\n" + and region = AST.expr_to_region expr in + let error = Unit.short_error ~offsets:IO.options#offsets + IO.options#mode msg region + in Stdlib.Error Region.{value=error; region} | Scoping.Error (Scoping.Reserved_name name) -> let token = Lexer.Token.mk_ident name.Region.value name.Region.region in diff --git a/src/passes/1-parser/reasonligo/SyntaxError.ml b/src/passes/1-parser/reasonligo/SyntaxError.ml index befbb27c2..e6d23dbed 100644 --- a/src/passes/1-parser/reasonligo/SyntaxError.ml +++ b/src/passes/1-parser/reasonligo/SyntaxError.ml @@ -1,4 +1,5 @@ type error = | WrongFunctionArguments of AST.expr + | InvalidWild of AST.expr exception Error of error \ No newline at end of file diff --git a/src/passes/1-parser/reasonligo/SyntaxError.mli b/src/passes/1-parser/reasonligo/SyntaxError.mli index f0cc1ca6e..5288ceb41 100644 --- a/src/passes/1-parser/reasonligo/SyntaxError.mli +++ b/src/passes/1-parser/reasonligo/SyntaxError.mli @@ -1,4 +1,5 @@ type error = | WrongFunctionArguments of AST.expr + | InvalidWild of AST.expr exception Error of error