From 1423a5f2ddf4f2976e15340eaf42889ddfc3ada6 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Thu, 21 Mar 2024 09:55:23 +0100 Subject: [PATCH] Add -D fail-fast (#11609) * Add -D fail-fast * [tests] add some tests for -D fast-fail * [tests] more tests for -D fast-fail * [tests] fix file names * Keep same signature with or without -D fail-fast --- src-json/define.json | 5 +++++ src/compiler/compilationContext.ml | 16 +++++++++++---- src/compiler/compiler.ml | 5 +++-- src/compiler/messageReporting.ml | 9 +++++++-- src/context/common.ml | 1 + src/core/define.ml | 2 +- .../projects/Issue6065/compile-fast-fail.hxml | 2 ++ .../Issue6065/compile-fast-fail.hxml.stderr | 8 ++++++++ .../projects/Issue6065/indent-fast-fail.hxml | 2 ++ .../Issue6065/indent-fast-fail.hxml.stderr | 7 +++++++ .../projects/Issue6065/pretty-fast-fail.hxml | 2 ++ .../Issue6065/pretty-fast-fail.hxml.stderr | 20 +++++++++++++++++++ .../projects/Issue8019/compile-fast-fail.hxml | 2 ++ .../Issue8019/compile-fast-fail.hxml.stderr | 2 ++ .../Issue8019/compile3-fast-fail.hxml | 2 ++ .../Issue8019/compile3-fast-fail.hxml.stderr | 1 + .../projects/issue5002/compile-fast-fail.hxml | 2 ++ .../issue5002/compile-fast-fail.hxml.stderr | 1 + .../issue5002/compile2-fast-fail.hxml | 2 ++ .../issue5002/compile2-fast-fail.hxml.stderr | 1 + .../issue5002/compile3-fast-fail.hxml | 2 ++ .../issue5002/compile3-fast-fail.hxml.stderr | 1 + .../issue5002/compile4-fast-fail.hxml | 2 ++ .../issue5002/compile4-fast-fail.hxml.stderr | 1 + 24 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 tests/misc/projects/Issue6065/compile-fast-fail.hxml create mode 100644 tests/misc/projects/Issue6065/compile-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/Issue6065/indent-fast-fail.hxml create mode 100644 tests/misc/projects/Issue6065/indent-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/Issue6065/pretty-fast-fail.hxml create mode 100644 tests/misc/projects/Issue6065/pretty-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/Issue8019/compile-fast-fail.hxml create mode 100644 tests/misc/projects/Issue8019/compile-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/Issue8019/compile3-fast-fail.hxml create mode 100644 tests/misc/projects/Issue8019/compile3-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/issue5002/compile-fast-fail.hxml create mode 100644 tests/misc/projects/issue5002/compile-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/issue5002/compile2-fast-fail.hxml create mode 100644 tests/misc/projects/issue5002/compile2-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/issue5002/compile3-fast-fail.hxml create mode 100644 tests/misc/projects/issue5002/compile3-fast-fail.hxml.stderr create mode 100644 tests/misc/projects/issue5002/compile4-fast-fail.hxml create mode 100644 tests/misc/projects/issue5002/compile4-fast-fail.hxml.stderr diff --git a/src-json/define.json b/src-json/define.json index 15c1d8db2e8..c3a3d03c91b 100644 --- a/src-json/define.json +++ b/src-json/define.json @@ -160,6 +160,11 @@ "doc": "Record per-method execution times in macro/interp mode. Implies eval-stack.", "platforms": ["eval"] }, + { + "name": "FailFast", + "define": "fail-fast", + "doc": "Abort compilation when first error occurs." + }, { "name": "FilterTimes", "define": "filter-times", diff --git a/src/compiler/compilationContext.ml b/src/compiler/compilationContext.ml index 5f1f91489dd..73902263304 100644 --- a/src/compiler/compilationContext.ml +++ b/src/compiler/compilationContext.ml @@ -80,16 +80,24 @@ let message ctx msg = ctx.messages <- msg :: ctx.messages let error ctx ?(depth=0) ?(from_macro = false) msg p = - message ctx (make_compiler_message ~from_macro msg p depth DKCompilerMessage Error); - ctx.has_error <- true + message ctx (make_compiler_message ~from_macro msg p depth DKCompilerMessage Error) + +let after_error ctx = + ctx.has_error <- true; + if Common.fail_fast ctx.com then raise Abort let error_ext ctx (err : Error.error) = Error.recurse_error (fun depth err -> error ~depth ~from_macro:err.err_from_macro ctx (Error.error_msg err.err_message) err.err_pos - ) err + ) err; + after_error ctx + +let error ctx ?(depth=0) ?(from_macro = false) msg p = + error ctx ~depth ~from_macro msg p; + after_error ctx let create_native_lib file extern kind = { lib_file = file; lib_extern = extern; lib_kind = kind; -} \ No newline at end of file +} diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 34f68860c14..2b8468634b5 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -417,8 +417,6 @@ let compile_safe ctx f = try f () with - | Abort -> - () | Error.Fatal_error err -> error_ext ctx err | Lexer.Error (m,p) -> @@ -454,6 +452,9 @@ with | e when (try Sys.getenv "OCAMLRUNPARAM" <> "b" with _ -> true) && not Helper.is_debug_run -> error ctx (Printexc.to_string e) null_pos +let compile_safe ctx f = + try compile_safe ctx f with Abort -> () + let finalize ctx = ctx.comm.flush ctx; List.iter (fun lib -> lib#close) ctx.com.hxb_libs; diff --git a/src/compiler/messageReporting.ml b/src/compiler/messageReporting.ml index d12d8d01232..f7f76d31123 100644 --- a/src/compiler/messageReporting.ml +++ b/src/compiler/messageReporting.ml @@ -356,10 +356,15 @@ let display_messages ctx on_message = begin let ectx = create_error_context absolute_positions in ectx.max_lines <- get_max_line ectx.max_lines ctx.messages; + let error msg = + ctx.has_error <- true; + on_message MessageSeverity.Error msg + in + let get_formatter _ def default = try get_formatter ctx.com def default with | ConfigError s -> - error ctx s null_pos; + error s; compiler_message_string in @@ -393,7 +398,7 @@ let display_messages ctx on_message = begin end with | Failure e | Sys_error e -> begin let def = Define.get_define_key Define.MessageLogFile in - error ctx (Printf.sprintf "Error opening log file: %s. Logging to file disabled (-D %s)" e def) null_pos; + error (Printf.sprintf "Error opening log file: %s. Logging to file disabled (-D %s)" e def); log_messages := false; end end; diff --git a/src/context/common.ml b/src/context/common.ml index c662ca54a48..55f3bf31cb5 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -469,6 +469,7 @@ let convert_define k = String.concat "_" (ExtString.String.nsplit k "-") let is_next com = defined com HaxeNext +let fail_fast com = defined com FailFast let external_defined ctx k = Define.raw_defined ctx.defines (convert_define k) diff --git a/src/core/define.ml b/src/core/define.ml index d71055df6d4..0071440c373 100644 --- a/src/core/define.ml +++ b/src/core/define.ml @@ -152,7 +152,7 @@ let get_signature def = Parser.parse_macro_ident as well (issue #5682). Note that we should removed flags like use_rtti_doc here. *) - | "display" | "use_rtti_doc" | "macro_times" | "display_details" | "no_copt" | "display_stdin" | "hxb.stats" + | "display" | "use_rtti_doc" | "macro_times" | "display_details" | "no_copt" | "display_stdin" | "hxb.stats" | "fail_fast" | "message.reporting" | "message.log_file" | "message.log_format" | "message.no_color" | "dump" | "dump_dependencies" | "dump_ignore_var_ids" -> acc | _ -> (k ^ "=" ^ v) :: acc diff --git a/tests/misc/projects/Issue6065/compile-fast-fail.hxml b/tests/misc/projects/Issue6065/compile-fast-fail.hxml new file mode 100644 index 00000000000..1c05658d214 --- /dev/null +++ b/tests/misc/projects/Issue6065/compile-fast-fail.hxml @@ -0,0 +1,2 @@ +compile-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/Issue6065/compile-fast-fail.hxml.stderr b/tests/misc/projects/Issue6065/compile-fast-fail.hxml.stderr new file mode 100644 index 00000000000..56668f3d86a --- /dev/null +++ b/tests/misc/projects/Issue6065/compile-fast-fail.hxml.stderr @@ -0,0 +1,8 @@ +Main.hx:8: characters 9-18 : Could not find a suitable overload, reasons follow +Main.hx:8: characters 9-18 : Overload resolution failed for () -> Void +Main.hx:8: characters 13-17 : Too many arguments +Main.hx:8: characters 9-18 : Overload resolution failed for (t : f.T) -> Void +Main.hx:8: characters 13-17 : Constraint check failure for f.T +Main.hx:8: characters 13-17 : ... String should be Int +Main.hx:8: characters 13-17 : ... For function argument 't' +Main.hx:8: characters 9-18 : End of overload failure reasons diff --git a/tests/misc/projects/Issue6065/indent-fast-fail.hxml b/tests/misc/projects/Issue6065/indent-fast-fail.hxml new file mode 100644 index 00000000000..5a550576c0c --- /dev/null +++ b/tests/misc/projects/Issue6065/indent-fast-fail.hxml @@ -0,0 +1,2 @@ +indent-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/Issue6065/indent-fast-fail.hxml.stderr b/tests/misc/projects/Issue6065/indent-fast-fail.hxml.stderr new file mode 100644 index 00000000000..5de57aae190 --- /dev/null +++ b/tests/misc/projects/Issue6065/indent-fast-fail.hxml.stderr @@ -0,0 +1,7 @@ +Main.hx:8: characters 9-18 : Could not find a suitable overload, reasons follow + Main.hx:8: characters 9-18 : Overload resolution failed for () -> Void + Main.hx:8: characters 13-17 : Too many arguments + Main.hx:8: characters 9-18 : Overload resolution failed for (t : f.T) -> Void + Main.hx:8: characters 13-17 : Constraint check failure for f.T + Main.hx:8: characters 13-17 : String should be Int + Main.hx:8: characters 13-17 : For function argument 't' diff --git a/tests/misc/projects/Issue6065/pretty-fast-fail.hxml b/tests/misc/projects/Issue6065/pretty-fast-fail.hxml new file mode 100644 index 00000000000..4983242e142 --- /dev/null +++ b/tests/misc/projects/Issue6065/pretty-fast-fail.hxml @@ -0,0 +1,2 @@ +pretty-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/Issue6065/pretty-fast-fail.hxml.stderr b/tests/misc/projects/Issue6065/pretty-fast-fail.hxml.stderr new file mode 100644 index 00000000000..6ae7cbf1bf0 --- /dev/null +++ b/tests/misc/projects/Issue6065/pretty-fast-fail.hxml.stderr @@ -0,0 +1,20 @@ +[ERROR] Main.hx:8: characters 9-18 + + 8 | C.f("hi"); + | ^^^^^^^^^ + | Could not find a suitable overload, reasons follow + + | Overload resolution failed for () -> Void + + 8 | C.f("hi"); + | ^^^^ + | Too many arguments + + | Overload resolution failed for (t : f.T) -> Void + + 8 | C.f("hi"); + | ^^^^ + | Constraint check failure for f.T + | String should be Int + | For function argument 't' + diff --git a/tests/misc/projects/Issue8019/compile-fast-fail.hxml b/tests/misc/projects/Issue8019/compile-fast-fail.hxml new file mode 100644 index 00000000000..1c05658d214 --- /dev/null +++ b/tests/misc/projects/Issue8019/compile-fast-fail.hxml @@ -0,0 +1,2 @@ +compile-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/Issue8019/compile-fast-fail.hxml.stderr b/tests/misc/projects/Issue8019/compile-fast-fail.hxml.stderr new file mode 100644 index 00000000000..b34a3575506 --- /dev/null +++ b/tests/misc/projects/Issue8019/compile-fast-fail.hxml.stderr @@ -0,0 +1,2 @@ +Macro.hx:9: characters 18-19 : "" is not a valid package name: +Macro.hx:9: characters 18-19 : Package name must not be empty diff --git a/tests/misc/projects/Issue8019/compile3-fast-fail.hxml b/tests/misc/projects/Issue8019/compile3-fast-fail.hxml new file mode 100644 index 00000000000..02597fcd931 --- /dev/null +++ b/tests/misc/projects/Issue8019/compile3-fast-fail.hxml @@ -0,0 +1,2 @@ +compile3-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/Issue8019/compile3-fast-fail.hxml.stderr b/tests/misc/projects/Issue8019/compile3-fast-fail.hxml.stderr new file mode 100644 index 00000000000..32a1a80dc17 --- /dev/null +++ b/tests/misc/projects/Issue8019/compile3-fast-fail.hxml.stderr @@ -0,0 +1 @@ +Macro2.hx:8: characters 18-19 : Module "" does not have a valid name. Module name must not be empty. diff --git a/tests/misc/projects/issue5002/compile-fast-fail.hxml b/tests/misc/projects/issue5002/compile-fast-fail.hxml new file mode 100644 index 00000000000..1c05658d214 --- /dev/null +++ b/tests/misc/projects/issue5002/compile-fast-fail.hxml @@ -0,0 +1,2 @@ +compile-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/issue5002/compile-fast-fail.hxml.stderr b/tests/misc/projects/issue5002/compile-fast-fail.hxml.stderr new file mode 100644 index 00000000000..056b0cf3c3f --- /dev/null +++ b/tests/misc/projects/issue5002/compile-fast-fail.hxml.stderr @@ -0,0 +1 @@ +Main.hx:1: characters 1-8 : "0" is not a valid field name. diff --git a/tests/misc/projects/issue5002/compile2-fast-fail.hxml b/tests/misc/projects/issue5002/compile2-fast-fail.hxml new file mode 100644 index 00000000000..4585f4855bf --- /dev/null +++ b/tests/misc/projects/issue5002/compile2-fast-fail.hxml @@ -0,0 +1,2 @@ +compile2-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/issue5002/compile2-fast-fail.hxml.stderr b/tests/misc/projects/issue5002/compile2-fast-fail.hxml.stderr new file mode 100644 index 00000000000..2f6dbe6ff04 --- /dev/null +++ b/tests/misc/projects/issue5002/compile2-fast-fail.hxml.stderr @@ -0,0 +1 @@ +Main2.hx:6: characters 3-32 : "0_variable" is not a valid variable name. diff --git a/tests/misc/projects/issue5002/compile3-fast-fail.hxml b/tests/misc/projects/issue5002/compile3-fast-fail.hxml new file mode 100644 index 00000000000..02597fcd931 --- /dev/null +++ b/tests/misc/projects/issue5002/compile3-fast-fail.hxml @@ -0,0 +1,2 @@ +compile3-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/issue5002/compile3-fast-fail.hxml.stderr b/tests/misc/projects/issue5002/compile3-fast-fail.hxml.stderr new file mode 100644 index 00000000000..2b10bd17bad --- /dev/null +++ b/tests/misc/projects/issue5002/compile3-fast-fail.hxml.stderr @@ -0,0 +1 @@ +Main3.hx:10: characters 18-19 : Module "lowercase" does not have a valid name. Module name should start with an uppercase letter: "lowercase" diff --git a/tests/misc/projects/issue5002/compile4-fast-fail.hxml b/tests/misc/projects/issue5002/compile4-fast-fail.hxml new file mode 100644 index 00000000000..e4736d81054 --- /dev/null +++ b/tests/misc/projects/issue5002/compile4-fast-fail.hxml @@ -0,0 +1,2 @@ +compile4-fail.hxml +-D fail-fast diff --git a/tests/misc/projects/issue5002/compile4-fast-fail.hxml.stderr b/tests/misc/projects/issue5002/compile4-fast-fail.hxml.stderr new file mode 100644 index 00000000000..7f4fc7338c2 --- /dev/null +++ b/tests/misc/projects/issue5002/compile4-fast-fail.hxml.stderr @@ -0,0 +1 @@ +Main4.hx:3: characters 7-9 : Variable names starting with a dollar are not allowed: "$i"