Multiple variable warning bug.

Fergus Henderson fjh at cs.mu.oz.au
Mon Apr 21 16:49:24 AEST 1997


Hi Tyson,

Can you please review this one?

In mail to mercury-bugs at cs.mu.oz.au, Tyson Richard DOWD wrote:
> 
> For function 
> 	:- func type_of(T) = type_info.
> 
> with the body containing a two _Type variables as arguments... eg:
> :- pragma(c_code, type_of(_Type::unused) = (_Type::out) , "
> 
> 
> std_util.m:912: In clause for predicate `std_util:type_of/2':
> std_util.m:912:   Warning: variable `_Type' occurs more than once in this scope.
> 
> It should report ...
> 		In clause for function `std_util:type_of/1':
> 		...

Estimated hours taken: 1

compiler/make_hlds.m:
	Fix a few places where it was treating functions as if they
	were predicates in some of the error and warning messages.
	Also change a few of the messages slightly for consistency
	with other messages.

tests/warnings/Mmake:
tests/warnings/pragma_c_code_unused_var.m:
tests/warnings/pragma_c_code_unused_var.exp:
tests/warnings/singleton_test.m:
tests/warnings/singleton_test.exp:
	Add some test cases for singleton warnings in functions.
	Move the test in pragma_c_code_unused_var.m into
	singleton_test.m and remove pragma_c_code_unused_var.m.

Index: make_hlds.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/compiler/make_hlds.m,v
retrieving revision 1.227
diff -u -r1.227 make_hlds.m
--- make_hlds.m	1997/04/08 02:26:21	1.227
+++ make_hlds.m	1997/04/21 06:38:06
@@ -1512,9 +1512,9 @@
 	->
 		{ module_info_incr_errors(ModuleInfo0, ModuleInfo) },
 		prog_out__write_context(Context),
-		io__write_string("Error: clause for predicate `"),
-		hlds_out__write_pred_call_id(PredName/Arity),
-		io__write_string("'\n"),
+		io__write_string("Error: clause for "),
+		hlds_out__write_call_id(PredOrFunc, PredName/Arity),
+		io__write_string("\n"),
 		prog_out__write_context(Context),
 		io__write_string("  with `:- pragma c_code' declaration preceding.\n"),
 		{ Info = Info0 }
@@ -1556,7 +1556,8 @@
 		},
 		( { Status \= opt_imported } ->
 			% warn about singleton variables 
-			maybe_warn_singletons(VarSet, PredName/Arity, Goal),
+			maybe_warn_singletons(VarSet,
+				PredOrFunc - PredName/Arity, Goal),
 			% warn about variables with overlapping scopes
 			maybe_warn_overlap(Warnings, VarSet, PredOrFunc,
 						PredName/Arity)
@@ -1625,7 +1626,7 @@
 		{ PredicateTable1 = PredicateTable0 }
 	;
 		maybe_undefined_pred_error(PredName, Arity, PredOrFunc,
-			Context, "pragma (c_code)"),
+			Context, "`:- pragma c_code' declaration"),
 		{ preds_add_implicit(PredicateTable0,
 				ModuleName, PredName, Arity, Context,
 				PredOrFunc, PredId, PredicateTable1) }
@@ -1685,7 +1686,8 @@
 				PredicateTable) },
 			{ module_info_set_predicate_table(ModuleInfo0, 
 				PredicateTable, ModuleInfo) },
-			maybe_warn_singletons(VarSet, PredName/Arity, Goal)
+			maybe_warn_singletons(VarSet,
+				PredOrFunc - PredName/Arity, Goal)
 		;
 			{ module_info_incr_errors(ModuleInfo0, ModuleInfo) }, 
 			io__stderr_stream(StdErr),
