[Mlir-commits] [mlir] [mlir] Improve error message when number of operands and types differ (PR #118488)

Jakub Kuderski llvmlistbot at llvm.org
Tue Dec 3 07:56:35 PST 2024


Markus =?utf-8?q?Böck?= <markus.boeck02 at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/118488 at github.com>


================
@@ -1602,14 +1602,33 @@ class OpAsmParser : public AsmParser {
                   SmallVectorImpl<Value> &result) {
     size_t operandSize = llvm::range_size(operands);
     size_t typeSize = llvm::range_size(types);
-    if (operandSize != typeSize)
-      return emitError(loc)
-             << operandSize << " operands present, but expected " << typeSize;
+    if (operandSize == typeSize) {
+      for (auto [operand, type] : llvm::zip_equal(operands, types))
+        if (resolveOperand(operand, type, result))
+          return failure();
+      return success();
+    }
 
-    for (auto [operand, type] : llvm::zip_equal(operands, types))
-      if (resolveOperand(operand, type, result))
-        return failure();
-    return success();
+    InFlightDiagnostic diag = emitError(loc)
+                              << "number of operands and types do not match";
+    std::string lesserQuantityText = "operand";
+    if (operandSize != 1)
+      lesserQuantityText += "s";
+    std::string higherQuantityText = "type";
+    if (typeSize != 1)
+      higherQuantityText += "s";
+
+    size_t lesserQuantity = operandSize;
+    size_t higherQuantity = typeSize;
+    if (operandSize > typeSize) {
+      std::swap(lesserQuantity, higherQuantity);
+      std::swap(lesserQuantityText, higherQuantityText);
+    }
+
+    diag.attachNote() << "got " << higherQuantity << " " << higherQuantityText
+                      << " but only " << lesserQuantity << " "
+                      << lesserQuantityText;
----------------
kuhar wrote:

Yeah I feel like the current version is a little bit overcomplicated for little benefit over what I suggested above

https://github.com/llvm/llvm-project/pull/118488


More information about the Mlir-commits mailing list