Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve codegen test coverage report #3824

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vyper/builtins/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def _bytes_to_num(arg, out_typ, signed):
num_zero_bits = ["mul", 8, ["sub", 32, _len]]
elif is_bytes_m_type(arg.typ):
num_zero_bits = 8 * (32 - arg.typ.m)
else:
raise CompilerPanic("unreachable") # pragma: notest
else: # pragma: nocover
raise CompilerPanic("unreachable")

if signed:
ret = sar(num_zero_bits, arg)
Expand Down Expand Up @@ -359,8 +359,8 @@ def to_decimal(expr, arg, out_typ):
# TODO: consider adding is_signed and bits to bool so we can use _int_to_fixed
arg = ["mul", arg, 10**out_typ.decimals]
return IRnode.from_list(arg, typ=out_typ)
else:
raise CompilerPanic("unreachable") # pragma: notest
else: # pragma: nocover
raise CompilerPanic("unreachable")


@_input_types(IntegerT, DecimalT, BytesM_T, AddressT, BytesT, BoolT)
Expand Down
2 changes: 1 addition & 1 deletion vyper/builtins/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def process_arg(arg, expected_arg_type, context):
if isinstance(expected_arg_type, VyperType):
return Expr(arg, context).ir_node

raise CompilerPanic(f"Unexpected type: {expected_arg_type}") # pragma: notest
raise CompilerPanic(f"Unexpected type: {expected_arg_type}") # pragma: nocover


def process_kwarg(kwarg_node, kwarg_settings, expected_kwarg_type, context):
Expand Down
8 changes: 4 additions & 4 deletions vyper/codegen/abi_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ def abi_encode(dst, ir_node, context, bufsz, returns_len=False):
size_bound = abi_t.size_bound()

assert isinstance(bufsz, int)
if bufsz < size_bound:
if bufsz < size_bound: # pragma: nocover
raise CompilerPanic("buffer provided to abi_encode not large enough")

if size_bound < dst.typ.memory_bytes_required:
if size_bound < dst.typ.memory_bytes_required: # pragma: nocover
raise CompilerPanic("Bad ABI size calc")

annotation = f"abi_encode {ir_node.typ}"
Expand Down Expand Up @@ -208,7 +208,7 @@ def abi_encode(dst, ir_node, context, bufsz, returns_len=False):
ir_ret.extend(encode_ir)
static_ofst += e.typ.abi_type.embedded_static_size()

else:
else: # pragma: nocover
raise CompilerPanic(f"unencodable type: {ir_node.typ}")

# declare IR variables.
Expand All @@ -221,7 +221,7 @@ def abi_encode(dst, ir_node, context, bufsz, returns_len=False):
ir_ret.append(calc_len)
elif abi_t.is_complex_type():
ir_ret.append("dyn_ofst")
else:
else: # pragma: nocover
raise CompilerPanic(f"unknown type {ir_node.typ}")

if abi_t.is_dynamic() and abi_t.is_complex_type():
Expand Down
5 changes: 2 additions & 3 deletions vyper/codegen/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def safe_div(x, y):

if is_decimal_type(x.typ):
lo, hi = typ.int_bounds
if max(abs(lo), abs(hi)) * typ.divisor > 2**256 - 1:
if max(abs(lo), abs(hi)) * typ.divisor > 2**256 - 1: # pragma: nocover
# stub to prevent us from adding fixed point numbers we don't know
# how to deal with
raise UnimplementedException("safe_mul for decimal{typ.bits}x{typ.decimals}")
Expand Down Expand Up @@ -340,8 +340,7 @@ def safe_mod(x, y):
# def safe_pow(x: IRnode, y: IRnode) -> IRnode:
def safe_pow(x, y):
typ = x.typ
if not is_integer_type(x.typ):
# type checker should have caught this
if not is_integer_type(x.typ): # pragma: nocover
raise TypeCheckFailure("non-integer pow")

GE = "sge" if typ.is_signed else "ge"
Expand Down
8 changes: 3 additions & 5 deletions vyper/codegen/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,10 @@ def new_variable(self, name: str, typ: VyperType, is_mutable: bool = True) -> in
var_size = typ.memory_bytes_required
return self._new_variable(name, typ, var_size, False, is_mutable=is_mutable)