@@ -1825,11 +1827,11 @@
 	io__write_string(StdErr, ":\n"),
 	prog_out__write_context(Context),
 	( { Vars = [Var] } ->
-		io__write_string(StdErr, "  Warning: variable `"),
+		io__write_string(StdErr, "  warning: variable `"),
 		mercury_output_var(Var, VarSet, no),
 		report_warning(StdErr, "' has overlapping scopes.\n")
 	;
-		io__write_string(StdErr, "  Warning: variables `"),
+		io__write_string(StdErr, "  warning: variables `"),
 		mercury_output_vars(Vars, VarSet, no),
 		report_warning(StdErr, "'\n"),
 		prog_out__write_context(Context),
@@ -1840,11 +1842,13 @@
 
 %-----------------------------------------------------------------------------%
 
+:- type pred_or_func_call_id == pair(pred_or_func, pred_call_id).
+
 	% Warn about variables which occur only once but don't start with
 	% an underscore, or about variables which do start with an underscore
 	% but occur more than once.
 	%
-:- pred maybe_warn_singletons(varset, pred_call_id, hlds_goal,
+:- pred maybe_warn_singletons(varset, pred_or_func_call_id, hlds_goal,
 				io__state, io__state).
 :- mode maybe_warn_singletons(in, in, in, di, uo) is det.
 
@@ -1857,8 +1861,8 @@
 		[]
 	).
 
-:- pred warn_singletons_in_goal(hlds_goal, set(var), varset, pred_call_id,
-				io__state, io__state).
+:- pred warn_singletons_in_goal(hlds_goal, set(var), varset,
+			pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons_in_goal(in, in, in, in, di, uo) is det.
 
 warn_singletons_in_goal(Goal - GoalInfo, QuantVars, VarSet, PredCallId) -->
@@ -1866,7 +1870,8 @@
 		PredCallId).
 
 :- pred warn_singletons_in_goal_2(hlds_goal_expr, hlds_goal_info, set(var),
-				varset, pred_call_id, io__state, io__state).
+				varset, pred_or_func_call_id,
+				io__state, io__state).
 :- mode warn_singletons_in_goal_2(in, in, in, in, in, di, uo) is det.
 
 warn_singletons_in_goal_2(conj(Goals), _GoalInfo, QuantVars, VarSet,
@@ -1881,8 +1886,8 @@
 			_GoalInfo, QuantVars, VarSet, PredCallId) -->
 	warn_singletons_in_cases(Cases, QuantVars, VarSet, PredCallId).
 
-warn_singletons_in_goal_2(not(Goal), _GoalInfo, QuantVars, VarSet, PredCallId)
-		-->
+warn_singletons_in_goal_2(not(Goal), _GoalInfo, QuantVars, VarSet,
+		PredCallId) -->
 	warn_singletons_in_goal(Goal, QuantVars, VarSet, PredCallId).
 
 warn_singletons_in_goal_2(some(Vars, SubGoal), GoalInfo, QuantVars, VarSet,
@@ -1951,7 +1956,7 @@
 		PredCallId).
 
 :- pred warn_singletons_in_goal_list(list(hlds_goal), set(var), varset,
-				pred_call_id, io__state, io__state).
+				pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons_in_goal_list(in, in, in, in, di, uo) is det.
 
 warn_singletons_in_goal_list([], _, _, _) --> [].
@@ -1959,8 +1964,8 @@
 	warn_singletons_in_goal(Goal, QuantVars, VarSet, CallPredId),
 	warn_singletons_in_goal_list(Goals, QuantVars, VarSet, CallPredId).
 
-:- pred warn_singletons_in_cases(list(case), set(var), varset, pred_call_id,
-					io__state, io__state).
+:- pred warn_singletons_in_cases(list(case), set(var), varset,
+				pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons_in_cases(in, in, in, in, di, uo) is det.
 
 warn_singletons_in_cases([], _, _, _) --> [].
@@ -1970,7 +1975,7 @@
 	warn_singletons_in_cases(Cases, QuantVars, VarSet, CallPredId).
 
 :- pred warn_singletons_in_unify(var, unify_rhs, hlds_goal_info, set(var),
-			varset, pred_call_id, io__state, io__state).
+			varset, pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons_in_unify(in, in, in, in, in, in, di, uo) is det.
 
 warn_singletons_in_unify(X, var(Y), GoalInfo, QuantVars, VarSet, CallPredId) -->
@@ -2015,11 +2020,11 @@
 	% warn_singletons_in_pragma_c_code checks to see if each variable is
 	% a substring of the given c code. If not, it gives a warning
 :- pred warn_singletons_in_pragma_c_code(string, list(maybe(string)),
-	term__context, pred_call_id, io__state, io__state).
+	term__context, pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons_in_pragma_c_code(in, in, in, in, di, uo) is det.
 
 warn_singletons_in_pragma_c_code(C_Code, ArgNames, 
-		Context, PredCallId) -->
+		Context, PredOrFunc - PredCallId) -->
 	{ c_code_to_name_list(C_Code, C_CodeList) },
 	{ warn_singletons_in_pragma_c_code_2(C_CodeList, ArgNames,
 		Context, SingletonVars) },
