[m-rev.] diff: improve foreign_proc pragma error messages

Julien Fischer jfischer at opturion.com
Fri Mar 17 14:54:52 AEDT 2017


Improve foreign_proc pragma error messages.

compiler/parse_pragma.m:
      Bring the error messages we generate for these into
      line with those of the other foreign language interface
      pragmas.

tests/invalid/bad_foreign_proc.err_exp:
      Conform to the above change.

Julien.

diff --git a/compiler/parse_pragma.m b/compiler/parse_pragma.m
index 58a95a7..6ade766 100644
--- a/compiler/parse_pragma.m
+++ b/compiler/parse_pragma.m
@@ -2187,21 +2187,20 @@ parse_pragma_foreign_code_pragma(ErrorTerm,

  parse_pragma_foreign_proc_pragma(ModuleName, VarSet, ErrorTerm,
          PragmaTerms, Context, SeqNum, MaybeIOM) :-
-    InvalidDeclPrefix = [words("Error: invalid"),
-        pragma_decl("foreign_proc"), words("declaration:")],
      (
          PragmaTerms = [LangTerm | RestTerms],
-        ( if term_to_foreign_language(LangTerm, ForeignLanguagePrime) then
-            ForeignLanguage = ForeignLanguagePrime,
+        LangContextPieces = cord.from_list([
+            words("In first argument of"), pragma_decl("foreign_proc"),
+            words("declaration:")
+        ]),
+        parse_foreign_language(LangContextPieces, VarSet, LangTerm,
+            MaybeForeignLanguage),
+        (
+            MaybeForeignLanguage = ok1(ForeignLanguage),
              LangSpecs = []
-        else
-            ForeignLanguage = lang_c,   % Dummy, ignored when LangSpecs \= []
-            LangPieces = InvalidDeclPrefix ++
-                [words("invalid language parameter."), nl],
-            LangSpec = error_spec(severity_error, phase_term_to_parse_tree,
-                [simple_msg(get_term_context(LangTerm),
-                    [always(LangPieces)])]),
-            LangSpecs = [LangSpec]
+        ;
+            MaybeForeignLanguage = error1(LangSpecs),
+            ForeignLanguage = lang_c  % Dummy, ignored when LangSpecs \= []
          ),
          ( if
              RestTerms = [PredAndVarsTerm, FlagsTerm, CodeTerm],
@@ -2223,16 +2222,20 @@ parse_pragma_foreign_proc_pragma(ModuleName, VarSet, ErrorTerm,
                  MaybeIOM = error1(LangSpecs ++ RestSpecs)
              )
          else
-            Pieces = InvalidDeclPrefix ++
-                [words("wrong number of arguments."), nl],
+            Pieces = [
+                words("Error: a "), pragma_decl("foreign_proc"),
+                words("declaration must have four arguments.")
+            ],
              Spec = error_spec(severity_error, phase_term_to_parse_tree,
                  [simple_msg(get_term_context(ErrorTerm), [always(Pieces)])]),
              MaybeIOM = error1([Spec])
          )
      ;
          PragmaTerms = [],
-        Pieces = InvalidDeclPrefix ++
-            [words("wrong number of arguments."), nl],
+        Pieces = [
+            words("Error: a "), pragma_decl("foreign_proc"),
+            words("declaration must have four arguments.")
+        ],
          Spec = error_spec(severity_error, phase_term_to_parse_tree,
              [simple_msg(get_term_context(ErrorTerm), [always(Pieces)])]),
          MaybeIOM = error1([Spec])
@@ -2287,9 +2290,11 @@ parse_pragma_ordinary_foreign_proc_pragma(ModuleName, VarSet,
          Impl0 = fp_impl_ordinary(Code, yes(CodeContext)),
          MaybeImpl = ok1(Impl0)
      else
-        ImplPieces = [words("Error in the fourth argument of"),
+        CodeTermStr = describe_error_term(VarSet, CodeTerm),
+        ImplPieces = [words("In the fourth argument of"),
              pragma_decl("foreign_proc"), words("declaration:"),
-            words("expected a string containing foreign code."), nl],
+            words("error: expected a string containing foreign code, got"),
+            quote(CodeTermStr), suffix("."), nl],
          ImplSpec = error_spec(severity_error, phase_term_to_parse_tree,
              [simple_msg(CodeContext, [always(ImplPieces)])]),
          MaybeImpl = error1([ImplSpec])
diff --git a/tests/invalid/bad_foreign_proc.err_exp b/tests/invalid/bad_foreign_proc.err_exp
index 60f4c60..738d506 100644
--- a/tests/invalid/bad_foreign_proc.err_exp
+++ b/tests/invalid/bad_foreign_proc.err_exp
@@ -1,9 +1,10 @@
-bad_foreign_proc.m:017: Error: invalid `:- pragma foreign_proc' declaration:
-bad_foreign_proc.m:017:   wrong number of arguments.
-bad_foreign_proc.m:021: Error: invalid `:- pragma foreign_proc' declaration:
-bad_foreign_proc.m:021:   wrong number of arguments.
-bad_foreign_proc.m:030: Error: invalid `:- pragma foreign_proc' declaration:
-bad_foreign_proc.m:030:   invalid language parameter.
+bad_foreign_proc.m:017: Error: a `:- pragma foreign_proc' declaration must have
+bad_foreign_proc.m:017:   four arguments.
+bad_foreign_proc.m:021: Error: a `:- pragma foreign_proc' declaration must have
+bad_foreign_proc.m:021:   four arguments.
+bad_foreign_proc.m:030: In first argument of `:- pragma foreign_proc'
+bad_foreign_proc.m:030:   declaration: error: invalid foreign language
+bad_foreign_proc.m:030:   `"InvalidLanuage"'.
  bad_foreign_proc.m:039: In the second argument of `:- pragma foreign_proc'
  bad_foreign_proc.m:039:   declaration: error: atom expected at "foo".
  bad_foreign_proc.m:043: In the third argument of `:- pragma foreign_proc'
@@ -12,16 +13,17 @@ bad_foreign_proc.m:043:   "promise_pure".
  bad_foreign_proc.m:053: In the third argument of `:- pragma foreign_proc'
  bad_foreign_proc.m:053:   declaration: error: conflicting attributes in
  bad_foreign_proc.m:053:   attribute list.
-bad_foreign_proc.m:060: Error in the fourth argument of
-bad_foreign_proc.m:060:   `:- pragma foreign_proc' declaration: expected a
-bad_foreign_proc.m:060:   string containing foreign code.
-bad_foreign_proc.m:065: Error: invalid `:- pragma foreign_proc' declaration:
-bad_foreign_proc.m:065:   invalid language parameter.
+bad_foreign_proc.m:060: In the fourth argument of `:- pragma foreign_proc'
+bad_foreign_proc.m:060:   declaration: error: expected a string containing
+bad_foreign_proc.m:060:   foreign code, got `5555'.
+bad_foreign_proc.m:065: In first argument of `:- pragma foreign_proc'
+bad_foreign_proc.m:065:   declaration: error: invalid foreign language
+bad_foreign_proc.m:065:   `"InvalidLanguage"'.
  bad_foreign_proc.m:066: In the second argument of `:- pragma foreign_proc'
  bad_foreign_proc.m:066:   declaration: error: atom expected at "foo".
  bad_foreign_proc.m:067: In the third argument of `:- pragma foreign_proc'
  bad_foreign_proc.m:067:   declaration: error: expected an attribute list, found
  bad_foreign_proc.m:067:   "promise_pure".
-bad_foreign_proc.m:068: Error in the fourth argument of
-bad_foreign_proc.m:068:   `:- pragma foreign_proc' declaration: expected a
-bad_foreign_proc.m:068:   string containing foreign code.
+bad_foreign_proc.m:068: In the fourth argument of `:- pragma foreign_proc'
+bad_foreign_proc.m:068:   declaration: error: expected a string containing
+bad_foreign_proc.m:068:   foreign code, got `6666'.


More information about the reviews mailing list