[m-rev.] for review: do not allow non-abstract instance decls in interfaces
Julien Fischer
juliensf at cs.mu.OZ.AU
Mon Apr 11 13:51:50 AEST 2005
For review by anyone.
Estimated hours taken: 2
Branches: main, release
Fix a long standing problem where the compiler would accept
non-abstract instance declarations in module interfaces, despite
the reference manual disallowing these.
compiler/modules.m:
Emit an error message when a non-abstract instance
declaration occurs in a module interface.
tests/invalid/Mmakefile:
tests/invalid/instance_bug.m:
tests/invalid/instance_bug.err_exp:
Test case for the above.
Julien.
Workspace:/home/swordfish/juliensf/ws75
Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.325
diff -u -r1.325 modules.m
--- compiler/modules.m 11 Apr 2005 02:50:58 -0000 1.325
+++ compiler/modules.m 11 Apr 2005 03:41:26 -0000
@@ -463,7 +463,9 @@
% - report an error if the `implementation' section of a sub-module
% is contained inside the `interface' section of its parent module
% - check for modules declared as both nested and separate sub-modules.
-
+ % - check for non-abstract typeclass instance declarations in module
+ % interfaces.
+ %
:- type module_list == list(pair(module_name, item_list)).
:- pred split_into_submodules(module_name::in, item_list::in, module_list::out,
@@ -6797,6 +6799,7 @@
InParentInterface = no,
split_into_submodules_2(ModuleName, Items0, InParentInterface,
Items, ModuleList, !IO),
+
%
% Check that there are no items after the end_module declaration.
%
@@ -6896,11 +6899,11 @@
%
( Item = module_defn(_, interface) - _Context ->
InInterface1 = yes
- ; Item = module_defn(_, implementation) - Context ->
+ ; Item = module_defn(_, implementation) - ImplContext ->
(
InParentInterface = yes,
- report_error_implementation_in_interface(ModuleName, Context,
- !IO)
+ report_error_implementation_in_interface(ModuleName,
+ ImplContext, !IO)
;
InParentInterface = no
),
@@ -6909,6 +6912,20 @@
InInterface1 = InInterface0
),
%
+ % Check to make sure that a non-abstract instance declaration
+ % does not occur in a module interface.
+ %
+ (
+ InInterface1 = yes,
+ Item = instance(_, _, _, Body, _, _) - InstanceContext,
+ Body \= abstract
+ ->
+ report_non_abstract_instance_in_interface(InstanceContext, !IO)
+ ;
+ true
+ ),
+
+ %
% parse the remaining items for this module,
%
split_into_submodules_3(ModuleName, Items1,
@@ -7019,6 +7036,15 @@
report_items_after_end_module(Context, !IO) :-
ErrorPieces = [words("Error: item(s) after end_module declaration.")],
+ write_error_pieces(Context, 0, ErrorPieces, !IO),
+ io.set_exit_status(1, !IO).
+
+:- pred report_non_abstract_instance_in_interface(prog_context::in,
+ io::di, io::uo) is det.
+
+report_non_abstract_instance_in_interface(Context, !IO) :-
+ ErrorPieces = [words("Error: non-abstract instance declaration"),
+ words("in module interface.")],
write_error_pieces(Context, 0, ErrorPieces, !IO),
io.set_exit_status(1, !IO).
Index: tests/invalid/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/Mmakefile,v
retrieving revision 1.161
diff -u -r1.161 Mmakefile
--- tests/invalid/Mmakefile 6 Apr 2005 05:08:08 -0000 1.161
+++ tests/invalid/Mmakefile 11 Apr 2005 03:36:49 -0000
@@ -85,6 +85,7 @@
impure_method_impl \
inline_conflict \
inst_list_dup \
+ instance_bug \
invalid_export_detism \
invalid_import_detism \
invalid_main \
Index: tests/invalid/instance_bug.err_exp
===================================================================
RCS file: tests/invalid/instance_bug.err_exp
diff -N tests/invalid/instance_bug.err_exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/invalid/instance_bug.err_exp 11 Apr 2005 03:37:43 -0000
@@ -0,0 +1,3 @@
+instance_bug.m:009: Error: non-abstract instance declaration in module
+instance_bug.m:009: interface.
+For more information, try recompiling with `-E'.
Index: tests/invalid/instance_bug.m
===================================================================
RCS file: tests/invalid/instance_bug.m
diff -N tests/invalid/instance_bug.m
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/invalid/instance_bug.m 11 Apr 2005 03:36:21 -0000
@@ -0,0 +1,18 @@
+:- module instance_bug.
+
+:- interface.
+
+:- typeclass foo(T) where [
+ func id(T) = T
+].
+ % Compiler should complain about this.
+:- instance foo(int) where [
+ id(X) = X
+].
+
+:- implementation.
+
+ % Compiler shouldn't complain about this.
+:- instance foo(float) where [
+ id(X) = X
+].
--------------------------------------------------------------------------
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