@@ -2029,19 +2034,19 @@
 		io__stderr_stream(StdErr),
 		io__set_output_stream(StdErr, OldStream),
 		prog_out__write_context(Context),
+		io__write_string("In `:- pragma c_code' for "),
+		hlds_out__write_call_id(PredOrFunc, PredCallId),
+		io__write_string(":\n"),
+		prog_out__write_context(Context),
 		( { SingletonVars = [_] } ->
-			io__write_string("Warning: variable `"),
+			io__write_string("  warning: variable `"),
 			write_string_list(SingletonVars),
-			io__write_string("' does not occur in C code\n")
+			io__write_string("' does not occur in the C code.\n")
 		;
-			io__write_string("Warning: variables `"),
+			io__write_string("  warning: variables `"),
 			write_string_list(SingletonVars),
-			io__write_string("' do not occur in C code\n")
+			io__write_string("' do not occur in the C code.\n")
 		),
-		prog_out__write_context(Context),
-		io__write_string("  in `:- pragma c_code' for "),
-		hlds_out__write_call_id(predicate, PredCallId),
-		io__write_string(".\n"),
 		io__set_output_stream(OldStream, _)
 	).
 
@@ -2069,6 +2074,7 @@
 	;
 		SingletonVars = SingletonVars0
 	).
+
 %-----------------------------------------------------------------------------%
 
 	% c_code_to_name_list(Code, List) is true iff List is a list of the 
@@ -2155,10 +2161,11 @@
 	%	in Vars do occur in NonLocals.
 
 :- pred warn_singletons(list(var), set(var), set(var), varset, term__context,
-			pred_call_id, io__state, io__state).
+			pred_or_func_call_id, io__state, io__state).
 :- mode warn_singletons(in, in, in, in, in, in, di, uo) is det.
 
-warn_singletons(GoalVars, NonLocals, QuantVars, VarSet, Context, PredCallId) -->
+warn_singletons(GoalVars, NonLocals, QuantVars, VarSet, Context,
+		PredOrFunc - CallId) -->
 	io__stderr_stream(StdErr),
 
 	% find all the variables in the goal that don't occur outside the
@@ -2184,16 +2191,16 @@
 		[]
 	;
 		prog_out__write_context(Context),
-		io__write_string(StdErr, "In clause for predicate `"),
-		hlds_out__write_pred_call_id(PredCallId),
-		io__write_string(StdErr, "':\n"),
+		io__write_string(StdErr, "In clause for "),
+		hlds_out__write_call_id(PredOrFunc, CallId),
+		io__write_string(StdErr, ":\n"),
 		prog_out__write_context(Context),
 		( { SingletonVars = [_] } ->
-			io__write_string(StdErr, "  Warning: variable `"),
+			io__write_string(StdErr, "  warning: variable `"),
 			mercury_output_vars(SingletonVars, VarSet, no),
 			report_warning(StdErr, "' occurs only once in this scope.\n")
 		;
-			io__write_string(StdErr, "  Warning: variables `"),
+			io__write_string(StdErr, "  warning: variables `"),
 			mercury_output_vars(SingletonVars, VarSet, no),
 			report_warning(StdErr, "' occur only once in this scope.\n")
 		)
@@ -2216,16 +2223,16 @@
 		[]
 	;
 		prog_out__write_context(Context),
-		io__write_string(StdErr, "In clause for predicate `"),
-		hlds_out__write_pred_call_id(PredCallId),
-		io__write_string(StdErr, "':\n"),
+		io__write_string(StdErr, "In clause for "),
+		hlds_out__write_call_id(PredOrFunc, CallId),
+		io__write_string(StdErr, ":\n"),
 		prog_out__write_context(Context),
 		( { MultiVars = [_] } ->
-			io__write_string(StdErr, "  Warning: variable `"),
+			io__write_string(StdErr, "  warning: variable `"),
 			mercury_output_vars(MultiVars, VarSet, no),
 			report_warning(StdErr, "' occurs more than once in this scope.\n")
 		;
-			io__write_string(StdErr, "  Warning: variables `"),
+			io__write_string(StdErr, "  warning: variables `"),
 			mercury_output_vars(MultiVars, VarSet, no),
 			report_warning(StdErr, "' occur more than once in this scope.\n")
 		)
