Commit Graph

55 Commits

Author SHA1 Message Date
Julien Fischer
b9777da30f Fix some documentation errors in the compiler.
Fix some mispelled variable names.

compiler/accumulator.m:
compiler/check_import_accessibility.m:
compiler/decide_type_repn.m:
compiler/du_type_layout.m:
compiler/error_spec.m:
compiler/hlds_data.m:
compiler/hlds_goal.m:
compiler/mlds.m:
compiler/parse_item.m:
compiler/prog_data.m:
compiler/type_util.m:
    As above.
2026-01-24 01:19:51 +11:00
Zoltan Somogyi
ce9c9230c9 Enforce an invariant using types.
library/edit_seq.m:
    Change to uint the types of all the integers that cannot be negative.
    This happens to be all of them.

NEWS.md:
    Announce the change.

compiler/add_type.m:
compiler/det_check_switch.m:
compiler/error_spec.m:
compiler/style_checks.m:
compiler/typecheck_msgs.m:
    Conform to the changes above.

tests/hard_coded/change_hunk_test.{m,exp}:
tests/hard_coded/edit_seq_test.{m,exp}:
    Change the code that sets up the parameters for testing edit_seq.m
    to use uints, and expect uints in the output.
2026-01-01 17:23:24 +11:00
Zoltan Somogyi
bdd2a574a8 Replace conditional_specs with a new severity.
compiler/error_spec.m:
    Delete conditional_spec from the error_spec type, which had
    exactly one use left in the compiler. Replace it with a conditional form
    of severity_error, which handles that use case.

    Rename error_severity to spec_severity, since obviously not all severities
    represent errors.

    For the same reason, rename error_phase to spec_phase.

compiler/error_util.m:
    Export a predicate for write_error_spec.m.

compiler/write_error_spec.m:
    Use that export to delete what used to be duplicate code.

compiler/add_pragma.m:
compiler/check_typeclass.m:
compiler/compiler_util.m:
compiler/error_sort.m:
compiler/mark_tail_calls.m:
compiler/parse_error.m:
compiler/parse_item.m:
    Conform to the changes above.
2025-11-15 10:29:23 +11:00
Zoltan Somogyi
823de2d37b Require warning/info messages to specify an option.
The objective of this step is two-fold:

- to fix --inhibit-warnings, making it shut up all warning
  and informational messages; and

- to ensure that it *stays* fixed, even when after new diagnostics
  are added.

As part of this fix, this diff adds a whole bunch of new warning
options, in order to control the warnings that previously were
not controlled by any option. (There was no need for new
informational options.)

As it happens, we have long used severity_informational for messages
that did not report any information about the code being compiled,
but to report actions that the compiler was taking. Create a new
option category, oc_report, for the new options that now control
those diagnostics.

---------------------

compiler/error_spec.m:
    Change severity_warning and severity_informational to take an option
    as as argument. The semantics is that the diagnostic in which
    the severity occurs is conditional on that option, meaning that
    it is printed only if that option is set to "yes".

    Delete the severity_conditional function symbol from the severity
    type, since the mechanism just above handles its only use case.

    Define subtypes to represent error_specs in a standard form.

compiler/error_sort.m:
    Provide operations to convert error specs into their standard form.

    Make the sorting operation itself operate on the standard form.

compiler/write_error_spec.m:
    Convert error_specs to standard form before writing them out,
    in order to avoid duplicating the code for their standardization.

    Change the code that writes out error_specs to operate on the
    standard form. Implement the test implicit in the warning and
    and informational severities in this code.

compiler/error_util.m:
compiler/compiler_util.m:
    Delete operations that do not make sense with the new severity type.

---------------------

compiler/options.m:
    Add new options to control all the previously-uncontrolled
    warning and informational messages.

NEWS.md:
    Announce the *public* new options.

compiler/option_categories.m:
compiler/print_help.m:
    Add the new option category, and fake-include it in the help text
    and the user guide. (The inclusion is fake because none of the
    options in the new category are user visible, meaning the section
    containing them is not visible either.)

---------------------

compiler/det_infer_goal.m:
    Start a severity warning diagnostic with "Warning:"
    instead of "Error:".

compiler/mark_trace_goals.m:
    Fix an incorrect error message.

compiler/purity.m:
    Replace a correct/incorrect color pair with two inconsistent colors,
    because there is a reasonable probability of each one being right.

---------------------

compiler/accumulator.m:
compiler/add_clause.m:
compiler/add_mode.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pred.m:
compiler/add_type.m:
compiler/check_module_interface.m:
compiler/check_type_inst_mode_defns.m:
compiler/check_typeclass.m:
compiler/color_schemes.m:
compiler/common.m:
compiler/convert_import_use.m:
compiler/convert_parse_tree.m:
compiler/dead_proc_elim.m:
compiler/det_check_proc.m:
compiler/det_check_switch.m:
compiler/det_infer_goal.m:
compiler/du_type_layout.m:
compiler/format_call_errors.m:
compiler/grab_modules.m:
compiler/hlds_call_tree.m:
compiler/inst_check.m:
compiler/introduce_parallelism.m:
compiler/make_hlds_error.m:
compiler/make_hlds_warn.m:
compiler/mark_tail_calls.m:
compiler/mark_trace_goals.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_make_hlds.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/module_qual.qual_errors.m:
compiler/opt_deps_spec.m:
compiler/options_file.m:
compiler/parse_goal.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/pre_typecheck.m:
compiler/purity.m:
compiler/read_modules.m:
compiler/recompilation.check.m:
compiler/simplify_goal.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_disj.m:
compiler/simplify_goal_ite.m:
compiler/split_parse_tree_src.m:
compiler/state_var.m:
compiler/stratify.m:
compiler/style_checks.m:
compiler/superhomogeneous.m:
compiler/table_gen.m:
compiler/term_constr_errors.m:
compiler/term_errors.m:
compiler/termination.m:
compiler/typecheck_clauses.m:
compiler/typecheck_error_overload.m:
compiler/typecheck_error_undef.m:
compiler/typecheck_errors.m:
compiler/typecheck_msgs.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/warn_unread_modules.m:
compiler/write_module_interface_files.m:
    Conform to the changes above, mostly by either

    - adding an option to all warning and informational messages,
      sometimes using existing warning options and sometimes new ones,
      or

    - turning already explicitly-conditional-on-an-option messages
      into implicitly-conditional-on-that-option messages.

---------------------

tests/invalid/one_member.m:
    Conform to the change in det_infer_goal.m.

tests/invalid/require_tailrec_1.err_exp:
tests/invalid/require_tailrec_2.err_exp:
    Actually obey the options for these modules in Mercury.options.

tests/invalid_purity/purity.err_exp:
tests/warnings/purity_warnings.err_exp:
    Conform to the change in purity.m.

tests/warnings/moved_trace_goal.err_exp:
    Conform to the change in mark_trace_goals.m.

tests/warnings/help_text.err_exp:
    Expect the documentation of all the new options.
2025-08-18 12:07:38 +02:00
Zoltan Somogyi
7034a07641 Improve source_file_map.m.
compiler/source_file_map.m:
    Ensure that the source_file_map is a bijection both

    - when we construct Mercury.modules, and
    - when we read it in (after possible modifications by the user).

    Report any errors instead of ignoring them, as best we can.
    Do not leave that job to our callers, which have less info.

    Leave the old Mercury.modules (if any) unchanged if the construction
    of its new version finds problems.

    Document the fact that the names of nested submodules won't ever
    be stored in source file maps.

compiler/file_names.m:
compiler/mercury_compile_main.m:
compiler/read_modules.m:
compiler/xml_documentation.m:
    Conform to the changes above.

compiler/parse_module.m:
    Fix accidentally-doubled-up word.

compiler/error_spec.m:
    Rename some fields to avoid double-defining "error_phase".
2025-07-16 11:24:42 +02:00
Zoltan Somogyi
fbed5b6760 Add new pred marker pragma "type_order_switch".
compiler/prog_item.m:
    Add a new impl_marker pragma, type_order_switch. Its meaning is
    to ask the compiler to check all the switches in the predicate
    or function named in the pragma to see whether

    - the order of function symbols in the definition of type being
      switched on (actually, in the definition of the top type constructor
      of that type)

    - is matched by the order in the text of the cases of the switch.

    The intention is to use this on the optdb predicate in options.m.

compiler/hlds_markers.m:
    Add a new predicate marker, whose presence indicates this pragma.

compiler/parse_pragma.m:
compiler/add_pragma.m:
    Parse the new pragma, and convert it to the new marker.

compiler/det_check_proc.m:
    Expand the existing that check whether we need to need to look for
    warnings for the switches in a procedure body.

compiler/det_check_switch.m:
    Implement the new pragma.

    In the process, put the arguments of the affected predicates
    into the order required by our coding style.

compiler/error_spec.m:
    To help implement the new pragma, add a predicate for constructing
    diffs between two string sequences. This factors out two existing
    pieces of code. (New code in det_check_switch.m would have contained
    a third copy.)

compiler/add_type.m:
compiler/style_checks.m:
    Use the new predicate to replace those two old copies, simplifing
    existing code.

compiler/convert_parse_tree.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/parse_tree_out_pragma.m:
compiler/table_gen.m:
    Conform to the changes above.

tests/warnings/bad_type_order_switch.{m,err_exp}:
    A test case for the new pragma.

tests/warnings/Mmakefile:
    Enable the new test case.
2025-06-17 15:35:57 +02:00
Zoltan Somogyi
f70b5d6de7 Implement options to warn about unused state vars.
The new --warn-unneeded-initial-statevar option asks the compiler
to warn about code such as

    pred_a(!.X, ...) :-
        ... code that uses !.X, but does not update it ...

In this case, the preferred fix is to just replace all occurrences
of !.X with X.

The new --warn-unneeded-final-statevar option asks the compiler
to warn about code such as

    pred_a(!X, ...) :-
        ... code that maybe uses !.X, but does not update it ...

In this case, the preferred fix also involves replacing all occurrences
of !.X with X, but it also involves either deleting the argument
containing !:X (the best option), or, if there is some reason why
the predicate's signature must stay unchanged, to replace !:X with X as well.
And if the clause body does not actually refer to either !.X or !:X, then
*both* arguments represented by !X should be deleted.

The first option is a style warning; the second option, due to the
signature change it may call for, is a non-style warning.

Both options have a version whose name adds a "-lambda" suffix, and which
does the same warnings for the heads of lambda expressions, not clauses.

Note that several of the modules below, including some that help to implement
the warnings, also contain code changes that result from *acting* on
the new warnings, e.g. by deleting unneeded statevar arguments.
Other, similar changes will also come after this diff is committed.

compiler/options.m:
doc/user_guide.texi:
    Document the new options.

