mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-09 10:52:24 +00:00
Fix a bug in the termination analyser where setting the termination
Estimated hours taken: 2 Branches: main Fix a bug in the termination analyser where setting the termination norm to `num-data-elems' causes the an internal abort when analysing code involving existential typeclass constraints. The bug is caused by the length of the list of arguments of a functor differing from the length of the list in the weight table that tells the compiler which arguments to count when computing the size of that functor. The length mismatch is caused by typeinfo related variables that are introduced by the compiler for existentially typed terms. The termination analyser includes them but the weight table does not. I committed a diff a few months ago that partially fixed this problem, but programs that use existential typeclass constraints break that fix as well. The diff implements the easiest solution to all this which is to have the termination analyser remove all the typeinfo related arguments of a term before calling term_norm.functor_norm/9. This diff also fixes a few things in the tests/term directory, namely making sure that we actually run the tests, updating the module qualifier in a few .trans_opt_exp files and updating some comments. compiler/term_norm.m: Ignore any typeinfo related arguments that a term has when building the weight table. compiler/term_traversal.m: Remove any typeinfo related arguments from the lists of arguments and modes before computing the size of a term. tests/term/Mercury.options: tests/term/existential_error3.m: tests/term/existential_error3.trans_opt_exp: Add a regression test for this bug. tests/term/Mmakefile: Make sure the $(TESTS) and $(PROGS) variables are fully defined before Mmake.common is included, otherwise the tests will not run. Add the new regression test. tests/term/existential_error1.m: tests/term/existential_error2.m: The code that caused these bugs has been moved from term_util.m to term_norm.m. Update the references to the old filename. tests/term/*.trans_opt_exp: Replaces instances of `:' as the module qualifier with `.'. Quite a few tests cases were failing because the .trans_opt files use the latter.
This commit is contained in:
@@ -146,26 +146,24 @@ find_weights_for_type(TypeCtor - TypeDefn, !Weights) :-
|
||||
:- pred find_weights_for_cons(type_ctor::in, list(type_param)::in,
|
||||
constructor::in, weight_table::in, weight_table::out) is det.
|
||||
|
||||
% XXX Currently, the weight of a functor is not affected by the presence
|
||||
% of any arguments that are type_info related. However, the set of
|
||||
% arguments whose sizes should be counted towards the total size of
|
||||
% the term will include any type-info related arguments.
|
||||
% For existentially typed data items the compiler may insert some
|
||||
% type-info related arguments into the functor. We ignore these
|
||||
% arguments when calculating the weight of a functor and we do not
|
||||
% include them in the list of counted arguments.
|
||||
|
||||
find_weights_for_cons(TypeCtor, Params, Ctor, !Weights) :-
|
||||
Ctor = ctor(ExistQVars, _Constraints, SymName, Args),
|
||||
list__length(ExistQVars, NumExistQVars),
|
||||
Ctor = ctor(_ExistQVars, _Constraints, SymName, Args),
|
||||
list__length(Args, Arity),
|
||||
( Arity > 0 ->
|
||||
find_and_count_nonrec_args(Args, TypeCtor, Params,
|
||||
NumNonRec, ArgInfos0),
|
||||
( NumNonRec = 0 ->
|
||||
Weight = 1,
|
||||
list__duplicate(Arity, yes, ArgInfos1)
|
||||
list__duplicate(Arity, yes, ArgInfos)
|
||||
;
|
||||
Weight = NumNonRec,
|
||||
ArgInfos1 = ArgInfos0
|
||||
ArgInfos = ArgInfos0
|
||||
),
|
||||
ArgInfos = list__duplicate(NumExistQVars, no) ++ ArgInfos1,
|
||||
WeightInfo = weight(Weight, ArgInfos)
|
||||
;
|
||||
WeightInfo = weight(0, [])
|
||||
|
||||
@@ -105,7 +105,7 @@
|
||||
:- import_module check_hlds__type_util.
|
||||
:- import_module hlds__hlds_data.
|
||||
|
||||
:- import_module bool, int, require.
|
||||
:- import_module assoc_list, bool, int, require.
|
||||
|
||||
traverse_goal(Goal, Params, Info0, Info) :-
|
||||
Goal = GoalExpr - GoalInfo,
|
||||
@@ -453,13 +453,26 @@ unify_change(OutVar, ConsId, Args0, Modes0, Params, Gamma, InVars, OutVars) :-
|
||||
\+ type_is_higher_order(Type, _, _, _, _),
|
||||
( type_to_ctor_and_args(Type, TypeCtor, _) ->
|
||||
params_get_module_info(Params, Module),
|
||||
filter_args_and_modes(VarTypes, Args0, Args1, Modes0, Modes1),
|
||||
functor_norm(FunctorInfo, TypeCtor, ConsId, Module,
|
||||
Gamma, Args0, Args, Modes0, Modes),
|
||||
Gamma, Args1, Args, Modes1, Modes),
|
||||
split_unification_vars(Args, Modes, Module, InVars, OutVars)
|
||||
;
|
||||
error("variable type in traverse_goal_2")
|
||||
).
|
||||
|
||||
:- pred filter_args_and_modes(map(prog_var, (type))::in, list(prog_var)::in,
|
||||
list(prog_var)::out, list(uni_mode)::in, list(uni_mode)::out) is det.
|
||||
|
||||
filter_args_and_modes(VarTypes, Args0, Args, Modes0, Modes) :-
|
||||
assoc_list__from_corresponding_lists(Args0, Modes0, ArgsAndModes0),
|
||||
IsNotTypeInfo = (pred(ArgMode::in) is semidet :-
|
||||
map__lookup(VarTypes, fst(ArgMode), Type),
|
||||
not is_introduced_type_info_type(Type)
|
||||
),
|
||||
list__filter(IsNotTypeInfo, ArgsAndModes0, ArgsAndModes),
|
||||
assoc_list__keys_and_values(ArgsAndModes, Args, Modes).
|
||||
|
||||
%-----------------------------------------------------------------------------%
|
||||
|
||||
:- pred record_change(bag(prog_var)::in, bag(prog_var)::in, int::in,
|
||||
|
||||
@@ -10,6 +10,7 @@ MCFLAGS-dds3_17=--term-norm=simple
|
||||
MCFLAGS-dds3_8=--term-norm=simple
|
||||
MCFLAGS-existential_error1=--term-norm=num-data-elems
|
||||
MCFLAGS-existential_error2=--term-norm=num-data-elems
|
||||
MCFLAGS-existential_error3=--term-norm=num-data-elems
|
||||
MCFLAGS-fold=--term-norm=simple
|
||||
MCFLAGS-my_list=--term-norm=simple
|
||||
MCFLAGS-lte=--term-norm=simple
|
||||
|
||||
@@ -21,6 +21,7 @@ TERM_PROGS= \
|
||||
dds3_8 \
|
||||
existential_error1 \
|
||||
existential_error2 \
|
||||
existential_error3 \
|
||||
fold \
|
||||
my_list \
|
||||
lte \
|
||||
@@ -69,11 +70,6 @@ TERM_PROGS= \
|
||||
vangelder
|
||||
endif # $(MMAKE_USE_MMC_MAKE) == no
|
||||
|
||||
TESTS=$(PROGS)
|
||||
SUBDIRS=
|
||||
TESTS_DIR=..
|
||||
include $(TESTS_DIR)/Mmake.common
|
||||
|
||||
# Module-specific options should go in Mercury.options so they
|
||||
# can be found by `mmc --make'.
|
||||
include Mercury.options
|
||||
@@ -85,6 +81,13 @@ else
|
||||
PROGS=$(TERM_PROGS)
|
||||
endif
|
||||
|
||||
#-----------------------------------------------------------------------------#
|
||||
|
||||
TESTS=$(PROGS)
|
||||
SUBDIRS=
|
||||
TESTS_DIR=..
|
||||
include $(TESTS_DIR)/Mmake.common
|
||||
|
||||
%.runtest: %.trans_opt_res ;
|
||||
|
||||
#-----------------------------------------------------------------------------#
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module dds1_2.
|
||||
:- pragma termination_info(dds1_2:permute((builtin.in), (builtin.out)), finite(0, [no, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(dds1_2.permute((builtin.in), (builtin.out)), finite(0, [no, yes, no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module dds3_13.
|
||||
:- pragma termination_info(dds3_13:duplicate((builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
:- pragma termination_info(dds3_13.duplicate((builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module dds3_14.
|
||||
:- pragma termination_info(dds3_14:sum((builtin.in), (builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
:- pragma termination_info(dds3_14.sum((builtin.in), (builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module dds3_15.
|
||||
:- pragma termination_info(dds3_15:merge((builtin.in), (builtin.in), (builtin.out)), finite(0, [yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(dds3_15.merge((builtin.in), (builtin.in), (builtin.out)), finite(0, [yes, yes, no]), cannot_loop).
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
:- module dds3_17.
|
||||
:- pragma termination_info(dds3_17:dis((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(dds3_17:con((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(dds3_17.dis((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(dds3_17.con((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module dds3_8.
|
||||
:- pragma termination_info(dds3_8:reverse((builtin.in), (builtin.out), (builtin.in)), finite(0, [no, yes, no, yes]), cannot_loop).
|
||||
:- pragma termination_info(dds3_8.reverse((builtin.in), (builtin.out), (builtin.in)), finite(0, [no, yes, no, yes]), cannot_loop).
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
% Regression test for term_util.m
|
||||
% Regression test for term_norm.m
|
||||
% Symptom: "Software Error: Unmatched lists in functor_norm_filter_args."
|
||||
% This was caused by the list of counted arguments in the weight table differing
|
||||
% from the list of arguments the termination analyser provided when it called
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
% Regression test for term_util.m
|
||||
% Regression test for term_norm.m
|
||||
% Symptom: "Software Error: Unmatched lists in functor_norm_filter_args."
|
||||
% (see existential_error1.m for a description of the cause)
|
||||
|
||||
|
||||
24
tests/term/existential_error3.m
Normal file
24
tests/term/existential_error3.m
Normal file
@@ -0,0 +1,24 @@
|
||||
% Regression test for term_norm.m
|
||||
% Symptom: "Software Error: Unmatched lists in functor_norm_filter_args."
|
||||
% (see existential_error1.m for the general cause of the problem).
|
||||
% In this particular case the mismatch in the number of arguments was
|
||||
% caused by the code generating the weight table not handling TypeClassInfos
|
||||
% correctly.
|
||||
|
||||
:- module existential_error3.
|
||||
|
||||
:- interface.
|
||||
|
||||
:- type foo
|
||||
---> some [A, B] foo(A, B) => baz(A, B)
|
||||
; bar.
|
||||
|
||||
:- typeclass baz(A, B) where [].
|
||||
|
||||
:- pred test(foo::in) is semidet.
|
||||
|
||||
:- implementation.
|
||||
|
||||
test(X) :- X = foo(_, _).
|
||||
|
||||
:- end_module existential_error3.
|
||||
2
tests/term/existential_error3.trans_opt_exp
Normal file
2
tests/term/existential_error3.trans_opt_exp
Normal file
@@ -0,0 +1,2 @@
|
||||
:- module existential_error3.
|
||||
:- pragma termination_info(existential_error3.test((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
@@ -1,3 +1,3 @@
|
||||
:- module pl1_1.
|
||||
:- pragma termination_info(pl1_1:append((builtin.in), (builtin.in), (builtin.out)), finite(0, [no, yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl1_1:append((builtin.out), (builtin.out), (builtin.in)), finite(0, [no, no, no, yes]), cannot_loop).
|
||||
:- pragma termination_info(pl1_1.append((builtin.in), (builtin.in), (builtin.out)), finite(0, [no, yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl1_1.append((builtin.out), (builtin.out), (builtin.in)), finite(0, [no, no, no, yes]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl1_2.
|
||||
:- pragma termination_info(pl1_2:perm((builtin.in), (builtin.out)), finite(0, [no, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl1_2.perm((builtin.in), (builtin.out)), finite(0, [no, yes, no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl2_3_1.
|
||||
:- pragma termination_info(pl2_3_1:p((builtin.in), (builtin.out)), finite(0, [yes, no]), can_loop).
|
||||
:- pragma termination_info(pl2_3_1.p((builtin.in), (builtin.out)), finite(0, [yes, no]), can_loop).
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
:- module pl3_1_1.
|
||||
:- pragma termination_info((pl3_1_1:a), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:b), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:c), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:d), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:e), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:f), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1:g), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.a), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.b), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.c), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.d), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.e), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.f), finite(0, []), can_loop).
|
||||
:- pragma termination_info((pl3_1_1.g), finite(0, []), can_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl3_5_6.
|
||||
:- pragma termination_info(pl3_5_6:p((builtin.out)), infinite, can_loop).
|
||||
:- pragma termination_info(pl3_5_6.p((builtin.out)), infinite, can_loop).
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
:- module pl4_01.
|
||||
:- pragma termination_info(pl4_01:append3((builtin.in), (builtin.in), (builtin.in), (builtin.out)), finite(0, [no, yes, yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl4_01:append3((builtin.out), (builtin.out), (builtin.out), (builtin.in)), finite(0, [no, no, no, no, yes]), cannot_loop).
|
||||
:- pragma termination_info(pl4_01.append3((builtin.in), (builtin.in), (builtin.in), (builtin.out)), finite(0, [no, yes, yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl4_01.append3((builtin.out), (builtin.out), (builtin.out), (builtin.in)), finite(0, [no, no, no, no, yes]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl4_4_3.
|
||||
:- pragma termination_info(pl4_4_3:merge((builtin.in), (builtin.in), (builtin.out)), finite(0, [yes, yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl4_4_3.merge((builtin.in), (builtin.in), (builtin.out)), finite(0, [yes, yes, no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl4_5_2.
|
||||
:- pragma termination_info(pl4_5_2:s((builtin.in), (builtin.out)), infinite, can_loop).
|
||||
:- pragma termination_info(pl4_5_2.s((builtin.in), (builtin.out)), infinite, can_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl5_2_2.
|
||||
:- pragma termination_info(pl5_2_2:turing((builtin.in), (builtin.in), (builtin.in), (builtin.out)), infinite, can_loop).
|
||||
:- pragma termination_info(pl5_2_2.turing((builtin.in), (builtin.in), (builtin.in), (builtin.out)), infinite, can_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl6_1_1.
|
||||
:- pragma termination_info(pl6_1_1:qsort((builtin.in), (builtin.out)), finite(0, [yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl6_1_1.qsort((builtin.in), (builtin.out)), finite(0, [yes, no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl7_2_9.
|
||||
:- pragma termination_info(pl7_2_9:mult((builtin.in), (builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
:- pragma termination_info(pl7_2_9.mult((builtin.in), (builtin.in), (builtin.out)), infinite, cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl8_2_1.
|
||||
:- pragma termination_info(pl8_2_1:mergesort((builtin.in), (builtin.out)), finite(0, [yes, no]), can_loop).
|
||||
:- pragma termination_info(pl8_2_1.mergesort((builtin.in), (builtin.out)), finite(0, [yes, no]), can_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl8_3_1.
|
||||
:- pragma termination_info(pl8_3_1:xminsort((builtin.in), (builtin.out)), infinite, can_loop).
|
||||
:- pragma termination_info(pl8_3_1.xminsort((builtin.in), (builtin.out)), infinite, can_loop).
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
:- module pl8_4_1.
|
||||
:- pragma termination_info(pl8_4_1:even((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(pl8_4_1:odd((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(pl8_4_1.even((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
:- pragma termination_info(pl8_4_1.odd((builtin.in)), finite(0, [no]), cannot_loop).
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
:- module pl8_4_2.
|
||||
:- pragma termination_info(pl8_4_2:e((builtin.in), (builtin.out)), finite(-1, [yes, no]), cannot_loop).
|
||||
:- pragma termination_info(pl8_4_2.e((builtin.in), (builtin.out)), finite(-1, [yes, no]), cannot_loop).
|
||||
|
||||
Reference in New Issue
Block a user