Skip to content

Commit

Permalink
chore: improve exception handling in IR generation (#3705)
Browse files Browse the repository at this point in the history
QOL improvement - improve unannotated exceptions that happen during IR
generation to include source info.
  • Loading branch information
charles-cooper authored Dec 20, 2023
1 parent 5a67b68 commit 9165926
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
6 changes: 5 additions & 1 deletion vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
from vyper.evm.address_space import DATA, IMMUTABLES, MEMORY, STORAGE, TRANSIENT
from vyper.evm.opcodes import version_check
from vyper.exceptions import (
CodegenPanic,
CompilerPanic,
EvmVersionException,
StructureException,
TypeCheckFailure,
TypeMismatch,
UnimplementedException,
VyperException,
tag_exceptions,
)
from vyper.semantics.types import (
AddressT,
Expand Down Expand Up @@ -79,7 +81,9 @@ def __init__(self, node, context):
if fn is None:
raise TypeCheckFailure(f"Invalid statement node: {type(node).__name__}", node)

self.ir_node = fn()
with tag_exceptions(node, fallback_exception_type=CodegenPanic):
self.ir_node = fn()

if self.ir_node is None:
raise TypeCheckFailure(f"{type(node).__name__} node did not produce IR.\n", node)

Expand Down
11 changes: 9 additions & 2 deletions vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
from vyper.codegen.expr import Expr
from vyper.codegen.return_ import make_return_stmt
from vyper.evm.address_space import MEMORY, STORAGE
from vyper.exceptions import CompilerPanic, StructureException, TypeCheckFailure
from vyper.exceptions import (
CodegenPanic,
CompilerPanic,
StructureException,
TypeCheckFailure,
tag_exceptions,
)
from vyper.semantics.types import DArrayT, MemberFunctionT
from vyper.semantics.types.function import ContractFunctionT
from vyper.semantics.types.shortcuts import INT256_T, UINT256_T
Expand All @@ -39,7 +45,8 @@ def __init__(self, node: vy_ast.VyperNode, context: Context) -> None:
raise TypeCheckFailure(f"Invalid statement node: {type(node).__name__}")

with context.internal_memory_scope():
self.ir_node = fn()
with tag_exceptions(node, fallback_exception_type=CodegenPanic):
self.ir_node = fn()

if self.ir_node is None:
raise TypeCheckFailure("Statement node did not produce IR")
Expand Down
20 changes: 18 additions & 2 deletions vyper/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import copy
import textwrap
import types
Expand Down Expand Up @@ -322,8 +323,9 @@ class VyperInternalException(_BaseVyperException):

def __str__(self):
return (
f"{self.message}\n\nThis is an unhandled internal compiler error. "
"Please create an issue on Github to notify the developers.\n"
f"{super().__str__()}\n\n"
"This is an unhandled internal compiler error. "
"Please create an issue on Github to notify the developers!\n"
"https://github.com/vyperlang/vyper/issues/new?template=bug.md"
)

Expand Down Expand Up @@ -354,3 +356,17 @@ class TypeCheckFailure(VyperInternalException):

class InvalidABIType(VyperInternalException):
"""An internal routine constructed an invalid ABI type"""


@contextlib.contextmanager
def tag_exceptions(
node, fallback_exception_type=CompilerPanic, fallback_message="unhandled exception"
):
try:
yield
except _BaseVyperException as e:
if not e.annotations and not e.lineno:
raise e.with_annotation(node) from None
raise e from None
except Exception as e:
raise fallback_exception_type(fallback_message, node) from e
13 changes: 1 addition & 12 deletions vyper/semantics/analysis/common.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import contextlib
from typing import Tuple

from vyper.exceptions import StructureException, VyperException


@contextlib.contextmanager
def tag_exceptions(node):
try:
yield
except VyperException as e:
if not e.annotations and not e.lineno:
raise e.with_annotation(node) from None
raise e from None
from vyper.exceptions import StructureException, tag_exceptions


class VyperNodeVisitorBase:
Expand Down

0 comments on commit 9165926

Please sign in to comment.