[mlir] Allow Diagnostic/InFlightDiagnostic to convert to FailureOr.
authorbzcheeseman <12992886+bzcheeseman@users.noreply.github.com>
Wed, 30 Mar 2022 08:36:41 +0000 (01:36 -0700)
committerRiver Riddle <riddleriver@gmail.com>
Wed, 30 Mar 2022 08:37:20 +0000 (01:37 -0700)
Allow conversion of a diagnostic to FailureOr. This conversion only results
in `failure` because in the case where operator LogicalResult would return
success, the FailureOr constructor would assert.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D122596

mlir/include/mlir/IR/Diagnostics.h

index 97f333a..600a6f9 100644 (file)
@@ -265,6 +265,13 @@ public:
   /// Allow a diagnostic to be converted to 'failure'.
   operator LogicalResult() const;
 
+  /// Allow a diagnostic to be converted to FailureOr<T>. Always results in
+  /// 'failure' because this cast cannot possibly return an object of 'T'.
+  template <typename T>
+  operator FailureOr<T>() const {
+    return failure();
+  }
+
 private:
   Diagnostic(const Diagnostic &rhs) = delete;
   Diagnostic &operator=(const Diagnostic &rhs) = delete;
@@ -347,6 +354,14 @@ public:
   /// 'success' if this is an empty diagnostic.
   operator LogicalResult() const;
 
+  /// Allow an inflight diagnostic to be converted to FailureOr<T>. Always
+  /// results in 'failure' because this cast cannot possibly return an object of
+  /// 'T'.
+  template <typename T>
+  operator FailureOr<T>() const {
+    return failure();
+  }
+
 private:
   InFlightDiagnostic &operator=(const InFlightDiagnostic &) = delete;
   InFlightDiagnostic &operator=(InFlightDiagnostic &&) = delete;