[m-rev.] for review: improve an error message for type class method declarations
Julien Fischer
jfischer at opturion.com
Wed Sep 17 14:06:37 AEST 2025
For review by anyone.
-----------------------------
Improve an error message for type class method declarations.
Currently, if a type class method declaration is incorrectly started with ":-",
we print the following error:
Error: `:-' is not a valid declaration type.
This is a bit mysterious since in nearly every other context in Mercury, ":-"
is exactly how you begin a declaration. Change the parser to check for ":-"
at the beginning of type class method declaration and print an error specific
to that situation.
compiler/parse_item.m:
As above.
tests/invalid_nodepend/Mmakefile:
tests/invalid_nodepend/bad_method_start.{m,err_exp}:
Add a test for the new error message.
Julien.
diff --git a/compiler/parse_item.m b/compiler/parse_item.m
index 7b1a71076..deb6ca0f4 100644
--- a/compiler/parse_item.m
+++ b/compiler/parse_item.m
@@ -597,6 +597,18 @@ parse_attributed_decl(ModuleName, VarSet, Term,
IsInClass, _Context, SeqNum,
PurityAttrs, QuantConstrAttrs, MaybeIOMPrime)
then
MaybeIOM = MaybeIOMPrime
+ else if
+ Functor = ":-",
+ IsInClass = decl_is_in_class
+ then
+ Pieces = [words("Error:")] ++
+ color_as_subject([quote(Functor)]) ++
+ color_as_incorrect([words("is not allowed")]) ++
+ [words("at the beginning of a type class method
declaration.")] ++
+ [nl],
+ Spec = spec($pred, severity_error, phase_t2pt, FunctorContext,
+ Pieces),
+ MaybeIOM = error1([Spec])
else
Spec = decl_functor_is_not_valid(Functor, FunctorContext),
MaybeIOM = error1([Spec])
diff --git a/tests/invalid_nodepend/Mmakefile b/tests/invalid_nodepend/Mmakefile
index ea48a50ff..87b872a6f 100644
--- a/tests/invalid_nodepend/Mmakefile
+++ b/tests/invalid_nodepend/Mmakefile
@@ -30,6 +30,7 @@ PROGS = \
bad_include \
bad_initialise_decl \
bad_inst_defn \
+ bad_method_start \
bad_mutable \
bad_pragma \
bad_with_inst \
diff --git a/tests/invalid_nodepend/bad_method_start.err_exp
b/tests/invalid_nodepend/bad_method_start.err_exp
new file mode 100644
index 000000000..071820c19
--- /dev/null
+++ b/tests/invalid_nodepend/bad_method_start.err_exp
@@ -0,0 +1,2 @@
+bad_method_start.m:014: Error: [38;5;87m`:-' [39;49m [38;5;203mis
not allowed [39;49m at the beginning of a type
+bad_method_start.m:014: class method declaration.
diff --git a/tests/invalid_nodepend/bad_method_start.m
b/tests/invalid_nodepend/bad_method_start.m
new file mode 100644
index 000000000..bce14911b
--- /dev/null
+++ b/tests/invalid_nodepend/bad_method_start.m
@@ -0,0 +1,18 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%---------------------------------------------------------------------------%
+% Test the error message for when ':-' occurs at the beginning
+% of a type class method declaration.
+%---------------------------------------------------------------------------%
+
+:- module bad_method_start.
+:- interface.
+
+:- type suppress_warnings ---> suppress_warnings.
+
+:- typeclass provider(Provider) where [
+ :- func get_provider_name(Provider) = string
+].
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
More information about the reviews
mailing list