[m-rev.] diff: fix error message for instances

Mark Brown mark at cs.mu.OZ.AU
Wed Aug 3 21:11:59 AEST 2005


This is in response to the problem reported by Peter Schachte.

Cheers,
Mark.

Estimated hours taken: 0.2
Branches: main, release

compiler/prog_io_typeclass.m:
	Report a more useful error if the arguments of an instance are not
	in the required form.  The error term is now the entire head of the
	instance, which has a meaningful context.

tests/invalid/Mmakefile:
tests/invalid/bad_instance.err_exp:
tests/invalid/bad_instance.m:
	Test case.

Index: compiler/prog_io_typeclass.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/prog_io_typeclass.m,v
retrieving revision 1.38
diff -u -r1.38 prog_io_typeclass.m
--- compiler/prog_io_typeclass.m	20 Apr 2005 12:57:15 -0000	1.38
+++ compiler/prog_io_typeclass.m	3 Aug 2005 10:35:00 -0000
@@ -605,55 +605,54 @@
 		MaybeClassName),
 	(
 		MaybeClassName = ok(ClassName, TermTypes0),
-			% check that the type in the name of the instance
-			% decl is a functor with vars as args
 		list__map(convert_type, TermTypes0, TermTypes),
-		IsFunctorAndVarArgs = (pred(Type::in) is semidet :-
-				% Is the top level functor an atom?
-			Type = term__functor(term__atom(Functor),
-					Args, _),
-			(
-				(	Functor = ":"
-				;	Functor = "."
-				)
-			->
-				Args = [_Module, Type1],
-					% Is the top level functor an
-					% atom?
-				Type1 = term__functor(term__atom(_), Args1, _),
-					% Are all the args of the
-					% functor variables?
-				list__map((pred(A::in, B::out) is semidet :-
-					prog_type__var(A, B)
-				), Args1, _)
-			;
-					% Are all the args of the
-					% functor variables?
-				list__map((pred(A::in, B::out) is semidet :-
-					prog_type__var(A, B)
-				), Args, _)
-			)
-		),
-		list__filter(IsFunctorAndVarArgs, TermTypes, _,
-			ErroneousTypes),
+
+			% Check that each type in the arguments of the instance
+			% decl is a functor with vars as args.
+			%
 		(
-			ErroneousTypes = [],
+			some [Type] (
+				list__member(Type, TermTypes),
+				\+ type_is_functor_and_vars(Type)
+			)
+		->
+				% We report the error as being in the name
+				% rather than the specific argument, since
+				% the argument types have had their contexts
+				% removed.
+				%
+			Result = error("types in instance declarations" ++
+				" must be functors with distinct variables" ++
+				" as arguments", Name)
+		;
 			Result = ok(instance([], ClassName,
 				TermTypes, abstract, TVarSet, ModuleName))
-		;
-				% XXX We should report an error for _each_
-				% XXX erroneous type
-			ErroneousTypes = [E0|_Es],
-			term__coerce(E0, E),
-			Result = error("expected type in " ++
-				"instance declaration to be " ++
-				"a functor with variables as args", E)
 		)
 	;
 		MaybeClassName = error(String, Term),
 		Result = error(String, Term)
 	).
 
+:- pred type_is_functor_and_vars((type)::in) is semidet.
+
+type_is_functor_and_vars(Type) :-
+		% Is the top level functor an atom?
+	Type = term__functor(term__atom(Functor), Args, _),
+	(
+		(	Functor = ":"
+		;	Functor = "."
+		)
+	->
+		Args = [_Module, Type1],
+		type_is_functor_and_vars(Type1)
+	;
+		% Are all the args of the functor variables?
+		all [Arg] (
+			list__member(Arg, Args) =>
+			prog_type__var(Arg, _)
+		)
+	).
+
 :- pred parse_non_empty_instance(module_name::in, term::in, term::in,
 	varset::in, tvarset::in, maybe1(item)::out) is det.
 
Index: tests/invalid/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/Mmakefile,v
retrieving revision 1.169
diff -u -r1.169 Mmakefile
--- tests/invalid/Mmakefile	13 May 2005 02:24:16 -0000	1.169
+++ tests/invalid/Mmakefile	3 Aug 2005 10:41:21 -0000
@@ -37,6 +37,7 @@
 	any_mode \
 	any_should_not_match_bound \
 	assert_in_interface \
+	bad_instance \
 	bigtest \
 	bind_in_negated \
 	bind_var_errors \
Index: tests/invalid/bad_instance.err_exp
===================================================================
RCS file: tests/invalid/bad_instance.err_exp
diff -N tests/invalid/bad_instance.err_exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/invalid/bad_instance.err_exp	3 Aug 2005 10:43:55 -0000
@@ -0,0 +1,3 @@
+bad_instance.m:005: Error: types in instance declarations must be functors with distinct variables as arguments: foo(bar(_1), _2).
+bad_instance.m:011: Error: types in instance declarations must be functors with distinct variables as arguments: foo(bar(_1), _1).
+For more information, try recompiling with `-E'.
Index: tests/invalid/bad_instance.m
===================================================================
RCS file: tests/invalid/bad_instance.m
diff -N tests/invalid/bad_instance.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/invalid/bad_instance.m	3 Aug 2005 10:43:39 -0000
@@ -0,0 +1,12 @@
+:- module bad_instance.
+:- interface.
+
+:- typeclass foo(A, B) where [].
+:- instance foo(bar(T), U).
+
+:- implementation.
+
+:- type bar(T) ---> bar(T).
+
+:- instance foo(bar(T), T) where [].
+
--------------------------------------------------------------------------
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