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

[mypyc] Generate error on duplicate function definitions #16309

Merged
merged 2 commits into from
Oct 27, 2023
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
9 changes: 9 additions & 0 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def __init__(
self.graph = graph
self.ret_types: list[RType] = []
self.functions: list[FuncIR] = []
self.function_names: set[tuple[str | None, str]] = set()
self.classes: list[ClassIR] = []
self.final_names: list[tuple[str, RType]] = []
self.callable_class_names: set[str] = set()
Expand Down Expand Up @@ -1315,6 +1316,14 @@ def error(self, msg: str, line: int) -> None:
def note(self, msg: str, line: int) -> None:
self.errors.note(msg, self.module_path, line)

def add_function(self, func_ir: FuncIR, line: int) -> None:
name = (func_ir.class_name, func_ir.name)
if name in self.function_names:
self.error(f'Duplicate definition of "{name[1]}" not supported by mypyc', line)
return
self.function_names.add(name)
self.functions.append(func_ir)


def gen_arg_defaults(builder: IRBuilder) -> None:
"""Generate blocks for arguments that have default values.
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def transform_func_def(builder: IRBuilder, fdef: FuncDef) -> None:
if func_reg:
builder.assign(get_func_target(builder, fdef), func_reg, fdef.line)
maybe_insert_into_registry_dict(builder, fdef)
builder.functions.append(func_ir)
builder.add_function(func_ir, fdef.line)


def transform_overloaded_func_def(builder: IRBuilder, o: OverloadedFuncDef) -> None:
Expand Down
24 changes: 24 additions & 0 deletions mypyc/test-data/irbuild-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -1123,3 +1123,27 @@ L6:
r14 = CPy_NoErrOccured()
L7:
return 1

[case testConditionalFunctionDefinition]
if int():
def foo() -> int:
return 0
else:
def foo() -> int: # E
return 1

def bar() -> int:
return 0

if int():
def bar() -> int: # E
return 1
[out]
main:5: error: Duplicate definition of "foo" not supported by mypyc
main:12: error: Duplicate definition of "bar" not supported by mypyc

[case testRepeatedUnderscoreFunctions]
def _(arg): pass
def _(arg): pass
[out]
main:2: error: Duplicate definition of "_" not supported by mypyc
4 changes: 0 additions & 4 deletions mypyc/test-data/run-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -1117,10 +1117,6 @@ for _ in range(2):
except AssertionError:
pass

[case testRepeatedUnderscoreFunctions]
def _(arg): pass
def _(arg): pass

[case testUnderscoreFunctionsInMethods]

class A:
Expand Down
Loading