@@ -129,6 +129,14 @@ public SSTNode registerGlobal(String[] names, int startOffset, int endOffset) {
129
129
ScopeInfo scopeInfo = scopeEnvironment .getCurrentScope ();
130
130
ScopeInfo globalScope = scopeEnvironment .getGlobalScope ();
131
131
for (String name : names ) {
132
+ if (scopeInfo .isExplicitNonlocalVariable (name )) {
133
+ throw errors .raiseInvalidSyntax (source , createSourceSection (startOffset , endOffset ), ErrorMessages .NONLOCAL_AND_GLOBAL , name );
134
+ }
135
+ if (scopeInfo .findFrameSlot (name ) != null ) {
136
+ // The expectation is that in the local context the variable can not have slot yet.
137
+ // The slot is created by assignment or declaration
138
+ throw errors .raiseInvalidSyntax (source , createSourceSection (startOffset , endOffset ), ErrorMessages .NAME_IS_ASSIGNED_BEFORE_GLOBAL , name );
139
+ }
132
140
scopeInfo .addExplicitGlobalVariable (name );
133
141
globalScope .createSlotIfNotPresent (name );
134
142
}
@@ -137,7 +145,13 @@ public SSTNode registerGlobal(String[] names, int startOffset, int endOffset) {
137
145
138
146
public SSTNode registerNonLocal (String [] names , int startOffset , int endOffset ) {
139
147
ScopeInfo scopeInfo = scopeEnvironment .getCurrentScope ();
148
+ if (scopeInfo .getScopeKind () == ScopeKind .Module ) {
149
+ throw errors .raiseInvalidSyntax (source , createSourceSection (startOffset , endOffset ), ErrorMessages .NONLOCAL_AT_MODULE_LEVEL );
150
+ }
140
151
for (String name : names ) {
152
+ if (scopeInfo .isExplicitGlobalVariable (name )) {
153
+ throw errors .raiseInvalidSyntax (source , createSourceSection (startOffset , endOffset ), ErrorMessages .NONLOCAL_AND_GLOBAL , name );
154
+ }
141
155
if (scopeInfo .findFrameSlot (name ) != null ) {
142
156
// the expectation is that in the local context the variable can not have slot yet.
143
157
// The slot is created by assignment or declaration
0 commit comments