compiler/state_var.m:
    Gather the information needed to decide what code merits the new warnings.
    Do so in three stages:

    - when processing the head of a clause or of a lambda expression,
    - when processing the body goal of that clause or lambda expression,
    - when finishing up the processing of the clause or lambda expression.

    Add a predicate to generate the warnings for lambda expressions.

    Do not generate the warnings for clauses. This is because whether or not
    we want to warn about state vars in some clauses depends on the properties
    of *other* clauses of the same predicate, and state_var.m has access
    to only one clause at a time. Instead,

    - return the info needed by the warning-generating code in pre_typecheck.m
      (one of the first passes we execute after adding all clauses
      to the HLDS), and

    - we export some functionality for use by that code.

    Switch to a convention for naming the program variables corresponding
    to the middle (non-initial, non-final) versions of state variables
    whose output is affected by changes in the code of the clause body goal
    only if they involve that specific state variable.

    Give some predicates more descriptive names.

compiler/make_hlds.m:
    Make state_var.m and its new functionality visible from outside
    the make_hlds package.

compiler/add_clause.m:
    Record the information gathered by state_var.m in each clause.

compiler/hlds_clauses.m:
    Add a slot to each clause for this information.

    Give some predicates more descriptive names.

compiler/headvar_names.m:
    Use the contents of the new slots to detect whether any clauses
    have unused state vars, and if so, return the chosen consensus names
    of the head vars to the code of pre_typecheck.m, which uses this info
    as part of the implementation of the new warnings.

compiler/pre_typecheck.m:
    Implement the new warnings.

compiler/mercury_compile_front_end.m:
    Record the warnings that pre_typecheck.m can now return.

compiler/error_spec.m:
compiler/write_error_spec.m:
    Add unsigned versions of the format pieces involving ints, for use
    by the new code in pre_typecheck.m, and implement them.

compiler/hlds_out_util.m:
compiler/maybe_util.m:
    Move two related types from hlds_out_util.m to maybe_util.m,
    in order to allow pre_typecheck.m to use one of them.

compiler/hlds_args.m:
    Add a new utility function for use by the new code above.

compiler/foreign.m:
    Act on the new warnings by deleting the long-unused predicates
    being warned about.

compiler/post_typecheck.m:
    Speed up this traversal. (I originally thought to implement
    the new warnings in this pass.)

compiler/add_foreign_proc.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pragma_type_spec.m:
compiler/add_pred.m:
compiler/add_type.m:
compiler/build_mode_constraints.m:
compiler/call_gen.m:
compiler/check_typeclass.m:
compiler/clause_to_proc.m:
compiler/code_loc_dep.m:
compiler/delay_info.m:
compiler/delay_partial_inst.m:
compiler/dense_switch.m:
compiler/det_check_goal.m:
compiler/det_infer_goal.m:
compiler/disj_gen.m:
compiler/du_type_layout.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_pred.m:
compiler/hlds_pred.m:
compiler/hlds_rtti.m:
compiler/inst_merge.m:
compiler/instance_method_clauses.m:
compiler/intermod.m:
compiler/interval.m:
compiler/ite_gen.m:
compiler/lookup_switch.m:
compiler/make_hlds_passes.m:
compiler/mark_tail_calls.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mode_errors.m:
compiler/parse_string_format.m:
compiler/passes_aux.m:
compiler/polymorphism.m:
compiler/polymorphism_info.m:
compiler/polymorphism_type_info.m:
compiler/pragma_c_gen.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/quantification.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_conj.m:
compiler/string_switch.m:
compiler/superhomogeneous.m:
compiler/switch_gen.m:
compiler/tag_switch.m:
compiler/type_constraints.m:
compiler/typecheck.m:
compiler/typecheck_clauses.m:
compiler/typecheck_coerce.m:
compiler/typecheck_error_unify.m:
compiler/unify_gen_deconstruct.m:
compiler/unify_proc.m:
compiler/var_origins.m:
    Conform to the changes above, and/or act on the new warnings.

browser/diff.m:
library/bit_buffer.m:
library/getopt.m:
library/getopt_io.m:
library/io.error_util.m:
library/io.file.m:
library/mercury_term_lexer.m:
library/parsing_utils.m:
library/pretty_printer.m:
library/robdd.m:
library/rtti_implementation.m:
library/string.builder.m:
library/string.parse_runtime.m:
mdbcomp/feedback.m:
    Act on the new warnings.

tests/hard_coded/sv_nested_closures.m:
    Change this test's code to avoid the new warnings, since
    (if --halt-at-warn is ever enabled) the warnings would interfere
    with its job .

tests/invalid/bug197.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/nullary_ho_func_error.err_exp:
tests/invalid/try_detism.err_exp:
tests/warnings/singleton_test_state_var.err_exp:
    Expect variable names for the middle versions of state vars
    using the new naming scheme.
2025-05-18 06:43:24 +10:00
Zoltan Somogyi
52ff48dafb Fix a comment. 2025-05-03 17:28:14 +10:00
Zoltan Somogyi
f0ab53e0ce Replace det_report.m with det_check_{proc,goal,switch}.m.
compiler/det_report.m:
    Delete this module, after moving all its code somewhere else.

compiler/det_check_proc.m:
compiler/det_check_goal.m:
compiler/det_check_switch.m:
    These new modules between them contain *most* of the code that used
    to be in det_report.m.

    - det_check_proc.m contains the code that checks for incompatibilities
      of a procedure's determinism with the other properties of the procedure
      (such as a I/O predicate being something other than det or cc_multi),

    - det_check_goal.m infers the determinism of goals, with the
      assistance of ...

    - det_check_switch.m handles checks on switches (such as reporting
      which cons_ids are not being handled by switches that should be
      complete but are not).

    det_check_switch.m is the largest of these modules; it is separate
    from det_check_goal.m *because* of its size.

compiler/check_hlds.m:
compiler/notes/compiler_design.html:
    Effect and document the replacement.

compiler/common.m:
compiler/det_infer_goal.m:
    Move some code from det_report.m to each of these modules.
    In each case, the moved code is used only by the moved-to module.

compiler/det_analysis.m:
    Move code from det_report.m, that is used only by *clients* of this module
    to control the operation of this module, to this module.

compiler/error_spec.m:
    Move code from det_report.m that is used by more than one of the new
    modules.

compiler/deforest.m:
compiler/det_util.m:
compiler/fact_table.m:
compiler/format_call.m:
compiler/pd_util.m:
compiler/try_expand.m:
    Conform to the changes above.
2025-03-11 23:27:30 +11:00
Zoltan Somogyi
83fe4d2331 Print more useful actual/expected type diffs.
compiler/typecheck_errors.m:
    When we report a mismatch between the actual and expected types,
    we print both types. When both these types are big, doing a "visual diff"
    between the two can be hard, which is why, for a while now, we have had
    code to print the diffs between the two types. However, the code computing
    this diff had a bug. It detected and reported situations in which the
    actual and expected types had, in the same position, the same type
    constructor name but with two different arities, but ignored situations
    in the type constructed differed in their names, which is a much more
    common problem. Fix this.

    When we report a mismatch between the actual and expected types,
    we print the types themselves colored incorrect and correct respectively.
    But when we diagnosed the sources of the difference, we printed
    only the report of the mismatch using the incorrect color, and printed
    the mismatched actual and expected entities using the "inconsistent" color.
    Fix this.

compiler/typecheck_error_type_assign.m:
    When the difference between actual and expected types occurs even when
    builtin qualifiers have been stripped type names in both, record the
    types so stripped not just in the format_pieces representations of the
    types, but the types themselves. This avoids the need for new code
    in typecheck_errors.m to consider the same issue.

compiler/error_spec.m:
    Minor improvements in comments.

tests/invalid/actual_expected.err_exp:
tests/invalid/arith_wrong_module.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/ext_type_bug.err_exp:
tests/invalid/fbnf.err_exp:
tests/invalid/higher_order_mode_mismatch.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid_purity/impure_pred_t1_fixed.err_exp:
tests/invalid_purity/impure_pred_t2.err_exp:
tests/invalid_purity/purity_nonsense_1.err_exp:
tests/invalid_purity/purity_nonsense_2.err_exp:
    Expect updated diagnostics.
2025-02-14 16:52:17 +11:00
Zoltan Somogyi
2e3cd678af Extend "did you mean" messages to singleton vars.
compiler/make_hlds_warn.m:
    When reporting that a variable is a singleton, check whether
    any of the other variable names in the scope are close similar to it,
    and if some are, then suggest them in a "did you mean" message.

    Since the "did you mean" suggestions will (except in contrived
    situations) obviously be different for each singleton variable,
    make a separate "did you mean" report for each singleton variable
    that has is sufficiently close to similar names. Continue to report
    all the other, non-"did you mean" singleton variables together.

compiler/error_spec.m:
    Extend "did you mean" messages to check for situations in which
    the erroneous name we are reporting, and its closest match
    (or, in general, one of its closest matches) differ only in case.
    Due to "psychological set", such differences can be hard to see
    even when one is looking directly at the two names, so add a
    message that draws users' attention *specifically* to this point.

tests/invalid/undef_symbol.{m,err_exp}:
    Add a test of the new capability to this existing test case.

tests/warnings/pragma_source_file.err_exp:
tests/warnings/singleton_test.err_exp:
    Expect the new output added by this diff.

tests/warnings/singleton_test.err_exp2:
tests/warnings/singleton_test.err_exp3:
    Expect the new output added by this diff, *and* the color
    added by a diff a few months ago.
2024-12-09 04:24:59 +11:00
Zoltan Somogyi
407525af59 Describe calls using not strings, but pieces.
compiler/hlds_out_util.m:
    Make the functions that describe calls return format_pieces instead of
    strings, and rename them accordingly.

compiler/det_report.m:
compiler/mode_errors.m:
compiler/typecheck_error_util.m:
    Use the returned pieces.

compiler/error_spec.m:
    Add a new format_piece, upper_case_next, for use in one of the modules
    above.

compiler/write_error_spec.m:
    Implement the new format_piece, by generalizing
    lower_case_next_if_not_first.
2024-11-01 16:46:28 +11:00
Zoltan Somogyi
454a9e2d84 Make --print-error-spec-id apply to extracted msgs.
When e.g. mode analysis prints a diagnostic for a mode error,
it does so by taking an error_spec generated for the *actual* error,
extracing the error_msgs from it, and wrapping a new error_spec around them.
This has meant that if you specify --print-error-spec-id, you get the
id of the predicate that does the wrapping, NOT the id of the predicate
that generated the message describing the actual error.

This diff fixes this.