cvs diff: Diffing tests/warnings
Index: Mmake
===================================================================
RCS file: /home/staff/zs/imp/tests/warnings/Mmake,v
retrieving revision 1.15
diff -u -r1.15 Mmake
--- Mmake	1997/03/21 01:26:21	1.15
+++ Mmake	1997/04/21 06:25:49
@@ -4,7 +4,7 @@
 
 #-----------------------------------------------------------------------------#
 
-PROGS=	pragma_c_code_unused_var unused_args_test singleton_test \
+PROGS=	unused_args_test singleton_test \
 	unused_import pragma_source_file missing_if double_underscore \
 	duplicate_call infinite_recursion
 
cvs diff: cannot find pragma_c_code_unused_var.exp
cvs diff: cannot find pragma_c_code_unused_var.m
Index: singleton_test.exp
===================================================================
RCS file: /home/staff/zs/imp/tests/warnings/singleton_test.exp,v
retrieving revision 1.2
diff -u -r1.2 singleton_test.exp
--- singleton_test.exp	1997/02/23 06:12:23	1.2
+++ singleton_test.exp	1997/04/21 06:47:41
@@ -1,2 +1,12 @@
-singleton_test.m:012: In clause for predicate `singleton_test:my_append/3':
-singleton_test.m:012:   Warning: variables `L1, L2' occur only once in this scope.
+singleton_test.m:022: In clause for predicate `singleton_test:my_append/3':
+singleton_test.m:022:   warning: variables `L1, L2' occur only once in this scope.
+singleton_test.m:026: In clause for function `singleton_test:my_append_func/2':
+singleton_test.m:026:   warning: variables `L1, L2' occur only once in this scope.
+singleton_test.m:027: In clause for function `singleton_test:my_append_func/2':
+singleton_test.m:027:   warning: variable `T' occurs only once in this scope.
+singleton_test.m:029: In `:- pragma c_code' for predicate `singleton_test:my_c_pred/3':
+singleton_test.m:029:   warning: variable `Y' does not occur in the C code.
+singleton_test.m:033: In `:- pragma c_code' for function `singleton_test:my_c_func/2':
+singleton_test.m:033:   warning: variable `X' does not occur in the C code.
+singleton_test.m:039: In `:- pragma c_code' for predicate `singleton_test:c_hello_world/3':
+singleton_test.m:039:   warning: variable `Msg' does not occur in the C code.
Index: singleton_test.m
===================================================================
RCS file: /home/staff/zs/imp/tests/warnings/singleton_test.m,v
retrieving revision 1.2
diff -u -r1.2 singleton_test.m
--- singleton_test.m	1997/02/23 06:12:24	1.2
+++ singleton_test.m	1997/04/21 06:45:51
@@ -1,14 +1,43 @@
 :- module singleton_test.
-:- import_module list, int.
-
 :- interface.
+:- import_module list, int, io.
 
 :- pred my_append(list(int), list(int), list(int)).
 :- mode my_append(in, in, out) is det.
 
+:- func my_append_func(list(int), list(int)) = list(int).
+:- mode my_append_func(in, in) = out is det.
+
+:- func my_c_func(int, int) = int.
+:- mode my_c_func(in, in) = out is det.
+
+:- pred my_c_pred(int, int, int).
+:- mode my_c_pred(in, in, out) is det.
+
+:- pred c_hello_world(string::in, io__state::di, io__state::uo) is det.
+
 :- implementation.
 
 my_append([], L, L) :-
 	L1 = L2.
 my_append([H | T], L, [H | NT]) :-
 	my_append(T, L, NT).
+
+my_append_func([], L) = L :- L1 = L2.
+my_append_func([H | T], L) = [H | my_append_func(L, L)].
+
+:- pragma c_code(my_c_pred(X::in, Y::in, Z::out), will_not_call_mercury, "
+	Z = 2 * X;
+").
+
+:- pragma c_code(my_c_func(X::in, Y::in) = (Z::out), will_not_call_mercury, "
+	Z = 2 * Y;
+").
+
+:- pragma c_header_code("#include <stdio.h>").
+
+:- pragma c_code(c_hello_world(Msg::in, IO0::di, IO::uo),
+		will_not_call_mercury, "
+	printf(""Hello, world"");
+	IO = IO0;
+").

-- 
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