[m-rev.] for review: initialisers and uncaught exceptions

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Feb 8 00:05:25 AEDT 2007


For review by anyone.
(Something similar needs to be done for finalisers but I'll do that
after this has been reviewed.)

Estimated hours taken: 1.5
Branches: main

Specify the behaviour of intialisers that terminate with
uncaught exceptions.

doc/reference_manual.texi.
 	Specify the behaviour of initialisers that terminate with an
 	uncaught exception.

 	Delete "Note that" in a spot.

tests/hard_coded/Mmakefile:
tests/hard_coded/init_excp.{m,exp}:
 	Test the behaviour of initialisers that throw exceptions.

Julien.

Index: doc/reference_manual.texi
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.380
diff -u -r1.380 reference_manual.texi
--- doc/reference_manual.texi	5 Feb 2007 03:12:52 -0000	1.380
+++ doc/reference_manual.texi	7 Feb 2007 13:02:17 -0000
@@ -4722,12 +4722,19 @@
  @var{Det} must be either @samp{det} or @samp{cc_multi}.

  The effect of the @samp{initialise} declaration is to ensure that
- at samp{initpredname/arity} is invoked before the program's @samp{main}
+ at samp{initpredname/arity} is invoked before the program's @samp{main/2}
  predicate.  Initialisation predicates within a module are executed in the
  order in which they are specified, although no order may be assumed between
  different modules or sub-modules.  Initialisation predicates are only invoked
  after any initialisation required by the Mercury standard library.

+If @samp{initpredname/arity} terminates with an uncaught exception then
+the program will immediately abort execution. 
+In this circumstance those predicates specified by other @samp{initialise}
+directives that have not yet been executed will not be executed,
+ at samp{main/2} will not be executed and no predicate specified in a
+ at samp{finalise} directive will be executed.
+
  @samp{initialize} is also allowed as a synonym for @samp{initialise}.

  Note: @samp{initialise} declarations are currently only supported on
@@ -4876,7 +4883,7 @@

  @end table

-Note that it is an error for a @samp{mutable} directive to appear in the
+It is an error for a @samp{mutable} directive to appear in the
  interface section of a module.  The usual visibility rules for sub-modules
  apply to the mutable variable access predicates.

Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.306
diff -u -r1.306 Mmakefile
--- tests/hard_coded/Mmakefile	2 Feb 2007 15:59:31 -0000	1.306
+++ tests/hard_coded/Mmakefile	7 Feb 2007 11:41:06 -0000
@@ -431,6 +431,7 @@
  		allow_stubs \
  		backend_external \
  		dir_test \
+		init_excp \
  		map_merge_test \
  		io_globals_deadlock \
  		test_array2d \
@@ -613,6 +614,17 @@
  		rm -f $@.tmp; \
  	fi

+# init_excp.out is expected to fail (it calls throw/1).
+#
+init_excp.out: init_excp
+	if ./init_excp > $@.tmp 2>&1; then \
+		grep  . $@.tmp; \
+		exit 1; \
+	else \
+		cat $@.tmp > $@; \
+		rm -f $@.tmp; \
+	fi
+
  # For the constant_prop_1 test case, we test that constant propagation
  # has been achieved by grepping the generated target code for particular
  # patterns that will only arise if the Mercury compiler did the intended
Index: tests/hard_coded/init_excp.exp
===================================================================
RCS file: tests/hard_coded/init_excp.exp
diff -N tests/hard_coded/init_excp.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/init_excp.exp	7 Feb 2007 11:34:09 -0000
@@ -0,0 +1,4 @@
+This is init_pred_a/2
+EXCEPTION
+Uncaught Mercury exception:
+magic_number_exception
Index: tests/hard_coded/init_excp.m
===================================================================
RCS file: tests/hard_coded/init_excp.m
diff -N tests/hard_coded/init_excp.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/init_excp.m	7 Feb 2007 12:44:11 -0000
@@ -0,0 +1,72 @@
+% Check that initialisers that terminate with an uncaught exception
+% cause the program to abort rather than keep running.
+%
+:- module init_excp.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module exception.
+
+:- initialise init_pred_a/2.	% Should be called first and succeed.
+:- initialise init_pred_b/2.	% Should be called second and abort.
+:- initialise init_pred_c/2.	% Should *not* be called.
+
+   % Catching an exception within an initialiser is okay.
+   %
+:- pred init_pred_a(io::di, io::uo) is cc_multi.
+
+init_pred_a(!IO) :-
+	io.write_string("This is init_pred_a/2\n", !IO),
+	try(get_string(561), Result),
+	(
+		Result = succeeded(_),
+		io.write_string("SUCCEEDED\n", !IO)
+	;
+		Result = exception(_),
+		io.write_string("EXCEPTION\n", !IO)
+	),
+	io.flush_output(!IO).
+
+:- pred get_string(int::in, string::out) is det.
+
+get_string(X, Str) :-
+	( X = 561 ->
+		throw(magic_number_exception)
+	;
+		Str = "not 561"
+	). 
+
+   % Not catching one should cause the program to abort.
+   %
+:- pred init_pred_b(io::di, io::uo) is det.
+
+init_pred_b(!IO) :- 
+	get_magic_number(X, !IO),
+	( X = 3 ->
+		throw(magic_number_exception)
+	;
+		io.write_string("This is init_pred_b/2\n", !IO)
+	).
+
+:- type magic_number_exception ---> magic_number_exception.
+
+:- pred get_magic_number(int::out, io::di, io::uo) is det.
+
+get_magic_number(3, !IO).
+
+   % Subsequent initialisers should not be run if this happens.
+   %
+:- pred init_pred_c(io::di, io::uo) is det.
+
+init_pred_c(!IO) :-
+	io.write_string("I should not be running.\n", !IO). 
+
+   % Nor should main/2.
+   %
+main(!IO) :-
+	io.write_string("This is main/2\n", !IO).

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list