diff: fix `end_module' bug

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Dec 7 14:11:20 AEDT 1998


Estimated hours taken: 0.75

Fix a bug that was reported by Warwick Harvey <wharvey at cs.monash.edu.au>
regarding the handling of `end_module' declarations for nested modules.

compiler/prog_io.m:
	Don't complain if the module name in the final `end_module'
	declaration doesn't match the expected name for the top-level
	module, because it could be for a nested module.

compiler/make_hlds.m:
	Report errors for unexpected `module' and `end_module' declarations.
	This will catch the cases that used to be caught by prog_io.m,
	among others.

Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.277
diff -u -r1.277 make_hlds.m
--- make_hlds.m	1998/11/10 22:23:01	1.277
+++ make_hlds.m	1998/11/24 17:52:34
@@ -257,6 +257,14 @@
 			report_warning("Warning: `external' declaration requires arity.\n"),
 			io__set_output_stream(OldStream, _)
 		)
+	; { ModuleDefn = module(_ModuleName) } ->
+		report_unexpected_decl("module", Context),
+		{ Status = Status0 },
+		{ Module = Module0 }
+	; { ModuleDefn = end_module(_ModuleName) } ->
+		report_unexpected_decl("end_module", Context),
+		{ Status = Status0 },
+		{ Module = Module0 }
 	;
 		{ Status = Status0 },
 		{ Module = Module0 },
@@ -4441,6 +4449,17 @@
 %-----------------------------------------------------------------------------%
 
 	% Predicates to write out the different warning and error messages.
+
+:- pred report_unexpected_decl(string, term__context,
+				io__state, io__state).
+:- mode report_unexpected_decl(in, in, di, uo) is det.
+
+report_unexpected_decl(Descr, Context) -->
+	io__set_exit_status(1),
+	prog_out__write_context(Context),
+	io__write_string("Error: unexpected or incorrect `"),
+	io__write_string(Descr),
+	io__write_string("' declaration.\n").
 
 :- pred multiple_def_error(sym_name, int, string, term__context, term__context,
 				io__state, io__state).
Index: compiler/prog_io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/prog_io.m,v
retrieving revision 1.177
diff -u -r1.177 prog_io.m
--- prog_io.m	1998/10/29 14:58:40	1.177
+++ prog_io.m	1998/11/24 17:49:47
@@ -288,11 +288,18 @@
 
 :- type module_end ---> no ; yes(module_name, term__context).
 
-:- pred get_end_module(item_list, item_list, module_end).
-:- mode get_end_module(in, out, out) is det.
+:- pred get_end_module(item_list, module_name, item_list, module_end).
+:- mode get_end_module(in, in, out, out) is det.
 
-get_end_module(RevItems0, RevItems, EndModule) :-
+get_end_module(RevItems0, ModuleName, RevItems, EndModule) :-
 	(
+		%
+		% Note: if the module name in the end_module declaration
+		% does not match what we expect, given the source file name,
+		% then we assume that it is for a nested module, and so
+		% we leave it alone.  If it is not for a nested module,
+		% the error will be caught by make_hlds.m.
+		%
 		RevItems0 = [
 			module_defn(_VarSet, end_module(ModuleName)) - Context
 			    | RevItems1]
@@ -403,7 +410,7 @@
 	% check that it matches the initial module declaration (if any),
 	% and remove both of them from the final item list.
 	%
-	{ get_end_module(RevItems0, RevItems, EndModule) },
+	{ get_end_module(RevItems0, ModuleName, RevItems, EndModule) },
 	{ list__reverse(RevMessages, Messages0) },
 	{ list__reverse(RevItems, Items0) },
 	check_end_module(EndModule,

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh at 128.250.37.3        |     -- leaked Microsoft memo.



More information about the developers mailing list