Skip to content

Commit

Permalink
Use promoted type to infer for-each iterables.
Browse files Browse the repository at this point in the history
Fixes #42653.

Change-Id: I90319b775207e6cc1c6b7a79d29b2c237b750845
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162625
Reviewed-by: Konstantin Shcheglov <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Paul Berry <[email protected]>
  • Loading branch information
stereotype441 authored and [email protected] committed Sep 16, 2020
1 parent 8cc4a12 commit 0eec9ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

void forEachStatement(Object x) {
if (x is int) {
/*int*/ x;
for (x in [0]) {
/*int*/ x;
}
}
}

forEachElementInListLiteral(Object x) {
if (x is int) {
/*int*/ x;
return [
for (x in [0]) /*int*/ x
];
}
}

forEachElementInMapLiteral(Object x) {
if (x is int) {
/*int*/ x;
return {
for (x in [0]) /*int*/ x: /*int*/ x
};
}
}

forEachElementInSetLiteral(Object x) {
if (x is int) {
/*int*/ x;
return {
for (x in [0]) /*int*/ x
};
}
}
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/resolver/for_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ForResolver {
if (identifier != null) {
identifierElement = identifier.staticElement;
if (identifierElement is VariableElement) {
valueType = identifierElement.type;
valueType = _resolver.localVariableTypeProvider.getType(identifier);
} else if (identifierElement is PropertyAccessorElement) {
var parameters = identifierElement.parameters;
if (parameters.isNotEmpty) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/front_end/lib/src/fasta/kernel/inference_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6061,8 +6061,14 @@ class LocalForInVariable implements ForInVariable {

LocalForInVariable(this.variableSet);

DartType computeElementType(TypeInferrerImpl inferrer) =>
variableSet.variable.type;
DartType computeElementType(TypeInferrerImpl inferrer) {
VariableDeclaration variable = variableSet.variable;
DartType promotedType;
if (inferrer.isNonNullableByDefault) {
promotedType = inferrer.flowAnalysis.promotedType(variable);
}
return promotedType ?? variable.type;
}

Expression inferAssignment(TypeInferrerImpl inferrer, DartType rhsType) {
Expression rhs = inferrer.ensureAssignable(
Expand Down

0 comments on commit 0eec9ee

Please sign in to comment.