compiler/error_spec.m:
    Make the predicates that extract error_msgs from error_specs
    also return the error_spec's id.

    Add a version of those predicates that adds a final error_msg
    to the extracted error_msgs that includes this id at the end
    of the message, if --print-error-spec-id is specified.

    To do that, move here the code from write_error_spec.m that
    has always done this job for --print-error-spec-id, and wrap
    an (exported) predicate around it.

compiler/write_error_spec.m:
    Replace the moved code with that predicate.

compiler/mode_errors.m:
compiler/recompilation.check.m:
    Include the id in any extracted error_msgs if asked for.

compiler/error_sort.m:
    Ignore extracted ids.
2024-10-25 16:18:17 +11:00
Zoltan Somogyi
c7a72fee02 Add a blank before "possible reason" messages.
Mode analysis and determinism analysis can generate error_specs that
have long had the form:

    context1:   There was a problem with X. The following messages give
    context1:    possible reasons for this problem.
    context2:   Possible reason 1.
    context3:   Possible reason 2.

This diff adds a line containing only a context before every reason, yielding
this modified output:

    context1:   There was a problem with X. The following messages give
    context1:    possible reasons for this problem.
    context2:
    context2:   Possible reason 1.
    context3:
    context3:   Possible reason 2.

The new layout makes it significantly easier for readers to find
where each reason's description begins, especially when a single reason's
description itself takes several lines.

compiler/error_spec.m:
    Add a new kind of error_msg for the new lines, which are blank
    except possibly (usually) for a context.

    Move a utility predicate here from error_sort.m, to make it available
    to error_util.m.

compiler/error_util.m:
    Add a predicate that prefaces each reason's error_msg with the new
    blank line error_msg.

compiler/write_error_spec.m:
    Implement the new kind of error_msg.

compiler/det_report.m:
compiler/mode_errors.m:
    Use the new capability in the error_specs that list possible reasons
    for a problem.

compiler/error_sort.m:
    Conform to the changes above.

    Delete a predicate moved to error_spec.

tests/invalid/bug150.err_exp:
tests/invalid/bug150_partial_color.err_exp:
tests/invalid/bug496.err_exp:
tests/invalid/det_atomic_goal_msgs.err_exp:
tests/invalid/det_errors.err_exp:
tests/invalid/det_errors_and_io.err_exp:
tests/invalid/det_errors_deep.err_exp:
tests/invalid/magicbox.err_exp:
tests/invalid/mode_inf.err_exp:
tests/invalid/not_a_switch.err_exp:
tests/invalid/try_detism.err_exp:
tests/invalid/user_field_access_decl_override_1.err_exp:
    Expect updated diagnostics.
2024-10-01 13:34:26 +02:00
Zoltan Somogyi
0e2997dc1e Enable, document and announce color diagnostics.
compiler/options.m:
    Make --color-diagnostics enabled by default.

    Add a synonym for the --color-scheme option that uses British spelling.
    (We already had such a synonym for --color-diagnostics.)

    Document the user-visible options controlling colors in diagnostics.

    Delete old, now-superseded commented-out documentation.

doc/user_guide.texi:
    Document the user-visible options and environment variables
    controlling colors in diagnostics.

    Add a new chapter that explains in some detail not only the use of color
    in diagnostics, but also the effects of the --verbose-error-messages,
    --reverse-error-order and --limit-error-contexts options.

    Delete old, now-superseded commented-out documentation.

NEWS.md:
    Announce the compiler's new capabilities.

The rest of this diff is concerned with fixing an old issue with
--limit-error-contexts, which is that it operated on error_msgs.
If an error_spec contained two or more error_msgs, the compiler
could print some error_msgs whose contexts fell into one of the to-be-printed
ranges of line numbers, while NOT printing some other error_msgs whose
contexts did not do so. Since the wording of a non-first error_msg
often builds upon and references the wording of previous error_msgs
in the same error_spec, this was a problem. This diff changes things
so that if *any* error_msg in an error_spec has a context that falls into
one of the to-be-printed ranges of line numbers, we print *all* of the
error_msgs in that error_spec.

compiler/write_error_spec.m:
    To make the above possible, separate out the task of deciding which parts
    of error_specs to print, and which parts not to print, from the task
    of actually writing them out. The decision part takes into account
    the conditional-on-verbosity-level and conditional-on-an-option-value
    parts of error_msgs, as well as the effect of --limit-error-contexts.

    Construct the color database and look up the max line width exactly once
    for each call to the exported predicates of this module.

compiler/error_spec.m:
    Delete the print_anything function symbol from the error_msg_component
    type. Its presence would have made the separation of making-decisions code
    from writing-out code in write_error_spec.m more complicated. Since
    the stuff it prints is not a list of pieces, handling it would have
    required using a data structure containing either lists of (either pieces
    or anythings) to communicate between the decision code and the writing
    code. While this is doable, it is not elegant, and it turns out to be
    far simpler to just eliminate print_anything.

    Take the opportunity to change the type of the extra indent field
    in error_msgs from int to uint.

compiler/mode_errors.m:
    Replace the only use of print_anything in the compiler with code
    that just uses lists of pieces. (This is possible now due to the
    rewrite of write_goal in terms of format_goal, which happened many years
    *after* we implemented print_anything specifically for this use case.)

compiler/accumulator.m:
    Fix the only place in the compiler that actually depended on the
    implicit newline we used to add between consecutive error_msgs in an
    error_spec (at least according to our test suite). Everywhere else,
    we routine end every error_msg with an *explicit* nl piece, and now
    we do so here as well.

    Simplify the code constructing the one error_spec here by replacing
    several conditional-on-an-option-value error_messages with a simple
    test of that option, since its value is trivially available here.
    (Two components of that error_spec *should* have been conditional
    on the same option but were not, so this is a bug fix as well.)

    Rename some predicates to avoid ambiguity.

compiler/check_typeclass.m:
compiler/common.m:
compiler/error_sort.m:
compiler/error_util.m:
compiler/fact_table.m:
compiler/mercury_compile_main.m:
compiler/options_file.m:
compiler/parse_module.m:
compiler/post_typecheck.m:
compiler/term_constr_errors.m:
compiler/term_errors.m:
    Conform to the int->uint change in error_spec.m.
2024-06-23 14:37:27 +10:00
Zoltan Somogyi
2526c4b8cb Split possible_cause into hint and inconsistent.
compiler/error_spec.m:
    Replace the possible_cause color name with the hint and inconsistent
    color names, as agreed on m-rev.

compiler/globals.m:
compiler/write_error_spec.m:
    Update the code handling color names and their mapping to color shades.

compiler/options.m:
    Update the invisible options that record that mapping.

tools/bootcheck:
    Update the color scheme used for the test suite.

compiler/accumulator.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/equiv_type.m:
compiler/format_call_errors.m:
compiler/inst_check.m:
compiler/make_hlds_error.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_warn.m:
compiler/mode_errors.m:
compiler/module_qual.qual_errors.m:
compiler/parse_item.m:
compiler/parse_mutable.m:
compiler/parse_pragma.m:
compiler/parse_type_defn.m:
compiler/post_typecheck.m:
compiler/simplify_goal_call.m:
compiler/simplify_proc.m:
compiler/split_parse_tree_src.m:
compiler/state_var.m:
compiler/termination.m:
compiler/typecheck_error_overload.m:
compiler/typecheck_error_undef.m:
compiler/typecheck_errors.m:
    Replace uses of the possible_cause color with one of its replacements.
    In a few cases, adjust some other colors as well.

tests/invalid/bad_statevar_bad_context.err_exp:
tests/invalid/bind_in_negated.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/coerce_disambig.err_exp:
tests/invalid/coerce_implied_mode.err_exp:
tests/invalid/coerce_infer.err_exp:
tests/invalid/coerce_non_du.err_exp:
tests/invalid/coerce_type_error.err_exp:
tests/invalid/coerce_unify_tvars.err_exp:
tests/invalid/conflicting_tabling_pragmas.err_exp:
tests/invalid/dcg_context.err_exp:
tests/invalid/default_ho_inst_2.err_exp:
tests/invalid/fbnf.err_exp:
tests/invalid/freefree.err_exp:
tests/invalid/functor_ho_inst_bad_1.err_exp:
tests/invalid/functor_ho_inst_bad_3.err_exp:
tests/invalid/higher_order_mode_mismatch.err_exp:
tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/merge_inst_error.err_exp:
tests/invalid/mode_inf.err_exp:
tests/invalid/modes_erroneous.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/partial_implied_mode.err_exp:
tests/invalid/quant_constraint_1.err_exp:
tests/invalid/quant_constraint_2.err_exp:
tests/invalid/string_format_bad.err_exp:
tests/invalid/string_format_unknown.err_exp:
tests/invalid/test_may_duplicate.err_exp:
tests/invalid/test_may_export_body.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid/typeclass_dup_method_mode.err_exp:
tests/invalid_make_int/bad_mutable_int.int_err_exp:
tests/invalid_nodepend/duplicate_modes.err_exp:
tests/invalid_nodepend/errors_2.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/invalid_nodepend/require_tailrec_invalid.err_exp:
tests/invalid_nodepend/test_with_type.err_exp:
tests/invalid_nodepend/unbound_type_vars.err_exp:
tests/invalid_purity/impure_pred_t1_fixed.err_exp:
tests/invalid_purity/impure_pred_t2.err_exp:
tests/invalid_purity/purity_nonsense_1.err_exp:
tests/invalid_purity/purity_nonsense_2.err_exp:
tests/warnings/foreign_term_invalid.err_exp:
tests/warnings/format_call_multi.err_exp:
tests/warnings/format_call_warning.err_exp:
tests/warnings/table_with_inline.err_exp:
tests/warnings/warn_succ_ind.err_exp:
tests/warnings/warn_succ_ind.err_exp2:
tests/warnings/warn_succ_ind.err_exp3:
    Expect updated diagnostics.
2024-06-07 08:42:26 +10:00
Zoltan Somogyi
7501c52c8b Allow some colors to be left unspecified.
compiler/globals.m:
    When a @specified color scheme leaves some colors unspecified,
    do not generate an error; generate an informational message instead.
    Change the data structure we use to record color schemes to accommodate
    this fact.

compiler/options.m:
    Add the option on which the new informational message is conditional.
    Do not document it yet.

compiler/handle_options.m:
    Ignore those informational messages for now.

compiler/error_spec.m:
    When coloring a list of pieces, record the color in the piece
    that marks the end of the list, as well as in the start,
    since new code in write_error_spec.m now needs this.

compiler/write_error_spec.m:
    When processing a piece that marks either the start or the end
    of a colored region, ignore the piece of the color role named
    in that piece has no assigned color. This leaves all the pieces
    in between with the same color as the pieces on the two sides
    of the to-be-ostensibly-colored region.
