Repository: vyperlang/vyper

Issue:
type inference failure on new events code
### Version Information

* vyper Version (output of `vyper --version`): 755d9fabd79d74c9f0db82fd89fd2b7a9c3f80a1 but probably introduced in #2403 

### What's your issue about?

Type inference doesn't propagate when logging the result of a binop

```python
# tmp/log.vy
event Foo:
    x: uint256

@external
def foo(a: uint256, b: uint256):
    log Foo(a + b)
```
```console
$ vyper --debug tmp/log.vy 
Error compiling: tmp/log.vy
Traceback (most recent call last):
  File "/home/charles/.venvs/vyper/bin/vyper", line 33, in <module>
    sys.exit(load_entry_point('vyper==0.2.15', 'console_scripts', 'vyper')())
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/cli/vyper_compile.py", line 53, in _parse_cli_args
    return _parse_args(sys.argv[1:])
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/cli/vyper_compile.py", line 124, in _parse_args
    compiled = compile_files(
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/cli/vyper_compile.py", line 235, in compile_files
    compiler_data = vyper.compile_codes(
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/evm/opcodes.py", line 223, in _wrapper
    return fn(*args, **kwargs)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/__init__.py", line 106, in compile_codes
    exc_handler(contract_name, exc)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/cli/vyper_compile.py", line 160, in exc_handler
    raise exception
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/__init__.py", line 103, in compile_codes
    out[contract_name][output_format] = OUTPUT_FORMATS[output_format](compiler_data)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/output.py", line 171, in build_bytecode_output
    return f"0x{compiler_data.bytecode.hex()}"
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/phases.py", line 125, in bytecode
    self._bytecode = generate_bytecode(self.assembly)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/phases.py", line 113, in assembly
    self._assembly = generate_assembly(self.lll_nodes)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/phases.py", line 101, in lll_nodes
    self._gen_lll()
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/phases.py", line 96, in _gen_lll
    self._lll_nodes, self._lll_runtime = generate_lll_nodes(self.global_ctx)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/compiler/phases.py", line 225, in generate_lll_nodes
    lll_nodes, lll_runtime = parser.parse_tree_to_lll(global_ctx)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/parser.py", line 207, in parse_tree_to_lll
    o, runtime = parse_other_functions(
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/parser.py", line 112, in parse_other_functions
    func_lll = parse_function(
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/function_definitions/parse_function.py", line 60, in parse_function
    o = parse_external_function(
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/function_definitions/parse_external_function.py", line 229, in parse_external_function
    + [parse_body(c, context) for c in code.body]
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/function_definitions/parse_external_function.py", line 229, in <listcomp>
    + [parse_body(c, context) for c in code.body]
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/stmt.py", line 554, in parse_body
    return parse_stmt(code, context)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/stmt.py", line 548, in parse_stmt
    return Stmt(stmt, context).lll_node
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/stmt.py", line 40, in __init__
    self.lll_node = fn()
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/stmt.py", line 113, in parse_Log
    buf, _len = events.allocate_buffer_for_log(event, self.stmt.value.args, self.context)
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/events.py", line 60, in allocate_buffer_for_log
    arg_types = [
  File "/home/charles/.venvs/vyper/lib/python3.8/site-packages/vyper-0.2.15-py3.8.egg/vyper/old_codegen/events.py", line 61, in <listcomp>
    arg._metadata["type"] for arg, is_index in zip(args, event.indexed) if not is_index
KeyError: 'type'
```

### How can it be fixed?

Fill this in if you know how to fix it.



Discussion:

Resolving patch:
diff --git a/vyper/old_codegen/events.py b/vyper/old_codegen/events.py
--- a/vyper/old_codegen/events.py
+++ b/vyper/old_codegen/events.py
@@ -1,6 +1,5 @@
-from typing import List, Tuple
+from typing import Tuple
 
-import vyper.ast as vy_ast
 from vyper.exceptions import TypeMismatch
 from vyper.old_codegen.abi import (
     ABI_Tuple,
@@ -47,19 +46,17 @@ def _gas_bound(num_topics, data_maxlen):
     return LOG_BASE_GAS + GAS_PER_TOPIC * num_topics + GAS_PER_LOG_BYTE * data_maxlen
 
 
-def allocate_buffer_for_log(
-    event: Event, args: List[vy_ast.VyperNode], context: Context
-) -> Tuple[int, int]:
+def allocate_buffer_for_log(event: Event, context: Context) -> Tuple[int, int]:
     """Allocate a buffer to ABI-encode the non-indexed (data) arguments into
 
     This must be done BEFORE compiling the event arguments to LLL,
     registering the buffer with the `context` variable (otherwise any
     function calls inside the event literal will clobber the buffer).
     """
+    arg_types = list(event.arguments.values())  # the types of the arguments
     # remove non-data args, as those don't go into the buffer
-    arg_types = [
-        arg._metadata["type"] for arg, is_index in zip(args, event.indexed) if not is_index
-    ]
+    arg_types = [arg_t for arg_t, is_index in zip(arg_types, event.indexed) if not is_index]
+
     # all args get encoded as one big tuple
     abi_t = ABI_Tuple([abi_type_of2(arg_t) for arg_t in arg_types])
 
diff --git a/vyper/old_codegen/stmt.py b/vyper/old_codegen/stmt.py
--- a/vyper/old_codegen/stmt.py
+++ b/vyper/old_codegen/stmt.py
@@ -110,7 +110,7 @@ def parse_Log(self):
 
         # do this BEFORE evaluating args to LLL to protect the buffer
         # from internal call clobbering
-        buf, _len = events.allocate_buffer_for_log(event, self.stmt.value.args, self.context)
+        buf, _len = events.allocate_buffer_for_log(event, self.context)
 
         args = [Expr(arg, self.context).lll_node for arg in self.stmt.value.args]
 


Test patch: