improve error messages
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 20 Dec 2021 00:02:01 +0000 (16:02 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 20 Dec 2021 00:03:36 +0000 (16:03 -0800)
src/language/expressions/parse.c

index 3b18be72ddf81bce64bfbd9c23be0547259b0b42..2b78b0b14b41491cc172fc301ac4476fdf0765ba 100644 (file)
@@ -593,10 +593,7 @@ parse_binary_operators__ (struct lexer *lexer, struct expression *e,
         return NULL;
 
       struct expr_node *node = expr_allocate_binary (e, optype, lhs, rhs);
-      bool lhs_ok = type_coercion (e, node, 0);
-      bool rhs_ok = type_coercion (e, node, 1);
-
-      if (!lhs_ok || !rhs_ok)
+      if (!is_coercible (node, 0) || !is_coercible (node, 1))
         {
           bool both = false;
           for (size_t i = 0; i < n_ops; i++)
@@ -615,15 +612,18 @@ parse_binary_operators__ (struct lexer *lexer, struct expression *e,
                     _("Both operands of %s must be strings."), name);
 
           msg_at (SN, expr_location (e, node->args[0]),
-                  _("The left-hand operand of %s has type '%s'."),
-                  name, atom_type_name (expr_node_returns (node->args[0])));
+                  _("This operand has type '%s'."),
+                  atom_type_name (expr_node_returns (node->args[0])));
           msg_at (SN, expr_location (e, node->args[1]),
-                  _("The right-hand operand of %s has type '%s'."),
-                  name, atom_type_name (expr_node_returns (node->args[1])));
+                  _("This operand has type '%s'."),
+                  atom_type_name (expr_node_returns (node->args[1])));
 
           return NULL;
         }
 
+      if (!type_coercion (e, node, 0) || !type_coercion (e, node, 1))
+        NOT_REACHED ();
+
       lhs = node;
     }
 }
@@ -1400,7 +1400,11 @@ static const char *
 atom_type_name (atom_type type)
 {
   assert (is_atom (type));
-  return operations[type].name;
+
+  /* The Boolean type is purely an internal concept that the documentation
+     doesn't mention, so it might confuse users if we talked about them in
+     diagnostics. */
+  return type == OP_boolean ? "number" : operations[type].name;
 }
 
 struct expr_node *