2024-06-07 00:58:14 +10:00
Zoltan Somogyi
76f95cfa98 Fit the x_list_to_y_pieces functions into a scheme.
compiler/error_spec.m:
    The existing functions for formatting lists of items as the
    components of diagnostics grew without design. Impose a design
    on them, by fitting them into an overall scheme with consistent
    rules, and document those rules.

    Add a utility function that me

compiler/format_call_errors.m:
    when generating a diagnostic reporting a mismatch between a
    conversion specifier and the value in a poly_type, and that kind of
    poly_type had more than one valid conversion specifier, we intended
    to print all of those valid conversion specifiers, but missed printing
    the first one. Fix this.

compiler/typecheck_error_type_assign.m:
    Fix an old bug: when dumping a type assign for the verbose part
    of a type error's diagnostic, we used to print the first of
    any typeclass constraints on the type vars of the type assign
    at the end of the line giving the type of the last program variable,
    even though that info is not specific to that program variable.

compiler/add_clause.m:
compiler/add_foreign_enum.m:
compiler/add_foreign_proc.m:
compiler/add_mode.m:
compiler/add_pragma.m:
compiler/check_import_accessibility.m:
compiler/check_pragma_format_call.m:
compiler/check_type_inst_mode_defns.m:
compiler/check_typeclass.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/direct_arg_in_out.m:
compiler/du_type_layout.m:
compiler/equiv_type.m:
compiler/error_msg_inst.m:
compiler/error_type_util.m:
compiler/handle_options.m:
compiler/hlds_call_tree.m:
compiler/hlds_error_util.m:
compiler/inst_check.m:
compiler/make_hlds_error.m:
compiler/make_hlds_warn.m:
compiler/mode_errors.m:
compiler/module_qual.qual_errors.m:
compiler/old_type_constraints.m:
compiler/options_file.m:
compiler/parse_class.m:
compiler/parse_goal.m:
compiler/parse_inst_mode_defn.m:
compiler/parse_inst_mode_name.m:
compiler/parse_item.m:
compiler/parse_pragma.m:
compiler/parse_pragma_foreign.m:
compiler/parse_type_defn.m:
compiler/post_typecheck.m:
compiler/prog_event.m:
compiler/prog_foreign_enum.m:
compiler/prog_mode.m:
compiler/recompilation.check.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_disj.m:
compiler/state_var.m:
compiler/superhomogeneous.m:
compiler/typecheck_error_overload.m:
compiler/typecheck_error_undef.m:
compiler/typecheck_errors.m:
    Conform to the changes in error_spec.m.

tests/invalid/mpj_1.err_exp:
tests/invalid/string_format_bad.err_exp:
tests/invalid/string_format_unknown.err_exp:
tests/warnings/format_call_warning.err_exp:
    Expect updated diagnostics.
2024-06-01 16:21:40 +10:00
Zoltan Somogyi
43fb03f6fb Move all x_list_to_y_pieces functions to the end. 2024-05-22 17:59:43 +10:00
Zoltan Somogyi
2630a758c9 Delete an unneeded argument.
compiler/error_spec.m:
    As above.

compiler/add_foreign_enum.m:
compiler/add_pragma.m:
compiler/check_typeclass.m:
compiler/post_typecheck.m:
compiler/prog_foreign_enum.m:
    Delete that argument from calls.
2024-05-22 17:42:08 +10:00
Zoltan Somogyi
0e1e909f80 More minor updates to diagnostics.
compiler/det_analysis.m:
compiler/post_typecheck.m:
compiler/typecheck_errors.m:
    Use color more sparingly. Improve wording.

compiler/error_spec.m:
    Add two new utility functions for coloring lists of items, and
    add a new kind of piece, purity_desc_article. Both are for use by
    new code in the modules above.

compiler/write_error_spec.m:
    Implement the new kind of piece.

tests/invalid/arg_permutation.err_exp:
tests/invalid/arith_wrong_module.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bug184.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug214.err_exp:
tests/invalid/bug257.err_exp:
tests/invalid/coerce_void.err_exp:
tests/invalid/fbnf.err_exp:
tests/invalid/freefree.err_exp:
tests/invalid/getopt_old.err_exp:
tests/invalid/higher_order_mode_mismatch.err_exp:
tests/invalid/integral_constant_no_suffix.err_exp:
tests/invalid/one_member.err_exp:
tests/invalid/pragma_c_code_no_det.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid/typeclass_test_8.err_exp:
tests/invalid/unsatisfiable_constraint.err_exp:
tests/invalid_purity/impure_pred_t1_fixed.err_exp:
tests/invalid_purity/impure_pred_t2.err_exp:
tests/invalid_purity/purity_nonsense_1.err_exp:
tests/invalid_purity/purity_nonsense_2.err_exp:
tests/warnings/singleton_test.err_exp:
    Expect updated diagnostics.
2024-05-21 23:40:31 +10:00
Zoltan Somogyi
4da9c3745e Use understated colors in mode_errors.m.
compiler/mode_errors.m:
    Use color more sparingly, to preserve the impact of when we *do* use it.

    Improve the wording of some diagnostics. In one case, include
    more information in a diagnostic.

    Add some XXXs about possible further improvements.

compiler/make_hlds_warn.m:
    Use color more sparingly, to preserve the impact of when we *do* use it.

compiler/error_spec.m:
    Add two utility functions for use by new code in mode_errors.m.

compiler/modecheck_call.m:
    Supply the extra detail now expected by mode_errors.m.

tests/invalid/any_mode.err_exp:
tests/invalid/any_passed_as_ground.err_exp:
tests/invalid/any_should_not_match_bound.err_exp:
tests/invalid/any_to_ground_in_ite_cond.err_exp:
tests/invalid/any_to_ground_in_ite_cond_nomax.err_exp:
tests/invalid/anys_in_negated_contexts.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bind_in_negated.err_exp:
tests/invalid/bind_var_errors.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug278.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/char_inst.err_exp:
tests/invalid/coerce_clobbered.err_exp:
tests/invalid/coerce_disambig.err_exp:
tests/invalid/coerce_instvar.err_exp:
tests/invalid/coerce_int.err_exp:
tests/invalid/coerce_mode_error.err_exp:
tests/invalid/coerce_mode_error2.err_exp:
tests/invalid/coerce_recursive_inst.err_exp:
tests/invalid/coerce_recursive_type.err_exp:
tests/invalid/constrained_poly_insts2.err_exp:
tests/invalid/currying_multimode_func.err_exp:
tests/invalid/dcg_context.err_exp:
tests/invalid/default_ho_inst_1.err_exp:
tests/invalid/default_ho_inst_2.err_exp:
tests/invalid/field_syntax_error.err_exp:
tests/invalid/freefree.err_exp:
tests/invalid/functor_ho_inst_bad_1.err_exp:
tests/invalid/functor_ho_inst_bad_2.err_exp:
tests/invalid/functor_ho_inst_bad_3.err_exp:
tests/invalid/ho_any_inst.err_exp:
tests/invalid/ho_default_func_1.err_exp:
tests/invalid/ho_default_func_2.err_exp:
tests/invalid/ho_default_func_3.err_exp:
tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/ho_unique_error.err_exp:
tests/invalid/invalid_integral_call_inst.err_exp:
tests/invalid/io_in_ite_cond.err_exp:
tests/invalid/merge_inst_error.err_exp:
tests/invalid/mode_inf.err_exp:
tests/invalid/modes_erroneous.err_exp:
tests/invalid/mostly_uniq1.err_exp:
tests/invalid/mostly_uniq2.err_exp:
tests/invalid/multimode_addr_problems.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/partial_implied_mode.err_exp:
tests/invalid/polymorphic_unification.err_exp:
tests/invalid/state_vars_test_1.err_exp:
tests/invalid/state_vars_test_2.err_exp:
tests/invalid/state_vars_test_5.err_exp:
tests/invalid/try_detism.err_exp:
tests/invalid/unify_mode_error.err_exp:
tests/invalid/uniq_modes.err_exp:
tests/invalid/uniq_neg.err_exp:
tests/invalid_nodepend/errors_1.err_exp:
tests/invalid_nodepend/errors_2.err_exp:
tests/invalid_nodepend/errors_3.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/invalid_purity/impure_func_t5_fixed.err_exp:
tests/invalid_purity/purity.err_exp:
tests/warnings/double_underscore.err_exp:
tests/warnings/foreign_singleton.err_exp:
tests/warnings/missing_singleton_warning.err_exp:
tests/warnings/pragma_source_file.err_exp:
tests/warnings/singleton_test.err_exp:
tests/warnings/singleton_test_state_var.err_exp:
tests/warnings/unknown_warning.err_exp:
    Expect updated diagnostics.
2024-05-21 18:46:21 +10:00
Zoltan Somogyi
c552c9e32c Update field name prefixes. 2024-05-13 11:17:21 +10:00
Zoltan Somogyi
9f2d94d3ed Use color in parse_type_defn.m and other modules.
compiler/add_clause.m:
compiler/check_for_missing_type_defns.m:
compiler/parse_type_defn.m:
compiler/field_access.m:
compiler/goal_expr_to_goal.m:
    Add color to the diagnostics generated by these modules.

    In a few cases, improve the wording of the diagnostic.

compiler/add_type.m:
    Address a review comment.

compiler/error_spec.m:
    Generalize a function for use by check_for_missing_type_defns.m.

compiler/make_hlds.m:
    Update the obsoleted part of a comment.

tests/invalid_make_int/Mmakefile:
    Enable the use of color for the diagnostics in this test directory.

tests/invalid/make_opt_error.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/multimode_syntax.err_exp:
tests/invalid/record_syntax_errors.err_exp:
tests/invalid/ref_to_implicit_comma.err_exp:
tests/invalid/subtype_user_compare.err_exp:
tests/invalid/try_io_else.err_exp:
tests/invalid/type_no_arrow.err_exp:
tests/invalid/type_with_no_defn.err_exp:
tests/invalid/types2.err_exp:
tests/invalid_make_int/bad_existential_data_type.int_err_exp:
tests/invalid_make_int/bad_foreign_type_int.int_err_exp:
tests/invalid_make_int/type_vars_int.int_err_exp:
tests/invalid_nodepend/bigtest.err_exp:
tests/invalid_nodepend/errors_1.err_exp:
tests/invalid_nodepend/errors_3.err_exp:
tests/invalid_nodepend/fundeps_unbound_in_ctor.err_exp:
tests/invalid_nodepend/subtype_syntax.err_exp:
tests/invalid_nodepend/type_lhs_var.err_exp:
tests/invalid_nodepend/types.err_exp:
tests/invalid_nodepend/uu_type.err_exp:
tests/invalid_nodepend/where_abstract_enum.err_exp:
tests/invalid_nodepend/where_direct_arg_1.err_exp:
tests/invalid_nodepend/where_direct_arg_2.err_exp:
tests/warnings/unneeded_mode_specific_clause.err_exp:
    Expect updated diagnostics.
