149 Commits

Author SHA1 Message Date
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
ee9c7d3a84 Speed up bound vs ground inst checks.
The code that checks whether a bound inst wrapped around
a list of bound_functors matched the ground inst did several things
in a suboptimal fashion.

- It looked up the definition of the type constructor of the relevant type
  (the type of the variable the inst is for) more than once. (This was
  not easily visible because the lookups were in different predicates.)
  This diff factors these out, not for the immesurably small speedup,
  but to make possible the fixes for the next two issues.

- To simplify the "is there a bound_functor for each constructor in the type"
  check, it sorted the constructors of the type by name and arity. (Lists of
  bound_functors are always sorted by name and arity.) Given that most
  modules contain more than one bound inst for any given type constructor,
  any sorting after the first was unnecessarily repeated work. This diff
  therefore extends the representation of du types, which until now has
  include only a list of the data constructors in the type definition
  in definition order, with a list of those exact same data constructors
  in name/arity order.

- Even if a list of bound_functors lists all the constructors of a type,
  the bound inst containing them is not equivalent to ground if the inst
  of some argument of some bound_inst is not equivalent to ground.
  This means that we need to know the actual argument of each constructor.
  The du type definition lists argument types that refer to the type
  constructor's type parameters; we need the instances of these argument types
  that apply to type of the variable at hand, which usually binds concrete
  types to those type parameters.

  We used to apply the type-parameter-to-actual-type substitution to
  each argument of each data constructor in the type before we compared
  the resulting filled-in data constructor descriptions against the list of
  bound_functors. However, in cases where the comparison fails, the
  substitution applications to arguments beyond the point of failure
  are all wasted work. This diff therefore applies the substitution
  only when its result is about to be needed.

This diff leads to a speedup of about 3.5% on tools/speedtest,
and about 38% (yes, more than a third) when compiling options.m.

compiler/hlds_data.m:
    Add the new field to the representation of du types.

    Add a utility predicate that helps construct that field, since it is
    now needed by two modules (add_type.m and equiv_type_hlds.m).

    Delete two functions that were used only by det_check_switch.m,
    which this diff moves to that module (in modified form).

compiler/inst_match.m:
    Implement the first and third changes listed above, and take advantage
    of the second.

    The old call to all_du_ctor_arg_types, which this diff replaces,
    effectively lied about the list of constructors it returned,
    by simply not returning any constructors containing existentially
    quantified  types, on the grounds that they "were not handled yet".
    We now fail explicitly when we find any such constructors.

    Perform the check for one-to-one match between bound_functors and
    constructors with less argument passing.

compiler/det_check_switch.m:
    Move the code deleted from hlds_data.m here, and simplify it,
    taking advantage of the new field in du types.

compiler/Mercury.options:
    Specify --optimize-constructor-last-call for det_check_switch.m
    to optimize the updated moved code.

compiler/add_foreign_enum.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/code_info.m:
compiler/dead_proc_elim.m:
compiler/direct_arg_in_out.m:
compiler/du_type_layout.m:
compiler/equiv_type_hlds.m:
compiler/hlds_out_type_table.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/intermod_decide.m:
compiler/lookup_switch_util.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen_test.m:
compiler/ml_unify_gen_util.m:
compiler/mlds.m:
compiler/post_term_analysis.m:
compiler/recompilation.usage.m:
compiler/resolve_unify_functor.m:
compiler/simplify_goal_ite.m:
compiler/table_gen.m:
compiler/tag_switch_util.m:
compiler/term_norm.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck_coerce.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above. This mostly means handling
    the new field in du types (usually by ignoring it).
2025-11-19 22:09:04 +11:00
Zoltan Somogyi
c5b0481b97 Simplify the construction of cons_infos.
typecheck_unify_var_functor.m used to have separate code paths
- to handle the functors of builtin types, and
- to handle the functors of all other types.

However, the second path invoked code in typecheck_cons_infos.m that
*also* handled builtin types.

compiler/typecheck_unify_var_functor.m:
    Simplify this arrangement by deleting the code that chose between
    the two paths, and handling functors of builtin types together with
    all the other functors in (what used to be) the second path.

compiler/typecheck_cons_infos.m:
    Return a distinct result for cons_ids of all builtin types
    (including some that typecheck_unify_var_functor.m did not handle
    via the first code path.)

    Document the meanings of the possible results.

compiler/hlds_cons.m:
    Change the ctor_field_table type to encode the invariant
    that we don't map sym_names to the empty list of field definitions.

compiler/hlds_pred.m:
    The predicates that test whether a sym_name/arity pair or pred_info
    refer to a field access function must search the ctor_field_table
    to answer the question. Make them return the info they get from that
    search, to save their callers from having to do it again.

    Give a predicate a more meaningful name.

compiler/typecheck_info.m:
    Record extra info if we are typechecking in a field access function,
    since it may be needed, and storing it costs next to nothing.

compiler/add_clause.m:
compiler/add_type.m:
compiler/check_field_access_functions.m:
compiler/field_access.m:
compiler/intermod_decide.m:
compiler/pre_typecheck.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
compiler/resolve_unify_functor.m:
compiler/typecheck_error_undef.m:
    Conform to the changes above.
2025-10-23 23:18:11 +11:00
Zoltan Somogyi
44d1f0db9c Give some predicates shorter names.
compiler/prog_type_subst.m:
compiler/type_util.m:
    Apply s/apply_variable_renaming_to_/apply_renaming_to_/ and
    s/_to_x_list/_to_xs/ to the names of predicate.

    Conform to the change in hlds_class.m below.

compiler/hlds_class.m:
    This module used to define types named (a) hlds_constraint, and
    (b) hlds_constraints, and the latter was NOT a list of items
    of type hlds_constraint. Rename the latter to hlds_constraint_db
    to free up the name apply_renaming_to_constraints to apply
    to list(hlds_constraint). However, the rename also makes code
    operating on hlds_constraint_dbs easier to understand. Before
    this diff, several modules used variables named Constraints
    to refer to a list(hlds_constraint) in some places and to
    what is now a hlds_constraint_db in other places, which is confusing;
    the latter are now named ConstraintDb.

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

    Add an XXX about some existing variable names that *look* right
    but turn out to be subtly misleading.

compiler/add_pragma_type_spec.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/comp_unit_interface.m:
compiler/cse_detection.m:
compiler/ctgc.util.m:
compiler/decide_type_repn.m:
compiler/deforest.m:
compiler/equiv_type.m:
compiler/equiv_type_hlds.m:
compiler/higher_order.higher_order_global_info.m:
compiler/higher_order.make_specialized_preds.m:
compiler/higher_order.specialize_calls.m:
compiler/hlds_rtti.m:
compiler/inlining.m:
compiler/modecheck_coerce.m:
compiler/old_type_constraints.m:
compiler/polymorphism_clause.m:
compiler/polymorphism_goal.m:
compiler/polymorphism_type_class_info.m:
compiler/prog_type_unify.m:
compiler/qual_info.m:
compiler/recompilation.version.m:
compiler/resolve_unify_functor.m:
compiler/typecheck.m:
compiler/typecheck_clauses.m:
compiler/typecheck_cons_infos.m:
compiler/typecheck_debug.m:
compiler/typecheck_error_type_assign.m:
compiler/typecheck_errors.m:
compiler/typecheck_unify_var_functor.m:
compiler/typecheck_util.m:
compiler/typeclasses.m:
compiler/unify_proc.m:
compiler/var_table.m:
compiler/vartypes.m:
    Conform to the changes above.
2025-10-21 18:21:35 +11:00
Zoltan Somogyi
f4acef12d0 Fix too-long lines. 2025-09-23 15:17:49 +10:00
Zoltan Somogyi
ca20c38987 Split --warn-redundant-code from --warn-simple-code.
compiler/options.m:
    Move --warn-simple-code to the "warnings about dodgy code"
    category, as

    - most of the issues that it warns about are indeed evidence
      of dodgy code, and
    - its existing documentation also states that it is intended
      to report likely-to-be-incorrect code.

    Change the internal name of --warn-simple-code, in order to force
    all its existing uses to be reclassified.

    Let the --warn-redundant-code part stay in the style issues category,
    since the code will work just the same if the redundancies are removed.

