Skip to content

Commit b60dcdb

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: Remove the create parameter from Variables.decoratedElementType.
We can tell whether a decorated type needs to be created based on whether the library is being migrated. Change-Id: I440c9a310ad103c800de6409d717492bc539b838 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107660 Reviewed-by: Dan Rubel <danrubel@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent 18c21ee commit b60dcdb

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType> {
125125
baseElement.isSynthetic &&
126126
!baseElement.variable.isSynthetic) {
127127
var variable = baseElement.variable;
128-
var decoratedElementType =
129-
_variables.decoratedElementType(variable, create: true);
128+
var decoratedElementType = _variables.decoratedElementType(variable);
130129
if (baseElement.isGetter) {
131130
decoratedBaseType = DecoratedType(baseElement.type, _graph.never,
132131
returnType: decoratedElementType);
@@ -136,8 +135,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType> {
136135
positionalParameters: [decoratedElementType]);
137136
}
138137
} else {
139-
decoratedBaseType =
140-
_variables.decoratedElementType(baseElement, create: true);
138+
decoratedBaseType = _variables.decoratedElementType(baseElement);
141139
}
142140
if (substitution != null) {
143141
DartType elementType;
@@ -302,8 +300,8 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType> {
302300
.substitute(_decoratedClassHierarchy
303301
.getDecoratedSupertype(classElement, superElement)
304302
.asSubstitution);
305-
var superConstructorDecoratedType = _variables
306-
.decoratedElementType(superConstructorElement, create: true);
303+
var superConstructorDecoratedType =
304+
_variables.decoratedElementType(superConstructorElement);
307305
var origin = ImplicitMixinSuperCallOrigin(_source, node.offset);
308306
_unionDecoratedTypeParameters(
309307
constructorDecoratedType, superConstructorDecoratedType, origin);
@@ -828,15 +826,13 @@ $stackTrace''');
828826
for (int i = 0; i < instantiatedType.typeArguments.length; i++) {
829827
_unionDecoratedTypes(
830828
instantiatedType.typeArguments[i],
831-
_variables.decoratedElementType(element.typeParameters[i],
832-
create: true),
829+
_variables.decoratedElementType(element.typeParameters[i]),
833830
origin);
834831
}
835832
} else {
836833
for (int i = 0; i < typeArguments.length; i++) {
837834
DecoratedType bound;
838-
bound = _variables.decoratedElementType(element.typeParameters[i],
839-
create: true);
835+
bound = _variables.decoratedElementType(element.typeParameters[i]);
840836
var argumentType =
841837
_variables.decoratedTypeAnnotation(_source, typeArguments[i]);
842838
if (argumentType == null) {

pkg/nnbd_migration/lib/src/node_builder.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,10 @@ abstract class VariableRepository {
499499
/// Retrieves the [DecoratedType] associated with the static type of the given
500500
/// [element].
501501
///
502-
/// If [create] is `true`, and no decorated type is found for the given
503-
/// element, one is synthesized using [DecoratedType.forElement].
504-
DecoratedType decoratedElementType(Element element, {bool create: false});
502+
/// If no decorated type is found for the given element, and the element is in
503+
/// a library that's not being migrated, a decorated type is synthesized using
504+
/// [DecoratedType.forElement].
505+
DecoratedType decoratedElementType(Element element);
505506

506507
/// Gets the [DecoratedType] associated with the given [typeAnnotation].
507508
DecoratedType decoratedTypeAnnotation(

pkg/nnbd_migration/lib/src/variables.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ class Variables implements VariableRecorder, VariableRepository {
3737
}
3838

3939
@override
40-
DecoratedType decoratedElementType(Element element, {bool create: false}) =>
41-
_decoratedElementTypes[element] ??= create
42-
? DecoratedType.forElement(element, _graph)
43-
: throw StateError('No element found');
40+
DecoratedType decoratedElementType(Element element) =>
41+
_decoratedElementTypes[element] ??= _createDecoratedElementType(element);
4442

4543
@override
4644
DecoratedType decoratedTypeAnnotation(
@@ -77,6 +75,7 @@ class Variables implements VariableRecorder, VariableRepository {
7775
}
7876

7977
void recordDecoratedElementType(Element element, DecoratedType type) {
78+
assert(_graph.isBeingMigrated(element.library.source));
8079
_decoratedElementTypes[element] = type;
8180
}
8281

@@ -155,6 +154,14 @@ class Variables implements VariableRecorder, VariableRepository {
155154
(_potentialModifications[source] ??= []).add(potentialModification);
156155
}
157156

157+
DecoratedType _createDecoratedElementType(Element element) {
158+
if (_graph.isBeingMigrated(element.library.source)) {
159+
throw StateError('A decorated type for $element should have been stored '
160+
'by the NodeBuilder via recordDecoratedElementType');
161+
}
162+
return DecoratedType.forElement(element, _graph);
163+
}
164+
158165
/// Creates an entry [_decoratedDirectSupertypes] for an already-migrated
159166
/// class.
160167
Map<ClassElement, DecoratedType> _decorateDirectSupertypes(

pkg/nnbd_migration/test/abstract_single_unit.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class AbstractSingleUnitTest extends AbstractContextTest {
2121

2222
String testCode;
2323
String testFile;
24+
Uri testUri;
2425
Source testSource;
2526
ResolvedUnitResult testAnalysisResult;
2627
CompilationUnit testUnit;
@@ -35,7 +36,7 @@ class AbstractSingleUnitTest extends AbstractContextTest {
3536
}
3637

3738
Future<void> resolveTestUnit(String code) async {
38-
addTestSource(code);
39+
addTestSource(code, testUri);
3940
testAnalysisResult = await session.getResolvedUnit(testFile);
4041
testUnit = testAnalysisResult.unit;
4142
if (verifyNoTestUnitErrors) {
@@ -59,5 +60,6 @@ class AbstractSingleUnitTest extends AbstractContextTest {
5960
void setUp() {
6061
super.setUp();
6162
testFile = convertPath('/home/test/lib/test.dart');
63+
testUri = Uri.parse('package:test/test.dart');
6264
}
6365
}

0 commit comments

Comments
 (0)