diff --git a/xdsl/ir/core.py b/xdsl/ir/core.py index b85fa09273..18e0e7d32f 100644 --- a/xdsl/ir/core.py +++ b/xdsl/ir/core.py @@ -815,7 +815,9 @@ def verify(self, verify_nested_ops: bool = True) -> None: try: self.verify_() except VerifyException as err: - self.emit_error("Operation does not verify: " + str(err)) + self.emit_error( + "Operation does not verify: " + str(err), underlying_error=err + ) def verify_(self) -> None: pass @@ -974,14 +976,17 @@ def is_structurally_equivalent( return True def emit_error( - self, message: str, exception_type: type[Exception] = VerifyException + self, + message: str, + exception_type: type[Exception] = VerifyException, + underlying_error: Exception | None = None, ) -> NoReturn: """Emit an error with the given message.""" from xdsl.utils.diagnostic import Diagnostic diagnostic = Diagnostic() diagnostic.add_message(self, message) - diagnostic.raise_exception(message, self, exception_type) + diagnostic.raise_exception(message, self, exception_type, underlying_error) def __eq__(self, other: object) -> bool: return self is other diff --git a/xdsl/utils/diagnostic.py b/xdsl/utils/diagnostic.py index 262f65ba1a..e4698bee47 100644 --- a/xdsl/utils/diagnostic.py +++ b/xdsl/utils/diagnostic.py @@ -20,6 +20,7 @@ def raise_exception( message: str, ir: IRNode, exception_type: type[Exception] = DiagnosticException, + underlying_error: Exception | None = None, ) -> NoReturn: """Raise an exception, that will also print all messages in the IR.""" from xdsl.printer import Printer @@ -38,4 +39,4 @@ def raise_exception( else: assert "xDSL internal error: get_toplevel_object returned unknown construct" - raise exception_type(message + "\n\n" + f.getvalue()) + raise exception_type(message + "\n\n" + f.getvalue()) from underlying_error