def fresh_varname(self, name: Optional[str] = None) -> str:
def fresh_varname(self, name: str) -> str:
"""
return a unique
return a unique variable name
"""
if name is None:
name = "var"
t = self._internal_var_iter
self._internal_var_iter += 1
return f"{name}{t}"
Expand Down Expand Up @@ -251,4 +249,4 @@ def pp_constancy(self):
return "a range expression"
elif self.constancy == Constancy.Constant:
return "a constant function"
raise CompilerPanic(f"unknown constancy in pp_constancy: {self.constancy}")
raise CompilerPanic(f"bad constancy: {self.constancy}") # pragma: nocover
110 changes: 55 additions & 55 deletions vyper/codegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ def make_byte_array_copier(dst, src):


def bytes_data_ptr(ptr):
if ptr.location is None:
if ptr.location is None: # pragma: nocover
raise CompilerPanic("tried to modify non-pointer type")
assert isinstance(ptr.typ, _BytestringT)
return add_ofst(ptr, ptr.location.word_scale)


def dynarray_data_ptr(ptr):
if ptr.location is None:
if ptr.location is None: # pragma: nocover
raise CompilerPanic("tried to modify non-pointer type")
assert isinstance(ptr.typ, DArrayT)
return add_ofst(ptr, ptr.location.word_scale)
Expand Down Expand Up @@ -510,8 +510,8 @@ def _get_element_ptr_tuplelike(parent, key):
ofst = 0 # offset from parent start

if parent.encoding == Encoding.ABI:
if parent.location == STORAGE:
raise CompilerPanic("storage variables should not be abi encoded") # pragma: notest
if parent.location in (STORAGE, TRANSIENT): # pragma: nocover
raise CompilerPanic("storage variables should not be abi encoded")

member_t = typ.member_types[attrs[index]]

Expand Down Expand Up @@ -544,7 +544,7 @@ def has_length_word(typ):
def _get_element_ptr_array(parent, key, array_bounds_check):
assert is_array_like(parent.typ)

if not is_integer_type(key.typ):
if not is_integer_type(key.typ): # pragma: nocover
raise TypeCheckFailure(f"{key.typ} used as array index")

subtype = parent.typ.value_type
Expand Down Expand Up @@ -580,8 +580,8 @@ def _get_element_ptr_array(parent, key, array_bounds_check):
ix.set_error_msg(f"{parent.typ} bounds check")

if parent.encoding == Encoding.ABI:
if parent.location == STORAGE:
raise CompilerPanic("storage variables should not be abi encoded") # pragma: notest
if parent.location in (STORAGE, TRANSIENT): # pragma: nocover
raise CompilerPanic("storage variables should not be abi encoded")

member_abi_t = subtype.abi_type

Expand All @@ -607,8 +607,7 @@ def _get_element_ptr_mapping(parent, key):
subtype = parent.typ.value_type
key = unwrap_location(key)

# TODO when is key None?
if key is None or parent.location not in (STORAGE, TRANSIENT):
if parent.location not in (STORAGE, TRANSIENT): # pragma: nocover
raise TypeCheckFailure("bad dereference on mapping {parent}[{key}]")

return IRnode.from_list(["sha3_64", parent, key], typ=subtype, location=parent.location)
Expand All @@ -630,18 +629,18 @@ def get_element_ptr(parent, key, array_bounds_check=True):
elif is_array_like(typ):
ret = _get_element_ptr_array(parent, key, array_bounds_check)

else:
raise CompilerPanic(f"get_element_ptr cannot be called on {typ}") # pragma: notest
else: # pragma: nocover
raise CompilerPanic(f"get_element_ptr cannot be called on {typ}")

return b.resolve(ret)


