Files
mercury/samples/diff/difftype.m
Zoltan Somogyi 4aed1a57e4 Print an arg type list diff for arg lists with wrong arity.
When a user writes a clause for a predicate (or function) that does not exist
with that arity, but does exist with one or more other arities, report not
just the list of the other arity/arities, but, for each such other arity,
a diff between the declared arg types and the inferred arg types.

After this diff, we generate output like this:

bad_pred_arity.m:027: Error: clause for predicate `bad_pred_arity.p'/4
bad_pred_arity.m:027:   without corresponding `:- pred' declaration.
bad_pred_arity.m:027:   However, predicates of that name do exist with arities
bad_pred_arity.m:027:   3 and 5.
bad_pred_arity.m:027: Inferred :- pred p(int, string, int, string).
bad_pred_arity.m:027:   The argument list difference from the arity 3 version
bad_pred_arity.m:027:   is
bad_pred_arity.m:027:     pred(
bad_pred_arity.m:027:         int,
bad_pred_arity.m:027:   +     string,
bad_pred_arity.m:027:         int,
bad_pred_arity.m:027:         string
bad_pred_arity.m:027:     )
bad_pred_arity.m:027:   The argument list difference from the arity 5 version
bad_pred_arity.m:027:   is
bad_pred_arity.m:027:     pred(
bad_pred_arity.m:027:         int,
bad_pred_arity.m:027:   -     float,
bad_pred_arity.m:027:         string,
bad_pred_arity.m:027:         int,
bad_pred_arity.m:027:         string
bad_pred_arity.m:027:     )

compiler/typecheck_errors.m:
    Generate the diff part of the message above.

compiler/typecheck.m:
    Invoke typecheck_errors.m when relevant.

compiler/error_util.m:
    When comparing two error_specs, switch from a two-level comparison
    (first the contexts of error_msgs, then everything else) to three levels
    first the contexts of error_msgs, then their error_msg_components,
    then everything else). This is needed to allow the error message from
    make_hlds_error.m (which reports the error and mentions the arities
    with which the named predicate or function does exist) come out before
    the informational message from typecheck.m that prints the inferred
    arg types and their differences from the other arities. (With the old
    comparison, the difference in severity would trump the invisible order
    components that this diff includes in both specs to force the desire
    order.)

    Base the code comparing error_specs on the code for comparing error_msgs.
    Move the two previously separate pieces code for those tasks next to each
    other.

compiler/make_hlds_error.m:
    Add the invisble ordering component.

    When we see clauses with two or more wrong arities for a given predicate
    or function, don't list the automatically created pred declaration
    for an *earlier* wrong-arity clause as a real declaration whose arity
    is to be listed in the error messages we generate for *later* wrong-arity
    clauses.

    Add some documentation.

compiler/add_pred.m:
    Factor out some common code.

library/edit_seq.m:
    A new module for computing diffs.

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

tests/hard_coded/edit_seq_test.{m,exp}:
    A new test case for the diff algorithm.

tests/invalid/bad_pred_arity.{m,err_exp}:
    A new test case for the new error message.

tests/hard_coded/Mmakefile:
tests/invalid/Mmakefile:
    Enable the new test cases.

tests/invalid/bigtest.err_exp:
tests/invalid/bug197.err_exp:
tests/invalid/bug278.err_exp:
tests/invalid/errors2.err_exp:
tests/invalid/invalid_binary_literal.err_exp:
tests/invalid/invalid_float_literal.err_exp:
tests/invalid/invalid_hex_literal.err_exp:
tests/invalid/invalid_main.err_exp:
tests/invalid/invalid_octal_literal.err_exp:
tests/invalid/multimode_dcg.err_exp:
tests/invalid/multisoln_func.err_exp:
tests/invalid/null_char.err_exp:
tests/invalid/state_vars_test3.err_exp:
tests/invalid/try_detism.err_exp2:
tests/invalid/typeclass_test_5.err_exp:
tests/invalid/typeclass_test_8.err_exp:
tests/invalid/unsatisfiable_constraint.err_exp:
tests/invalid_purity/impure_func_t3.err_exp:
    Update these files to expect error messages in the new order.

