Skip to content

Commit ee32255

Browse files
jensjohaCommit Queue
authored and
Commit Queue
committed
[parser] Support record type in pattern
Fixes http://dartbug.com/51169 Fixes http://dartbug.com/51176 Change-Id: Iaa38585ce311319f2f4c106fc979c99d931eaa15 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280106 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent 1631284 commit ee32255

11 files changed

+2352
-0
lines changed

pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9481,6 +9481,17 @@ class Parser {
94819481
// variablePattern ::= ( 'var' | 'final' | 'final'? type )? identifier
94829482
return parseVariablePattern(token, patternContext);
94839483
case '(':
9484+
// "(" could start a record type (which has to be followed by an
9485+
// identifier though), e.g. `(int, int) foo`.
9486+
if (next.endGroup!.next!.isIdentifier) {
9487+
TypeInfo typeInfo = computeVariablePatternType(token);
9488+
if (typeInfo is ComplexTypeInfo &&
9489+
typeInfo.isRecordType &&
9490+
!typeInfo.recovered) {
9491+
return parseVariablePattern(token, patternContext,
9492+
typeInfo: typeInfo);
9493+
}
9494+
}
94849495
// parenthesizedPattern ::= '(' pattern ')'
94859496
// recordPattern ::= '(' patternFields? ')'
94869497
// patternFields ::= patternField ( ',' patternField )* ','?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Class {
2+
dynamic field;
3+
Class(this.field);
4+
}
5+
6+
test(a) {
7+
switch (a) {
8+
case (foo: int b) when b != 2:
9+
print(b);
10+
case (foo: (int, int) b) when b != (2, 3):
11+
print(b);
12+
case Class(field: int b) when b != 2:
13+
print(b);
14+
case Class(field: (int, int) b) when b != (2, 3):
15+
print(b);
16+
}
17+
}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
beginCompilationUnit(class)
2+
beginMetadataStar(class)
3+
endMetadataStar(0)
4+
beginClassOrMixinOrNamedMixinApplicationPrelude(class)
5+
handleIdentifier(Class, classOrMixinDeclaration)
6+
handleNoTypeVariables({)
7+
beginClassDeclaration(class, null, null, null, null, null, null, null, null, null, Class)
8+
handleNoType(Class)
9+
handleClassExtends(null, 1)
10+
handleClassNoWithClause()
11+
handleImplements(null, 0)
12+
handleClassHeader(class, class, null)
13+
beginClassOrMixinOrExtensionBody(DeclarationKind.Class, {)
14+
beginMetadataStar(dynamic)
15+
endMetadataStar(0)
16+
beginMember()
17+
beginFields(DeclarationKind.Class, null, null, null, null, null, null, null, {)
18+
handleIdentifier(dynamic, typeReference)
19+
handleNoTypeArguments(field)
20+
handleType(dynamic, null)
21+
handleIdentifier(field, fieldDeclaration)
22+
handleNoFieldInitializer(;)
23+
endClassFields(null, null, null, null, null, null, null, 1, dynamic, ;)
24+
endMember()
25+
beginMetadataStar(Class)
26+
endMetadataStar(0)
27+
beginMember()
28+
beginMethod(DeclarationKind.Class, null, null, null, null, null, null, Class)
29+
handleNoType(;)
30+
handleIdentifier(Class, methodDeclaration)
31+
handleNoTypeVariables(()
32+
beginFormalParameters((, MemberKind.NonStaticMethod)
33+
beginMetadataStar(this)
34+
endMetadataStar(0)
35+
beginFormalParameter(this, MemberKind.NonStaticMethod, null, null, null)
36+
handleNoType(()
37+
handleIdentifier(field, fieldInitializer)
38+
handleFormalParameterWithoutValue())
39+
endFormalParameter(this, null, ., field, null, null, FormalParameterKind.requiredPositional, MemberKind.NonStaticMethod)
40+
endFormalParameters(1, (, ), MemberKind.NonStaticMethod)
41+
handleNoInitializers()
42+
handleAsyncModifier(null, null)
43+
handleEmptyFunctionBody(;)
44+
endClassConstructor(null, Class, (, null, ;)
45+
endMember()
46+
endClassOrMixinOrExtensionBody(DeclarationKind.Class, 2, {, })
47+
endClassDeclaration(class, })
48+
endTopLevelDeclaration(test)
49+
beginMetadataStar(test)
50+
endMetadataStar(0)
51+
beginTopLevelMember(test)
52+
beginTopLevelMethod(}, null, null)
53+
handleNoType(})
54+
handleIdentifier(test, topLevelFunctionDeclaration)
55+
handleNoTypeVariables(()
56+
beginFormalParameters((, MemberKind.TopLevelMethod)
57+
beginMetadataStar(a)
58+
endMetadataStar(0)
59+
beginFormalParameter(a, MemberKind.TopLevelMethod, null, null, null)
60+
handleNoType(()
61+
handleIdentifier(a, formalParameterDeclaration)
62+
handleFormalParameterWithoutValue())
63+
endFormalParameter(null, null, null, a, null, null, FormalParameterKind.requiredPositional, MemberKind.TopLevelMethod)
64+
endFormalParameters(1, (, ), MemberKind.TopLevelMethod)
65+
handleAsyncModifier(null, null)
66+
beginBlockFunctionBody({)
67+
beginSwitchStatement(switch)
68+
handleIdentifier(a, expression)
69+
handleNoTypeArguments())
70+
handleNoArguments())
71+
handleSend(a, ))
72+
handleParenthesizedCondition((, null, null)
73+
beginSwitchBlock({)
74+
beginCaseExpression(case)
75+
handleIdentifier(foo, namedRecordFieldReference)
76+
handleIdentifier(int, typeReference)
77+
handleNoTypeArguments(b)
78+
handleType(int, null)
79+
handleVariablePattern(null, b, false)
80+
handlePatternField(:)
81+
handleRecordPattern((, 1)
82+
beginSwitchCaseWhenClause(when)
83+
handleIdentifier(b, expression)
84+
handleNoTypeArguments(!=)
85+
handleNoArguments(!=)
86+
handleSend(b, !=)
87+
beginBinaryExpression(!=)
88+
handleLiteralInt(2)
89+
endBinaryExpression(!=)
90+
endSwitchCaseWhenClause(2)
91+
endCaseExpression(case, when, :)
92+
beginSwitchCase(0, 1, case)
93+
handleIdentifier(print, expression)
94+
handleNoTypeArguments(()
95+
beginArguments(()
96+
handleIdentifier(b, expression)
97+
handleNoTypeArguments())
98+
handleNoArguments())
99+
handleSend(b, ))
100+
endArguments(1, (, ))
101+
handleSend(print, ;)
102+
handleExpressionStatement(;)
103+
endSwitchCase(0, 1, null, null, 1, case, case)
104+
beginCaseExpression(case)
105+
handleIdentifier(foo, namedRecordFieldReference)
106+
beginRecordType(()
107+
beginRecordTypeEntry()
108+
beginMetadataStar(int)
109+
endMetadataStar(0)
110+
handleIdentifier(int, typeReference)
111+
handleNoTypeArguments(,)
112+
handleType(int, null)
113+
handleNoName(,)
114+
endRecordTypeEntry()
115+
beginRecordTypeEntry()
116+
beginMetadataStar(int)
117+
endMetadataStar(0)
118+
handleIdentifier(int, typeReference)
119+
handleNoTypeArguments())
120+
handleType(int, null)
121+
handleNoName())
122+
endRecordTypeEntry()
123+
endRecordType((, null, 2, false)
124+
handleVariablePattern(null, b, false)
125+
handlePatternField(:)
126+
handleRecordPattern((, 1)
127+
beginSwitchCaseWhenClause(when)
128+
handleIdentifier(b, expression)
129+
handleNoTypeArguments(!=)
130+
handleNoArguments(!=)
131+
handleSend(b, !=)
132+
beginBinaryExpression(!=)
133+
beginParenthesizedExpressionOrRecordLiteral(()
134+
handleLiteralInt(2)
135+
handleLiteralInt(3)
136+
endRecordLiteral((, 2, null)
137+
endBinaryExpression(!=)
138+
endSwitchCaseWhenClause())
139+
endCaseExpression(case, when, :)
140+
beginSwitchCase(0, 1, case)
141+
handleIdentifier(print, expression)
142+
handleNoTypeArguments(()
143+
beginArguments(()
144+
handleIdentifier(b, expression)
145+
handleNoTypeArguments())
146+
handleNoArguments())
147+
handleSend(b, ))
148+
endArguments(1, (, ))
149+
handleSend(print, ;)
150+
handleExpressionStatement(;)
151+
endSwitchCase(0, 1, null, null, 1, case, case)
152+
beginCaseExpression(case)
153+
handleNoTypeArguments(()
154+
handleIdentifier(field, namedArgumentReference)
155+
handleIdentifier(int, typeReference)
156+
handleNoTypeArguments(b)
157+
handleType(int, null)
158+
handleVariablePattern(null, b, false)
159+
handlePatternField(:)
160+
handleObjectPatternFields(1, (, ))
161+
handleObjectPattern(Class, null, null)
162+
beginSwitchCaseWhenClause(when)
163+
handleIdentifier(b, expression)
164+
handleNoTypeArguments(!=)
165+
handleNoArguments(!=)
166+
handleSend(b, !=)
167+
beginBinaryExpression(!=)
168+
handleLiteralInt(2)
169+
endBinaryExpression(!=)
170+
endSwitchCaseWhenClause(2)
171+
endCaseExpression(case, when, :)
172+
beginSwitchCase(0, 1, case)
173+
handleIdentifier(print, expression)
174+
handleNoTypeArguments(()
175+
beginArguments(()
176+
handleIdentifier(b, expression)
177+
handleNoTypeArguments())
178+
handleNoArguments())
179+
handleSend(b, ))
180+
endArguments(1, (, ))
181+
handleSend(print, ;)
182+
handleExpressionStatement(;)
183+
endSwitchCase(0, 1, null, null, 1, case, case)
184+
beginCaseExpression(case)
185+
handleNoTypeArguments(()
186+
handleIdentifier(field, namedArgumentReference)
187+
beginRecordType(()
188+
beginRecordTypeEntry()
189+
beginMetadataStar(int)
190+
endMetadataStar(0)
191+
handleIdentifier(int, typeReference)
192+
handleNoTypeArguments(,)
193+
handleType(int, null)
194+
handleNoName(,)
195+
endRecordTypeEntry()
196+
beginRecordTypeEntry()
197+
beginMetadataStar(int)
198+
endMetadataStar(0)
199+
handleIdentifier(int, typeReference)
200+
handleNoTypeArguments())
201+
handleType(int, null)
202+
handleNoName())
203+
endRecordTypeEntry()
204+
endRecordType((, null, 2, false)
205+
handleVariablePattern(null, b, false)
206+
handlePatternField(:)
207+
handleObjectPatternFields(1, (, ))
208+
handleObjectPattern(Class, null, null)
209+
beginSwitchCaseWhenClause(when)
210+
handleIdentifier(b, expression)
211+
handleNoTypeArguments(!=)
212+
handleNoArguments(!=)
213+
handleSend(b, !=)
214+
beginBinaryExpression(!=)
215+
beginParenthesizedExpressionOrRecordLiteral(()
216+
handleLiteralInt(2)
217+
handleLiteralInt(3)
218+
endRecordLiteral((, 2, null)
219+
endBinaryExpression(!=)
220+
endSwitchCaseWhenClause())
221+
endCaseExpression(case, when, :)
222+
beginSwitchCase(0, 1, case)
223+
handleIdentifier(print, expression)
224+
handleNoTypeArguments(()
225+
beginArguments(()
226+
handleIdentifier(b, expression)
227+
handleNoTypeArguments())
228+
handleNoArguments())
229+
handleSend(b, ))
230+
endArguments(1, (, ))
231+
handleSend(print, ;)
232+
handleExpressionStatement(;)
233+
endSwitchCase(0, 1, null, null, 1, case, })
234+
endSwitchBlock(4, {, })
235+
endSwitchStatement(switch, })
236+
endBlockFunctionBody(1, {, })
237+
endTopLevelMethod(test, null, })
238+
endTopLevelDeclaration()
239+
endCompilationUnit(2, )

0 commit comments

Comments
 (0)