File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -1791,9 +1791,7 @@ class ExprTypeComputer {
1791
1791
_doAssignToIndex ();
1792
1792
break ;
1793
1793
case UnlinkedConstOperation .extractIndex:
1794
- stack.length -= 2 ;
1795
- // TODO(paulberry): implement.
1796
- stack.add (DynamicTypeImpl .instance);
1794
+ _doExtractIndex ();
1797
1795
break ;
1798
1796
case UnlinkedConstOperation .invokeMethodRef:
1799
1797
_doInvokeMethodRef ();
@@ -1924,6 +1922,20 @@ class ExprTypeComputer {
1924
1922
stack.add (type);
1925
1923
}
1926
1924
1925
+ void _doExtractIndex () {
1926
+ stack.removeLast (); // index
1927
+ DartType target = stack.removeLast ();
1928
+ stack.add (() {
1929
+ if (target is InterfaceType ) {
1930
+ MethodElement method = target.lookUpMethod ('[]' , library);
1931
+ if (method != null ) {
1932
+ return method.returnType;
1933
+ }
1934
+ }
1935
+ return DynamicTypeImpl .instance;
1936
+ }());
1937
+ }
1938
+
1927
1939
void _doExtractProperty () {
1928
1940
DartType target = stack.removeLast ();
1929
1941
String propertyName = _getNextString ();
Original file line number Diff line number Diff line change @@ -209,6 +209,33 @@ class AstInferredTypeTest extends AbstractResynthesizeTest
209
209
super .test_genericMethods_inferJSBuiltin ();
210
210
}
211
211
212
+ void test_infer_extractIndex_custom () {
213
+ var unit = checkFile ('''
214
+ class A {
215
+ String operator [](_) => null;
216
+ }
217
+ var a = new A();
218
+ var b = a[0];
219
+ ''' );
220
+ expect (unit.topLevelVariables[1 ].type.toString (), 'String' );
221
+ }
222
+
223
+ void test_infer_extractIndex_fromList () {
224
+ var unit = checkFile ('''
225
+ var a = <int>[1, 2, 3];
226
+ var b = a[0];
227
+ ''' );
228
+ expect (unit.topLevelVariables[1 ].type.toString (), 'int' );
229
+ }
230
+
231
+ void test_infer_extractIndex_fromMap () {
232
+ var unit = checkFile ('''
233
+ var a = <int, double>{};
234
+ var b = a[0];
235
+ ''' );
236
+ expect (unit.topLevelVariables[1 ].type.toString (), 'double' );
237
+ }
238
+
212
239
void test_infer_extractProperty_getter () {
213
240
checkFile (r'''
214
241
var a = 1.isEven;
You can’t perform that action at this time.
0 commit comments