[m-rev.] for review: will_not_throw_exception foreign proc attribute

Julien Fischer juliensf at cs.mu.OZ.AU
Mon Dec 6 17:37:40 AEDT 2004


On Sun, 5 Dec 2004, Julien Fischer wrote:
>
> On Fri, 3 Dec 2004, Zoltan Somogyi wrote:
>
> > On 03-Dec-2004, Julien Fischer <juliensf at cs.mu.OZ.AU> wrote:
> >
> >
> > > +	;	depends_on_mercury_calls.
> > > +			% Whether or not the foreign code throws an
> > > +			% exception depend on whether it makes alls back
> > > +			% to Mercury (all foreign procs that don't make
> > > +			% calls back to Mercury cannot cause an exception
> > > +			% to be thrown).
> >
> > They may, since throw itself may one day be implemented as a foreign proc.
> > However, that can be handled with a comment that any such predicate should
> > have a fake may_call_mercury annotation.
> >
> I think the problem here is that the documentation is inaccurate.
> `will_not_throw_exception' should not be applied to foreign procs
> that have determinism erroneous.  Erroneous foreign procs, whether
> they make calls back to mercury or not, should always be marked
> as throwing an exception.  I'll modify the exception analysis so
> that will_not_thorw_exception cannot be applied to erroneous preds.
>

Here is a modified log message and relative diff:

Estimated hours taken: 6
Branches: main

Add the foreign proc. attribute `will_not_throw_exception'.
This allows the user to promise the exception analysis that
foreign procs that do not have determinism erroneous and make
calls back to Mercury will not throw an exception.

The behaviour for erroneous foreign procs and those that do
not make calls back to Mercury is unchanged.

compiler/prog_data.m:
compiler/prog_io_pragma.m:
	Handle the new attribute.

compiler/exception_analysis.m:
	If the user has provided the `will_not_throw_exception'
	attribute on a foreign proc that makes calls back to
	Mercury then set then have the exception analysis
	take account of this information.

	Fix a typo.

compiler/det_analysis.m:
compiler/det_report.m:
	Emit an error if the `will_not_throw_exception'
	attribute is used on foreign procs. that have
	been declared erroneous.

doc/reference_manual.texi:
	Mention the new foreign proc attribute.

tests/term/Mmakefile:
tests/term/Mercury.options:
tests/term/promise_no_throw_exception.m:
tests/term/promise_no_throw_exception.trans_opt_exp:
tests/invalid/Mmakefile:
tests/invalid/erroneous_promise_throw.m:
tests/invalid/erroneous_proimse_throw.err_exp:
	Test cases for the above.

vim/syntax/mercury.vim:
	Highlight the annotation appropriately.