2024-05-12 10:51:51 +10:00
Zoltan Somogyi
3846b3f74d Use color in inst_check.m.
compiler/inst_check.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
    Generalize an existing function for the needs of inst_check.m, and
    fix an inconsistency between its name and the name of another function.

compiler/add_foreign_proc.m:
compiler/check_typeclass.m:
compiler/det_analysis.m:
compiler/error_spec.m:
compiler/make_hlds_warn.m:
compiler/post_typecheck.m:
compiler/state_var.m:
    Conform to the change in error_spec.m.

tests/invalid/bad_inst_for_type.err_exp:
tests/invalid/inst_for_eqv_type.err_exp:
tests/warnings/inst_with_no_type.err_exp:
    Expect updated diagnostics.
2024-05-11 08:51:11 +10:00
Zoltan Somogyi
ca9234fef7 Use color in add_mode.m and det_analysis.m.
compiler/add_mode.m:
compiler/det_analysis.m:
    Add color to the diagnostics generated by these modules.

compiler/error_spec.m:
    Fix variable names.

tests/invalid/bad_inst_for_type.err_exp:
tests/invalid/bug238.err_exp:
tests/invalid/circ_inst_1.err_exp:
tests/invalid/circ_inst_2.err_exp:
tests/invalid/circ_inst_3.err_exp:
tests/invalid/circ_inst_4.err_exp:
tests/invalid/circ_inst_5.err_exp:
tests/invalid/circ_mode_1.err_exp:
tests/invalid/circ_mode_2.err_exp:
tests/invalid/circ_mode_3.err_exp:
tests/invalid/circ_mode_4.err_exp:
tests/invalid/det_errors_cc.err_exp:
tests/invalid/one_member.err_exp:
tests/invalid/promise_equivalent_solutions_test_1.err_exp:
tests/invalid/promise_equivalent_solutions_test_2.err_exp:
tests/invalid/try_detism.err_exp:
    Expect updated diagnostics.
2024-05-11 06:32:09 +10:00
Zoltan Somogyi
91278a7fad Use color to add_foreign_proc.m.
compiler/add_foreign_proc.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
compiler/make_hlds_warn.m:
    Move a function from make_hlds_warn.m to error_spec.m, to make it
    available to add_foreign_proc.m.

tests/invalid/foreign_purity_mismatch.err_exp:
tests/invalid/pragma_c_code_dup_var.err_exp:
    Expect updated diagnostics.
2024-05-10 04:02:52 +10:00
Zoltan Somogyi
803a3f5c5e Use color in make_hlds_warn.m.
compiler/make_hlds_warn.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
    Delete stray space.

tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug278.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/field_syntax_error.err_exp:
tests/invalid/state_vars_test2.err_exp:
tests/invalid/state_vars_test5.err_exp:
tests/invalid_nodepend/errors.err_exp:
tests/invalid_nodepend/errors1.err_exp:
tests/invalid_nodepend/errors2.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/warnings/double_underscore.err_exp:
tests/warnings/foreign_singleton.err_exp:
tests/warnings/missing_singleton_warning.err_exp:
tests/warnings/pragma_source_file.err_exp:
tests/warnings/singleton_test.err_exp:
tests/warnings/singleton_test_state_var.err_exp:
tests/warnings/unknown_warning.err_exp:
tests/warnings/warn_succ_ind.err_exp:
    Expect the updated diagnostics.
2024-05-09 17:06:30 +10:00
Zoltan Somogyi
7363b2dd4c Use color in state_var.m.
compiler/state_var.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
    Add a utility function needed by new code in state_var.m. Implement it
    using a general predicate that has a bunch of existing functions
    as its special cases.

tests/invalid/bad_statevar_bad_context.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/state_vars_test2.err_exp:
tests/invalid/state_vars_test3.err_exp:
tests/invalid/state_vars_test4.err_exp:
tests/warnings/state_vars_test.err_exp:
    Expect the updated diagnostics.
2024-05-09 10:34:06 +10:00
Zoltan Somogyi
5c3601629a Use color in post_typecheck.m.
compiler/post_typecheck.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
    Add a utility function needed by the new code.

tests/recompilation/Mmakefile:
    Enable color for the diagnostics in this test directory.

tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bug184.err_exp:
tests/invalid/bug214.err_exp:
tests/invalid/bug257.err_exp:
tests/invalid/coerce_void.err_exp:
tests/invalid/freefree.err_exp:
tests/invalid/typeclass_dup_method_mode.err_exp:
tests/invalid/typeclass_test_8.err_exp:
tests/invalid/unsatisfiable_constraint.err_exp:
tests/invalid_nodepend/duplicate_modes.err_exp:
tests/invalid_nodepend/invalid_main.err_exp:
tests/recompilation/typeclass_method_pragma_r.err_exp.2:
tests/recompilation/with_type_re.err_exp.2:
tests/warnings/singleton_test.err_exp:
    Expect the updated diagnostics.
2024-05-09 08:41:01 +10:00
Zoltan Somogyi
e0ed52b225 Use color in check_typeclass.m.
compiler/check_typeclass.m:
    Add color to the diagnostics generated by this module.

compiler/error_spec.m:
    Add a utility function for new code for check_typeclass.m.

tests/invalid/ambiguous_method.err_exp:
tests/invalid/ambiguous_method_2.err_exp:
tests/invalid/bad_instance2.err_exp:
tests/invalid/complex_constraint_err.err_exp:
tests/invalid/constraint_proof_bug_lib.err_exp:
tests/invalid/fundeps_coverage.err_exp:
tests/invalid/instance_var_bug.err_exp:
tests/invalid/invalid_instance_declarations.err_exp:
tests/invalid/quant_constraint_1.err_exp:
tests/invalid/quant_constraint_2.err_exp:
tests/invalid/range_restrict.err_exp:
tests/invalid/typeclass_constraint_extra_var.err_exp:
tests/invalid/typeclass_test_12.err_exp:
tests/invalid/typeclass_test_9.err_exp:
tests/invalid_nodepend/fundeps_unbound_in_ctor.err_exp:
tests/invalid_nodepend/type_vars.err_exp:
tests/invalid_nodepend/unbound_type_vars.err_exp:
tests/invalid_nodepend/undetermined_existential_data_type.err_exp:
    Expect the updated diagnostics.
2024-05-09 04:12:53 +10:00
Zoltan Somogyi
75dc210ab3 Color subjects in mode errors.
compiler/mode_errors.m:
    Use color to emphasize the subject variables and terms of mode errors.

    Use color to emphasize disagreements as possible causes
    in a few more places.

    Require reporters of the "no matching mode" error to specify
    what the match was against. This is to allow us to generate
    better diagnostics. Note the things that should still be improved.

    Use the names of numbers (as in two, three etc) instead of their numeral
    versions.

    Improve the wording and/or the punctuation of some error messages.

compiler/inst_mode_type_prop.m:
    Use the names of numbers (as in two, three etc) instead of their numeral
    versions.

compiler/error_spec.m:
compiler/write_error_spec.m:
    Move two formerly private utility functions of write_error_spec.m
    to error_spec.m, and export them. One of them is now used in
    the modules above.

compiler/modecheck_call.m:
compiler/modecheck_goal.m:
compiler/modecheck_unify.m:
compiler/modecheck_util.m:
compiler/unique_modes.m:
    Pass around info about "what the match was against" for the change
    in mode_errors.m descrived above.

compiler/hlds_out_util.m:
    Improve variable name.

compiler/hlds_module.m:
    Add an XXX.

tests/invalid/any_mode.err_exp:
tests/invalid/any_passed_as_ground.err_exp:
tests/invalid/any_should_not_match_bound.err_exp:
tests/invalid/any_to_ground_in_ite_cond.err_exp:
tests/invalid/any_to_ground_in_ite_cond_nomax.err_exp:
tests/invalid/anys_in_negated_contexts.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bind_in_negated.err_exp:
tests/invalid/bind_var_errors.err_exp:
tests/invalid/bug117.err_exp:
tests/invalid/bug191.err_exp:
tests/invalid/bug278.err_exp:
tests/invalid/bug415.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/char_inst.err_exp:
tests/invalid/coerce_clobbered.err_exp:
tests/invalid/coerce_disambig.err_exp:
tests/invalid/coerce_implied_mode.err_exp:
tests/invalid/coerce_instvar.err_exp:
tests/invalid/coerce_int.err_exp:
tests/invalid/coerce_mode_error.err_exp:
tests/invalid/coerce_mode_error2.err_exp:
tests/invalid/coerce_recursive_inst.err_exp:
tests/invalid/coerce_recursive_type.err_exp:
tests/invalid/coerce_uniq.err_exp:
tests/invalid/constrained_poly_insts2.err_exp:
tests/invalid/currying_multimode_func.err_exp:
tests/invalid/dcg_context.err_exp:
tests/invalid/default_ho_inst.err_exp:
tests/invalid/default_ho_inst_2.err_exp:
tests/invalid/freefree.err_exp:
tests/invalid/functor_ho_inst_bad.err_exp:
tests/invalid/functor_ho_inst_bad_2.err_exp:
tests/invalid/functor_ho_inst_bad_3.err_exp:
tests/invalid/ho_any_inst.err_exp:
tests/invalid/ho_default_func_1.err_exp:
tests/invalid/ho_default_func_2.err_exp:
tests/invalid/ho_default_func_3.err_exp:
tests/invalid/ho_default_func_4.err_exp:
tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/ho_unique_error.err_exp:
tests/invalid/html.err_exp:
tests/invalid/inst_matches_final_bug.err_exp:
tests/invalid/invalid_integral_call_inst.err_exp:
tests/invalid/io_in_ite_cond.err_exp:
tests/invalid/merge_ground_any.err_exp:
tests/invalid/merge_inst_error.err_exp:
tests/invalid/mode_error_arg_number.err_exp:
tests/invalid/mode_inf.err_exp:
tests/invalid/modes_erroneous.err_exp:
tests/invalid/mostly_uniq1.err_exp:
tests/invalid/mostly_uniq2.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/no_ho_inst.err_exp:
tests/invalid/partial_implied_mode.err_exp:
tests/invalid/polymorphic_unification.err_exp:
tests/invalid/state_vars_test1.err_exp:
tests/invalid/state_vars_test5.err_exp:
tests/invalid/try_detism.err_exp:
tests/invalid/unify_mode_error.err_exp:
tests/invalid/uniq_modes.err_exp:
tests/invalid/uniq_neg.err_exp:
tests/invalid_nodepend/constrained_poly_insts.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/invalid_purity/impure_func_t5_fixed.err_exp:
tests/invalid_purity/purity.err_exp:
tests/warnings/simple_code.err_exp:
tests/warnings/unify_f_g.err_exp:
    Expect the updated versions of diagnostics.
