[m-rev.] diff: improve error message for unsatisfied typeclass constraints

Julien Fischer juliensf at cs.mu.OZ.AU
Thu Oct 20 19:14:47 AEST 2005


Estimated hours taken: 0.5
Branches: main

Improve the error message for unsatisfied typeclass constraints.
As an aid to readability we now print each unsatisfied constraint on a
separate line.

compiler/post_typecheck.m:
	When printing out unsatisfied typeclass constraints, print them out
	one-per-line.  The error messages are difficult to decipher otherwise.

	Use error_util to handle the error message for unsatisfied typeclass
	constraints.

tests/invalid/unsatisfied_constraint.err_exp:
tests/invalid/typeclass_test_8.err_exp:
	Conform to the new format of the unsatisfied typeclass constraint
	error message.

Julien.

Index: compiler/post_typecheck.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/post_typecheck.m,v
retrieving revision 1.83
diff -u -r1.83 post_typecheck.m
--- compiler/post_typecheck.m	20 Oct 2005 07:42:30 -0000	1.83
+++ compiler/post_typecheck.m	20 Oct 2005 08:44:57 -0000
@@ -322,34 +322,48 @@
     apply_subst_to_constraint_map(VoidSubst, !ConstraintMap).

 %-----------------------------------------------------------------------------%
+%
+% Report unsatisfied typeclass constraints
+%

-    % Report an error: unsatisfied type class constraints.
-    %
 :- pred report_unsatisfied_constraints(list(prog_constraint)::in,
     pred_id::in, pred_info::in, module_info::in, io::di, io::uo) is det.

 report_unsatisfied_constraints(Constraints, PredId, PredInfo, ModuleInfo,
         !IO) :-
-    io__set_exit_status(1, !IO),
+    io.set_exit_status(1, !IO),

     pred_info_typevarset(PredInfo, TVarSet),
     pred_info_context(PredInfo, Context),
-
-    prog_out__write_context(Context, !IO),
-    io__write_string("In ", !IO),
-    hlds_out__write_pred_id(ModuleInfo, PredId, !IO),
-    io__write_string(":\n", !IO),
-
-    prog_out__write_context(Context, !IO),
-    io__write_string("  type error: unsatisfied typeclass constraint(s):\n",
-        !IO),
-
-    prog_out__write_context(Context, !IO),
-    io__write_string("  `", !IO),
-    AppendVarnums = no,
-    io__write_list(Constraints, "', `",
-        mercury_output_constraint(TVarSet, AppendVarnums), !IO),
-    io__write_string("'.\n", !IO).
+
+    Pieces0 = constraints_to_error_pieces(TVarSet, Constraints),
+
+    PredIdStr = pred_id_to_string(ModuleInfo, PredId),
+
+    Pieces = [
+        words("In"), fixed(PredIdStr), suffix(":"), nl,
+        fixed("type error: unsatisfied typeclass " ++
+            choose_number(Constraints, "constraint:", "constraints:")),
+            nl_indent_delta(2) | Pieces0 ],
+
+    write_error_pieces(Context, 0, Pieces, !IO).
+
+:- func constraints_to_error_pieces(tvarset, list(prog_constraint))
+    = format_components.
+
+constraints_to_error_pieces(_, []) = [].
+constraints_to_error_pieces(TVarset, [C]) =
+    [constraint_to_error_piece(TVarset, C)].
+constraints_to_error_pieces(TVarset, [ C0, C1 | Cs]) = Components :-
+    Format0    = [ constraint_to_error_piece(TVarset, C0), nl ],
+    Components = Format0 ++ constraints_to_error_pieces(TVarset, [C1 | Cs]).
+
+:- func constraint_to_error_piece(tvarset, prog_constraint) = format_component.
+
+constraint_to_error_piece(TVarset, Constraint) =
+    fixed("`" ++ mercury_constraint_to_string(TVarset, Constraint) ++ "'").
+
+%-----------------------------------------------------------------------------%

     % Report a warning: uninstantiated type parameter.
     %
Index: tests/invalid/typeclass_test_8.err_exp
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/typeclass_test_8.err_exp,v
retrieving revision 1.6
diff -u -r1.6 typeclass_test_8.err_exp
--- tests/invalid/typeclass_test_8.err_exp	14 Sep 2005 05:26:53 -0000	1.6
+++ tests/invalid/typeclass_test_8.err_exp	20 Oct 2005 09:07:09 -0000
@@ -1,6 +1,6 @@
 typeclass_test_8.m:004: In predicate `typeclass_test_8.main/2':
-typeclass_test_8.m:004:   type error: unsatisfied typeclass constraint(s):
-typeclass_test_8.m:004:   `typeclass_test_8.fooable(T)'.
+typeclass_test_8.m:004:   type error: unsatisfied typeclass constraint:
+typeclass_test_8.m:004:       `typeclass_test_8.fooable(T)'
 typeclass_test_8.m:004: In predicate `typeclass_test_8.main/2':
 typeclass_test_8.m:004:   warning: unresolved polymorphism.
 typeclass_test_8.m:004:   The variable with an unbound type was:
Index: tests/invalid/unsatisfiable_constraint.err_exp
===================================================================
RCS file: /home/mercury1/repository/tests/invalid/unsatisfiable_constraint.err_exp,v
retrieving revision 1.3
diff -u -r1.3 unsatisfiable_constraint.err_exp
--- tests/invalid/unsatisfiable_constraint.err_exp	20 Oct 2005 07:42:31 -0000	1.3
+++ tests/invalid/unsatisfiable_constraint.err_exp	20 Oct 2005 09:06:52 -0000
@@ -1,6 +1,8 @@
 unsatisfiable_constraint.m:038: In predicate `unsatisfiable_constraint.test/1':
-unsatisfiable_constraint.m:038:   type error: unsatisfied typeclass constraint(s):
-unsatisfiable_constraint.m:038:   `unsatisfiable_constraint.a(A, B, A, V_8)', `unsatisfiable_constraint.a(A, B, C, V_14)', `unsatisfiable_constraint.b(A, C)'.
+unsatisfiable_constraint.m:038:   type error: unsatisfied typeclass constraints:
+unsatisfiable_constraint.m:038:       `unsatisfiable_constraint.a(A, B, A, V_8)'
+unsatisfiable_constraint.m:038:       `unsatisfiable_constraint.a(A, B, C, V_14)'
+unsatisfiable_constraint.m:038:       `unsatisfiable_constraint.b(A, C)'
 unsatisfiable_constraint.m:038: In predicate `unsatisfiable_constraint.test/1':
 unsatisfiable_constraint.m:038:   warning: unresolved polymorphism.
 unsatisfiable_constraint.m:038:   The variables with unbound types were:
--------------------------------------------------------------------------
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