[m-rev.] diff: improved error message from typecheck

Zoltan Somogyi zs at cs.mu.OZ.AU
Thu May 20 13:27:54 AEST 2004


compiler/typecheck.m:
	Even if there is more than one type assignment involved in a type error
	for a unification of the shape X = f(Ys), print a focused error message
	that lists the actual/expected types only for the arguments which
	could possibly cause the type mismatch.

There is no existing test affected by this change, and the motivation that
caused me to make the change (a far too big error message after I modified
a compiler module) is too difficult to cut down to test case size. I did verify
that the compiler now generates a far more comprehensible error message in
that case than before.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/typecheck.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/typecheck.m,v
retrieving revision 1.352
diff -u -b -r1.352 typecheck.m
--- compiler/typecheck.m	14 May 2004 08:40:28 -0000	1.352
+++ compiler/typecheck.m	20 May 2004 00:09:15 -0000
@@ -2490,11 +2490,20 @@
 
 :- type args_type_assign
 	--->	args(
-			type_assign, 		% Type assignment,
-			list(type), 		% types of callee args,
-			class_constraints	% constraints from callee
+			caller_arg_assign	:: type_assign,
+						% Type assignment,
+			callee_arg_types	:: list(type),
+						% types of callee args,
+			callee_constraints	:: class_constraints
+						% constraints from callee
 		).
 
+:- func get_caller_arg_assign(args_type_assign) = type_assign.
+:- func get_callee_arg_types(args_type_assign) = list(type).
+
+get_caller_arg_assign(ArgsTypeAssign) = ArgsTypeAssign ^ caller_arg_assign.
+get_callee_arg_types(ArgsTypeAssign) = ArgsTypeAssign ^ callee_arg_types.
+
 :- pred typecheck_unify_var_functor_get_ctors(type_assign_set::in,
 	typecheck_info::in, list(cons_type_info)::in,
 	cons_type_assign_set::in, cons_type_assign_set::out) is det.
@@ -4816,30 +4825,40 @@
 	io__write_string("\n"),
 	prog_out__write_context(Context),
 	io__write_string("  and term `"),
-	{ strip_builtin_qualifier_from_cons_id(Functor, Functor1) },
-	hlds_out__write_functor_cons_id(Functor1, Args, VarSet, ModuleInfo, no),
+	{ strip_builtin_qualifier_from_cons_id(Functor, StrippedFunctor) },
+	hlds_out__write_functor_cons_id(StrippedFunctor, Args, VarSet,
+		ModuleInfo, no),
 	io__write_string("':\n"),
 	prog_out__write_context(Context),
 	io__write_string("  type error in argument(s) of "),
-	write_functor_name(Functor1, Arity),
+	write_functor_name(StrippedFunctor, Arity),
 	io__write_string(".\n"),
 
+	{ ConsArgTypesSet = list__map(get_callee_arg_types,
+		ArgsTypeAssignSet) },
+
 	% If we know the type of the function symbol, and each argument
 	% also has at most one possible type, then we prefer to print an
 	% error message that mentions the actual and expected types of the
 	% arguments only for the arguments in which the two types differ.
 	(
-		{ ArgsTypeAssignSet = [SingleArgsTypeAssign] },
-		{ SingleArgsTypeAssign = args(TypeAssign, ConsArgTypes, _) },
+		{ list__all_same(ConsArgTypesSet) },
+		{ ConsArgTypesSet = [ConsArgTypes | _] },
 		{ assoc_list__from_corresponding_lists(Args, ConsArgTypes,
 			ArgExpTypes) },
-		{ find_mismatched_args(ArgExpTypes, [TypeAssign], 1,
-			Mismatches) },
-		{ Mismatches = [_ | _] }
+		{ TypeAssigns = list__map(get_caller_arg_assign,
+			ArgsTypeAssignSet) },
+		{ find_mismatched_args(ArgExpTypes, TypeAssigns, 1,
+			SimpleMismatches, ComplexMismatches, AllMismatches) },
+		{ require(list__is_not_empty(AllMismatches),
+			"report_error_functor_arg_types: no mismatches") },
+		{ ComplexMismatches = [] }
 	->
-		report_mismatched_args(Mismatches, yes, VarSet, Functor,
+		report_mismatched_args(SimpleMismatches, yes, VarSet, Functor,
 			Context)
 	;
+		% XXX If we can compute AllMismatches, then we should use it
+		% to report which arguments are OK, and which are suspect.
 
 		{ convert_args_type_assign_set(ArgsTypeAssignSet,
 			TypeAssignSet) },
@@ -4877,9 +4896,15 @@
 	).
 
 :- type mismatch_info
-	--->	mismatch(
+	--->	mismatch_info(
 			int,		% argument number, starting from 1
 			prog_var,	% variable in that position
+			list(type_mismatch)
+					% list of possible type mismatches
+		).
+
+:- type type_mismatch
+	--->	type_mismatch(
 			type,		% actual type of that variable
 			type,		% expected type of that variable
 			tvarset,	% the type vars in the expected
@@ -4888,15 +4913,43 @@
 		).
 
 :- pred find_mismatched_args(assoc_list(prog_var, type)::in,
-	type_assign_set::in, int::in, list(mismatch_info)::out) is semidet.
+	type_assign_set::in, int::in, list(mismatch_info)::out,
+	list(mismatch_info)::out, list(mismatch_info)::out) is det.
 
-find_mismatched_args([], _, _, []).
+find_mismatched_args([], _, _, [], [], []).
 find_mismatched_args([Arg - ExpType | ArgExpTypes], TypeAssignSet, ArgNum0,
-		Mismatched) :-
+		SimpleMismatches, ComplexMismatches, AllMismatches) :-
 	ArgNum1 = ArgNum0 + 1,
