[m-dev.] for review: bug fix for typeclasses

Simon Taylor stayl at cs.mu.OZ.AU
Mon Jan 15 17:15:36 AEDT 2001


David Jeffery wrote:
> On 14-Jan-2001, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> > 
> > Estimated hours taken: 6
> > 
> > Fix a bug which caused some programs using typeclasses to
> > crash with a segmentation fault.
> 
> Just a couple of comments below.

Estimated hours taken: 6

Fix a bug which caused some programs using typeclasses to
crash with a segmentation fault.

compiler/polymorphism.m:
	For typeclass method implementations, make sure the
	order of the type-info and typeclass-info arguments
	matches the order used by do_call_class_method.
	The type-infos for the unconstrained type variables in
	the instance declaration and the typeclass-infos for the
	constraints on the instance declaration must come
	before any other type-infos and typeclass-infos.

compiler/hlds_pred.m:
	Add a field to the pred_info type to record for each
	typeclass method implementation which class constraints 
	come from the `:- instance' declaration and which come
	from the predicate or function declaration in the
	`:- typeclass' declaration.

compiler/check_typeclass.m:
	Fill in the new field in the pred_info.

compiler/typecheck.m:
	Apply the necessary renamings to the types and
	class constraints in the new field in the pred_info.

tests/hard_coded/typeclasses/Mmakefile:
tests/hard_coded/typeclasses/typeclass_order_bug.{m,exp}
tests/hard_coded/typeclasses/typeclass_order_bug2.{m,exp}
tests/hard_coded/typeclasses/typeclass_order_bug3.{m,exp}
	Test cases.

--- compiler/polymorphism.m	2001/01/15 05:16:31	1.2
+++ compiler/polymorphism.m	2001/01/15 05:24:06
@@ -173,6 +173,16 @@
 %	then the ExistTypeClassInfos (for existentially quantified constraints)
 %	and finally the original arguments of the predicate.
 %
+% The convention for class method implementations is slightly different
+% to match the order that the type_infos and typeclass_infos are passed
+% in by do_call_class_method (in runtime/mercury_ho_call.c):
+%
+%	First the type_infos for the unconstrained type variables in
+% 		the instance declaration
+%	then the typeclass_infos for the class constraints on the
+%		instance declaration
+% 	then the remainder of the type_infos and typeclass_infos as above.
+%
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -694,7 +704,7 @@
 
 	%
 	% For class method implementations, do_call_class_method
-	% extras type-infos and typeclass-infos from the
+	% takes the type-infos and typeclass-infos from the
 	% typeclass-info and pastes them onto the front of
 	% the argument list. We need to match that order here.
 	%
--- tests/hard_coded/typeclasses/Mmakefile	2001/01/15 06:07:38	1.2
+++ tests/hard_coded/typeclasses/Mmakefile	2001/01/15 06:07:54
@@ -51,6 +51,7 @@
 	typeclass_exist_method_2 \
 	typeclass_order_bug \
 	typeclass_order_bug2 \
+	typeclass_order_bug3 \
 	typeclass_test_5 \
 	typeclass_test_6 \
 	type_spec \
Index: tests/hard_coded/typeclasses/typeclass_order_bug3.m
===================================================================
RCS file: tests/hard_coded/typeclasses/typeclass_order_bug3.m
diff -N tests/hard_coded/typeclasses/typeclass_order_bug3.m
--- /dev/null	Mon Jan 15 17:05:01 2001
+++ typeclass_order_bug3.m	Mon Jan 15 17:05:30 2001
@@ -0,0 +1,52 @@
+% This module tests class methods with type variables
+% which are not constrained in the head of the `:- typeclass'
+% declaration.
+%
+% The code generated by the compiler of 13/1/2001 for this test case
+% segfaults. The type_infos and typeclass_infos were being passed
+% in the wrong order.
+%
+:- module typeclass_order_bug3.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module int, list.
+
+:- typeclass class(T) where [
+        pred p(U::in, V::in, T::in,
+		io__state::di, io__state::uo) is det <= writeable(U)
+].
+
+:- typeclass writeable(T) where [
+	pred write_t(T::in, io__state::di, io__state::uo) is det
+].
+
+:- instance class(list(T)) <= class(T) where [
+        (p(U, V, List) -->
+		list__foldl(p(U, V), List),
+		write_t(U),
+		io__nl,
+		io__write(V),
+		io__nl
+	)
+].
+
+:- instance class(int) where [
+        (p(_, _, Int) -->
+        	io__write_int(Int + 1),
+        	io__nl
+	)
+].
+
+:- instance writeable(string) where [
+	write_t(String) --> io__write_string(String)
+].
+
+main -->
+        p("string", "string2", [1,2,3]).  
Index: tests/hard_coded/typeclasses/typeclass_order_bug3.exp
===================================================================
RCS file: tests/hard_coded/typeclasses/typeclass_order_bug3.exp
diff -N tests/hard_coded/typeclasses/typeclass_order_bug3.exp
--- /dev/null	Mon Jan 15 17:05:01 2001
+++ typeclass_order_bug3.exp	Mon Jan 15 17:06:47 2001
@@ -0,0 +1,5 @@
+2
+3
+4
+string
+"string2"
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list