diff: fix bug with import of nested module

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Sep 25 04:43:11 AEST 1998


--------------------

Estimated hours taken: 3.5

Fix a bug reported by Warwick Harvey <wharvey at cs.monash.edu.au>
where importing a nested module without first importing the
parent module resulted in a software error when building the
dependencies.

compiler/modules.m:
	Change a call to `map__det_insert' into `map__set',
	to avoid a `map__det_insert failed' error,
	and add some detailed comments explaining why.

tests/invalid/Mmakefile:
tests/invalid/sub_a.m:
tests/invalid/sub_b.m:
tests/invalid/sub_b.err_exp:
tests/invalid/sub_c.m:
tests/invalid/sub_c.err_exp:
	Add a couple of test cases.

Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.85
diff -u -r1.85 modules.m
--- modules.m	1998/09/03 11:27:23	1.85
+++ modules.m	1998/09/24 16:26:35
@@ -2732,12 +2732,44 @@
 		{ map__lookup(DepsMap, Module, deps(Done, ModuleImports)) }
 	).
 
+	%
+	% insert_into_deps_map/3:
+	%
+	% Insert a new entry into the deps_map.
+	% If the module already occured in the deps_map, then we just
+	% replace the old entry (presumed to be a dummy entry) with the
+	% new one.
+	%
+	% This can only occur for sub-modules which have
+	% been imported before their parent module was imported:
+	% before reading a module and inserting it into the
+	% deps map, we check if it was already there, but
+	% when we read in the module, we try to insert not just
+	% that module but also all the nested sub-modules inside
+	% that module.  If a sub-module was previously imported,
+	% then it may already have an entry in the deps_map.
+	% However, unless the sub-module is defined both as a
+	% separate sub-module and also as a nested sub-module,
+	% the previous entry will be a dummy entry that we inserted
+	% after trying to read the source file and failing.
+	%
+	% XXX We could make some effort here to catch the case where a
+	% module is defined as both a separate sub-module and also
+	% as a nested sub-module.  However, that doesn't seem worthwhile,
+	% since not all such cases would arrive here anyway --
+	% it would be nice to catch that case but this is not the
+	% place to catch it.
+	% (Currently for that case we just ignore the file containing the
+	% separate sub-module.  Since we don't consider the
+	% file containing the separate sub-module to be part of the
+	% program's source, there's no duplicate definition, and thus
+	% no requirement to report any error message.)
+	%
 :- pred insert_into_deps_map(module_imports, deps_map, deps_map).
 :- mode insert_into_deps_map(in, in, out) is det.
 insert_into_deps_map(ModuleImports, DepsMap0, DepsMap) :-
 	module_imports_get_module_name(ModuleImports, ModuleName),
-	map__det_insert(DepsMap0, ModuleName, deps(no, ModuleImports),
-		DepsMap).
+	map__set(DepsMap0, ModuleName, deps(no, ModuleImports), DepsMap).
 
 	% Read a module to determine the (direct) dependencies
 	% of that module and any nested sub-modules it contains.
Index: tests/invalid/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/Mmakefile,v
retrieving revision 1.24
diff -u -r1.24 Mmakefile
--- Mmakefile	1998/08/30 04:04:50	1.24
+++ Mmakefile	1998/09/24 18:28:07
@@ -39,6 +39,8 @@
 	prog_io_erroneous.m \
 	qual_basic_test2.m \
 	qualified_cons_id2.m \
+	sub_b.m \
+	sub_c.m \
 	test_nested.m \
 	type_inf_loop.m \
 	type_loop.m \
@@ -47,11 +49,11 @@
 	typeclass_test_2.m \
 	typeclass_test_3.m \
 	typeclass_test_4.m \
+	types.m	\
 	unbound_inst_var.m \
 	undef_lambda_mode.m \
 	undef_mode.m \
 	undef_type.m \
-	types.m	\
 	vars_in_wrong_places.m
 
 # we do not yet pass the following tests:
Index: tests/invalid/sub_a.m
===================================================================
RCS file: sub_a.m
diff -N sub_a.m
--- /dev/null	Fri Sep 25 04:05:00 1998
+++ sub_a.m	Fri Sep 25 04:26:48 1998
@@ -0,0 +1,31 @@
+
+:- module sub_a.
+
+:- interface.
+
+:- type t1.
+
+:- module sub1.
+
+:- interface.
+
+:- type t2.
+
+:- end_module sub1.
+
+:- implementation.
+
+:- module sub1.
+
+:- implementation.
+
+:- import_module int.
+
+:- type t2 == int.
+
+:- end_module sub1.
+
+:- import_module float.
+
+:- type t1 == float.
+
Index: tests/invalid/sub_b.err_exp
===================================================================
RCS file: sub_b.err_exp
diff -N sub_b.err_exp
--- /dev/null	Fri Sep 25 04:05:00 1998
+++ sub_b.err_exp	Fri Sep 25 04:33:38 1998
@@ -0,0 +1,4 @@
+sub_b.m:012: In module `sub_b':
+sub_b.m:012:   error in `import_module' declaration:
+sub_b.m:012:   module `sub_a:sub1' is inaccessible.
+For more information, try recompiling with `-E'.
Index: tests/invalid/sub_b.m
===================================================================
RCS file: sub_b.m
diff -N sub_b.m
--- /dev/null	Fri Sep 25 04:05:00 1998
+++ sub_b.m	Fri Sep 25 04:26:48 1998
@@ -0,0 +1,19 @@
+
+:- module sub_b.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module sub_a:sub1.
+:- import_module sub_a.
+
+main -->
+        io__write_string("Hello.\n").
+
+
+
Index: tests/invalid/sub_c.err_exp
===================================================================
RCS file: sub_c.err_exp
diff -N sub_c.err_exp
--- /dev/null	Fri Sep 25 04:05:00 1998
+++ sub_c.err_exp	Fri Sep 25 04:38:00 1998
@@ -0,0 +1,18 @@
+
+:- module sub_c.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module sub_a:sub1.
+
+main -->
+        io__write_string("Hello.\n").
+
+
+
Index: tests/invalid/sub_c.m
===================================================================
RCS file: sub_c.m
diff -N sub_c.m
--- /dev/null	Fri Sep 25 04:05:00 1998
+++ sub_c.m	Fri Sep 25 04:26:48 1998
@@ -0,0 +1,18 @@
+
+:- module sub_c.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module sub_a:sub1.
+
+main -->
+        io__write_string("Hello.\n").
+
+
+

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