Skip to content

Commit

Permalink
[mypyc] Generate error on duplicate function definitions (#16309)
Browse files Browse the repository at this point in the history
Previously we produced duplicate functions in C, which caused C
compiler errors.
  • Loading branch information
JukkaL committed Oct 27, 2023
1 parent 090a414 commit f7d047c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
9 changes: 9 additions & 0 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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 @@ -1326,6 +1327,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

0 comments on commit f7d047c

Please sign in to comment.