فهرست منبع

removed unused variable warnings and improved pattern matcher errors (fixed issue #1720)

Simon Krajewski 12 سال پیش
والد
کامیت
0940956f83
2فایلهای تغییر یافته به همراه27 افزوده شده و 7 حذف شده
  1. 2 4
      common.ml
  2. 25 3
      matcher.ml

+ 2 - 4
common.ml

@@ -183,9 +183,8 @@ module Define = struct
 		| NoRoot
 		| NoSwfCompress
 		| NoTraces
-		| NoUnusedVarWarnings
 		| PhpPrefix
-    | RealPosition
+		| RealPosition
 		| ReplaceFiles
 		| Scriptable
 		| Swc
@@ -245,9 +244,8 @@ module Define = struct
 		| NoMacroCache -> ("no_macro_cache","Disable macro context caching")
 		| NoSwfCompress -> ("no_swf_compress","Disable SWF output compression")
 		| NoTraces -> ("no_traces","Disable all trace calls")
-		| NoUnusedVarWarnings -> ("no_unused_var_warnings","Do not warn about unused catch-variables in patterns")
 		| PhpPrefix -> ("php_prefix","Compiled with --php-prefix")
-    | RealPosition -> ("real_position","Disables haxe source mapping when targetting C#")
+		| RealPosition -> ("real_position","Disables haxe source mapping when targetting C#")
 		| ReplaceFiles -> ("replace_files","GenCommon internal")
 		| Scriptable -> ("scriptable","GenCPP internal")
 		| Swc -> ("swc","Output a SWC instead of a SWF")

+ 25 - 3
matcher.ml

@@ -890,8 +890,8 @@ let replace_locals mctx out e =
 			Type.map_expr loop e
 	in
 	let e = loop e in
-	if not (Common.defined mctx.ctx.com Define.NoUnusedVarWarnings) then
-		Hashtbl.iter (fun _ (v,p) -> if (String.length v.v_name) > 0 && v.v_name.[0] <> '_' then mctx.ctx.com.warning "This variable is unused" p) all_subterms;
+(* 	if not (Common.defined mctx.ctx.com Define.NoUnusedVarWarnings) then
+		Hashtbl.iter (fun _ (v,p) -> if (String.length v.v_name) > 0 && v.v_name.[0] <> '_' then mctx.ctx.com.warning "This variable is unused" p) all_subterms; *)
 	e
 
 let rec st_eq st1 st2 = match st1.st_def,st2.st_def with
@@ -1190,11 +1190,33 @@ let match_expr ctx e cases def with_type p =
 		let out = mk_out mctx i e eg pl (pos ep) in
 		Array.of_list pl,out
 	) cases in
+	let unused p =
+		let check_expr e p =
+			try
+				let old_error = ctx.on_error in
+				ctx.on_error <- (fun ctx s p -> ctx.on_error <- old_error; error s p);
+				ignore (type_expr ctx e Value);
+				ctx.on_error <- old_error;
+				display_error ctx "Case expression must be a constant value or a pattern, not an arbitrary expression" (pos e)
+			with _ ->
+				ctx.on_error <- old_error;
+				display_error ctx "This pattern is unused" p
+		in
+		let rec loop prev cl = match cl with
+			| ((e,p2) :: _,_,_) :: cl ->
+				if p2.pmin = p.pmin then check_expr prev p else loop (e,p2) cl
+			| _ :: cl ->
+				assert false
+			| [] ->
+				check_expr prev p
+		in
+		loop (EConst (Ident "null"),Ast.null_pos) cases
+	in
 	begin try
 		let dt = compile mctx stl pl in
 		PMap.iter (fun _ out -> if out.o_num_paths = 0 then begin
 			if out.o_pos == p then display_error ctx "The default pattern is unused" p
-			else display_error ctx "This pattern is unused" out.o_pos;
+			else unused out.o_pos;
 			if mctx.toplevel_or then match evals with
 				| [{etype = t}] when (match follow t with TAbstract({a_path=[],"Int"},[]) -> true | _ -> false) ->
 					display_error ctx "Note: Int | Int is an or-pattern now" p;