From 59cdbaafd8271f344021132d2fbbfb49ab28b01c Mon Sep 17 00:00:00 2001 From: Zoltan Somogyi Date: Fri, 10 Jan 2025 18:29:01 +1100 Subject: [PATCH] Move a predicate to its natural home. compiler/hlds_error_util.m: Move the maybe_write_out_errors predicate from hlds_error_util.m to write_error_spec.m. Delete the definitely_write_out_errors predicate, because (a) it is currently unused, and (b) its definition is so trivial that it won't be needed again. compiler/write_error_spec.m: Add the moved predicate. Update its documentation. Delete the pre_hlds_maybe_write_out_errors predicate that was already here, because its definition was identical to that of the moved predicate, and is very likely to remain so from now on. compiler/grab_modules.m: compiler/mercury_compile_front_end.m: compiler/mercury_compile_make_hlds.m: compiler/mercury_compile_middle_passes.m: Conform to the changes above. --- compiler/grab_modules.m | 10 ++-- compiler/hlds_error_util.m | 62 ------------------------ compiler/mercury_compile_front_end.m | 4 +- compiler/mercury_compile_make_hlds.m | 17 +++---- compiler/mercury_compile_middle_passes.m | 4 +- compiler/write_error_spec.m | 50 ++++++++++++++++--- 6 files changed, 57 insertions(+), 90 deletions(-) diff --git a/compiler/grab_modules.m b/compiler/grab_modules.m index 49cc78e16..296f3d4cc 100644 --- a/compiler/grab_modules.m +++ b/compiler/grab_modules.m @@ -2,7 +2,7 @@ % vim: ft=mercury ts=4 sw=4 et %---------------------------------------------------------------------------% % Copyright (C) 1996-2011 The University of Melbourne. -% Copyright (C) 2019-2024 The Mercury team. +% Copyright (C) 2019-2025 The Mercury team. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %---------------------------------------------------------------------------% @@ -527,7 +527,7 @@ grab_plain_opt_and_int_for_opt_files(ProgressStream, ErrorStream, Globals, OwnOptFileName, OwnOptModuleErrors, OptSpecs0, OptSpecs1, OptError0, OptError) ), - pre_hlds_maybe_write_out_errors(ErrorStream, VeryVerbose, Globals, + maybe_write_out_errors(ErrorStream, VeryVerbose, Globals, OptSpecs1, OptSpecs, !IO) else ParseTreePlainOpts = ParseTreePlainOpts0, @@ -1330,8 +1330,7 @@ read_plain_opt_files(ProgressStream, Globals, VeryVerbose, DontQueueOptModules1 = DontQueueOptModules0 ), - pre_hlds_maybe_write_out_errors(ProgressStream, VeryVerbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ProgressStream, VeryVerbose, Globals, !Specs, !IO), read_plain_opt_files(ProgressStream, Globals, VeryVerbose, ReadOptFilesTransitively, ModuleNames1, DontQueueOptModules1, !ParseTreePlainOptsCord, !ExplicitDeps, !ImplicitNeeds, @@ -1363,8 +1362,7 @@ read_trans_opt_files(ProgressStream, Globals, VeryVerbose, update_opt_error_status_on_failure(Globals, warn_missing_trans_opt_files, FileName, Errors, !Specs, !OptError) ), - pre_hlds_maybe_write_out_errors(ProgressStream, VeryVerbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ProgressStream, VeryVerbose, Globals, !Specs, !IO), read_trans_opt_files(ProgressStream, Globals, VeryVerbose, ModuleNames, !ParseTreeTransOptsCord, !Specs, !OptError, !IO). diff --git a/compiler/hlds_error_util.m b/compiler/hlds_error_util.m index cadcc0485..066bd6f3f 100644 --- a/compiler/hlds_error_util.m +++ b/compiler/hlds_error_util.m @@ -23,16 +23,12 @@ :- import_module hlds.hlds_module. :- import_module hlds.hlds_pred. :- import_module hlds.pred_table. -:- import_module libs. -:- import_module libs.globals. :- import_module parse_tree. :- import_module parse_tree.error_spec. :- import_module parse_tree.parse_tree_out_info. :- import_module parse_tree.prog_data. :- import_module assoc_list. -:- import_module bool. -:- import_module io. :- import_module list. :- import_module maybe. :- import_module pair. @@ -196,47 +192,6 @@ :- func project_pred_form_arity_int(pred_form_arity) = int. -%---------------------------------------------------------------------------% -% -% Every possible path of execution in mercury_compile.m should call -% definitely_write_out_errors exactly once, just after the compiler -% has finished doing all the things that can generate error reports. -% -% If Verbose = no, then this call is intended to write out all at once -% all the error specifications accumulated until then. They are written out -% all at once so that write_error_specs can sort them by context. -% -% If Verbose = yes, then keeping all the error messages until the end would -% be confusing, since we would be reporting that e.g. the program had type -% errors *before* printing the type error messages. In that case, we want to -% print (using maybe_write_out_errors or its pre-HLDS twin) all the -% accumulated errors before each message to the user. -% -% This applies to *all* messages. -% -% - The calls to maybe_write_out_errors before a message that announces -% the completion (and success or failure) of a phase obviously report -% the errors (if any) discovered by the phase. -% -% - The calls to maybe_write_out_errors before a message that announces -% the phase the compiler is about to enter serve to write out any messages -% from previous phases that have not yet been written out. -% -% We could require each phase to write out the errors it discovers when it -% finishes (if Verbose = yes, that is), but that would eliminate any -% opportunity to group and sort together the error messages of two or more -% adjacent phases that are *not* separated by a message to the user even with -% Verbose = yes. Since the cost of calling maybe_write_out_errors -% when there is nothing to print is so low (a few dozen instructions), -% we can easily afford to incur it unnecessarily once per compiler phase. - -:- pred definitely_write_out_errors(io.text_output_stream::in, globals::in, - list(error_spec)::in, io::di, io::uo) is det. - -:- pred maybe_write_out_errors(io.text_output_stream::in, bool::in, - globals::in, list(error_spec)::in, list(error_spec)::out, - io::di, io::uo) is det. - %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% @@ -251,7 +206,6 @@ :- import_module parse_tree.parse_tree_out_misc. :- import_module parse_tree.prog_mode. :- import_module parse_tree.prog_util. -:- import_module parse_tree.write_error_spec. :- import_module int. :- import_module map. @@ -543,22 +497,6 @@ project_user_arity_int(user_arity(A)) = A. project_pred_form_arity_int(pred_form_arity(A)) = A. -%---------------------------------------------------------------------------% - -definitely_write_out_errors(Stream, Globals, Specs, !IO) :- - write_error_specs(Stream, Globals, Specs, !IO). - -maybe_write_out_errors(Stream, Verbose, Globals, !Specs, !IO) :- - % pre_hlds_maybe_write_out_errors in write_error_spec.m is a - % pre-HLDS version of this predicate. - ( - Verbose = no - ; - Verbose = yes, - write_error_specs(Stream, Globals, !.Specs, !IO), - !:Specs = [] - ). - %---------------------------------------------------------------------------% :- end_module hlds.hlds_error_util. %---------------------------------------------------------------------------% diff --git a/compiler/mercury_compile_front_end.m b/compiler/mercury_compile_front_end.m index 9bbf21202..2d932f0ad 100644 --- a/compiler/mercury_compile_front_end.m +++ b/compiler/mercury_compile_front_end.m @@ -2,7 +2,7 @@ % vim: ft=mercury ts=4 sw=4 et %---------------------------------------------------------------------------% % Copyright (C) 1994-2011 The University of Melbourne. -% Copyright (C) 2014-2024 The Mercury team. +% Copyright (C) 2014-2025 The Mercury team. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %---------------------------------------------------------------------------% @@ -116,7 +116,6 @@ :- import_module hlds.goal_mode. :- import_module hlds.hlds_call_tree. :- import_module hlds.hlds_clauses. -:- import_module hlds.hlds_error_util. :- import_module hlds.hlds_pred. :- import_module hlds.hlds_statistics. :- import_module libs.file_util. @@ -133,6 +132,7 @@ :- import_module parse_tree.parse_tree_out. :- import_module parse_tree.parse_tree_out_info. :- import_module parse_tree.prog_data. +:- import_module parse_tree.write_error_spec. :- import_module top_level.mercury_compile_middle_passes. :- import_module transform_hlds. :- import_module transform_hlds.dead_proc_elim. diff --git a/compiler/mercury_compile_make_hlds.m b/compiler/mercury_compile_make_hlds.m index 53c64ec59..5bda2a00f 100644 --- a/compiler/mercury_compile_make_hlds.m +++ b/compiler/mercury_compile_make_hlds.m @@ -1,7 +1,7 @@ %---------------------------------------------------------------------------% % vim: ts=4 sw=4 et ft=mercury %---------------------------------------------------------------------------% -% Copyright (C) 2022-2024 The Mercury Team. +% Copyright (C) 2022-2025 The Mercury Team. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %---------------------------------------------------------------------------% @@ -165,8 +165,7 @@ make_hlds_pass(ProgressStream, ErrorStream, Globals, maybe_read_event_set(Globals, EventSetFileName, EventSetName, EventSpecMap0, EventSetErrors, !Specs, !IO), - pre_hlds_maybe_write_out_errors(ErrorStream, Verbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ErrorStream, Verbose, Globals, !Specs, !IO), maybe_write_string(ProgressStream, Verbose, "% Module qualifying items...\n", !IO), maybe_flush_output(ProgressStream, Verbose, !IO), @@ -175,8 +174,7 @@ make_hlds_pass(ProgressStream, ErrorStream, Globals, MQUndefTypes, MQUndefInsts, MQUndefModes, MQUndefTypeClasses, [], QualifySpecs), !:Specs = QualifySpecs ++ !.Specs, - pre_hlds_maybe_write_out_errors(ErrorStream, Verbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ErrorStream, Verbose, Globals, !Specs, !IO), maybe_write_string(ProgressStream, Verbose, "% done.\n", !IO), maybe_report_stats(ProgressStream, Stats, !IO), @@ -189,8 +187,7 @@ make_hlds_pass(ProgressStream, ErrorStream, Globals, RecompInfo0, RecompInfo, ExpandSpecs), ExpandErrors = contains_errors(Globals, ExpandSpecs), !:Specs = ExpandSpecs ++ !.Specs, - pre_hlds_maybe_write_out_errors(ErrorStream, Verbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ErrorStream, Verbose, Globals, !Specs, !IO), maybe_write_string(ProgressStream, Verbose, "% done.\n", !IO), maybe_report_stats(ProgressStream, Stats, !IO), mq_info_set_recompilation_info(RecompInfo, MQInfo0, MQInfo), @@ -625,8 +622,7 @@ make_hlds(ProgressStream, ErrorStream, Globals, AugCompUnit, EventSet, MQInfo, TypeEqvMap, UsedModules, Verbose, Stats, !:HLDS, QualInfo, FoundInvalidType, FoundInvalidInstOrMode, FoundSemanticError, !Specs, !IO) :- - pre_hlds_maybe_write_out_errors(ErrorStream, Verbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ErrorStream, Verbose, Globals, !Specs, !IO), maybe_write_string(ProgressStream, Verbose, "% Converting parse tree to hlds...\n", !IO), ParseTreeModuleSrc = AugCompUnit ^ acu_module_src, @@ -650,8 +646,7 @@ make_hlds(ProgressStream, ErrorStream, Globals, AugCompUnit, EventSet, MQInfo, else FoundSemanticError = no ), - pre_hlds_maybe_write_out_errors(ErrorStream, Verbose, Globals, - !Specs, !IO), + maybe_write_out_errors(ErrorStream, Verbose, Globals, !Specs, !IO), maybe_write_string(ProgressStream, Verbose, "% done.\n", !IO), maybe_report_stats(ProgressStream, Stats, !IO). diff --git a/compiler/mercury_compile_middle_passes.m b/compiler/mercury_compile_middle_passes.m index 534eb4f1a..f9db2e795 100644 --- a/compiler/mercury_compile_middle_passes.m +++ b/compiler/mercury_compile_middle_passes.m @@ -2,7 +2,7 @@ % vim: ft=mercury ts=4 sw=4 et %---------------------------------------------------------------------------% % Copyright (C) 2009-2012 The University of Melbourne. -% Copyright (C) 2013-2024 The Mercury team. +% Copyright (C) 2013-2025 The Mercury team. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %---------------------------------------------------------------------------% @@ -60,7 +60,6 @@ :- import_module analysis.operations. :- import_module backend_libs. :- import_module backend_libs.type_ctor_info. -:- import_module hlds.hlds_error_util. :- import_module hlds.hlds_pred. :- import_module hlds.mark_static_terms. :- import_module libs.file_util. @@ -73,6 +72,7 @@ :- import_module parse_tree.module_cmds. :- import_module parse_tree.parse_tree_out. :- import_module parse_tree.parse_tree_out_info. +:- import_module parse_tree.write_error_spec. :- import_module top_level.mercury_compile_front_end. :- import_module transform_hlds. :- import_module transform_hlds.accumulator. diff --git a/compiler/write_error_spec.m b/compiler/write_error_spec.m index 063a7836b..d90bf5af2 100644 --- a/compiler/write_error_spec.m +++ b/compiler/write_error_spec.m @@ -2,7 +2,7 @@ % vim: ft=mercury ts=4 sw=4 et %---------------------------------------------------------------------------% % Copyright (C) 1997-2012 The University of Melbourne. -% Copyright (C) 2022-2024 The Mercury team. +% Copyright (C) 2022-2025 The Mercury team. % This file may only be copied under the terms of the GNU General % Public License - see the file COPYING in the Mercury distribution. %---------------------------------------------------------------------------% @@ -160,9 +160,47 @@ %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% -:- pred pre_hlds_maybe_write_out_errors(io.text_output_stream::in, - bool::in, globals::in, - list(error_spec)::in, list(error_spec)::out, io::di, io::uo) is det. + % maybe_write_out_errors(Stream, Verbose, Globals, !Specs, !IO): + % + % Every possible path of execution in mercury_compile.m should call + % write_error_specs on the accumulated but not-yet-printed error_specs + % exactly once, just after the compiler has finished doing all the things + % that can generate error reports. This predicate is intended to manage + % the printing of error_specs before that point. + % + % If Verbose = no, then that call to write_error_specs should write out + % all at once all the error specifications accumulated until then. + % Being written out all at once, write_error_specs can sort them + % by context. + % + % If Verbose = yes, then keeping all the error messages until the end would + % be confusing, since we would be reporting that e.g. the program had type + % errors *before* printing the type error messages. In that case, + % we want to print (using maybe_write_out_errors) all the accumulated + % errors before each message to the user. + % + % This applies to *all* messages. + % + % - The calls to maybe_write_out_errors before a message that announces + % the completion (and success or failure) of a phase should obviously + % report the errors (if any) discovered by the phase. + % + % - The calls to maybe_write_out_errors before a message that announces + % the phase the compiler is about to enter serve to write out any + % messages from previous phases that have not yet been written out. + % + % We could require each phase to write out the errors it discovers + % when it finishes (if Verbose = yes, that is), but that would eliminate + % any opportunity to group and sort together the error messages + % of two or more adjacent phases that are *not* separated by a message + % to the user even with Verbose = yes. Since the cost of calling + % maybe_write_out_errors when there is nothing to print is so low + % (a few dozen instructions), we can easily afford to incur it + % unnecessarily once per compiler phase. + % +:- pred maybe_write_out_errors(io.text_output_stream::in, bool::in, + globals::in, list(error_spec)::in, list(error_spec)::out, + io::di, io::uo) is det. %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% @@ -2438,9 +2476,7 @@ pop_stack_ignore_empty(Stack0, Stack) :- %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% -pre_hlds_maybe_write_out_errors(Stream, Verbose, Globals, !Specs, !IO) :- - % maybe_write_out_errors in hlds_error_util.m is a HLDS version - % of this predicate. The documentation is in that file. +maybe_write_out_errors(Stream, Verbose, Globals, !Specs, !IO) :- ( Verbose = no ;