Skip to content

Commit b2b526b

Browse files
authored
Merge pull request #1854 from Shaikh-Ubaid/error_on_param_redecl2
Give an error message if function parameter is declared twice
2 parents bac8685 + b163980 commit b2b526b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,16 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
39373937
v = ASR::down_cast<ASR::symbol_t>(_tmp);
39383938

39393939
}
3940+
if (current_scope->get_scope().find(arg_s) !=
3941+
current_scope->get_scope().end()) {
3942+
ASR::symbol_t *orig_decl = current_scope->get_symbol(arg_s);
3943+
throw SemanticError(diag::Diagnostic(
3944+
"Parameter is already declared",
3945+
diag::Level::Error, diag::Stage::Semantic, {
3946+
diag::Label("original declaration", {orig_decl->base.loc}, false),
3947+
diag::Label("redeclaration", {v->base.loc}),
3948+
}));
3949+
}
39403950
current_scope->add_symbol(arg_s, v);
39413951
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc,
39423952
v)));

tests/errors/func_06.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def init_X(m:i32, n: i32, b: CPtr, m: i32) -> None:
2+
B: Pointer[i16[m*n]] = c_p_pointer(b, i16[m*n])
3+
i: i32
4+
j: i32
5+
for i in range(m):
6+
for j in range(n):
7+
B[i * n + j] = i16((i + j) % m)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_06-62e738c",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_06.py",
5+
"infile_hash": "2b8a5fa5d38ad35eeec67177ab8848255901548fe929c964a9eefef1",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_06-62e738c.stderr",
11+
"stderr_hash": "a97e6d8f812ec9e81cb4b0565dc2fd30f3bd194ccab41770962f6e32",
12+
"returncode": 2
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
semantic error: Parameter is already declared
2+
--> tests/errors/func_06.py:1:12
3+
|
4+
1 | def init_X(m:i32, n: i32, b: CPtr, m: i32) -> None:
5+
| ~~~~~ original declaration
6+
|
7+
1 | def init_X(m:i32, n: i32, b: CPtr, m: i32) -> None:
8+
| ^^^^^^ redeclaration

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ asr = true
10981098
filename = "errors/func_05.py"
10991099
asr = true
11001100

1101+
[[test]]
1102+
filename = "errors/func_06.py"
1103+
asr = true
1104+
11011105
# tests/runtime_errors
11021106
[[test]]
11031107
filename = "runtime_errors/test_list_01.py"

0 commit comments

Comments
 (0)