NEWS.md:
    Announce the new option.

compiler/simplify_goal_call.m:
    Reclassify all uses of --warn-simple-code.

    Add a missing condition in an if-then-else.

compiler/add_clause.m:
compiler/add_type.m:
compiler/check_type_inst_mode_defns.m:
compiler/convert_import_use.m:
compiler/convert_parse_tree.m:
compiler/det_analysis.m:
compiler/det_infer_goal.m:
compiler/mercury_compile_front_end.m:
compiler/mode_errors.m:
compiler/simplify_goal.m:
compiler/simplify_goal_disj.m:
compiler/simplify_goal_ite.m:
compiler/simplify_info.m:
compiler/simplify_tasks.m:
compiler/state_var.m:
    Reclassify all uses of --warn-simple-code.

tests/warnings/help_text.err_exp:
    Expect the updated help text.
2025-08-18 14:53:08 +02: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
a2ede48ac2 Eliminate unneeded redundancy in inner_cons_entries.
compiler/hlds_cons.m:
    An inner_cons_entry used to list 2xN versions of a du_ctor, with

    - the factor of two representing the fact that we for every
      du_ctor version containing the actual type_ctor the du_ctor is for
      there as another version containing cons_id_dummy_type_ctor, and

    - the N representing the number of fully qualified, partially qualified
      or unqualified version of the du_ctor's sym_name.

    Each entry contained the du_ctor's arity 2xN times, and its type_ctor
    N times.

    Eliminate these redundancies by storing just one fully qualified
    du_ctor, and N-1 partially-qualified or unqualified sym_names.

    Change the code of the access predicates to support the same semantics
    as before.

    It would be nice to eliminate the redundancy inherent in storing
    several sym_names that are all just less-qualified versions of
    one specific sym_name, but I won't see how to do that that wouldn't
    be more complex than it's worth.

    Change predicates' argument order to put more stable inputs
    before less stable inputs.

compiler/add_type.m:
    When adding data constructors to the cons_table, supply sym_names
    instead of du_ctors.

compiler/hlds_out_module.m:
2025-07-23 00:49:30 +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
458b2a5fef Improve more warnings.
compiler/add_type.m:
compiler/purity.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_disj.m:
compiler/typecheck_error_undef.m:
compiler/unused_args.m:
    Improve the wording of some warnings, and add color to some others.

tests/invalid_purity/impure_func_t3.err_exp:
tests/warnings/bug311.err_exp:
tests/warnings/purity_warnings.err_exp:
tests/warnings/simple_code.err_exp:
tests/warnings/subtype_order.err_exp:
tests/warnings/unused_args_test.err_exp:
tests/warnings/warn_stubs.err_exp:
    Expect the updated warnings.
2024-12-15 10:55:55 +11:00
Peter Wang
a079c91e89 Require subtypes to repeat existential type vars and constraints exactly.
We previously checked for a one-to-one mapping from the existentially
quantified type variables for a subtype constructor to those of the
supertype. However, the _order_ that existentially quantified type
variables and existential class constraints are listed in a du
constructor definition is reflected in the order of type_info and
type_class_info arguments in a heap cell, e.g.

   :- type pair1
      --->  some [A, B] pair(A, B).

   :- type pair2 =< pair1
      --->  some [B, A] pair(A, B).

A heap cell representing the term pair(1, "two") of type pair1 would
have type_info arguments in the order [int, string],
whereas a heap cell representing the same term of type pair2 would
have type_info arguments in the opposite order [string, int].

Similarly for existential class constraints.

For safe conversion between a subtype and supertype, all type_info and
type_class_info arguments must be in the same order in the heap cell.

This change checks that existentially quantified type variables for a
subtype constructor and existential class constraints are repeated
in the same order as in the supertype constructor definition.

doc/reference_manual.texi:
    Document the new rules.

    Add a XXX about a dubious language feature.

compiler/add_type.m:
    Check the new rules. Unlike before, we first check the conditions on
    existential type vars and existential class constraints before
    checking the types of constructor arguments.

compiler/prog_data.m:
    Fix a comment.

tests/invalid/subtype_exist_vars.m:
tests/invalid/subtype_exist_vars.err_exp:
tests/invalid/subtype_exist_constraints.m:
tests/invalid/subtype_exist_constraints.err_exp:
    Extend test cases.

tests/invalid/subtype_exist_constraints.err_exp:
    Update expected output.

NEWS.md:
    Announce change to compiler.
2024-07-08 17:38:38 +10:00
Peter Wang
d60ed6ac77 Fix check of existentially typed args in subtype definitions.
We did not correctly enforce the rule that requires a one-to-one mapping
between the existentially quantified type variables for subtype
constructor, and the variables for the same constructor in the supertype
definition, e.g. this was inadvertently allowed:

    :- type pear2
	--->	some [T, U] pear(T, U).

    :- type pear1 =< pear2
	--->	some [A] pear(A, A).  % should be rejected

compiler/add_type.m:
    Use a bimap to represent the correspondence between existentially
    quantified type variables in the subtype and supertype.

    Improve check_is_subtype_var_var with an extra check and comments.

tests/invalid/subtype_exist_vars.err_exp:
tests/invalid/subtype_exist_vars.m:
    Extend test case.
2024-07-04 12:24:16 +10:00
Zoltan Somogyi
5a45d1afb8 Replace the cons/3 cons_id ...
... with the du_data_ctor/1 cons_id, which contains a du_ctor/3.
Putting all the info about discriminated union data constructors into a
separate type allows code that works only with the cons_ids of du types
to stop having to worry about all the other kinds of cons_ids.

compiler/prog_data.m:
    As above.

    Delete the cons_ctor type from this module after moving it to
    recompilation.usage.m, which is the only part of the compiler
    that uses it.

compiler/add_foreign_enum.m:
    Replace cons_id_to_tag_map with du_ctor_to_tag_map, since the only
    cons_ids the map handles are du_ctors.

compiler/hlds_cons.m:
    Replace cons_ids with du_ctors in the cons_table and in the
    info we keep about du types' fields.

compiler/hlds_data.m:
    Replace cons_ids with du_ctors in cheaper_tag_tests.

    Provide two forms of a utility function. Both return du_ctors,
    but one wraps them up as cons_ids.

compiler/type_assign.m:
    Replace cons_ids with du_ctors in cons_type_info_sources.

compiler/type_util.m:
    Switch many of the utility predicates and functions in this module
    to operate on du_ctors instead of cons_ids. Split some of the others
    to have both du_ctor and cons_id versions.

    Replace a predicate that returned a set of solutions one at a time
    on backtracking with a predicate that returns all the solutions
    at once in a list.

    Reduce unnecessary variability in variable names.

    Add some XXXs for code with unclear motivations.

compiler/typecheck_error_undef.m:
    Delete a function argument that was used only in a sanity check,
    because the code at its only call site derived that argument using code
    that made it impossible for the sanity check to fail.

    Factor out some common code.

compiler/parse_tree_out_cons_id.m:
    For three functions that operate on cons_ids, add versions
    that do the same job on du_ctors.

compiler/inst_match.m:
    Conform to the changes above. This diff rewrites from scratch
    the algorithm for testing whether a list of bound insts covers
    *all* the du_ctors of a type, because the old code was both inefficient
    and very opaque.

compiler/float_regs.m:
    Conform to the changes above, and delete a conditionally enabled abort
    that shouldn't ever be enabled.

compiler/inst_util.m:
    Conform to the changes above, and rename a predicate to avoid
    an ambiguity.

compiler/mode_errors.m:
    Conform to the changes above, and switch to printing the cons_ids
    in some error messages using the standard mechanisms of
    write_error_spec.m.

compiler/resolve_unify_functor.m:
    Conform to the changes above. Factor out repeated tests against
    du_data_ctor.

compiler/term_norm.m:
    Conform to the changes above. Add XXXs for some bugs.

