Skip to content

Commit b3260e6

Browse files
committed
Fine. Support for more constant expressions.
Found a few missing cases while running over Flutter. We only need to support valid constant expressions, because we serialize only constant expressions. So, it is OK to say that if there was invalid expression, it does not match with any expression, we will consider it invalidated until the user fixes the code. We support this with `isValid` flag now. * Added support for `identical()`. * Added support for `ImportPrefixReference`. * Added support for `SymbolLiteral`. Change-Id: Ief8714f281cd49b62d242bf85e92c2531b79af95 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443623 Reviewed-by: Johnni Winther <[email protected]>
1 parent a2fe6ce commit b3260e6

File tree

3 files changed

+480
-41
lines changed

3 files changed

+480
-41
lines changed

pkg/analyzer/lib/src/fine/manifest_ast.dart

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ enum ManifestAstElementKind {
4343

4444
/// Enough information to decide if the node is the same.
4545
///
46+
/// We used it to store information AST nodes exposed through the element
47+
/// model: constant initializers, constructor initializers, and annotations.
48+
///
4649
/// We don't store ASTs, instead we rely on the fact that the same tokens
4750
/// are parsed into the same AST (when the same language features, which is
4851
/// ensured outside).
4952
///
5053
/// In addition we record all referenced elements.
5154
class ManifestNode {
55+
/// Whether the encoded AST node has only nodes that we support.
56+
final bool isValid;
57+
5258
/// The concatenated lexemes of all tokens.
5359
final String tokenBuffer;
5460

@@ -86,19 +92,31 @@ class ManifestNode {
8692
);
8793
node.accept(collector);
8894

89-
return ManifestNode._(
90-
tokenBuffer: buffer.toString(),
91-
tokenLengthList: Uint32List.fromList(lengthList),
92-
elements:
93-
collector.map.keys
94-
.map((element) => ManifestElement.encode(context, element))
95-
.toFixedList(),
96-
elementIndexList: Uint32List.fromList(collector.elementIndexList),
97-
);
95+
if (collector.isValid) {
96+
return ManifestNode._(
97+
isValid: true,
98+
tokenBuffer: buffer.toString(),
99+
tokenLengthList: Uint32List.fromList(lengthList),
100+
elements:
101+
collector.map.keys
102+
.map((element) => ManifestElement.encode(context, element))
103+
.toFixedList(),
104+
elementIndexList: Uint32List.fromList(collector.elementIndexList),
105+
);
106+
} else {
107+
return ManifestNode._(
108+
isValid: false,
109+
tokenBuffer: '',
110+
tokenLengthList: Uint32List(0),
111+
elements: const [],
112+
elementIndexList: Uint32List(0),
113+
);
114+
}
98115
}
99116

100117
factory ManifestNode.read(SummaryDataReader reader) {
101118
return ManifestNode._(
119+
isValid: reader.readBool(),
102120
tokenBuffer: reader.readStringUtf8(),
103121
tokenLengthList: reader.readUInt30List(),
104122
elements: ManifestElement.readList(reader),
@@ -107,13 +125,18 @@ class ManifestNode {
107125
}
108126

109127
ManifestNode._({
128+
required this.isValid,
110129
required this.tokenBuffer,
111130
required this.tokenLengthList,
112131
required this.elements,
113132
required this.elementIndexList,
114133
});
115134

116135
bool match(MatchContext context, AstNode node) {
136+
if (!isValid) {
137+
return false;
138+
}
139+
117140
var tokenIndex = 0;
118141
var tokenOffset = 0;
119142
var token = node.beginToken;
@@ -162,6 +185,7 @@ class ManifestNode {
162185
}
163186

164187
void write(BufferedSink sink) {
188+
sink.writeBool(isValid);
165189
sink.writeStringUtf8(tokenBuffer);
166190
sink.writeUint30List(tokenLengthList);
167191
sink.writeList(elements, (e) => e.write(sink));
@@ -177,7 +201,8 @@ class ManifestNode {
177201
}
178202
}
179203

180-
class _ElementCollector extends ThrowingAstVisitor<void> {
204+
class _ElementCollector extends GeneralizingAstVisitor<void> {
205+
bool isValid = true;
181206
final int Function(TypeParameterElementImpl) indexOfTypeParameter;
182207
final int Function(FormalParameterElementImpl) indexOfFormalParameter;
183208
final Map<Element, int> map = Map.identity();
@@ -257,6 +282,11 @@ class _ElementCollector extends ThrowingAstVisitor<void> {
257282
node.visitChildren(this);
258283
}
259284

285+
@override
286+
void visitImportPrefixReference(ImportPrefixReference node) {
287+
_addElement(node.element);
288+
}
289+
260290
@override
261291
void visitInstanceCreationExpression(InstanceCreationExpression node) {
262292
node.visitChildren(this);
@@ -288,6 +318,17 @@ class _ElementCollector extends ThrowingAstVisitor<void> {
288318
node.visitChildren(this);
289319
}
290320

321+
@override
322+
void visitMethodInvocation(MethodInvocation node) {
323+
if (node.methodName.element case TopLevelFunctionElement element) {
324+
if (element.isDartCoreIdentical) {
325+
node.visitChildren(this);
326+
return;
327+
}
328+
}
329+
isValid = false;
330+
}
331+
291332
@override
292333
void visitNamedExpression(NamedExpression node) {
293334
node.expression.accept(this);
@@ -299,6 +340,11 @@ class _ElementCollector extends ThrowingAstVisitor<void> {
299340
_addElement(node.element);
300341
}
301342

343+
@override
344+
void visitNode(AstNode node) {
345+
isValid = false;
346+
}
347+
302348
@override
303349
void visitNullLiteral(NullLiteral node) {}
304350

@@ -364,6 +410,9 @@ class _ElementCollector extends ThrowingAstVisitor<void> {
364410
node.visitChildren(this);
365411
}
366412

413+
@override
414+
void visitSymbolLiteral(SymbolLiteral node) {}
415+
367416
@override
368417
void visitTypeArgumentList(TypeArgumentList node) {
369418
node.visitChildren(this);

0 commit comments

Comments
 (0)