diff: prog_io.m error detection fixes

Fergus Henderson fjh at cs.mu.oz.au
Thu Jul 24 07:02:06 AEST 1997


Fix some bugs where the compiler was silently ignoring certain errors.

compiler/prog_io.m:
	Check for some possible errors in function declarations that
	were previously being silently ignored.  Specifically, we
	now catch cases where a function type declaration includes
	modes for some but not all of its arguments or return value,
	or where it includes a determinism but no modes.

tests/invalid/Mmake:
tests/invalid/func_errors.m:
tests/invalid/func_errors.err_exp:
	Test case for the above change.

Index: prog_io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/prog_io.m,v
retrieving revision 1.157
diff -u -r1.157 prog_io.m
--- prog_io.m	1997/07/08 05:49:44	1.157
+++ prog_io.m	1997/07/23 20:49:35
@@ -1088,9 +1088,35 @@
 process_func_2(ok(F, As0), FuncTerm, ReturnTypeTerm, VarSet, MaybeDet, Cond,
 		Result) :-
 	( convert_type_and_mode_list(As0, As) ->
-		( convert_type_and_mode(ReturnTypeTerm, ReturnType) ->
-			Result = ok(func(VarSet, F, As, ReturnType, MaybeDet,
-					Cond))
+		( \+ verify_type_and_mode_list(As) ->
+			Result = error("some but not all arguments have modes",
+					FuncTerm)
+		; convert_type_and_mode(ReturnTypeTerm, ReturnType) ->
+			(
+				As = [type_and_mode(_, _) | _],
+				ReturnType = type_only(_)
+			->
+				Result = error(
+		"function arguments have modes, but function result doesn't",
+					FuncTerm)
+			;
+				As = [type_only(_) | _],
+				ReturnType = type_and_mode(_, _)
+			->
+				Result = error(
+		"function result has mode, but function arguments don't",
+					FuncTerm)
+			;
+				ReturnType = type_only(_),
+				MaybeDet = yes(_)
+			->
+				Result = error(
+"function declaration specifies a determinism but does not specify the mode",
+					FuncTerm)
+			;
+				Result = ok(func(VarSet, F, As, ReturnType,
+					MaybeDet, Cond))
+			)
 		;
 			Result = error(
 			"syntax error in return type of `:- func' declaration",


cvs diff: Diffing .
Index: Mmake
===================================================================
RCS file: /home/staff/zs/imp/tests/invalid/Mmake,v
retrieving revision 1.20
diff -u -r1.20 Mmake
--- Mmake	1997/07/23 15:36:49	1.20
+++ Mmake	1997/07/23 20:50:09
@@ -15,6 +15,7 @@
 	errors1.m \
 	errors2.m \
 	external.m \
+	func_errors.m \
 	funcs_as_preds.m \
 	ho_type_mode_bug.m \
 	inline_conflict.m \
Index: func_errors.err_exp
===================================================================
RCS file: func_errors.err_exp
diff -N func_errors.err_exp
--- /dev/null	Thu Jul 24 06:57:10 1997
+++ func_errors.err_exp	Thu Jul 24 06:58:45 1997
@@ -0,0 +1,6 @@
+func_errors.m:009: Error: function declaration specifies a determinism but does not specify the mode: foo(int, int).
+func_errors.m:010: Error: some but not all arguments have modes: bar(int :: in, int).
+func_errors.m:011: Error: function arguments have modes, but function result doesn't: baz(int :: in, int :: in).
+func_errors.m:012: Error: function result has mode, but function arguments don't: quux(int, int).
+func_errors.m:018: Error: some but not all arguments have modes: q(int :: in, int).
+For more information, try recompiling with `-E'.
Index: func_errors.m
===================================================================
RCS file: func_errors.m
diff -N func_errors.m
--- /dev/null	Thu Jul 24 06:57:10 1997
+++ func_errors.m	Thu Jul 24 06:54:21 1997
@@ -0,0 +1,32 @@
+:- module func_errors.
+
+:- interface.
+:- import_module int.
+
+% it is an error to declare determinism but not modes,
+% or to only declare some of the modes
+
+:- func foo(int, int) = int is semidet.
+:- func bar(int::in, int) = int is semidet.
+:- func baz(int::in, int::in) = int is semidet.
+:- func quux(int, int) = (int::out) is semidet.
+
+:- func ok(int::in, int::in) = (int::out) is semidet.
+
+:- pred p(int, int) is semidet.
+:- mode p(in, in) is semidet.
+:- pred q(int::in, int) is semidet.
+
+:- implementation.
+
+% foo(X, Y) = X + Y :- X > 0.
+% bar(X, Y) = X + Y :- X > 0.
+% baz(X, Y) = X + Y :- X > 0.
+% quux(X, Y) = X + Y :- X > 0.
+
+p(X, Y) :- X > Y.
+% q(X, Y) :- X > Y.
+
+ok(X, Y) = X + Y :- X > 0.
+
+

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list