-	find_mismatched_args(ArgExpTypes, TypeAssignSet, ArgNum1, Mismatched1),
+	find_mismatched_args(ArgExpTypes, TypeAssignSet, ArgNum1,
+		SimpleMismatchesTail, ComplexMismatchesTail,
+		AllMismatchesTail),
 	get_type_stuff(TypeAssignSet, Arg, TypeStuffList),
-	TypeStuffList = [TypeStuff],
+	list__filter_map(substitute_types_check_match(ExpType), TypeStuffList,
+		TypeMismatches0),
+	list__sort_and_remove_dups(TypeMismatches0, TypeMismatches),
+	(
+		TypeMismatches = [],
+		SimpleMismatches = SimpleMismatchesTail,
+		ComplexMismatches = ComplexMismatchesTail,
+		AllMismatches = AllMismatchesTail
+	;
+		TypeMismatches = [_],
+		Mismatch = mismatch_info(ArgNum0, Arg, TypeMismatches),
+		SimpleMismatches = [Mismatch | SimpleMismatchesTail],
+		ComplexMismatches = ComplexMismatchesTail,
+		AllMismatches = [Mismatch | AllMismatchesTail]
+	;
+		TypeMismatches = [_, _ | _],
+		Mismatch = mismatch_info(ArgNum0, Arg, TypeMismatches),
+		SimpleMismatches = SimpleMismatchesTail,
+		ComplexMismatches = [Mismatch | ComplexMismatchesTail],
+		AllMismatches = [Mismatch | AllMismatchesTail]
+	).
+
+:- pred substitute_types_check_match((type)::in, type_stuff::in,
+	type_mismatch::out) is semidet.
+
+substitute_types_check_match(ExpType, TypeStuff, TypeMismatch) :-
 	TypeStuff = type_stuff(ArgType, TVarSet, TypeBindings, HeadTypeParams),
 	term__apply_rec_substitution(ArgType, TypeBindings, FullArgType),
 	term__apply_rec_substitution(ExpType, TypeBindings, FullExpType),
@@ -4911,10 +4964,10 @@
 			FullArgType = term__functor(term__atom("<any>"), [], _)
 		)
 	->
-		Mismatched = Mismatched1
+		fail
 	;
-		Mismatched = [mismatch(ArgNum0, Arg, FullArgType, FullExpType,
-			TVarSet, HeadTypeParams) | Mismatched1]
+		TypeMismatch = type_mismatch(FullArgType, FullExpType,
+			TVarSet, HeadTypeParams)
 	).
 
 :- pred report_mismatched_args(list(mismatch_info)::in, bool::in,
@@ -4923,8 +4976,13 @@
 report_mismatched_args([], _, _, _, _) --> [].
 report_mismatched_args([Mismatch | Mismatches], First, VarSet, Functor,
 		Context) -->
-	{ Mismatch = mismatch(ArgNum, Var, ActType, ExpType, TVarSet,
-		HeadTypeParams) },
+	{ Mismatch = mismatch_info(ArgNum, Var, TypeMismatches) },
+	{ TypeMismatches = [TypeMismatch] ->
+		TypeMismatch = type_mismatch(ActType, ExpType, TVarSet,
+			HeadTypeParams)
+	;
+		error("report_mismatched_args: more than one type mismatch")
+	},
 	prog_out__write_context(Context),
 	(
 		% Handle higher-order syntax such as ''(F, A) specially:
@@ -5000,13 +5058,13 @@
 :- pred write_functor_name(cons_id::in, int::in, io::di, io::uo) is det.
 
 write_functor_name(Functor, Arity) -->
-	{ strip_builtin_qualifier_from_cons_id(Functor, Functor1) },
+	{ strip_builtin_qualifier_from_cons_id(Functor, StrippedFunctor) },
 	( { Arity = 0 } ->
 		io__write_string("constant `"),
 		( { Functor = cons(Name, _) } ->
 			prog_out__write_sym_name(Name)
 		;
-			hlds_out__write_cons_id(Functor1)
+			hlds_out__write_cons_id(StrippedFunctor)
 		),
 		io__write_string("'")
 	; { Functor = cons(unqualified(""), _) } ->
@@ -5015,7 +5073,7 @@
 		io__write_string(")")
 	;
 		io__write_string("functor `"),
-		hlds_out__write_cons_id(Functor1),
+		hlds_out__write_cons_id(StrippedFunctor),
 		io__write_string("'")
 	).
 
@@ -5863,8 +5921,8 @@
 		;
 			io__write_string("  error: undefined symbol `"),
 			{ strip_builtin_qualifier_from_cons_id(Functor,
-				Functor1) },
-			hlds_out__write_cons_id(Functor1),
+				StrippedFunctor) },
+			hlds_out__write_cons_id(StrippedFunctor),
 			io__write_string("'"),
 			(
 				{ Functor = cons(Constructor, _) },
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list