diff -u compiler/exception_analysis.m compiler/exception_analysis.m
--- compiler/exception_analysis.m	3 Dec 2004 06:24:28 -0000
+++ compiler/exception_analysis.m	6 Dec 2004 04:37:18 -0000
@@ -330,7 +330,11 @@
 		foreign_proc(Attributes, _, _, _, _, _), !Result) :-
 	( may_call_mercury(Attributes) = may_call_mercury ->
 		may_throw_exception(Attributes) = MayThrowException,
-		( MayThrowException = depends_on_mercury_calls ->
+		%
+		% We do not need to deal with erroneous predicates
+		% here because they will have already been processed.
+		%
+		( MayThrowException = default_exception_behaviour ->
 			!:Result = !.Result ^ status :=
 				may_throw(user_exception)
 		;
diff -u compiler/prog_data.m compiler/prog_data.m
--- compiler/prog_data.m	2 Dec 2004 15:27:53 -0000
+++ compiler/prog_data.m	6 Dec 2004 02:42:25 -0000
@@ -998,17 +998,16 @@

 :- type may_throw_exception
 	--->	will_not_throw_exception
-			% The foreign code makes calls back to Mercury
-			% but none of them will result in an exception
-			% being thrown.
-
-	;	depends_on_mercury_calls.
-			% Whether or not the foreign code throws an
-			% exception depend on whether it makes alls back
-			% to Mercury (all foreign procs that don't make
-			% calls back to Mercury cannot cause an exception
-			% to be thrown).
+			% The foreign code will not result in an
+			% exception being thrown.

+	;	default_exception_behaviour.
+			% If the foreign proc. is erroneous then
+			% mark it as throwing an exception.  Otherwise
+			% mark it as throwing an exception if it makes
+			% calls back to Mercury and not throwing an
+			% exception otherwise.
+
 :- type pragma_foreign_proc_extra_attribute
 	--->	max_stack_size(int).

@@ -1682,7 +1681,7 @@
 default_attributes(Language) =
 	attributes(Language, may_call_mercury, not_thread_safe,
 		not_tabled_for_io, impure, depends_on_mercury_calls,
-		depends_on_mercury_calls, no, no, []).
+		default_exception_behaviour, no, no, []).

 set_may_call_mercury(MayCallMercury, Attrs0, Attrs) :-
 	Attrs = Attrs0 ^ may_call_mercury := MayCallMercury.
@@ -1761,7 +1760,7 @@
 		Exceptions = will_not_throw_exception,
 		ExceptionsStrList = ["will_not_throw_exception"]
 	;
-		Exceptions = depends_on_mercury_calls,
+		Exceptions = default_exception_behaviour,
 		ExceptionsStrList = []
 	),
 	(
diff -u doc/reference_manual.texi doc/reference_manual.texi
--- doc/reference_manual.texi	3 Dec 2004 06:16:38 -0000
+++ doc/reference_manual.texi	6 Dec 2004 06:24:20 -0000
@@ -5769,9 +5769,9 @@
 @item @samp{will_not_throw_exception}
 This attribute promises that the given predicate or function will not
 make calls back to Mercury that may result in an exception being thrown.
-This attribute only applies to code that makes calls back to Mercury.
-It is ignored for code which is declared as not making calls back
-to Mercury via a @samp{will_not_call_mercury} attribute.
+It is an error to apply this attribute to procedures that have determinism
+erroneous.  This attribute is ignored for code that is declared as not
+making calls back to Mercury via the @samp{will_not_call_mercury} attribute.
 Note: Predicates or functions that have polymorphic arguments but
 do not explicitly throw an exception, via a call to exception.throw/1
 or require.error/1, may still throw exceptions because they may be
@@ -5784,5 +5784,4 @@
 @end table

-
 @c -----------------------------------------------------------------------

diff -u tests/term/Mercury.options tests/term/Mercury.options
--- tests/term/Mercury.options	3 Dec 2004 05:47:41 -0000
+++ tests/term/Mercury.options	3 Dec 2004 15:16:22 -0000
@@ -53,7 +53,7 @@
 MCFLAGS-pl8_4_2=--term-norm=simple
 MCFLAGS-pragma_non_term=--term-norm=simple
 MCFLAGS-pragma_term=--term-norm=simple
-MCFLAGS-promise_will_not_thorw=--analyse-exceptions
+MCFLAGS-promise_will_not_throw=--analyse-exceptions
 MCFLAGS-queens=--term-norm=simple
 MCFLAGS-quicksort=--term-norm=simple
 MCFLAGS-select=--term-norm=simple
only in patch2:
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/invalid/erroneous_throw_promise.m	6 Dec 2004 06:14:34 -0000
@@ -0,0 +1,23 @@
+:- module erroneous_throw_promise.
+
+:- interface.
+
+:- pred foo(T::in) is erroneous.
+
+:- pred bar(T::in) is erroneous.
+
+:- implementation.
+
+:- pragma foreign_proc("C",
+	foo(X::in),
+	[will_not_call_mercury, promise_pure, will_not_throw_exception],
+"
+	/* Do something with X */
+").
+
+:- pragma foreign_proc("C",
+	bar(X::in),
+	[may_call_mercury, promise_pure, will_not_throw_exception],
+"
+	/* Do something with X */
+").
only in patch2:
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/invalid/erroneous_throw_promise.err_exp	6 Dec 2004 06:15:33 -0000
@@ -0,0 +1,11 @@
+erroneous_throw_promise.m:007: `bar(in)'has determinism erroneous but also has
+erroneous_throw_promise.m:007:   foreign clauses that have a
+erroneous_throw_promise.m:007:   `will_not_throw_exception' attribute. This
+erroneous_throw_promise.m:007:   attribute cannot be applied to erroneous
+erroneous_throw_promise.m:007:   procedures.
+erroneous_throw_promise.m:005: `foo(in)'has determinism erroneous but also has
+erroneous_throw_promise.m:005:   foreign clauses that have a
+erroneous_throw_promise.m:005:   `will_not_throw_exception' attribute. This
+erroneous_throw_promise.m:005:   attribute cannot be applied to erroneous
+erroneous_throw_promise.m:005:   procedures.
+For more information, try recompiling with `-E'.
only in patch2:
--- tests/invalid/Mmakefile	27 Oct 2004 07:41:50 -0000	1.153
+++ tests/invalid/Mmakefile	6 Dec 2004 06:15:04 -0000
@@ -58,6 +58,7 @@
 	det_errors \
 	duplicate_modes \
 	duplicate_module_test \
+	erroneous_throw_promise \
 	errors \
 	errors1 \
 	errors2 \
only in patch2:
--- compiler/det_report.m	14 Jun 2004 04:16:01 -0000	1.90
+++ compiler/det_report.m	6 Dec 2004 05:15:21 -0000
@@ -60,7 +60,8 @@
 		;	par_conj_not_det(determinism, pred_id, proc_id,
 				hlds_goal_info, list(hlds_goal))
 		; 	pragma_c_code_without_det_decl(pred_id, proc_id)
-		;	has_io_state_but_not_det(pred_id, proc_id).
+		;	has_io_state_but_not_det(pred_id, proc_id)
+		;	will_not_throw_with_erroneous(pred_id, proc_id).

 :- type seen_call_id
 	--->	seen_call(pred_id, proc_id)
@@ -1059,6 +1060,7 @@
 det_msg_get_type(par_conj_not_det(_, _, _, _, _), error).
 det_msg_get_type(pragma_c_code_without_det_decl(_, _), error).
 det_msg_get_type(has_io_state_but_not_det(_, _), error).
+det_msg_get_type(will_not_throw_with_erroneous(_, _), error).

 det_msg_is_any_mode_msg(multidet_disj(_, _), all_modes).
 det_msg_is_any_mode_msg(det_disj(_, _), all_modes).
@@ -1082,6 +1084,7 @@
 det_msg_is_any_mode_msg(par_conj_not_det(_, _, _, _, _), any_mode).
 det_msg_is_any_mode_msg(pragma_c_code_without_det_decl(_, _), any_mode).
 det_msg_is_any_mode_msg(has_io_state_but_not_det(_, _), any_mode).
+det_msg_is_any_mode_msg(will_not_throw_with_erroneous(_, _), any_mode).

 :- pred det_report_msg(det_msg::in, module_info::in, io::di, io::uo) is det.

@@ -1378,6 +1381,19 @@
 	;
 		VerboseErrors = no
 	).
+det_report_msg(will_not_throw_with_erroneous(PredId, ProcId), ModuleInfo,
+		!IO) :-
+	module_info_pred_proc_info(ModuleInfo, PredId, ProcId, _, ProcInfo),
+	proc_info_context(ProcInfo, Context),
+	describe_one_proc_name_mode(ModuleInfo, should_not_module_qualify,
+		proc(PredId, ProcId), Desc),
+	Pieces = [words(Desc ++ "has determinism erroneous but also has"),
+		  words("foreign clauses that have a"),
+		  fixed("`will_not_throw_exception'"),
+		  words("attribute.  This attribute cannot be applied"),
+		  words("to erroneous procedures.")
+		],
+	write_error_pieces(Context, 0, Pieces, !IO).

 %-----------------------------------------------------------------------------%

only in patch2:
--- compiler/det_analysis.m	24 Jun 2004 07:57:40 -0000	1.171
+++ compiler/det_analysis.m	6 Dec 2004 05:19:28 -0000
@@ -747,6 +747,14 @@
 	proc_info_declared_determinism(ProcInfo, MaybeDetism),
 	( MaybeDetism = yes(Detism0) ->
 		determinism_components(Detism0, CanFail, NumSolns0),
+		(
+			may_throw_exception(Attributes) = will_not_throw_exception,
+			Detism0 = erroneous
+		->
+			Msgs0 = [will_not_throw_with_erroneous(PredId, ProcId)]
+		;
+			Msgs0 = []
+		),
 		( PragmaCode = nondet(_, _, _, _, _, _, _, _, _) ->
 			% pragma C codes of this form
 			% can have more than one solution
@@ -759,10 +767,10 @@
 			SolnContext \= first_soln
 		->
 			Msgs = [cc_pred_in_wrong_context(GoalInfo, Detism0,
-					PredId, ProcId)],
+					PredId, ProcId) | Msgs0 ],
 			NumSolns = at_most_many
 		;
-			Msgs = [],
+			Msgs = Msgs0,
 			NumSolns = NumSolns1
 		),
 		determinism_components(Detism, CanFail, NumSolns)
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list