2024-05-07 10:11:11 +10:00
Zoltan Somogyi
2fef92cfa8 Color the subjects of error messages.
compiler/error_spec.m:
    Add a new color name, color_subject, which is intended to be used
    as the color for the subject of an error message. The subject is
    usually a variable or a term.

compiler/options.m:
doc/user_guide.texi:
    Add an option to control the actual color for this color name.

compiler/globals.m:
    Include the new color in the color specification database.

compiler/write_error_spec.m:
    Handle the conversion of the new color name to the selected
    corresponding actual color.

compiler/typecheck_errors.m:
    Color the subjects of (most of) the error messages generated by
    this module with the new color.

tests/invalid/abstract_eqv.err_exp:
tests/invalid/actual_expected.err_exp:
tests/invalid/actual_more_expected.err_exp:
tests/invalid/arg_permutation.err_exp:
tests/invalid/arith_wrong_module.err_exp:
tests/invalid/bad_statevar_bad_context.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/coerce_infer.err_exp:
tests/invalid/coerce_non_du.err_exp:
tests/invalid/coerce_type_error.err_exp:
tests/invalid/coerce_unify_tvars.err_exp:
tests/invalid/comparison.err_exp:
tests/invalid/error_in_list.err_exp:
tests/invalid/exist_foreign_error.err_exp:
tests/invalid/ext_type.err_exp:
tests/invalid/ext_type_bug.err_exp:
tests/invalid/fbnf.err_exp:
tests/invalid/foreign_procs_exist_type.err_exp:
tests/invalid/getopt_old.err_exp:
tests/invalid/higher_order_mode_mismatch.err_exp:
tests/invalid/ho_type_arity_bug.err_exp:
tests/invalid/illtyped_compare.err_exp:
tests/invalid/integral_constant_no_suffix.err_exp:
tests/invalid/method_impl.err_exp:
tests/invalid/mixed_up_streams.err_exp:
tests/invalid/mpj1.err_exp:
tests/invalid/mpj4.err_exp:
tests/invalid/no_method.err_exp:
tests/invalid/nullary_ho_func_error.err_exp:
tests/invalid/overloading.err_exp:
tests/invalid/record_syntax_errors.err_exp:
tests/invalid/try_bad_params.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid/type_error_ambiguous.err_exp:
tests/invalid/type_error_in_arg.err_exp:
tests/invalid/type_mismatch.err_exp:
tests/invalid/types2.err_exp:
tests/invalid/user_field_access_decl_override2.err_exp:
tests/invalid_nodepend/errors2.err_exp:
tests/invalid_purity/impure_func_t5.err_exp:
tests/invalid_purity/impure_pred_t1.err_exp:
tests/invalid_purity/impure_pred_t1_fixed.err_exp:
tests/invalid_purity/impure_pred_t2.err_exp:
tests/invalid_purity/purity_nonsense.err_exp:
tests/invalid_purity/purity_nonsense2.err_exp:
tests/invalid_purity/purity_type_error.err_exp:
    Expect the new-colored output.
2024-05-05 09:49:35 +10:00
Zoltan Somogyi
66b309dcd4 Handle color changes next to prefixes/suffixes.
compiler/write_error_spec.m:
    Redesign the code we use to decide what text goes onto each line.
    The new design rests on these two new principles:

    - Representing the current word separately from the words before it
      on the line. This allows us to merge together an arbitrary number
      of prefixes, a plain word, and an arbitrary number of suffixes
      without the two additional passes that the old algorithm used
      to accomplish the same goal.

    - Representing the spaces between words *explicitly*. Together with
      the previous principle, this allows us to do case analyses of the
      possible configurations of text, spaces and color changes with
      significantly simpler code than was possible with the previous design.
      The new code is longer, but

      - it is easier to see both that it does the right thing in each
        of these situations, including interactions between prefixes and
        suffixes on the one hand and color changes on the other, and

      - it is easier to check that all possible combinations of the
        relevant circumstances have code to handle them.

compiler/typecheck_error_undef.m:
    Use the new capability by using color in arity mismatch reports,
    which have the form of "(4, should be 2, 3 or 5)", where the numbers,
    which are surrounded by two parentheses which are respectively
    a prefix and a suffix, are colored.

compiler/error_spec.m:
    Exclude from the list of pieces to be colored not just any final
    subsequence of newline pieces, but any initial subsequence of newlines
    as well. This avoids the output having lines which end in two consecutive
    color changes with nothing between them: the first of which switches
    to a color, and the second of which resets the current color to neutral.

library/cord.m:
library/list.m:
library/one_or_more.m:
library/ra_list.m:
    Add a predicate named is_non_empty, which does what its name says,
    to each of these modules. In cord.m, this is new functionality,
    which is now used by write_error_spec.m. In the other modules,
    this new predicate is a synonym for the existing predicate
    is_not_empty. These modules were in a minority; all other modules
    that had a predicate to test for nonemptyness named it is_noN_empty,
    not is_noT_empty.

NEWS.md:
    Announce the new is_non_empty predicates.

tests/invalid/constructor_warning.err_exp:
tests/invalid/getopt_io_old.err_exp:
tests/invalid/invalid_event.err_exp:
tests/invalid/no_method.err_exp:
tests/invalid/types2.err_exp:
tests/invalid/wrong_arity_function.err_exp:
    Expect the new colored output from typecheck_error_undef.m,
    without the extra spaces around parentheses that the version
    of write_error_spec.m before this diff would have put around them.

tests/invalid/bug117.err_exp:
tests/invalid/bug191.err_exp:
tests/invalid/bug415.err_exp:
tests/invalid/char_inst.err_exp:
tests/invalid/coerce_int.err_exp:
tests/invalid/coerce_mode_error.err_exp:
tests/invalid/coerce_recursive_type.err_exp:
tests/invalid/constrained_poly_insts2.err_exp:
tests/invalid/default_ho_inst.err_exp:
tests/invalid/det_errors.err_exp:
tests/invalid/det_errors_and_io.err_exp:
tests/invalid/functor_ho_inst_bad.err_exp:
tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/html.err_exp:
tests/invalid/merge_ground_any.err_exp:
tests/invalid/mode_error_arg_number.err_exp:
tests/invalid/partial_implied_mode.err_exp:
tests/invalid/polymorphic_unification.err_exp:
tests/invalid/require_scopes.err_exp:
tests/invalid/type_prop_into_inst.err_exp:
    Expect the absence of switches to a color followed IMMEDIATELY
    by a color reset, thanks to the update of error_spec.m.
2024-05-04 00:27:34 +10:00
Zoltan Somogyi
af90a1702c Use color in mode error diagnostics.m.
compiler/mode_errors.m:
    Add color to diagnostics about mode errors where appropriate.

compiler/error_spec.m:
    Fix an issue raised by the changes to mode_errors.m, which was that
    having a colored span end in two or more newline pieces led to
    an unnecessary seemingly blank line at the end. (The line actually
    contained escape sequences to first set a color, and to then reset colors,
    but none of that was visible.) The fix is to always add color resets
    *before* any final sequence of newline pieces in the span being colored.

compiler/error_msg_inst.m:
    Add a note to a comment.

tests/invalid/any_mode.err_exp:
tests/invalid/any_should_not_match_bound.err_exp:
tests/invalid/any_to_ground_in_ite_cond.err_exp:
tests/invalid/any_to_ground_in_ite_cond_nomax.err_exp:
tests/invalid/anys_in_negated_contexts.err_exp:
tests/invalid/bad_sv_unify_msg.err_exp:
tests/invalid/bind_in_negated.err_exp:
tests/invalid/bind_var_errors.err_exp:
tests/invalid/bug117.err_exp:
tests/invalid/bug191.err_exp:
tests/invalid/bug278.err_exp:
tests/invalid/bug415.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/char_inst.err_exp:
tests/invalid/coerce_clobbered.err_exp:
tests/invalid/coerce_disambig.err_exp:
tests/invalid/coerce_implied_mode.err_exp:
tests/invalid/coerce_instvar.err_exp:
tests/invalid/coerce_int.err_exp:
tests/invalid/coerce_mode_error.err_exp:
tests/invalid/coerce_mode_error2.err_exp:
tests/invalid/coerce_recursive_inst.err_exp:
tests/invalid/coerce_recursive_type.err_exp:
tests/invalid/coerce_uniq.err_exp:
tests/invalid/constrained_poly_insts2.err_exp:
tests/invalid/currying_multimode_func.err_exp:
tests/invalid/dcg_context.err_exp:
tests/invalid/default_ho_inst.err_exp:
tests/invalid/functor_ho_inst_bad.err_exp:
tests/invalid/functor_ho_inst_bad_2.err_exp:
tests/invalid/functor_ho_inst_bad_3.err_exp:
tests/invalid/ho_any_inst.err_exp:
tests/invalid/ho_default_func_2.err_exp:
tests/invalid/ho_type_mode_bug.err_exp:
tests/invalid/ho_unique_error.err_exp:
tests/invalid/html.err_exp:
tests/invalid/inst_matches_final_bug.err_exp:
tests/invalid/invalid_integral_call_inst.err_exp:
tests/invalid/io_in_ite_cond.err_exp:
tests/invalid/merge_ground_any.err_exp:
tests/invalid/merge_inst_error.err_exp:
tests/invalid/mode_error_arg_number.err_exp:
tests/invalid/modes_erroneous.err_exp:
tests/invalid/mostly_uniq1.err_exp:
tests/invalid/mostly_uniq2.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/no_ho_inst.err_exp:
tests/invalid/partial_implied_mode.err_exp:
tests/invalid/polymorphic_unification.err_exp:
tests/invalid/qualified_cons_id2.err_exp:
tests/invalid/state_vars_test5.err_exp:
tests/invalid/unify_mode_error.err_exp:
tests/invalid/uniq_modes.err_exp:
tests/invalid/uniq_neg.err_exp:
tests/invalid_nodepend/constrained_poly_insts.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/invalid_purity/impure_func_t5_fixed.err_exp:
tests/invalid_purity/purity.err_exp:
tests/warnings/simple_code.exp:
tests/warnings/unify_f_g.exp:
    Expect colors in these diagnostics.
2024-04-30 19:27:32 +10:00
Zoltan Somogyi
ce085c6b7e Use color in determinism diagnostics.
compiler/det_report.m:
    Use color in determinism diagnostics.