def LOAD(ptr: IRnode) -> IRnode:
if ptr.location is None:
if ptr.location is None: # pragma: nocover
raise CompilerPanic("cannot dereference non-pointer type")
op = ptr.location.load_op
if op is None:
raise CompilerPanic(f"unreachable {ptr.location}") # pragma: notest
if op is None: # pragma: nocover
raise CompilerPanic(f"unreachable {ptr.location}")
return IRnode.from_list([op, ptr])


Expand All @@ -655,11 +654,11 @@ def eval_once_check(name):


def STORE(ptr: IRnode, val: IRnode) -> IRnode:
if ptr.location is None:
if ptr.location is None: # pragma: nocover
raise CompilerPanic("cannot dereference non-pointer type")
op = ptr.location.store_op
if op is None:
raise CompilerPanic(f"unreachable {ptr.location}") # pragma: notest
if op is None: # pragma: nocover
raise CompilerPanic(f"unreachable {ptr.location}")

_check = _freshname(f"{op}_")

Expand Down Expand Up @@ -735,45 +734,46 @@ def dummy_node_for_type(typ):


def _check_assign_bytes(left, right):
if right.typ.maxlen > left.typ.maxlen:
raise TypeMismatch(f"Cannot cast from {right.typ} to {left.typ}") # pragma: notest
if right.typ.maxlen > left.typ.maxlen: # pragma: nocover
raise TypeMismatch(f"Cannot cast from {right.typ} to {left.typ}")

# stricter check for zeroing a byte array.
if right.value == "~empty" and right.typ.maxlen != left.typ.maxlen:
raise TypeMismatch(f"Cannot cast from empty({right.typ}) to {left.typ}") # pragma: notest
# TODO: these should be TypeCheckFailure instead of TypeMismatch
if right.value == "~empty" and right.typ.maxlen != left.typ.maxlen: # pragma: nocover
raise TypeMismatch(f"Cannot cast from empty({right.typ}) to {left.typ}")


def _check_assign_list(left, right):
def FAIL(): # pragma: no cover
raise TypeCheckFailure(f"assigning {right.typ} to {left.typ}")

if left.value == "multi":
if left.value == "multi": # pragma: nocover
# Cannot do something like [a, b, c] = [1, 2, 3]
FAIL() # pragma: notest
FAIL()

if isinstance(left.typ, SArrayT):
if not is_array_like(right.typ):
FAIL() # pragma: notest
if left.typ.count != right.typ.count:
FAIL() # pragma: notest
if not is_array_like(right.typ): # pragma: nocover
FAIL()
if left.typ.count != right.typ.count: # pragma: nocover
FAIL()

# TODO recurse into left, right if literals?
check_assign(
dummy_node_for_type(left.typ.value_type), dummy_node_for_type(right.typ.value_type)
)

if isinstance(left.typ, DArrayT):
if not isinstance(right.typ, DArrayT):
FAIL() # pragma: notest
if not isinstance(right.typ, DArrayT): # pragma: nocover
FAIL()

if left.typ.count < right.typ.count:
FAIL() # pragma: notest
if left.typ.count < right.typ.count: # pragma: nocover
FAIL()

# stricter check for zeroing
if right.value == "~empty" and right.typ.count != left.typ.count:
if right.value == "~empty" and right.typ.count != left.typ.count: # pragma: nocover
raise TypeCheckFailure(
f"Bad type for clearing bytes: expected {left.typ} but got {right.typ}"
) # pragma: notest
)