compiler/add_type.m:
compiler/assertion.m:
compiler/builtin_lib_types.m:
compiler/comp_unit_interface.m:
compiler/complexity.m:
compiler/const_struct.m:
compiler/cse_detection.m:
compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/direct_arg_in_out.m:
compiler/distance_granularity.m:
compiler/du_type_layout.m:
compiler/error_msg_inst.m:
compiler/field_access.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/goal_util.m:
compiler/hhf.m:
compiler/higher_order.specialize_calls.m:
compiler/higher_order.specialize_unify_compare.m:
compiler/hlds_code_util.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_type_table.m:
compiler/hlds_out_util.m:
compiler/implementation_defined_literals.m:
compiler/inst_abstract_unify.m:
compiler/inst_check.m:
compiler/inst_merge.m:
compiler/inst_mode_type_prop.m:
compiler/inst_test.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/intermod_decide.m:
compiler/lco.m:
compiler/ml_global_data.m:
compiler/ml_unify_gen_construct.m:
compiler/ml_unify_gen_deconstruct.m:
compiler/ml_unify_gen_util.m:
compiler/mode_top_functor.m:
compiler/mode_util.m:
compiler/modecheck_coerce.m:
compiler/modecheck_goal.m:
compiler/modecheck_unify.m:
compiler/modecheck_util.m:
compiler/module_qual.qualify_items.m:
compiler/old_type_constraints.m:
compiler/parse_inst_mode_name.m:
compiler/parse_tree_out_item.m:
compiler/parse_tree_to_term.m:
compiler/polymorphism_goal.m:
compiler/polymorphism_lambda.m:
compiler/pre_typecheck.m:
compiler/prog_ctgc.m:
compiler/prog_mode.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/qual_info.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/recompilation.used_file.m:
compiler/recompute_instmap_deltas.m:
compiler/simplify_goal.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_switch.m:
compiler/simplify_goal_unify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_opt.m:
compiler/structure_reuse.direct_choose_reuse.m:
compiler/structure_reuse.direct_detect_garbage.m:
compiler/superhomogeneous.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/typecheck_clauses.m:
compiler/typecheck_error_util.m:
compiler/typecheck_errors.m:
compiler/unify_gen_test.m:
compiler/unify_gen_util.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above.

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:
    Expect diagnostics generated using the standard error_spec representations
    of cons_ids.
2024-06-30 21:36:03 +10:00
Zoltan Somogyi
5564e5cbe4 More tweaks to diagnostics.
compiler/add_type.m:
    Improve the wording of a diagnostic.

compiler/parse_item.m:
    Add color to a diagnostic.

compiler/parse_sym_name.m:
    Include more info in a diagnostic.

compiler/parse_type_defn.m:
    Don't color inessential words.

tests/invalid_make_int/test_type_spec_int.int_err_exp:
tests/invalid_nodepend/bad_include.err_exp:
tests/invalid_nodepend/errors_1.err_exp:
tests/invalid_nodepend/errors_3.err_exp:
tests/invalid_nodepend/subtype_syntax.err_exp:
tests/invalid_nodepend/types.err_exp:
tests/invalid_nodepend/var_as_class_name.err_exp:
tests/invalid_nodepend/var_as_pred_name.err_exp:
    Expect updated diagnostics.
2024-06-13 07:47:27 +10:00
Zoltan Somogyi
8f490f6b69 Make module_qual.m a proper package.
compiler/module_qual.m:
    Delete from this module everything except include_module declarations,
    making it a proper package, after moving (most of) the deleted things
    to other modules. The rest were already unused.

compiler/module_qual.mq_info.m:
    This new submodule of module_qual.m contains the definition of the
    mq_info type, its getter/setter predicates, and some utility operations.
    This is the bulk of the old module_qual.m.

    Delete two fields of the old mq_info/mq_sub_info structure, because
    they were never used.

compiler/module_qual.qualify_items.m:
    Move the predicates that module qualify entire aug_comp_units,
    aug_make_int_units and parse_tree_int3s here from module_qual.m.

    Rename some already-here predicates to avoid name clashes with
    the newly moved predicates.

    Export only the predicates that are used outside the module *now*.

    Update some comments.

compiler/module_qual.id_set.m:
    Move a type and a predicate here from module_qual.m.

    Make a comment more detailed.

    Reorder the args of a predicate.

compiler/module_qual.collect_mq_info.m:
    Delete some trace goals that lost their usefulness a long time ago.

compiler/notes/compiler_design.html:
    Update the documentation of the module_qual package.

    Conform to the changes above.

compiler/add_clause.m:
compiler/add_type.m:
compiler/comp_unit_interface.m:
compiler/hlds_module.m:
compiler/make_hlds_passes.m:
compiler/mercury_compile_make_hlds.m:
compiler/module_qual.qual_errors.m:
compiler/pred_table.m:
compiler/qual_info.m:
compiler/superhomogeneous.m:
    Conform to the changes above.

compiler/add_class.m:
compiler/add_foreign_proc.m:
compiler/higher_order.make_specialized_preds.m:
compiler/ml_lookup_switch.m:
compiler/mode_errors.m:
compiler/tag_switch_util.m:
    Sort import_module declarations.
2024-06-08 02:28:36 +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
4d32e801f6 Use color in prog_event.m.
compiler/prog_event.m:
    Add color to the diagnostics generated by this module. Add the word
    "Error" to the start of each diagnostic.

compiler/add_type.m:
compiler/format_call_errors.m:
    Refine just what gets colored in some diagnostics.

compiler/mode_errors.m:
    Fix a singular/plural disagreement.

tests/invalid/any_passed_as_ground.err_exp:
tests/invalid/constrained_poly_insts_2.err_exp:
tests/invalid/ho_default_func_1.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/state_vars_test_1.err_exp:
tests/invalid/string_format_bad.err_exp:
tests/invalid/string_format_unknown.err_exp:
tests/invalid/subtype_foreign_supertype_1.err_exp:
tests/invalid/subtype_foreign_supertype_2.err_exp:
tests/invalid/synth_attr_error.err_exp:
tests/invalid/try_detism.err_exp:
tests/warnings/format_call_multi.err_exp:
tests/warnings/format_call_warning.err_exp:
    Expect updated diagnostics.
2024-06-01 23:08:34 +10:00
Zoltan Somogyi
90b580723d More tweaks to diagnostics.
compiler/add_foreign_proc.m:
compiler/add_type.m:
compiler/check_type_inst_mode_defns.m:
compiler/det_analysis.m:
compiler/module_qual.qual_errors.m:
    Tweaks to move towards a situation where the messages generated
    for similar errors either in different circumstances or to different
    kinds of entities use the same terminology and use color the same way
    to the extent possible.

tests/invalid/bad_fact_table_decls.err_exp:
tests/invalid/ee_invalid.err_exp:
tests/invalid/oisu_check_add_pragma_errors.err_exp:
tests/invalid/one_member.err_exp:
tests/invalid/pragma_c_code_dup_var.err_exp:
tests/invalid/pragma_qual_error.err_exp:
tests/invalid/subtype_abstract.err_exp:
tests/invalid/subtype_circular.err_exp:
tests/invalid/undef_lambda_mode.err_exp:
tests/invalid/undef_mode_and_no_clauses.err_exp:
tests/invalid/wrong_type_arity.err_exp:
tests/invalid_make_int/bad_type_class_constraint_intermodule.int_err_exp:
tests/invalid_make_int/bug499.int_err_exp:
tests/invalid_make_int/bug521.int_err_exp:
tests/invalid_make_int/builtin_int.int_err_exp:
tests/invalid_make_int/int_impl_imports.int_err_exp:
tests/invalid_make_int/missing_interface_import.int_err_exp:
tests/invalid_make_int/missing_interface_import_test_2.int_err_exp:
tests/invalid_make_int/transitive_import.int_err_exp:
tests/invalid_make_int/type_arity.int_err_exp:
tests/invalid_make_int/undef_inst.int_err_exp:
tests/invalid_make_int/undef_mode.int_err_exp:
tests/invalid_make_int/undef_type_int.int_err_exp:
tests/invalid_make_int/undef_type_mod_qual.int_err_exp:
tests/invalid_nodepend/bad_mutable.err_exp:
tests/invalid_nodepend/errors_1.err_exp:
tests/invalid_nodepend/errors_3.err_exp:
tests/invalid_nodepend/instance_bug.err_exp:
tests/invalid_nodepend/kind.err_exp:
tests/invalid_nodepend/subtype_invalid_supertype.err_exp:
tests/recompilation/remove_type_re.err_exp.2:
tests/recompilation/type_qual_re.err_exp.2:
    Expect updated diagnostics.
