mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
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.
156 lines
4.9 KiB
Mathematica
156 lines
4.9 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 1995-1998, 2011 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
|
|
% Simplified by Marnix Klooster <marnix@worldonline.nl>
|
|
%
|
|
% This module provides file input. One can read a file entirely,
|
|
% select a single line from a read file, get the number of lines
|
|
% in a read file, and convert a read file to a list of strings.
|
|
%
|
|
% Every file has a filename attached to it.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module file.
|
|
:- interface.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- import_module io.
|
|
:- import_module list.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type file.
|
|
|
|
% read_file reads a file from a filename.
|
|
%
|
|
:- pred read_file(string::in, io.res(file)::out, io::di, io::uo) is det.
|
|
|
|
% read_input reads a file from the input stream.
|
|
%
|
|
:- pred read_input(string::in, io.res(file)::out, io::di, io::uo) is det.
|
|
|
|
% get_line retrieves a line from a file.
|
|
% (Lines are numbered from 0.)
|
|
% Fails if the line is out of bounds.
|
|
%
|
|
:- pred get_line(file::in, int::in, string::out) is semidet.
|
|
|
|
% get_numlines returns the number of lines in a file.
|
|
%
|
|
:- pred get_numlines(file::in, int::out) is det.
|
|
|
|
% from_list converts a list of lines to a file.
|
|
%
|
|
:- pred from_list(string::in, list(string)::in, file::out) is det.
|
|
|
|
% to_list converts a file into a list of lines.
|
|
%
|
|
:- pred to_list(file::in, list(string)::out) is det.
|
|
|
|
% get_file_name returns the name of the file.
|
|
%
|
|
:- pred get_file_name(file::in, string::out) is det.
|
|
|
|
% set_file_name sets the name of the file.
|
|
%
|
|
:- pred set_file_name(file::in, string::in, file::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module array.
|
|
:- import_module int.
|
|
:- import_module require.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type file
|
|
---> file(
|
|
file_name :: string,
|
|
file_contents :: array(string)
|
|
).
|
|
|
|
read_file(FileName, File, !IO) :-
|
|
% Open the stream, read from the stream, then close the stream.
|
|
io.open_input(FileName, Res, !IO),
|
|
(
|
|
Res = ok(InputStream),
|
|
file.read_stream(InputStream, Contents, !IO),
|
|
io.close_input(InputStream, !IO),
|
|
File = ok(file(FileName, Contents))
|
|
;
|
|
Res = error(Error),
|
|
File = error(Error)
|
|
).
|
|
|
|
read_input(FileName, ok(file(FileName, Contents)), !IO) :-
|
|
% Get the input stream, then read from it.
|
|
io.input_stream(InputStream, !IO),
|
|
file.read_stream(InputStream, Contents, !IO).
|
|
|
|
% read_stream is the "real" file reader.
|
|
%
|
|
:- pred read_stream(io.input_stream::in, array(string)::array_uo,
|
|
io::di, io::uo) is det.
|
|
|
|
read_stream(Stream, File, !IO) :-
|
|
file.read_stream2(Stream, 0, File, !IO).
|
|
|
|
% Given a Stream from which LinesIn lines have already been read,
|
|
% fill File[LinesIn] to File[LinesOut-1] with the rest of the lines.
|
|
% LinesOut is the number of lines in the file.
|
|
% (Note that line numbering starts at zero.)
|
|
%
|
|
:- pred read_stream2(io.input_stream::in, int::in,
|
|
array(string)::array_uo, io::di, io::uo) is det.
|
|
|
|
read_stream2(Stream, LineNo, File, !IO) :-
|
|
io.read_line_as_string(Stream, Res, !IO),
|
|
(
|
|
Res = eof,
|
|
array.init(LineNo, "", File)
|
|
;
|
|
Res = ok(Line),
|
|
file.read_stream2(Stream, LineNo + 1, File0, !IO),
|
|
array.set(LineNo, Line, File0, File)
|
|
;
|
|
Res = error(Error),
|
|
io.error_message(Error, Msg),
|
|
error(Msg)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
get_line(file(_, Contents), LineNo, Line) :-
|
|
array.semidet_lookup(Contents, LineNo, Line).
|
|
|
|
get_numlines(file(_, Contents), NumLines1 + 1) :-
|
|
array.bounds(Contents, _, NumLines1).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
to_list(file(_, Contents), List) :-
|
|
array.to_list(Contents, List).
|
|
|
|
from_list(FileName, List, file(FileName, Contents)) :-
|
|
array.from_list(List, Contents).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
get_file_name(file(FileName, _), FileName).
|
|
|
|
set_file_name(file(_, B), FileName, file(FileName, B)).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module file.
|
|
%-----------------------------------------------------------------------------%
|