Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit d60520b

Browse files
[[ Bug 16452 ]] Add error for global var shadowing a local var
1 parent 9165759 commit d60520b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

docs/notes/bugfix-16452.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# global variable shadowing local variable is not detected

engine/src/keywords.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,41 @@ Parse_stat MCGlobal::parse(MCScriptPoint &sp)
5656
(PE_GLOBAL_BADNAME, sp);
5757
return PS_ERROR;
5858
}
59+
60+
// SN-2015-11-19: [[ Bug 16452 ]] We need to check whether a local
61+
// variable already exists at a script/handler level, to avoid shadowing
62+
bool t_shadowing;
63+
t_shadowing = false;
64+
65+
MCVarref* t_var;
66+
t_var = NULL;
5967
if (sp.gethandler() == NULL)
60-
sp.gethlist()->newglobal(sp.gettoken_nameref());
68+
{
69+
sp.gethlist() -> findvar(sp.gettoken_nameref(), false, &t_var);
70+
if (t_var == NULL || !MCexplicitvariables)
71+
sp.gethlist()->newglobal(sp.gettoken_nameref());
72+
else
73+
t_shadowing = true;
74+
}
6175
else
62-
sp.gethandler()->newglobal(sp.gettoken_nameref());
76+
{
77+
sp.gethandler()->findvar(sp.gettoken_nameref(), &t_var);
78+
if (t_var == NULL || !MCexplicitvariables)
79+
sp.gethandler()->newglobal(sp.gettoken_nameref());
80+
else
81+
t_shadowing = true;
82+
}
83+
84+
// Clearup fetched var
85+
delete t_var;
86+
87+
// In case we are shadowing a local variable, then we raise an error
88+
if (t_shadowing)
89+
{
90+
MCperror -> add(PE_GLOBAL_SHADOW, sp);
91+
return PS_ERROR;
92+
}
93+
6394
switch (sp.next(type))
6495
{
6596
case PS_NORMAL:

engine/src/parseerrors.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,11 @@ enum Parse_errors
16741674
// MM-2014-06-13: [[ Bug 12567 ]] New variant open socket <socket> with verification for host <host>
16751675
// {PE-0545} open: expected 'host'
16761676
PE_OPEN_NOHOST,
1677+
1678+
// SN-2015-11-15: [[ Bug 165452 ]] New error, if a global variable shadows
1679+
// a local variable declared beforehand
1680+
// {PE-0546} global: shadowing a local variable
1681+
PE_GLOBAL_SHADOW,
16771682
};
16781683

16791684
extern const char *MCparsingerrors;

0 commit comments

Comments
 (0)