2024-05-31 07:58:32 +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
8ec40f7920 Use color in add_type.m.
compiler/add_type.m:
    Add color to the diagnostics generated by this module.

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

tests/invalid/extra_info_prompt.err_exp:
tests/invalid/foreign_type_visibility.err_exp:
tests/invalid/subtype_abstract.err_exp:
tests/invalid/subtype_circular.err_exp:
tests/invalid/subtype_ctor_arg.err_exp:
tests/invalid/subtype_eqv.err_exp:
tests/invalid/subtype_exist_constraints.err_exp:
tests/invalid/subtype_exist_vars.err_exp:
tests/invalid/subtype_foreign_supertype_1.err_exp:
tests/invalid/subtype_foreign_supertype_2.err_exp:
tests/invalid/subtype_ho.err_exp:
tests/invalid/subtype_not_subset.err_exp:
tests/invalid/user_eq_dummy.err_exp:
tests/invalid_nodepend/errors.err_exp:
tests/invalid_nodepend/errors1.err_exp:
tests/invalid_nodepend/subtype_invalid_supertype.err_exp:
tests/invalid_nodepend/types.err_exp:
tests/warnings/subtype_order.err_exp:
    Expect updated diagnostics.
2024-05-11 12:23:31 +10:00
Zoltan Somogyi
ca932da67e Delete the lambda_eval_method type and its uses.
compiler/prog_data.m:
    The lambda_eval_method type has only ever been used by the Aditi backend,
    and it has been a dummy type since that backend has been deleted.
    Delete this type, and the lambda_eval_method fields from closure_cons
    cons_ids, from the corresponding closure_tag cons_tag, and from
    the representation of higher order types, in order to avoid having to
    maintain code that sets or copies fields that have no purpose.

    The lambda_eval_method type is (now was) separate from the eval method
    stored in proc_infos, which does have alternatives to the normal
    eval method. All of these alternatives are forms of tabling, but
    lambdas cannot be tabled on their own.

compiler/hlds_goal.m:
    Delete the lambda_eval_method field from rhs_lambda_goals.