compiler/error_spec.m:
    Shorten the names of some functions, to allow calls to them
    to fit on one line more frequently.

compiler/typecheck_error_undef.m:
compiler/typecheck_errors.m:
compiler/typecheck_msgs.m:
    Conform to the change in error_spec.m.

tests/invalid/bug150.err_exp:
tests/invalid/bug496.err_exp:
tests/invalid/det_atomic_goal_msgs.err_exp:
tests/invalid/det_errors.err_exp:
tests/invalid/det_errors_and_io.err_exp:
tests/invalid/hawkins_mm_fail_reset.err_exp:
tests/invalid/ho_unique_error.err_exp:
tests/invalid/io_in_ite_cond.err_exp:
tests/invalid/loopcheck.err_exp:
tests/invalid/magicbox.err_exp:
tests/invalid/mostly_uniq1.err_exp:
tests/invalid/mostly_uniq2.err_exp:
tests/invalid/not_a_switch.err_exp:
tests/invalid/one_member.err_exp:
tests/invalid/pragma_c_code_no_det.err_exp:
tests/invalid/qualified_cons_id2.err_exp:
tests/invalid/ref_to_implicit_comma.err_exp:
tests/invalid/ref_to_implicit_pred.err_exp:
tests/invalid/require_det_in_lambda.err_exp:
tests/invalid/require_scopes.err_exp:
tests/invalid/require_switch_arms_detism.err_exp:
tests/invalid/switch_arm_multi_not_det.err_exp:
tests/invalid/try_detism.err_exp:
tests/invalid/user_field_access_decl_override.err_exp:
tests/invalid_nodepend/invalid_main.err_exp:
tests/invalid_nodepend/occurs.err_exp:
tests/warnings/det_infer_warning.exp:
    Expect updated diagnostic output.
2024-04-29 17:51:54 +10:00
Zoltan Somogyi
9ab507e5ae Add color to the output of typecheck_error_undef.m.
compiler/typecheck_error_undef.m:
    Use color as appropriate in diagnostics.

    Mark one place where color would be useful, but which write_error_spec.m
    cannot handle without formatting errors.

    Use more specific wording for an error message.

compiler/typecheck_msgs.m:
compiler/error_spec.m:
    Add colors in these subconstractors of typecheck_error_undef.m.

compiler/write_error_spec.m:
    Fix a bug: make the piece that lower-cases the first character
    of the next word have effect through color changes.

    Move two predicates to their relatives.

    Replace a use of reversed lists with cords.

tests/invalid_submodules/Mmakefile:
    Enable the use of color for diagnostics in this test directory.

tests/invalid/bad_pred_arity.err_exp:
tests/invalid/bug113.err_exp:
tests/invalid/getopt_io_old.err_exp:
tests/invalid/multimode_syntax.err_exp:
tests/invalid/multiply_star.err_exp:
tests/invalid/qual_basic_test2.err_exp:
tests/invalid/transitive_import_class.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid/type_error_use_module.err_exp:
tests/invalid/types2.err_exp:
tests/invalid/undef_symbol.err_exp:
tests/invalid_nodepend/errors2.err_exp:
tests/invalid_nodepend/funcs_as_preds.err_exp:
tests/invalid_nodepend/kind.err_exp:
tests/invalid_nodepend/reserved_type_name.err_exp:
tests/invalid_submodules/undef_mod_qual.err_exp:
    Expect color, and in one case, revised wording.
2024-04-28 23:06:22 +10:00
Zoltan Somogyi
39b5e3439a Add color to the output of typecheck_errors.m.
compiler/typecheck_errors.m:
    Add colors to the parts of diagnostics that contrast incorrect type
    with the expected correct types, and the parts that document possible
    causes.

compiler/error_spec.m:
    Add some utility functions needed by the above.

compiler/write_error_spec.m:
    Fix several issues revealed by the effect of the above changes
    on the outputs we generate for invalid test cases.

    Issue one. We had a sanity check that, after joining two lines,
    checked whether the sum of the recorded line lengths matched
    the number of code points in the joined string. Delete this check,
    because it does the wrong thing in the presence of color changes.
    The reason is that the recorded lengths consider the escape sequences
    implementing them to be zero length (since they occupy zero columns),
    but string.count_code_points counts them anyway.

    Issue two. We used to handle colors in each paragraph separately,
    effectively starting each paragraph with an empty color stack.
    This caused problems with messages in which a color change is supposed
    to affect several lines *that have explicit newlines between them*,
    since the code puts paragraph breaks after each explicit newline.
    Fix this by setting the initial color stack of each nonfirst
    paragraph to the final color stack of the previous paragraph.

    Issue three. It is possible for an unbreakable string (such as the name
    of a predicate) that is too long to fit in the space available on a line.
    We have to include such strings in a line despite the violation of the
    length limit. Previously, we had to do only when the string was the first
    thing on the line. Now, it is possible for such a string to be preceded
    by one or more color changes, which occupy zero columns. Include
    too-long-to-fit unbreakable strings on the line in such cases as well.

    Issue four. Some callers of the color_pieces functions pass them
    piece lists that end in newlines. In such cases, we don't want
    the color reset to take place at the start of the next line after the
    colored pieces, because that makes the output harder to check visually
    for correctness in .err_exp files. In such cases, swap the order
    of the color_end and newline pieces. (Since newlines cannot be colored,
    this has no semantic effect.)

    Issue five. If a line either started out colored or contained pieces
    that started a new color, we marked it to be followed by a color reset
    when it is printed. However, if the color was already reset by pieces
    that are also contained in the line, so that the color stack was empty
    at the end of the line, then this final additional reset was redundant.
    Arrange not to print such redundant resets, also in the interest of
    making .err_exp files easier to check.

    Improve the debugging infrastructure a bit.

    Don't duplicate blank lines zero times.

tests/invalid/Mmakefile:
tests/invalid_nodepend/Mmakefile:
tests/invalid_purity/Mmakefile:
tests/warnings/Mmakefile:
    Enable color diagnostics in these test directories.

tests/invalid/abstract_eqv.err_exp:
tests/invalid/actual_expected.err_exp:
tests/invalid/actual_more_expected.err_exp:
tests/invalid/arith_wrong_module.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/error_in_list.err_exp:
tests/invalid/ext_type.err_exp:
tests/invalid/ext_type_bug.err_exp:
tests/invalid/fbnf.err_exp:
tests/invalid/foreign_procs_exist_type.err_exp:
tests/invalid/getopt_old.err_exp:
tests/invalid/higher_order_mode_mismatch.err_exp:
tests/invalid/ho_type_arity_bug.err_exp:
tests/invalid/integral_constant_no_suffix.err_exp:
tests/invalid/method_impl.err_exp:
tests/invalid/mixed_up_streams.err_exp:
tests/invalid/mpj1.err_exp:
tests/invalid/mpj4.err_exp:
tests/invalid/no_method.err_exp:
tests/invalid/nullary_ho_func_error.err_exp:
tests/invalid/overloading.err_exp:
tests/invalid/record_syntax_errors.err_exp:
tests/invalid/try_bad_params.err_exp:
tests/invalid/type_diff.err_exp:
tests/invalid/type_error_ambiguous.err_exp:
tests/invalid/type_error_in_arg.err_exp:
tests/invalid/type_mismatch.err_exp:
tests/invalid/types2.err_exp:
tests/invalid/user_field_access_decl_override2.err_exp:
tests/invalid_nodepend/errors2.err_exp:
tests/invalid_purity/impure_func_t5.err_exp:
tests/invalid_purity/impure_pred_t1.err_exp:
tests/invalid_purity/impure_pred_t1_fixed.err_exp:
tests/invalid_purity/impure_pred_t2.err_exp:
tests/invalid_purity/purity_nonsense.err_exp:
tests/invalid_purity/purity_nonsense2.err_exp:
tests/invalid_purity/purity_type_error.err_exp:
tests/warnings/inconsistent_pred_order.exp:
tests/warnings/subtype_order.exp:
    Expect color in these expected output files.
2024-04-28 17:12:36 +10:00
Zoltan Somogyi
cbd54592ce Set up options to control colors in diagnostics.
compiler/options.m:
    Define options to control the use of color by diagnostics.
    The default disallows the use of colors, and the options intended
    for use by programmers are not yet documented.

compiler/error_spec.m:
    Add a third use for color, intended to be used for the descriptions of
    possible causes of the error being reported.

compiler/globals.m:
    Add a function for checking whether the options intended to select colors
    have meaningful values.

compiler/handle_options.m:
    Call that function to check the values of those options, and use
    the values of the options intended to control the use of color,
    and the values of the relevant environment variable, to decide
    whether to actually use color.

compiler/write_error_spec.m:
    Base the decision about whether to use color, and if so what colors to use,
    on the values of the new options.
2024-04-27 01:08:07 +10:00
Zoltan Somogyi
132168d6ee Switch spelling from colour to color. 2024-04-26 03:13:12 +10:00
Zoltan Somogyi
c5c50008cd Add prelim support for colour in error messages.
compiler/error_spec.m:
    Add and export two functions, which promise to make a given piece list
    will be printed in colour (if colour capability is enabled).
    Support two colours for now: one each indicating correct and incorrect
    code. The intention is to draw programmers eyes to these parts of
    error messages.

    The new capability extends the format_piece type, but in a way that
    asks users to leave its use to code inside error_spec.m itself
    (at least for now).

    As a trial, change the implementation of change_hunk_to_pieces,
    which transforms the output of diff algorithms into parts of
    error_specs. We use this capability to point out inconsistencies
    between the order in which preds/funcs are declared and the order
    in which they are defined.

compiler/write_error_spec.m:
    Add code to turn the semantic colour names defined in error_spec.m
    into actual colors we can print using the escape sequences supported
    by many (most?) kinds of terminals, if this capability is enabled.

    Extend each stage of the multi-stage machinery we use to convert
    format_piece sequences into error lines to handle colour changes.

    This required changes in how we handle string lengths and spaces.
    Previously, we could compute the length of a string on the screen
    by calling string.count_code_points, but this does not work when
    some of those code points represent terminal control sequences
    (that set a new current colour) that take no space on screen.
    Also, we could just put a space between each pair of strings on a line
    when those string were each one word, but that would lead to incorrect
    output when some of those strings are terminal control sequences.
    This required adding support for setting up an initial colour
    on each line, and resetting the colour at the end of each line.

    Change the representation of unlimited length lines. Instead of the
    representation being "no", i.e. the absence of a limit, it is now
    int.max_int, a limit that is impossible to reach in practice.
    The reason for this is that we used to compute the length of a line
    using string.count_code_points if MaybeLimit was "no", and as mentioned
    above, this does not work anymore.

    This new machinery has been tested manually, and it works.
    It is disabled for now,

    - until we figure out how we want to control it,
    - until we figure out how we want to test it, and
    - until we start using it in more commonly seen error messages.
