poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] libpoke: typify: Make cast error msgs more helpful


From: Mohammad-Reza Nabipoor
Subject: [PATCH] libpoke: typify: Make cast error msgs more helpful
Date: Wed, 7 Jul 2021 04:09:28 +0430

Type checking for "cast to struct type", incorrectly reports the
location of expression's type, instead of the location of
expression itself. This leads to a confusing error message when the
expression is a function call.

Running this code:

```
fun f = uint16: { return 0; }
type HL16 = struct uint<16> { uint8 hi; uint8 lo; };
f as HL16;
```

Emits this error message (which is confusing):

```
t.pk:1:9: error: invalid cast, expected struct
fun f = uint16: { return 0; }
        ^~~~~~
```

New behavior:

```
t.pk:3:1: error: invalid cast, expected struct, got ushort
f as HiLo16;
^
```

Besides this fix, this commit also adds the type name of the unexpected
type in the error message.

2021-07-06  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/pkl-typify.c (pkl_typify1_ps_cast): Make cast error
        messages more helpful.
---
 ChangeLog            |  5 +++++
 libpoke/pkl-typify.c | 15 ++++++++++-----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 76ea7c5d..2abd0dea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-07-06  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/pkl-typify.c (pkl_typify1_ps_cast): Make cast error
+       messages more helpful.
+
 2021-06-06  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * common/pk-utils.h (pk_format_binary): New function declaration.
diff --git a/libpoke/pkl-typify.c b/libpoke/pkl-typify.c
index 103d5113..73725ada 100644
--- a/libpoke/pkl-typify.c
+++ b/libpoke/pkl-typify.c
@@ -493,8 +493,10 @@ PKL_PHASE_BEGIN_HANDLER (pkl_typify1_ps_cast)
   if (PKL_AST_TYPE_CODE (type) == PKL_TYPE_STRUCT
       && PKL_AST_TYPE_CODE (exp_type) != PKL_TYPE_STRUCT)
     {
-      PKL_ERROR (PKL_AST_LOC (exp_type),
-                 "invalid cast, expected struct");
+      char *found_type = pkl_type_str (exp_type, 1);
+
+      PKL_ERROR (PKL_AST_LOC (exp),
+                 "invalid cast, expected struct, got %s", found_type);
       PKL_TYPIFY_PAYLOAD->errors++;
       PKL_PASS_ERROR;
     }
@@ -509,9 +511,12 @@ PKL_PHASE_BEGIN_HANDLER (pkl_typify1_ps_cast)
         ;
       else
         {
-          PKL_ERROR (PKL_AST_LOC (type),
-                     "invalid cast, expected struct%s",
-                     PKL_AST_TYPE_S_ITYPE (exp_type) ? " or integer" : "");
+          char *type_str = pkl_type_str (type, 1);
+
+          PKL_ERROR (
+              PKL_AST_LOC (type), "invalid cast, expected struct%s, got %s",
+              PKL_AST_TYPE_S_ITYPE (exp_type) ? " or integral type" : "",
+              type_str);
           PKL_TYPIFY_PAYLOAD->errors++;
           PKL_PASS_ERROR;
         }
-- 
2.32.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]