Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@

if TYPE_CHECKING:
from guppylang_internals.definition.declaration import RawFunctionDecl
from guppylang_internals.definition.traced import RawTracedFunctionDef
from guppylang_internals.tys.param import Parameter

PyFunc = Callable[..., Any]


def default_func_link_name(raw_def: "RawFunctionDef | RawFunctionDecl") -> str:
def default_func_link_name(
raw_def: "RawFunctionDef | RawFunctionDecl | RawTracedFunctionDef",
) -> str:
if (parent_ty_id := DEF_STORE.type_member_parents.get(raw_def.id)) is not None:
parent = ENGINE.get_parsed(parent_ty_id)
if isinstance(parent, ParsedStructDef | ParsedEnumDef):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
ParsableDef,
)
from guppylang_internals.definition.function import (
default_func_link_name,
make_subprogram_record,
monomorphized_link_name,
parse_py_func,
)
from guppylang_internals.definition.value import (
Expand Down Expand Up @@ -167,7 +169,10 @@ def compile_outer(
"""
func_type = self.ty.to_hugr_poly(ctx)
func_def = module.module_root_builder().define_function(
self.name, func_type.body.input, func_type.body.output, func_type.params
monomorphized_link_name(default_func_link_name(self), self.inst),
func_type.body.input,
func_type.body.output,
func_type.params,
)
add_metadata(
module.hugr[func_def].metadata,
Expand Down
4 changes: 2 additions & 2 deletions tests/stack_traces/comptime.err
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Panic (#1001): Always panics
Guppy traceback (most recent call last):
File "$PATH_TO_FILE/comptime.py", line 7, in __hugr__.panicking_func.7:
File "$PATH_TO_FILE/comptime.py", line 7, in stack_traces.comptime.panicking_func:
|
5 | @guppy.comptime
6 | def panicking_func() -> None:
7 | panic("Always panics")
| ^ Always panics

File "$PATH_TO_FILE/comptime.py", line 12, in __hugr__.main.1:
File "$PATH_TO_FILE/comptime.py", line 12, in stack_traces.comptime.main:
|
10 | @guppy.comptime
11 | def main() -> None:
Expand Down
86 changes: 86 additions & 0 deletions tests/test_comptime_function_names.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason this needs to be a top level file, especially since this is doing some integration / end-to-end testing. Please move these tests to the existing tests.integration.test_link_name module. You should be able to reuse some functions (such as the function name extractor) from there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the four comptime name tests into tests.integration.test_link_name, reused _func_names and the qualifier fixture, and removed the top-level file and its duplicate extractor. The production fix is unchanged. The 17 link-name tests pass, and the four moved regressions still fail without the production fix.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Comptime functions use the same qualified names as checked Guppy functions."""

from guppylang import guppy
from hugr.ops import FuncDefn


def function_names(package):
hugr = package.modules[0]
return [
hugr[n].op.f_name
for n in hugr.children(hugr.module_root)
if isinstance(hugr[n].op, FuncDefn)
]


def test_comptime_qualified_name():
@guppy.comptime
def function() -> None:
pass

names = function_names(function.with_minimal_opt().compile())
assert f"{__name__}.test_comptime_qualified_name.<locals>.function" in names


def test_comptime_generic_names_are_distinct():
@guppy.comptime
def identity[T](value: T) -> T:
return value

@guppy
def main() -> None:
_ = identity(1)
_ = identity(True)

names = function_names(main.with_minimal_opt().compile())
prefix = f"{__name__}.test_comptime_generic_names_are_distinct.<locals>.identity"
assert f"{prefix}$int" in names
assert f"{prefix}$bool" in names


def test_comptime_constant_names():
from guppylang.std.array import array

n = guppy.nat_var("n")

@guppy.comptime
def values() -> array[int, n]:
return list(range(n))

@guppy
def main() -> None:
_ = values[2]()
_ = values[3]()

names = function_names(main.with_minimal_opt().compile())
prefix = f"{__name__}.test_comptime_constant_names.<locals>.values"
assert f"{prefix}$2" in names
assert f"{prefix}$3" in names


def test_comptime_same_name_different_scopes():
def first_scope():
@guppy.comptime
def same_name() -> int:
return 1

return same_name

def second_scope():
@guppy.comptime
def same_name() -> int:
return 2

return same_name

first = first_scope()
second = second_scope()

@guppy
def main() -> int:
return first() + second()

names = function_names(main.with_minimal_opt().compile())
prefix = f"{__name__}.test_comptime_same_name_different_scopes.<locals>"
assert f"{prefix}.first_scope.<locals>.same_name" in names
assert f"{prefix}.second_scope.<locals>.same_name" in names
Loading