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

Distinguish type error from name error in pyparser #733

Merged
merged 2 commits into from
Oct 23, 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
11 changes: 7 additions & 4 deletions src/exo/frontend/pyparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,8 @@ def parse_expr(self, e):
nm = self.locals[nm_node.id]
elif nm_node.id in self.globals:
nm = self.globals[nm_node.id]
else:
nm = None
else: # could not resolve name to anything
self.err(nm_node, f"variable '{nm_node.id}' undefined")

if isinstance(nm, SizeStub):
nm = nm.nm
Expand All @@ -899,8 +899,11 @@ def parse_expr(self, e):
)
else:
return UAST.Const(nm, self.getsrcinfo(e))
else: # could not resolve name to anything
self.err(nm_node, f"variable '{nm_node.id}' undefined")
else:
self.err(
nm_node,
f"variable '{nm_node.id}' has unsupported type {type(nm)}",
)

if is_window:
return UAST.WindowExpr(nm, idxs, self.getsrcinfo(e))
Expand Down
66 changes: 64 additions & 2 deletions tests/test_uast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from __future__ import annotations

import pytest

from exo import DRAM
from exo.frontend.pyparser import Parser, get_src_locals, get_ast_from_python
from exo.frontend.pyparser import (
Parser,
get_src_locals,
get_ast_from_python,
ParseError,
)


def to_uast(f):
Expand All @@ -10,7 +17,7 @@
body,
getsrcinfo,
func_globals=f.__globals__,
srclocals=get_src_locals(depth=3),
srclocals=get_src_locals(depth=2),
instr=("TEST", ""),
as_func=True,
)
Expand Down Expand Up @@ -57,3 +64,58 @@
res[i, j] = rloc[j]

assert str(to_uast(alloc_nest)) == golden


global_str = "What is 6 times 9?"
global_num = 42


def test_variable_lookup_positive():
def func(f: f32):
for i in seq(0, 42):
f += 1

Check warning on line 76 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L75-L76

Added lines #L75 - L76 were not covered by tests

reference = to_uast(func)

def func(f: f32):
for i in seq(0, global_num):
f += 1

Check warning on line 82 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L81-L82

Added lines #L81 - L82 were not covered by tests

test_global = to_uast(func)
assert str(test_global) == str(reference)

local_num = 42

def func(f: f32):
for i in seq(0, local_num):
f += 1

Check warning on line 91 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L90-L91

Added lines #L90 - L91 were not covered by tests

test_local = to_uast(func)
assert str(test_local) == str(reference)


def test_variable_lookup_type_error():
def func(f: f32):
for i in seq(0, global_str):
f += 1

Check warning on line 100 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L99-L100

Added lines #L99 - L100 were not covered by tests

with pytest.raises(ParseError, match="type <class 'str'>"):
to_uast(func)

local_str = "xyzzy"

def func(f: f32):
for i in seq(0, local_str):
f += 1

Check warning on line 109 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L108-L109

Added lines #L108 - L109 were not covered by tests

with pytest.raises(ParseError, match="type <class 'str'>"):
to_uast(func)


def test_variable_lookup_name_error():
def func(f: f32):
for i in seq(0, xyzzy):
f += 1

Check warning on line 118 in tests/test_uast.py

View check run for this annotation

Codecov / codecov/patch

tests/test_uast.py#L117-L118

Added lines #L117 - L118 were not covered by tests

with pytest.raises(ParseError, match="'xyzzy' undefined"):
to_uast(func)
Loading