samples/diff/*.m:
    Fix comments, mostly by moving them to where our programming style
    wants them.
2019-01-03 08:57:20 +11:00

142 lines
4.6 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%-----------------------------------------------------------------------------%
% Copyright (C) 1995-1998, 2006 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% Main author: bromage
% Based heavily on lcsstype.m, written by bromage and simplified by
% Marnix Klooster <marnix@worldonline.nl>
%
% This module contains the type of a diff.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module difftype.
:- interface.
:- import_module list.
:- import_module pair.
%-----------------------------------------------------------------------------%
% A pos is a non-negative number representing a position in a list.
% The position before all elements is 0, the one between the first
% and second elements is 1, etc.
%
:- type pos == int.
%-----------------------------------------------------------------------------%
% A segment is a pair of positions. Numbering items from 0,
% segment P-Q stands for items P up to, but not including, Q.
% (Rationale: see the interpretation of type pos above.)
%
% Invariant: In any segment X - Y, it should always be true that X =< Y.
% If X=Y, the segment is empty.
%
:- type segment == pair(pos,pos).
% An edit operation is an addition, a deletion or a change.
%
:- type edit
---> add(pos, segment)
; delete(segment, pos)
; change(segment, segment).
% The complete diff of two file is a list of edit operations.
%
% Invariant: The edits must be in order, and must not overlap or touch.
%
:- type diff == list(edit).
%-----------------------------------------------------------------------------%
:- pred first_mentioned_positions(edit::in, pos::out, pos::out) is det.
:- pred last_mentioned_positions(edit::in, pos::out, pos::out) is det.
%-----------------------------------------------------------------------------%
% Add an edit to the start of a diff, producing a new diff.
% This predicate determines what kind of edit this is, and merges with the
% adjacent edits if appropriate.
%
:- pred add_edit(segment::in, segment::in, diff::in, diff::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
%-----------------------------------------------------------------------------%
first_mentioned_positions(add(X, Y - _), X, Y).
first_mentioned_positions(delete(X - _, Y), X, Y).
first_mentioned_positions(change(X - _, Y - _), X, Y).
last_mentioned_positions(add(X, _ - Y), X, Y).
last_mentioned_positions(delete(_ - X, Y), X, Y).
last_mentioned_positions(change(_ - X, _ - Y), X, Y).
%-----------------------------------------------------------------------------%
add_edit(X1 - X2, Y1 - Y2, [], Diff) :-
( if X1 = X2 then
( if Y1 = Y2 then
Diff = []
else
Diff = [add(X1, Y1 - Y2)]
)
else
( if Y1 = Y2 then
Diff = [delete(X1 - X2, Y1)]
else
Diff = [change(X1 - X2, Y1 - Y2)]
)
).
add_edit(X1 - X2, Y1 - Y2, [Edit0 | Diff0], Diff) :-
( if
Edit0 = add(X2, Y2 - Y3)
then
( if X1 = X2 then
Diff = [add(X1, Y1 - Y3) | Diff0]
else
Diff = [change(X1 - X2, Y1 - Y3) | Diff0]
)
else if
Edit0 = delete(X2 - X3, Y2)
then
( if Y1 = Y2 then
Diff = [delete(X1 - X3, Y1) | Diff0]
else
Diff = [change(X1 - X3, Y1 - Y2) | Diff0]
)
else if
Edit0 = change(X2 - X3, Y2 - Y3)
then
Diff = [change(X1 - X3, Y1 - Y3) | Diff0]
else
% This is just copied from the base case. Pretty much.
( if X1 = X2 then
( if Y1 = Y2 then
Diff = [Edit0 | Diff0]
else
Diff = [add(X1, Y1 - Y2), Edit0 | Diff0]
)
else
( if Y1 = Y2 then
Diff = [delete(X1 - X2, Y1), Edit0 | Diff0]
else
Diff = [change(X1 - X2, Y1 - Y2), Edit0 | Diff0]
)
)
).
%-----------------------------------------------------------------------------%
:- end_module difftype.
%-----------------------------------------------------------------------------%