Issue 41019. Fix for readType of IndexExpression.

Bug: https://github.com/dart-lang/sdk/issues/41019
Change-Id: I861b355c14c6bb30e570bb0c0101871db423ffb0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139821
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov
2020-03-17 17:16:56 +00:00
committed by commit-bot@chromium.org
parent 44669206b3
commit faae2ec2c5
3 changed files with 34 additions and 7 deletions
@@ -204,9 +204,7 @@ class PostfixExpressionResolver {
operand.accept(_resolver);
operand = node.operand;
var operandType = getReadType(
operand,
);
var operandType = getReadType(operand);
var type = _typeSystem.promoteToNonNull(operandType);
_inferenceHelper.recordStaticType(node, type);
@@ -37,10 +37,14 @@ DartType getExpressionType(
DartType getReadType(Expression expression) {
if (expression is IndexExpression) {
var staticElement = expression.auxiliaryElements?.staticElement;
return staticElement == null
? DynamicTypeImpl.instance
: staticElement.returnType;
var aux = expression.auxiliaryElements;
if (aux != null) {
var staticElement = aux.staticElement;
return staticElement == null
? DynamicTypeImpl.instance
: staticElement.returnType;
}
return expression.staticType;
}
{
Element setter;
@@ -159,6 +159,31 @@ main(Function f2) {
''');
}
test_nullCheck_indexExpression() async {
await assertNoErrorsInCode(r'''
main(Map<String, int> a) {
int v = a['foo']!;
v;
}
''');
assertIndexExpression(
findNode.index('a['),
readElement: elementMatcher(
mapElement.getMethod('[]'),
substitution: {'K': 'String', 'V': 'int'},
),
writeElement: null,
type: 'int?',
);
assertPostfixExpression(
findNode.postfix(']!'),
element: null,
type: 'int',
);
}
test_nullCheck_null() async {
await assertNoErrorsInCode('''
main(Null x) {