compiler/*.m:
    Conform to the changes above.
2024-04-23 20:00:06 +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
Zoltan Somogyi
625ec287f1 Carve five new modules out of prog_type.m.
compiler/prog_type_construct.m:
    New module for constructing types.

compiler/prog_type_repn.m:
    New module for testing things related to type representation.

compiler/prog_type_scan.m:
    New module for gather type vars in types.

compiler/prog_type_test.m:
    New module containing simple tests on types.

compiler/prog_type_unify.m:
    New module for testing whether two types unify, or whether
    one type subsumes another.

compiler/prog_type.m:
    Delete the code moved to the new modules.

compiler/parse_tree.m:
    Include the new modules.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/*.m:
    Conform to the changes above, by adjusting imports as needed,
    and by deleting any explicit module qualifications that
    this diff makes obsolete.
2023-10-06 08:42:43 +11:00
Zoltan Somogyi
8e7c5dfbd1 Use string builder in more of parse_tree_out*.m.
compiler/parse_tree_out_sym_name.m:
    Use the string builder instance of pt_output instead of the plain string
    instance, due to its better worst case behavior.

    Rename sym_name_arity_to_string to unescaped_sym_name_arity_to_string,
    to make it clear that it does not escape the sym_names it is given.
    Rename write_sym_name_arity in the same way.

    Add format_X versions of some operations that did not have them.

    Delete the write_quoted_sym_name predicate, because it was used only
    once, so it is not worth generalizing.

    Delete the escaped_module_name_to_string function, because it was
    unused.

compiler/parse_tree_out.m:
compiler/parse_tree_out_cons_id.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_misc.m:
compiler/parse_tree_out_sym_name.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type.m:
    Use the string builder instance of pt_output instead of the plain string
    instance, due to its better worst case behavior.

    Add format_X versions of some operations that did not have them.

    Implement write_X and format_X in terms of X_to_string where X_to_string
    is trivial, and (mostly) returns existing strings.

compiler/add_clause.m:
compiler/add_type.m:
compiler/analysis.file.m:
compiler/complexity.m:
    Conform to the changes above.
2023-07-09 16:47:48 +02:00
Zoltan Somogyi
b6178ef723 Delete prog_out.m, moving its code to other modules.
compiler/parse_tree_out_cons_id.m:
    Move the predicates and functions in prog_out.m that deal with cons_ids
    to this module.

compiler/parse_tree_out_sym_name.m:
    Move the predicates and functions in prog_out.m that deal with sym_names
    and similar entities to this module.

compiler/parse_tree_out_type.m:
    Move the predicates and functions in prog_out.m that deal with types
    to this module.

compiler/parse_tree_out_misc.m:
    Move the predicates and functions in prog_out.m that deal with simple
    types to this module.

    Delete mercury_output_det and mercury_format_det, replacing all their
    uses with calls to mercury_det_to_string.

compiler/prog_out.m:
    Delete this module.

compiler/parse_tree.m:
    Delete prog_out from the parse_tree package.

compiler/Mercury.options:
compiler/notes/compiler_design.html:
    Delete references to prog_out.m.

compiler/*.m:
    Update imports and any explicit module qualifications to account
    for the moved code.

tools/filter_sort_imports:
    Automatically filter out any repeated imports. This can help with
    changes like this that redistribute the contents of one module to other
    modules. In this case, after a global replacement of prog_out's import
    with the import of parse_tree_out_misc, this updated script could
    remove this changed import from modules that already imported
    parse_tree_out_misc.
2023-04-09 16:23:13 +10:00
Zoltan Somogyi
e2a8a8cbfa Break up mercury_to_mercury.m.
compiler/mercury_to_mercury.m:
    Delete this module, and replace it with ...

compiler/parse_tree_out_cons_id.m:
compiler/parse_tree_out_sym_name.m:
compiler/parse_tree_out_type.m:
compiler/parse_tree_out_misc.m:
    ... these four modules. The first three write out the entities
    in their names: cons_ids, sym_names, and types. The fourth contains
    the rest of the old mercury_to_mercury.m, plus a few predicates
    moved there from prog_out.m that deal with indentation.

compiler/parse_tree.m:
    Include the four new modules, and stop including the deleted module.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/prog_out.m:
    Delete the code moved to parse_tree_out_misc.m.

compiler/*.m:
    Adjust the imports as needed. Most modules need only one, maybe two
    of mercury_to_mercury's four successor modules.
2023-04-06 15:32:48 +10:00
Zoltan Somogyi
da3e2e80d3 Make invoked_by_mmc_make part of the op_mode.
compiler/op_mode.m:
    Add a field that specifies whether the compiler was invoked by
    "mmc --make" to the opm_top_args top-level op_mode. This is the
    class of op_modes for which we need that info.

    Fill in this new field from the value of the --invoked-by-mmc-make
    option.

compiler/options.m:
    Add an "only_opmode_" prefix to the internal name of the
    --invoked-by-mmc-make option. Move this option next to the --make option.

    Improve the wording of some options' usage messages.

doc/user_guide.texi:
    Make the same changes in wording here as well.

compiler/add_pragma.m:
compiler/add_type.m:
compiler/exception_analysis.m:
compiler/handle_options.m:
compiler/headvar_names.m:
compiler/hlds_module.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_make_hlds.m:
compiler/mode_errors.m:
compiler/op_mode.m:
compiler/options.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/tabling_analysis.m:
compiler/term_util.m:
compiler/trailing_analysis.m:
compiler/unused_args.m:
    Conform to the changes above, either by finding out whether
    the compiler was invoked by mmc --make using the op_mode instead
    of the option, or by ignoring that info where not needed.
2023-03-11 16:55:06 +08:00
Zoltan Somogyi
5638bca5bf Fix too-long lines. 2022-11-18 20:23:11 +11:00
Zoltan Somogyi
307b1dc148 Split up error_util.m into five modules.
compiler/error_spec.m:
    This new module contains the part of the old error_util.m that defines
    the error_spec type, and some functions that can help construct pieces
    of error_specs. Most modules of the compiler that deal with errors
    will need to import only this part of the old error_util.m.

    This change also renames the format_component type to format_piece,
    which matches our long-standing naming convention for variables containing
    (lists of) values of this type.

compiler/write_error_spec.m:
    This new module contains the part of the old error_util.m that
    writes out error specs, and converts them to strings.

    This diff marks as obsolete the versions of predicates that
    write out error specs to the current output stream, without
    *explicitly* specifying the intended stream.

compiler/error_sort.m:
    This new module contains the part of the old error_util.m that
    sorts lists of error specs and error msgs.

compiler/error_type_util.m:
    This new module contains the part of the old error_util.m that
    convert types to format_pieces that generate readable output.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Include and document the new modules.

compiler/error_util.m:
    The code remaining in the original error_util.m consists of
    general utility predicates and functions that don't fit into
    any of the modules above.

    Delete an unneeded pair of I/O states from the argument list
    of a predicate.

compiler/file_util.m:
    Move the unable_to_open_file predicate here from error_util.m,
    since it belongs here. Mark another predicate that writes
    to the current output stream as obsolete.

compiler/hlds_error_util.m:
    Mark two predicates that wrote out error_spec to the current output
    stream as obsolete, and add versions that take an explicit output stream.

compiler/Mercury.options:
    Compile the modules that call the newly obsoleted predicates
    with --no-warn-obsolete, for the time being.

compiler/*.m:
    Conform to the changes above, mostly by updating import_module
    declarations, and renaming format_component to format_piece.
2022-10-12 20:50:16 +11:00
Zoltan Somogyi
07f877bc3f Carve term_context.m out of term.m.
library/term.m:
library/term_context.m:
    As above.

    Rename the term.context type as term_context.term_context, with
    term.context now being defined as an equivalence type.

    Replace the context_init function and predicate and the dummy_context_init
    function with just one function: dummy_context. This name includes
    the important part (the fact that it return a *dummy* context) and deletes
    the nonimportant part (dummy contexts are just about never updated,
    so the function does not really "initialize" them).

    Reduce function/predicate pairs that do the same thing to just a function.

library/MODULES_DOC:
library/library.m:
    Add the new module to the list of standard library modules.

NEWS:
    Mention the new module, and the obsoleting of the moved predicates
    and functions in term.m.

compiler/*.m:
library/*.m:
    Conform to the changes above.
2022-08-23 12:56:37 +10:00
Zoltan Somogyi
92438b2ec6 Make make_hlds.m a package.
compiler/make_hlds.m:
    This module used to both

    - include several submodules, and
    - define several types and predicates.

    For some years now, we have preferred to do these two kinds of things
    in separate modules, with package modules containing *only* include_module
    declarations, and other modules containing *no* include_module
    declarations. The reason is that submodules of a package automatically
    get the imports of their parent, which creates unwanted coupling
    between the submodule and its parent. This problem vanishes if
    the parent module contains no imports, but the only practical way
    to sustain that over time is for the parent to define nothing.

    This diff therefore moves the definitions of types and predicates
    out of make_hlds.m. It moves the types to a new submodule,
    make_hlds_types.m. Since the predicates it defined were only
    forwarding predicates, they can be deleted without replacement,
    provided that the predicates they forwarded to are in *public*
    submodules of make_hlds.m. This diff therefore arranges that.
    It does the same with a forwarding type.

compiler/make_hlds_types.m:
    A new submodule of make_hlds.m that contains the type definitions
    that used to be in make_hlds.m itself.

compiler/instance_method_clauses.m:
    A new submodule of make_hlds.m that contains the parts of add_class.m
    that generate the clauses that define predicates that implement
    instance methods. This part of add_class.m was completely separate
    from the rest of add_class.m (it wasn't even invoked from there),
    so putting it in its own module eliminates unnecessary coupling.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/add_class.m:
    Delete the code that has been moved to instance_method_clauses.m.

compiler/add_clause.m:
compiler/add_foreign_proc.m:
compiler/add_mode.m:
compiler/add_mutable_aux_preds.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/add_solver.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/goal_expr_to_goal.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_warn.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_main.m:
compiler/qual_info.m:
compiler/state_var.m:
compiler/superhomogeneous.m:
    Conform to the changes above, mostly by adding explicit import_module
    declarations to replace a subset of the import_module declarations
    that these modules used to implicitly inherit from their parent.
2022-03-31 16:54:33 +11:00
Zoltan Somogyi
cb1da20600 Stop ancestor imports "shadowing" unused local imports.
compiler/make_hlds_passes.m:
    We used to add all modules imported by an ancestor of the current module
    to the set of used modules. Once upon a time, this was meant to stop
    the compiler generating misleading warnings about imports being unused
    when the import wasn't even done by the current module. However, since
    we introduced structured representations of import- and use_module
    declarations and taught unused_imports.m to use them, that has not been
    an issue. However, a bad side-effect remained, which was that if
    a module A imported a module B but did not use it, or it imported
    module B in its interface but did not use in its interface, then
    any warning we could generate about that import being unused was
    suppressed by any import of module B in any of module A's ancestors.
    (The "shadowing" mentioned above.)

    Fix the problem by adding modules imported by ancestors of the
    current module NOT to the set of used modules, but to a new field
    in the module_info.

compiler/hlds_module.m:
    Add this new field. As it happens, it is not needed right now,
    but it may be needed later.

    Update some documentation.

    Note an only-tangentially-related problem.

compiler/unused_imports.m:
    Fix a bug that was hiding behind the shadowing, which was that whether
    the text of the warning message we generated for an unused local import-
    or use_module declaration could be affected by the presence of an
    import- or use_module declaration in an ancestor module.

    Improve debugging infrastructure.

    Make a predicate name more descriptive.

NEWS:
    Announce the bugfix.

compiler/add_pragma_tabling.m:
compiler/add_solver.m:
compiler/add_type.m:
compiler/parse_string_format.m:
compiler/recompilation.usage.m:
compiler/recompilation.used_file.m:
library/io.call_system.m:
library/io.text_read.m:
library/random.sfc32.m:
library/random.sfc64.m:
library/random.system_rng.m:
library/string.parse_runtime.m:
library/string.parse_util.m:
library/string.to_string.m:
library/thread.closeable_channel.m:
mdbcomp/feedback.automatic_parallelism.m:
    Delete imports that the fixed compiler now generates unused import
    warnings for.
2022-03-30 13:06:37 +11:00
Zoltan Somogyi
217820e50d Report multiply defined entities using user arities.
compiler/make_hlds_error.m:
    As above.

compiler/add_class.m:
compiler/add_mode.m:
compiler/add_pred.m:
compiler/add_type.m:
    Conform to the change above.
2022-02-24 01:38:30 +11:00
Zoltan Somogyi
6077285f19 Simplify some parts of the termination analysers.
compiler/term_constr_initial.m:
compiler/termination.m:
    Move the computation of a flag out of loops. The flag said whether
    to treat check_termination pragmas as implying that termination
    will actually be checked on this compiler invocation.

    Delete the code that computation, as it has been moved to term_util.m.

compiler/term_util.m:
    Move the code that did that here. Make it return a value of a bespoke type
    instead of a bool.

    Give a predicate a meaningful name, and our usual argument order.

    Delete a predicate that is used in only one module.

    Move a comment block before *both* the predicates it is about.

compiler/op_mode.m:
    Give the op_modes that call for building .opt and .trans_opt files
    more meaningful and less misleading names, by deleting the "int"
    part of their names. The presence of that part made the comment
    on the moved predicate quite confusing.

compiler/term_traversal.m:
    Add the predicate moved here from term_util.m. Give it state-variable
    friendly argument order.

compiler/add_type.m:
compiler/handle_options.m:
compiler/headvar_names.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_main.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/tabling_analysis.m:
compiler/term_pass1.m:
compiler/trailing_analysis.m:
compiler/unused_args.m:
    Conform to the changes above.
2022-02-03 13:25:34 +11:00
Zoltan Somogyi
243491523d Stop counting errors in module_infos.
Over time, we have almost completely switched over to using error_specs,
using their severity (if not yet printed out) and their effect on the
exit status (if already printed out) to represent the presence of errors,
leaving only a few places that either updated or paid attention to the
num_error field in the module_info. This diff completes that process.

compiler/hlds_module.m:
    Delete the num_errors field in the module_info, and the predicates
    that operate on it.

compiler/hlds_error_util.m:
    Delete module_info arguments whose only purpose was to update the
    now-delete field.

compiler/error_util.m:
    Delete the old write_error_spec predicates that updated a count of warnings
    and a count of errors.

    Rename the write_error_spec_ignore predicates, which ignored those counts,
    by deleting the "_ignore" from their names. This makes them take the
    place of the deleted predicates.

compiler/add_pragma_type_spec.m:
compiler/add_pred.m:
compiler/add_type.m:
compiler/compile_target_code.m:
compiler/cse_detection.m:
compiler/find_module.m:
compiler/generate_dep_d_files.m:
compiler/handle_options.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.top_level.m:
compiler/make.track_flags.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
compiler/mercury_compile_mlds_back_end.m:
compiler/mode_info.m:
compiler/modes.m:
compiler/pd_util.m:
compiler/pred_table.m:
compiler/recompilation.check.m:
compiler/typecheck.m:
compiler/write_module_interface_files.m:
    Conform to the changes above.
2022-01-15 16:33:38 +11:00
Zoltan Somogyi
d64961d79d Use checked types/insts/modes in parse_tree_module_src.
This means that we can do the exact same checks (and, if needed, generate
the exact same error messages) when generating interface files as when
generating target language code.

This should also allow us to simplify the process of adding type, inst
and mode definitions to the HLDS.

compiler/prog_item.m:
    As above.

    Delete unused function.

compiler/error_util.m:
    Add mechanisms that allow us to distinguish (a) error specs that represent
    a type, inst or mode name definition being invalid, from (b) other error
    specs.

compiler/check_type_inst_mode_defns.m:
    Improve the error messages we generate, in several ways.

    First, for each message, specify a real severity. When the messages
    could be seen only when generating interface files, making them all
    warnings was fine and preserved old behavior, but since soon these
    will be the only place for these checks, we need to call errors errors.

    Second, specify, in the phase, which errors represent a invalid type,
    inst or mode definition, and which don't.

    Third, improve the wording of messages. In some places, do this by
    being clearer about the distinction between declarations and definitions.
    In others, do it by including more information in the message. In yet
    others, do it by recovering some kinds of mistakes (mostly items being
    in the wrong section) enough to avoid avalanche errors.

    Fourth, fix a bug. If a type ctor has an exported *declaration*,
    then it is ok for any foreign type definitions for that type_ctor
    being in the implementation section, but if the type_ctor has an
    exported Mercury *definition*, then any foreign_type definitions
    must be in the interface section as well. The code that handled
    both these situations did not enforce that.

    Fifth, fix another bug: do not include foreign type definitions
    in the source definitions of a type_ctor twice, once as a "du" (sic)
    definition, and once as itself.

compiler/convert_parse_tree.m:
    Check type, inst and mode definitions in raw_compilation_units
    when creating parse_tree_module_srcs.

compiler/comp_unit_interface.m:
    Make the code constructing interface files work from the checked maps
    in parse_tree_module_srcs.

compiler/make_hlds_passes.m:
    Set the flags that say we have found invalid type, inst or mode definitions
    from the error specs constructed during the creation of the checked
    type, inst and mode maps.

compiler/add_type.m:
    Comment out an error message for a condition that will be detected and
    reported by check_type_inst_mode_defns.m.

compiler/make_hlds_separate_items.m:
    For now, turn checked maps of type, inst and mode definitions
    back to item lists for addition to the HLDS. Adding whole checked
    definitions to the HLDS will be done by a future change.

compiler/make_hlds_error.m:
    Fix misleading indentation in an error message.

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

    Generate output whose indentation does not depend on tab settings.

compiler/check_raw_comp_unit.m:
compiler/equiv_type.m:
compiler/get_dependencies.m:
compiler/grab_modules.m:
compiler/module_qual.collect_mq_info.m:
compiler/module_qual.qualify_items.m:
    Conform to the changes above.

compiler/parse_type_defn.m:
    Fix misleading error message for ":- type <name> = <type>".

tests/debugger/foreign_type.{m,exp}:
    Delete a redundant type declaration to avoid a warning, and update
    the .exp file to expect the new line numbers.

tests/invalid/any_mode.err_exp:
tests/invalid/bug436.err_exp:
tests/invalid/bug476.err_exp:
tests/invalid/exported_foreign_enum.err_exp:
tests/invalid/fe_unmapped_nonverbose.err_exp:
tests/invalid/fe_unmapped_verbose.err_exp:
tests/invalid/foreign_enum_invalid.err_exp:
tests/invalid/foreign_solver_type.err_exp:
tests/invalid/foreign_type_visibility.err_exp:
tests/invalid/pragma_qual_error.err_exp:
tests/invalid/repeated_field_name.err_exp:
tests/invalid/subtype_foreign.err_exp:
tests/invalid/type_with_no_defn.err_exp:
tests/invalid/types2.err_exp:
tests/invalid/user_field_access_decl_conflict.err_exp:
tests/invalid_nodepend/bad_foreign_type.err_exp:
tests/invalid_nodepend/bigtest.err_exp:
tests/invalid_nodepend/invalid_typeclass.err_exp:
tests/invalid_nodepend/types.err_exp:
tests/invalid_nodepend/uu_type.err_exp:
tests/invalid_nodepend/where_abstract_enum.err_exp:
    Expect the new error messages.

tests/invalid/abstract_solver_type.{m,err_exp}:
tests/warnings/abstract_solver_type.{m,exp}:
    Move the abstract_solver_type test case from invalid to warnings, because
    this diff changes its only error to be only a warning.

tests/invalid/Mmakefile
2021-10-16 17:37:36 +11:00
Zoltan Somogyi
4126519139 Separate subtypes from du types in parse trees.
compiler/prog_data.m:
    Split type_details_sub from type_details_du, and separate
    parse_tree_sub_type from parse_tree_du_type. This gets us two things:

    - It allows us to encode the prohibition on user-specified equality and
      comparison predicates, and "where direct_arg is" clauses,
      on subtype definitions.

    - It gives us data types we can use to represent (a) only subtype
      definitions, and (b) only non-subtype du definitions.

    Note that this diff deliberately leaves the HLDS representation
    of subtypes unchanged. This is because while a subtype *definition*
    may not specify e.g. equality and comparison predicates, a subtype *can*
    have equality and comparison predicates; it just has to get them from
    its base type. And the same applies to direct args.

compiler/parse_type_defn.m:
    Enforce the invariant that subtypes may not have user-specified equality
    and comparison predicates, or "where direct_arg is" clauses.

    If we find a subtype definition that does have one or both of these
    non-allowed and now-nonrepresentable components, we do still want to
    return the rest of the type definition, in order to avoid misleading
    error messages about that type having no definition at all. To make this
    possible, allow the parsing predicate to return error_specs "alongside",
    as well as "instead of", the item or marker being parsed.

compiler/parse_item.m:
compiler/parse_module.m:
compiler/parse_class.m:
    Handle these "alongside" error messages.

compiler/prog_item.m:
    Separate out subtypes from other du types in type definition maps.

compiler/add_type.m:
    Delete a semantic check that is now in parse_type_defn.m.

    Conform to the change to the representations of subtypes/du types
    in the parse tree.

compiler/item_util.m:
    Add a new utility function for subtypes.

compiler/check_type_inst_mode_defns.m:
compiler/comp_unit_interface.m:
compiler/convert_parse_tree.m:
compiler/decide_type_repn.m:
compiler/equiv_type.m:
compiler/get_dependencies.m:
compiler/intermod.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_separate_items.m:
compiler/module_qual.collect_mq_info.m:
compiler/module_qual.qualify_items.m:
compiler/parse_tree_out.m:
compiler/prog_type.m:
compiler/recompilation.check.m:
compiler/recompilation.version.m:
    Conform to the change to the representations of subtypes/du types
    in the parse tree.
2021-10-02 16:45:26 +10:00
Zoltan Somogyi
8e9896a84d Clarify some code. 2021-09-13 05:43:48 +10:00
Zoltan Somogyi
efc3ce720b Simplify some code. 2021-09-13 05:02:15 +10:00
Zoltan Somogyi
045897e4e0 Simplify the code checking subtypes.
compiler/add_type.m:
    Make the code that looks up supertypes also check whether they are
    du types without a foreign definition, since all its uses need that check.

    Add each supertype to the "list of previous supertypes" when it actually
    becomes "previous".

    Factor out code that was repeated in (a) checking the immediate supertype,
    and (b) checking more distant ancestor supertypes.

    Put the code that constructs error_specs next to the code that constructs
    their main components, the pieces.

    The above changes make it possible to inline both check_subtype_defn_2
    and check_subtype_defn_3 into check_subtype_defn.

    Inline some other predicates/functions into their only call sites as well.

    Give some predicates and variables more meaningful names.
2021-07-12 10:58:16 +10:00
Zoltan Somogyi
09aff57259 Represent "is this a subtype" using a bespoke type.
compiler/prog_data.m:
compiler/hlds_data.m:
    Use a new type, maybe_subtype, to say whether a du type is a subtype.

compiler/add_foreign_enum.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/check_parse_tree_type_defns.m:
compiler/comp_unit_interface.m:
compiler/decide_type_repn.m:
compiler/du_type_layout.m:
compiler/equiv_type.m:
compiler/equiv_type_hlds.m:
compiler/hlds_out_module.m:
compiler/ml_type_gen.m:
compiler/mlds.m:
compiler/module_qual.qualify_items.m:
compiler/parse_tree_out.m:
compiler/parse_type_defn.m:
compiler/prog_type.m:
compiler/recompilation.usage.m:
compiler/switch_util.m:
compiler/table_gen.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
    Conform to the changes above.
2021-07-09 09:25:04 +10:00
Zoltan Somogyi
4d699229ff Provide more info in a warning.
compiler/add_type.m:
    When a subtype lists data constructors in a different order than its
    supertype, print a diff of the two orders, not just the list of
    out-of-order constructors.

    Give some predicates more expressive names.

compiler/style_checks.m:
compiler/error_util.m:
    Move two predicates from style_checks to error_util.m
    to allow them to be called from add_type.m.

tests/warnings/subtype_order.exp:
    Expect the updated warning.
2021-07-03 21:52:59 +10:00
Zoltan Somogyi
254cd500bf Add bespoke type for du types' details.
compiler/hlds_data.m:
    As above. The other kinds of types already had bespoke types
    for *their* details.

compiler/add_type.m:
compiler/du_type_layout.m:
    Instead of passing values of the hlds_type_body with an inst
    that said they were du types, pass values of the new types instead,
    which is significantly simpler.

compiler/add_foreign_enum.m:
compiler/add_special_pred.m:
compiler/check_typeclass.m:
compiler/code_info.m:
compiler/dead_proc_elim.m:
compiler/det_report.m:
compiler/direct_arg_in_out.m:
compiler/equiv_type_hlds.m:
compiler/foreign.m:
compiler/hlds_out_module.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen_test.m:
compiler/ml_unify_gen_util.m:
compiler/mlds.m:
compiler/mode_util.m:
compiler/post_term_analysis.m:
compiler/recompilation.usage.m:
compiler/resolve_unify_functor.m:
compiler/simplify_goal_ite.m:
compiler/special_pred.m:
compiler/switch_util.m:
compiler/table_gen.m:
compiler/term_norm.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above.
2021-07-01 08:26:04 +10:00
Zoltan Somogyi
9be9fa6ed7 Address a review comment. 2021-07-01 00:48:57 +10:00
Zoltan Somogyi
814a357449 Improve diagnostics for subtype errors.
compiler/add_type.m:
    Provide more information in error messages for subtype errors.

library/list.m:
    Add a utility predicate needed by the new code in add_type.m.

NEWS:
    Announce the new predicate in list.m.

tests/invalid/subtype_invalid_supertype.{m,err_exp}:
    Add some errors to this test case to exercise new error messages.

tests/invalid/subtype_abstract.err_exp:
tests/invalid/subtype_circular.err_exp:
tests/invalid/subtype_ctor_arg.err_exp:
tests/invalid/subtype_eqv.err_exp:
tests/invalid/subtype_exist_constraints.err_exp:
tests/invalid/subtype_exist_vars.err_exp:
tests/invalid/subtype_ho.err_exp:
tests/invalid/subtype_user_compare.err_exp:
tests/invalid_submodules/subtype_submodule.err_exp:
    Update the expected outputs for the subtype errors in these test cases.
2021-06-30 19:19:32 +10:00
Peter Wang
43d7e737dc Allow duplicate field names in the same module.
Allow the same field name to be used in different types in the same
module. The main motivation is that when defining a subtype it often
makes sense to use the same field names as in the base/super type,
rather than trying to invent unique field names.

compiler/add_type.m:
    Check for duplicate field names within a type only,
    not across the module.

compiler/check_parse_tree_type_defns.m:
    Only report duplicate field names within the same type.

compiler/typecheck.m:
    Make user-supplied declarations for field access functions only
    override automatically generated declarations for the same type
    constructor, e.g. a user declaration ':- func foo ^ f1 = int'
    should not affect 'X ^ f1' for X of type 'bar'.

doc/reference_manual.texi:
    Allow duplicate field names in a module, but not within a type.

    Describe how user-supplied declarations interact with duplicate
    field names.

NEWS:
    Announce change.

tests/invalid/repeated_field_name.err_exp:
    Update expected error messages.

tests/invalid/Mmakefile:
tests/invalid/user_field_access_decl_conflict.err_exp:
tests/invalid/user_field_access_decl_conflict.m:
tests/invalid/user_field_access_decl_override.err_exp:
tests/invalid/user_field_access_decl_override.m:
tests/invalid/user_field_access_decl_override2.err_exp:
tests/invalid/user_field_access_decl_override2.m:
    Add test cases.
2021-05-07 17:07:39 +10:00
Peter Wang
d1522cbcb2 Make subtypes inherit user-defined equality/comparison from base type.
Since values of a subtype are tested for equality or compared
by first converting to the base type then testing/comparing,
it means that subtypes implicitly inherited any user-defined
equality/comparison from the base type. Allowing a subtype to also
define its own equality/comparison predicates seems more confusing
than useful, so disallow it.

compiler/prog_data.m:
    Add a option noncanon_subtype to maybe_canonical.

compiler/add_type.m:
    Report an error if a subtype defined in the current module
    has user-defined equality or comparison.

    Set the maybe_canonical field of hlds_type_defn for a subtype if the
    if the base type is noncanonical. Setting it to noncanon() makes the
    type_ctor_info have a MR_TypeCtorRep value of
    MR_TYPECTOR_REP_*_USEREQ.

compiler/unify_proc.m:
    Handle subtypes ahead of (other) noncanonical types when generating
    unify or compare proc clauses.

compiler/dead_proc_elim.m:
compiler/hlds_out_module.m:
compiler/intermod.m:
compiler/post_term_analysis.m:
compiler/special_pred.m:
    Conform to changes.

doc/reference_manual.texi:
    Document change.

tests/hard_coded/Mmakefile:
tests/hard_coded/subtype_user_compare.exp:
tests/hard_coded/subtype_user_compare.m:
tests/hard_coded/subtype_user_compare2.m:
tests/invalid/Mercury.options:
tests/invalid/Mmakefile:
tests/invalid/subtype_user_compare.err_exp:
tests/invalid/subtype_user_compare.m:
    Add test cases.
2021-04-13 11:38:15 +10:00
Peter Wang
5205908cd3 Make subtypes share standard ordering with base type.
doc/reference_manual.texi:
    Define the standard ordering of a subtype to be the same as that of
    its base type.

    Suggest that subtype constructors should be declared in the same
    order as in the supertype.

compiler/add_type.m:
    Warn if a subtype's constructors are declared in a different
    relative order to the supertype.

compiler/unify_proc.m
    Generate unify/compare procs for subtypes that cast to the base type
    then call the unify/compare proc for the base type. This was
    previously done only for the high-level data representation.

tests/hard_coded/Mmakefile:
tests/hard_coded/subtype_order.exp:
tests/hard_coded/subtype_order.m:
tests/warnings/Mmakefile:
tests/warnings/subtype_order.exp:
tests/warnings/subtype_order.m:
    Add test cases.
2021-04-09 17:41:23 +10:00
Peter Wang
6a345ff5dc Make subtypes share low-level data representation with base type.
Make subtypes share data representation with base type when using
low-level data. High-level data grades are unchanged, so subtypes
are still represented with distinct classes from their base types.

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

compiler/prog_data.m:
    Add abstract_subtype option for type_details_abstract.

    Correct XXX comment.

compiler/prog_item.m:
    Add type item_type_repn_info_subtype.

    Add tcrepn_is_subtype_of option for type_ctor_repn_info.

compiler/equiv_type.m:
    Replace equivalences in tcrepn_is_subtype_of.

compiler/module_qual.qualify_items.m:
    Module qualify in tcrepn_is_subtype_of.

compiler/prog_type.m:
    Rename some predicates that can only work on non-subtype du types.

    Update comments.

compiler/check_parse_tree_type_defns.m:
    Classify subtype type definitions as std_mer_type_du_subtype
    instead of std_mer_type_du_all_plain_constants or
    std_mer_type_du_not_all_plain_constants.

    Update some comments.

compiler/du_type_layout.m:
    Add two sub-passes to handle subtypes.

compiler/comp_unit_interface.m:
    Extend this module to handle subtype type definitions,
    analogous to the way equivalence type definitions are handled.

    Rename some predicates to clarify that they must not be used
    to test subtypes.

    Record an abstract version of a subtype type definition using
    the abstract_subtype option in type_details_abstract.
    This allows the super type ctor of the subtype to be known,
    and hence the base type ctor, when a subtype is abstract exported.

    Update comments.

compiler/decide_type_repn.m:
    Extend this module to handle subtype type definitions.

    Generate type_representation items for subtype type definitions
    which include the super type ctor of the subtype.

compiler/parse_tree_out.m:
    Write out abstract_subtype as
    "where type_is_abstract_subtype(Name/Arity)" on type definitions.

compiler/parse_type_defn.m:
    Parse "type_is_abstract_subtype(Name/Arity)" declarations.

compiler/parse_tree_out_type_repn.m:
    Write type_representation items with "is_subtype_of(TypeCtor/Arity)".

compiler/parse_type_repn.m:
    Parse "is_subtype_of(TypeCtor/Arity)" in type_representation items.

compiler/add_type.m:
compiler/convert_parse_tree.m:
compiler/opt_debug.m:
compiler/type_util.m:
    Conform to changes.

    Update comments.

compiler/direct_arg_in_out.m:
    Delete XXX, nothing to do.

compiler/parse_util.m:
    Rename overly specific variable.

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

runtime/mercury_type_info.h:
    Add a flag in MR_TypeCtorInfo to indicate if the enum/du layout
    array may be indexed by an enum value or du ptag value.
    Subtypes break the invariant that the layout array contains entries
    for every enum/ptag value from 0 up to the maximum value.
    The presence of the flag MR_TYPE_CTOR_FLAG_LAYOUT_INDEXABLE tells
    the runtime that it can directly index the layout array instead of
    searching through it (which is the common case, for non-subtypes).

    Add a field MR_du_ptag to MR_DuPtagLayout. This is necessary to find
    an entry for a given primary tag value in a MR_DuPtagLayout array.

    Add a field MR_du_ptag_flags to MR_DuPtagLayout, currently with one
    possible flag MR_DU_PTAG_FLAG_SECTAG_ALTERNATIVES_INDEXABLE.
    As with primary tags, subtypes break the invariant that the
    sectag_alternatives array contains entries for every secondary tag
    value from 0 up to the maximum value. The presence of the flag tells
    the runtime that it can directly index the sectag_alternatives array
    (which is the common case, for non-subtypes).

    The two fields added to MR_DuPtagLayout occupy space that was
    previously padding, so the size of MR_DuPtagLayout is unchanged.

    In MR_EnumFunctorDesc, replace the MR_enum_functor_ordinal field by
    MR_enum_functor_value, i.e. the integer value representing the
    functor in memory. Storing *both* the functor ordinal and enum value
    would increase the size of the MR_EnumFunctorDesc struct, and would
    be redundant in the common case of non-subtype enums (both fields
    would contain equal values). We forgo having the functor ordinal
    directly available, at the cost of needing to search through an
    MR_EnumFunctorDesc array when a functor ordinal is required for a
    subtype enum, which should be rare.

compiler/rtti.m:
    Swap enum "functor ordinal" and "value" in places.

    Use a type 'enum_value' to try to ensure we do not mix up enum
    functor ordinals and enum values.

    Add code to encode the MR_TYPE_CTOR_FLAG_LAYOUT_INDEXABLE flag.

    Add code to encode the MR_DU_PTAG_FLAG_SECTAG_ALTERNATIVES_INDEXABLE
    flag.

compiler/rtti_out.m:
    Write out "enum_ordinal_ordered_tables" ordered by functor ordinals
    instead of "enum_value_ordered_tables" ordered by enum values.

    Output the enum value for MR_EnumFunctorDesc instead of functor
    ordinal.

    Output the MR_du_ptag and MR_du_ptag_flags fields for
    MR_DuPtagLayout.

    Relax sanity check on primary tags. A subtype may not necessarily
    use ptag 0, and may skip ptag values.

compiler/rtti_to_mlds.m:
    Generate "enum_ordinal_ordered_tables" instead of
    "enum_value_ordered_tables".

    Fill in the enum value for a MR_EnumFunctorDesc instead of
    the functor ordinal.

compiler/type_ctor_info.m:
    Add predicate to generate the MR_du_ptag_flags field.

    Add the MR_TYPE_CTOR_FLAG_LAYOUT_INDEXABLE flag to type_ctor_infos
    when appropriate.

    Bump the type_ctor_info_rtti_version.

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

runtime/mercury_ml_expand_body.h:
    Search through an enum layout array to find the matching enum value,
    unless the array can be indexed.

    Search through a ptag layout array to find the matching ptag value,
    unless the array can be indexed.

    Search through a sectag_alternatives array to find the matching
    secondary tag value, unless the array can be indexed.

    Factor out the code to search through a foreign enum layout array
    into a separate macro.

runtime/mercury_construct.c:
runtime/mercury_construct.h:
    Add a functor_ordinal field to the MR_Construct_Info_Struct.
    This will hold the functor ordinal now that it is not available in
    MR_EnumFunctorDesc.

    Make MR_get_functors_check_range take an argument to indicate if the
    functor_ordinal field needs to be filled in properly. Most callers
    do not need the field.

library/construct.m:
    Conform to changes to MR_get_functors_check_range and
    MR_EnumFunctorDesc.

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

runtime/mercury_dotnet.cs.in:
    Modify RTTI classes for C# backend, analogous to the changes for the
    C runtime.

    Add methods to index/search through enum layout arrays, ptag layout
    arrays, and sectag_alternatives arrays.

java/runtime/DuPtagLayout.java:
java/runtime/EnumFunctorDesc.java:
java/runtime/TypeCtorInfo_Struct.java:
    Modify RTTI classes for Java backend, analogous to the changes for the
    C runtime.

    Add methods to index/search through enum layout arrays, ptag layout
    arrays, and sectag_alternatives arrays.

library/rtti_implementation.m:
    Conform to MR_EnumFunctorDesc field change.

    Index or search through the enum layout array or ptag layout array
    based on the MR_TYPE_CTOR_FLAG_LAYOUT_INDEXABLE flag.

    Index or search through the sectag_alternatives array depending on
    the MR_DU_PTAG_FLAG_SECTAG_ALTERNATIVES_INDEXABLE flag.

    Add separator lines.

    Slightly reorder some code.

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

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/subtype_pack.m:
tests/hard_coded/subtype_pack_2.m:
tests/hard_coded/subtype_pack.exp:
tests/hard_coded/subtype_rtti.m:
tests/hard_coded/subtype_rtti.exp:
tests/hard_coded/subtype_rtti.exp2:
    Add test cases.
2021-04-09 17:36:38 +10:00