2024-04-24 22:54:57 +10:00
Zoltan Somogyi
dc12878708 Shorten function symbols in error_specs.
compiler/error_spec.m:
    Replace simplest_spec with spec, and simplest_no_context_spec with
    no_ctxt_spec. These are now the most frequently used function symbols
    to create error specs, so their name should not make them out to be
    the exception.

    Replace simplest_msg with msg, and simplest_no_context_msg with
    no_ctxt_msg for the same reason.

    Abbreviate some of the long phase names.

compiler/*.m:
    Conform to the changes above. Most of these changes were done by a script,
    with minor manual tidying up, which consisted mostly of fitting code
    constructing error specs onto fewer lines than before.
2024-04-20 21:51:15 +10:00
Julien Fischer
d708dd28f1 "Did you mean ..." messages for long options.
Generate "Did you mean ..." messages for unrecognized long options.

compiler/mercury_compile_main.m:
    If we have an unrecognized long option or negated long option,
    then attempt to generate a "Did you mean ..." message for it.

compiler/error_spec.m:
    Add a version of maybe_construct_did_you_mean_pieces that lets
    you add a prefix to the strings before including them in the pieces.
    This is required for handling the "--" and "--no-" long option
    prefixes. Both versions of maybe_construct_did_you_mean pieces
    are implemented using the same underlying piece of code.

compiler/options.m:
     Add predicates for returning the set of all long options and all negatable
     long options. To make this possible, shift the long option table into a
     new predicate and add an (out, out) is multi mode for it.

library/getopt.m:
    Update the documentation to include maybe_string_special options
    among the option types that are negatable.

library/string.m:
    Add the function add_prefix/2.

NEWS:
    Announce the addition to the string module.
2024-04-20 03:33:19 +10:00
Julien Fischer
f5e71b1e90 Fix copyright notices in recently modified files.
compiler/*.m:
library/*.m:
mdbcomp/*.m:
runtime/*.[ch]:
    As above.

    Fix spelling in some spots.
2024-02-20 15:09:17 +11:00
Zoltan Somogyi
4e1660e754 Don't look for "dym" matches in the empty set.
compiler/error_spec.m:
    When trying to construct a "did you mean" addendum to an error message
    by trying to find the nearest matches to a not-found name, check that
    the set of other names is not actually empty. Looking in the empty set
    leads to an exception, and thus a compiler abort.

compiler/options.m:
    Provide a way to allow the presence of this fix to be detected.
2024-02-08 09:44:00 +11:00
Zoltan Somogyi
4b3b6a9ed8 Add find_best_close_enough_seqs.
library/edit_distance.m:
    Add the above predicate, and its specialized version
    find_best_close_enough_strings, which allow their callers to specify
    a edit distance beyond which the results would not be useful.

compiler/error_spec.m:
    Use the new functionality to restrict the set of "close enough"
    candidates to those that replace at most half the characters in the name
    for which we are generating a "did you mean" message.
2024-01-26 15:34:16 +11:00
Zoltan Somogyi
191eb9eb64 Make --generate-dependencies-ints more practical.
scripts/Mmake.rules:
    Add a rule for the pattern target %.depend_ints which invokes mmc
    with --generate-dependencies-ints, just as %.depend invokes mmc
    with --generate-dependencies.

    Document the reason why this addition requires yet more code duplication.

scripts/Mmake.vars.in:
    Add the make variable definitions needed by the new code in Mmake.rules.

compiler/mercury_compile_main.m:
    Make it practical to use --generate-dependencies-ints in a multi-directory
    project like the Mercury implementation, in which some directories
    may use --generate-dependencies-ints as the target of "mmake depend",
    while other directories, which come earlier in the build process,
    still do "mmake depend" using just --generate-dependencies.
    In such cases, mmc --generate-dependencies-ints in the later directory
    may fail to generate .int0, .int or .int2 files simply because
    the .int3 file of a module they import (from an earlier directory
    in which "mmake depend" uses just --generate-dependencies) hasn't been
    made yet. (This is what would happen if someone executed "mmake depend"
    at the top level in a freshly checked out workspace.)

    The practical fix is to simply report this fix using an error message
    that still allows the compiler to exit with an exit status that
    indicates success.

    Most of the rest of this diff is there to make this possible.

compiler/error_spec.m:
    Add a new phase, phase_find_files, that specifically indicates
    that we couldn't read a file because we couldn't find it.
    Give it a string argument naming the file, to allow mercury_compile_main.m
    to replace several error_specs that each report not being able to find
    one file with a single error_spec that reports not being able to find
    many files.

    Add a utility function for use by mercury_compile_main.m to construct
    that error message.

    Add another new phase, phase_make_int, that indicates a problem
    we discovered in the code of a Mercury source file while trying
    to decide what should got into one of the that module's interface files.

compiler/parse_error.m:
    Here also use separate function symbols in the fatal_module_read_error
    type for the situations "couldn't find a file" and "couldn't read a file".

    Update the predicates constructing error_specs to handle this distinction
    (which the callers make using the new function symbol in the above type).

compiler/compile_target_code.m:
compiler/error_util.m:
compiler/grab_modules.m:
compiler/options_file.m:
compiler/parse_module.m:
compiler/read_modules.m:
compiler/recompilation.check.m:
compiler/write_module_interface_files.m:
    Conform to the changes above.

compiler/options.m:
    Add a way to detect the presence of this diff in an installed compiler.
2023-10-15 13:10:40 +11:00
Zoltan Somogyi
25f8a6cc99 Add new option --generate-dependencies-ints.
This new option extends --generate-dependencies to take advantage of the
opportunity afforded by the fact that "mmc --generate-dependencies prog.m"
reads in every Mercury source file in the current directory that is
part of "prog". It does this by

- generating the .int3 file of all local-directory modules of the program;
- generating the .int0 file for each these modules that has submodules;
- generating the .int and .int2 files of all local-directory modules
  of the program.

Normally, the process of creating .int0, .int and .int3 files would
need to read in .int0 and .int3 files, but in every one of these cases,
we have just written out those files, so simply holding onto their
parse trees, we can skip this step. On my laptop, on a directory
containing library/*.m, mdbcomp/*.m and compiler/*.m, generating
the dependencies and generating all the .int3/.int0/.int/.int2 files
takes just over 25 seconds. Using the new option, the same process
takes less than 10 seconds.

compiler/options.m:
    Add the new option.

compiler/op_mode.m:
    Add a new variant of the existing op_mode for --generate-dependencies,
    which we select in the presence of the new option.

compiler/mercury_compile_main.m:
    Implement the new op_mode.

    Fix an old issue, which is that "mmc --make-private-interface x.m"
    generated a .int0 file for *every* module in x.m, even the modules
    that don't have any submodules.

compiler/deps_map.m:
    The code implementing the new option needs to know which modules
    of the program are in the current directory. The deps_map structure
    built by the code shared with the implementation of --generate-dependencies
    has not needed that info until now, so add a new field to the deps
    structure of each module to provide this info.

compiler/generate_dep_d_files.m:
    Return the deps_map created by the code that implements both
    --generate-dependencies and the new option to mercury_compile_main.m.

compiler/write_module_interface_files.m:
    Add a flag to the predicates that first construct the parse trees of,
    and then write out, .int3/.int0/.int/.int2 files, that
    mercury_compile_main.m can use to tell them to record the parse trees
    in the have read module maps.

    Add new variants of two of those predicates that take some of their
    arguments from burdened_module structures, since that is the form
    in which mercury_compile_main.m has that info.

compiler/module_baggage.m:
    The predicates in write_module_interface_files.m that generate
    .int0/.int/.int2 files take an argument that should be the timestamp
    of the source file they are being derived from, if that timestamp
    is needed for smart recompilation. Until now, we only ever invoked
    those predicates when we have just read in the source file,
    and this timestamp was readily available. The code implementing
    the new option needs to store this info for a short time, and
    the module baggage is the obvious place to store it, so add this field
    to the baggage.

compiler/error_spec.m:
    An invocation of the compiler with new option may report errors that
    prevent the construction of interface files for several modules.
    The new code in mercury_compile.m prints the error_specs that have
    contexts in the order of those contexts, but we want to print
    the messages without contexts (which in this case report that
    certain files could not be written or copied) to have a useful
    order too. Make this possible by allowing the invisible pieces
    we use for ordering to specify that order via a string (in this case,
    the name of the file that e.g. could not be written), rather than
    the only previous option, an integer.

compiler/grab_modules.m:
compiler/make.get_module_dep_info.m:
compiler/make.module_dep_file.m:
    Fill in the new field in the module baggage.

compiler/check_module_interface.m:
compiler/handle_options.m:
compiler/make_hlds_error.m:
compiler/parse_module.m:
compiler/prog_foreign_enum.m:
compiler/typecheck_error_util.m:
compiler/typecheck_msgs.m:
compiler/write_deps_file.m:
compiler/write_error_spec.m:
    Conform to the changes above.
2023-10-12 06:50:07 +11:00
Zoltan Somogyi
f508f24b79 Extend "did you mean" suggestions beyond preds.
compiler/typecheck_error_undef.m:
    Suggest possible "did you mean" names when reporting references
    to undefined functions, not just undefined predicates.

    Generalize the infrastructure used for predicate name suggestions
    to work for function name suggestions as well.

    Delete the code moved to error_spec.m.

compiler/module_qual.qual_errors.m:
    When reporting an undefined entity name that has way to become defined
    without changing its name, suggest possible names.

compiler/error_spec.m:
    Move the code for constructing "did you mean" suggestions here from
    typecheck_error_undef.m, to make it accessible from
    module_qual.qual_errors.m as well.

compiler/hlds_cons.m:
    Add a way to retrieve the names of all the function symbols,
    for use by the new code in typecheck_error_undef.m.

compiler/module_qual.id_set.m:
    Add a way to retrieve the names of all the ids in an id_set,
    for use by the new code in module_qual.qual_errors.m.

    Expand a old comment.

tests/invalid/bug113.err_exp:
tests/invalid/multiply_star.err_exp:
tests/invalid/multiply_star.m:
tests/invalid_nodepend/kind.err_exp:
tests/invalid_nodepend/reserved_type_name.err_exp:
    Expect the "did you mean" messages we now generate.
2023-09-26 18:51:18 +10:00
Zoltan Somogyi
bb5b24173c Add an explicit stream to the print_anything method.
compiler/error_spec.m:
    As above.

compiler/mode_errors.m:
    Get the stream from this extra arg.

compiler/write_error_spec.m:
    Supply the stream argument.
2023-09-13 10:19:52 +10:00