Skip to content

Commit b3ab0be

Browse files
committed
LR/SSA: Fix problem with assignemnt to an unpacking
1 parent c999cbd commit b3ab0be

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎src/FAST-Python-Tools-Tests/FASTPythonLocalResolverTest.class.st‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ print(x)'.
4444
self assert: usedVariable equals: assignedVariable1 localDeclaration localUses third
4545
]
4646

47+
{ #category : 'tests - globals' }
48+
FASTPythonLocalResolverTest >> testAssignUnpacking [
49+
50+
| assignedVariable1 assignedVariable2 |
51+
self parseAndResolve: 'a, *b = [1, 2, 3]'.
52+
53+
assignedVariable1 := model module statements first left initializers first.
54+
self assert: assignedVariable1 localDeclaration equals: assignedVariable1.
55+
self assert: assignedVariable1 localDeclaration name equals: 'a'.
56+
self assert: assignedVariable1 localDeclaration isVariableWriteAccess.
57+
self deny: assignedVariable1 localDeclaration isNonLocalDeclaration.
58+
self assert: assignedVariable1 localDeclaration localUses size equals: 1.
59+
self assert: assignedVariable1 localDeclaration variableDeclarator equals: model module statements first.
60+
61+
assignedVariable2 := model module statements first left initializers second expression.
62+
self assert: assignedVariable2 localDeclaration equals: assignedVariable2.
63+
self assert: assignedVariable2 localDeclaration name equals: 'b'.
64+
self assert: assignedVariable2 localDeclaration isVariableWriteAccess.
65+
self deny: assignedVariable2 localDeclaration isNonLocalDeclaration.
66+
self assert: assignedVariable2 localDeclaration localUses size equals: 1.
67+
self assert: assignedVariable2 localDeclaration variableDeclarator equals: model module statements first
68+
]
69+
4770
{ #category : 'tests - globals' }
4871
FASTPythonLocalResolverTest >> testAssignementToList [
4972

‎src/FAST-Python-Tools-Tests/FASTPythonSSATest.class.st‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,27 @@ print(y.x)'.
242242
self assert: usedVar ssaVersion identicalTo: assignedVar2 ssaVersion
243243
]
244244

245+
{ #category : 'tests - assignments' }
246+
FASTPythonSSATest >> testAssignementToUnpackingAndUsage [
247+
248+
| assignedVar1 assignedVar2 usedVar |
249+
self parseAndResolve: 'a, *b = [1, 2, 3]
250+
251+
print(a)'.
252+
253+
assignedVar1 := model module statements first left initializers first.
254+
self assert: assignedVar1 ssaVersion version equals: 1.
255+
self assert: assignedVar1 ssaName equals: 'a_1'.
256+
self assert: assignedVar1 ssaVersion localUses size equals: 2.
257+
258+
assignedVar2 := model module statements first left initializers second expression.
259+
self assert: assignedVar2 ssaVersion version equals: 1.
260+
self assert: assignedVar2 ssaName equals: 'b_1'.
261+
262+
usedVar := model module statements second arguments first.
263+
self assert: usedVar ssaVersion identicalTo: assignedVar1 ssaVersion
264+
]
265+
245266
{ #category : 'tests - if' }
246267
FASTPythonSSATest >> testAssignmentInIfWithMultipleElif [
247268

‎src/FAST-Python-Tools/FASTPyEntity.extension.st‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ FASTPyEntity >> localResolverKind [
4646

4747
{ #category : '*FAST-Python-Tools' }
4848
FASTPyEntity >> selfOrTopmostAssignableCollection [
49-
"If I am in a tuple, I return the tuple (this applies recursively, if I am in a tuple in a tuple I return the topmost tuple).
49+
"If I am in a tuple, I return the tuple (this applies recursively, if I am in a tuple in a tuple I return the topmost tuple).
50+
If I am in a splat used as assignment target (e.g. `a, *b = ...`), I also go through the splat.
5051
5152
Else, return myself."
5253

@@ -56,5 +57,7 @@ FASTPyEntity >> selfOrTopmostAssignableCollection [
5657
[ node collectionInitializer isNotNil and: [ node collectionInitializer isTuple or: [ node collectionInitializer isList ] ] ] whileTrue: [
5758
node := node collectionInitializer ].
5859

60+
node parentSplat ifNotNil: [ :splat | ^ splat selfOrTopmostAssignableCollection ].
61+
5962
^ node
6063
]

0 commit comments

Comments
 (0)