Skip to content

Commit 276aa29

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2js] Clone call receiver for protobuf replacement placeholder.
Adding a reference to node.receiver in the replacement placeholder was modifying its parent pointer eagerly. When running Dart2js with serialization the pointers were reset by the serialization process after the closed world phase. However, when running without serialization the pointer stayed corrupted and lead to a malfomed tree in later phases. This manifested as an error trying to lookup the location (i.e. the kernel Location) of the receiver. This fixes the issue by cloning the receiver of the call for the placeholder. This avoids updating the original receiver's parent pointer. Today the receiver is always a VariableGet referencing a variable defined in a surrounding Let scope (this is how the CFE encodes x..a()..b()). We could cast the receiver but to be a bit more resilient I've opted to clone the node. The cloner in the kernel package fails on free variable (i.e. if the variable is not declared in the cloning scope). But in this case we want to simply use the same declaration reference from the cloned node. I've added a cloner that provides that fallback behavior. I also considered using a closure to lazily create the replacement but closures are expensive and we'd be creating one per field per proto which would be a considerable cost for large programs. There wasn't really a clean way to do the same with a static tearoff. Change-Id: I7ef0e24a9457cfa9f2e116eb3569b0652d321c08 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352563 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Mayank Patke <[email protected]>
1 parent 6cfb95c commit 276aa29

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pkg/compiler/lib/src/ir/protobuf_impacts.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:kernel/ast.dart' as ir;
6+
import 'package:kernel/clone.dart' as ir;
67
import 'package:kernel/type_algebra.dart' as ir;
78

89
import '../kernel/element_map.dart';
@@ -139,7 +140,7 @@ class ProtobufImpactHandler implements ConditionalImpactHandler {
139140
ir.InstanceInvocation node) {
140141
return ir.InstanceInvocation(
141142
ir.InstanceAccessKind.Instance,
142-
node.receiver,
143+
_CloneVisitorLenientVariables().clone(node.receiver),
143144
_builderInfoAddMethod.name,
144145
ir.Arguments(
145146
<ir.Expression>[
@@ -207,3 +208,12 @@ class ProtobufImpactHandler implements ConditionalImpactHandler {
207208
source: node, replacement: _buildProtobufMetadataPlaceholder(node)));
208209
}
209210
}
211+
212+
/// Clones nodes but returns same variable declaration on VariableGet if the
213+
/// declaration is not in scope.
214+
class _CloneVisitorLenientVariables extends ir.CloneVisitorNotMembers {
215+
@override
216+
ir.VariableDeclaration getVariableClone(ir.VariableDeclaration variable) {
217+
return super.getVariableClone(variable) ?? variable;
218+
}
219+
}

0 commit comments

Comments
 (0)