# TODO recurse into left, right if literals?
check_assign(
Expand All @@ -785,29 +785,29 @@ def _check_assign_tuple(left, right):
def FAIL(): # pragma: no cover
raise TypeCheckFailure(f"assigning {right.typ} to {left.typ}")

if not isinstance(right.typ, left.typ.__class__):
FAIL() # pragma: notest
if not isinstance(right.typ, left.typ.__class__): # pragma: nocover
FAIL()

if isinstance(left.typ, StructT):
for k in left.typ.member_types:
if k not in right.typ.member_types:
FAIL() # pragma: notest
if k not in right.typ.member_types: # pragma: nocover
FAIL()
# TODO recurse into left, right if literals?
check_assign(
dummy_node_for_type(left.typ.member_types[k]),
dummy_node_for_type(right.typ.member_types[k]),
)

for k in right.typ.member_types:
if k not in left.typ.member_types:
FAIL() # pragma: notest
if k not in left.typ.member_types: # pragma: nocover
FAIL()

if left.typ.name != right.typ.name:
FAIL() # pragma: notest
if left.typ.name != right.typ.name: # pragma: nocover
FAIL()

else:
if len(left.typ.member_types) != len(right.typ.member_types):
FAIL() # pragma: notest
if len(left.typ.member_types) != len(right.typ.member_types): # pragma: nocover
FAIL()
for left_, right_ in zip(left.typ.member_types, right.typ.member_types):
# TODO recurse into left, right if literals?
check_assign(dummy_node_for_type(left_), dummy_node_for_type(right_))
Expand All @@ -831,8 +831,8 @@ def FAIL(): # pragma: no cover

elif left.typ._is_prim_word:
# TODO once we propagate types from typechecker, introduce this check:
# if left.typ != right.typ:
# FAIL() # pragma: notest
# if left.typ != right.typ: # pragma: nocover
# FAIL()
pass

else: # pragma: no cover
Expand All @@ -859,8 +859,8 @@ def reset_names():
def needs_clamp(t, encoding):
if encoding == Encoding.VYPER:
return False
if encoding != Encoding.ABI:
raise CompilerPanic("unreachable") # pragma: notest
if encoding != Encoding.ABI: # pragma: nocover
raise CompilerPanic("unreachable")
if isinstance(t, (_BytestringT, DArrayT)):
return True
if isinstance(t, FlagT):
Expand All @@ -872,7 +872,7 @@ def needs_clamp(t, encoding):
if t._is_prim_word:
return t not in (INT256_T, UINT256_T, BYTES32_T)

raise CompilerPanic("unreachable") # pragma: notest
raise CompilerPanic("unreachable") # pragma: nocover


# Create an x=y statement, where the types may be compound
Expand Down Expand Up @@ -1113,8 +1113,8 @@ def sar(bits, x):

def clamp_bytestring(ir_node):
t = ir_node.typ
if not isinstance(t, _BytestringT):
raise CompilerPanic(f"{t} passed to clamp_bytestring") # pragma: notest
if not isinstance(t, _BytestringT): # pragma: nocover
raise CompilerPanic(f"{t} passed to clamp_bytestring")
ret = ["assert", ["le", get_bytearray_length(ir_node), t.maxlen]]
return IRnode.from_list(ret, error_msg=f"{ir_node.typ} bounds check")

Expand All @@ -1129,8 +1129,8 @@ def clamp_dyn_array(ir_node):
# clampers for basetype
def clamp_basetype(ir_node):
t = ir_node.typ
if not t._is_prim_word:
raise CompilerPanic(f"{t} passed to clamp_basetype") # pragma: notest
if not t._is_prim_word: # pragma: nocover
raise CompilerPanic(f"{t} passed to clamp_basetype")

# copy of the input
ir_node = unwrap_location(ir_node)
Expand Down Expand Up @@ -1168,8 +1168,8 @@ def int_clamp(ir_node, bits, signed=False):
in bounds. (Consumers should use clamp_basetype instead which uses
type-based dispatch and is a little safer.)
"""
if bits >= 256:
raise CompilerPanic(f"invalid clamp: {bits}>=256 ({ir_node})") # pragma: notest
if bits >= 256: # pragma: nocover
raise CompilerPanic(f"invalid clamp: {bits}>=256 ({ir_node})")

u = "u" if not signed else ""
msg = f"{u}int{bits} bounds check"
Expand All @@ -1193,7 +1193,7 @@ def int_clamp(ir_node, bits, signed=False):


def bytes_clamp(ir_node: IRnode, n_bytes: int) -> IRnode:
if not (0 < n_bytes <= 32):
if not (0 < n_bytes <= 32): # pragma: nocover
raise CompilerPanic(f"bad type: bytes{n_bytes}")
msg = f"bytes{n_bytes} bounds check"
with ir_node.cache_when_complex("val") as (b, val):
Expand Down
Loading
Loading