Skip to content

Commit 6e9b277

Browse files
committed
wip
1 parent b478122 commit 6e9b277

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,7 +3213,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
32133213

32143214
public:
32153215
ASR::asr_t *asr;
3216-
std::map<std::string, std::pair<int64_t, bool>> goto_name2id;
3216+
std::map<std::string, std::tuple<int64_t, bool, Location>> goto_name2id;
32173217
int64_t gotoids;
32183218

32193219

@@ -3329,10 +3329,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
33293329
tmp = nullptr;
33303330

33313331
for( auto itr: goto_name2id ) {
3332-
if( !itr.second.second ) {
3333-
throw SemanticError("Label " + itr.first + " is not defined in "
3334-
+ std::string(x.m_name),
3335-
x.base.base.loc);
3332+
if( !std::get<1>(itr.second) ) {
3333+
throw SemanticError("Label '" + itr.first + "' is not defined in '"
3334+
+ std::string(x.m_name) + "'", std::get<2>(itr.second));
33363335
}
33373336
}
33383337
}
@@ -3987,23 +3986,27 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
39873986
if( value == "label" ) {
39883987
std::string labelname = x.m_attr;
39893988
if( goto_name2id.find(labelname) == goto_name2id.end() ) {
3990-
goto_name2id[labelname] = std::make_pair(gotoids, true);
3989+
goto_name2id[labelname] = std::make_tuple(gotoids, true, x.base.base.loc);
39913990
gotoids += 1;
3992-
} else if( !goto_name2id[labelname].second ) {
3993-
goto_name2id[labelname] = std::make_pair(goto_name2id[labelname].first, true);
3991+
} else if( !std::get<1>(goto_name2id[labelname]) ) {
3992+
goto_name2id[labelname] = std::make_tuple(
3993+
std::get<0>(goto_name2id[labelname]),
3994+
true,
3995+
std::get<2>(goto_name2id[labelname])
3996+
);
39943997
}
3995-
int id = goto_name2id[labelname].first;
3998+
int id = std::get<0>(goto_name2id[labelname]);
39963999
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id, x.m_attr);
39974000
return ;
39984001
}
39994002

40004003
if (value == "goto"){
40014004
std::string labelname = std::string(x.m_attr);
40024005
if( goto_name2id.find(labelname) == goto_name2id.end() ) {
4003-
goto_name2id[labelname] = std::make_pair(gotoids, false);
4006+
goto_name2id[labelname] = std::make_tuple(gotoids, false, x.base.base.loc);
40044007
gotoids += 1;
40054008
}
4006-
int id = goto_name2id[labelname].first;
4009+
int id = std::get<0>(goto_name2id[labelname]);
40074010
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id, x.m_attr);
40084011
return ;
40094012
}

tests/errors/test_goto.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ltypes import with_goto, goto, label, i32
2+
3+
@with_goto
4+
def f():
5+
i:i32
6+
for i in range(10):
7+
if i == 5:
8+
goto .end
9+
10+
assert i == 5
11+
12+
f()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_goto-ba9fd22",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_goto.py",
5+
"infile_hash": "8b81c2245b3ca31576ac41f49247b3781d1759ac6be61e8512bfd0f1",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_goto-ba9fd22.stderr",
11+
"stderr_hash": "a1e26c1edcd8784938199af965004496663f071968ff7d58a33be725",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: Label 'end' is not defined in 'f'
2+
--> tests/errors/test_goto.py:8:13
3+
|
4+
8 | goto .end
5+
| ^^^^^^^^^

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,3 +906,7 @@ asr = true
906906
[[test]]
907907
filename = "errors/enum_02.py"
908908
asr = true
909+
910+
[[test]]
911+
filename = "errors/test_goto.py"
912+
asr = true

0 commit comments

Comments
 (0)