Skip to content

Commit 6539e20

Browse files
natebiggsCommit Queue
authored and
Commit Queue
committed
[dart2js] Migrate inferrer/type_graph_dump.dart to null safety.
Change-Id: Idc6af28c0bb257e78f11aa4c89ba04c1a67aebed Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/267242 Commit-Queue: Nate Biggs <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 2bfabc6 commit 6539e20

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

pkg/compiler/lib/src/inferrer/type_graph_dump.dart

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.10
6-
75
library dart2js.inferrer.type_graph_dump;
86

97
import '../../compiler_api.dart' as api;
@@ -70,22 +68,21 @@ class TypeGraphDump {
7068
Map<MemberEntity, List<TypeInformation>> nodes =
7169
<MemberEntity, List<TypeInformation>>{};
7270
for (TypeInformation node in inferrer.types.allTypes) {
73-
if (node.contextMember != null) {
74-
nodes
75-
.putIfAbsent(node.contextMember, () => <TypeInformation>[])
76-
.add(node);
71+
final contextMember = node.contextMember;
72+
if (contextMember != null) {
73+
nodes.putIfAbsent(contextMember, () => <TypeInformation>[]).add(node);
7774
}
7875
}
7976
// Print every group separately.
8077
for (MemberEntity element in nodes.keys) {
81-
api.OutputSink output;
78+
api.OutputSink? output;
8279
try {
8380
String name = filenameFromElement(element);
8481
output = compilerOutput.createOutputSink(
8582
'$outputDir/$name', 'dot', api.OutputType.debug);
8683
_GraphGenerator visitor = _GraphGenerator(
8784
this, element, output, inferrer.abstractValueDomain.getCompactText);
88-
for (TypeInformation node in nodes[element]) {
85+
for (TypeInformation node in nodes[element]!) {
8986
visitor.visit(node);
9087
}
9188
visitor.addMissingNodes();
@@ -106,22 +103,22 @@ class TypeGraphDump {
106103
String filenameFromElement(MemberEntity element) {
107104
// The toString method of elements include characters that are unsuitable
108105
// for URIs and file systems.
109-
List<String> parts = <String>[];
106+
List<String?> parts = [];
110107
parts.add(element.library.canonicalUri.pathSegments.last);
111108
parts.add(element.enclosingClass?.name);
112109
if (element.isGetter) {
113110
parts.add('get-${element.name}');
114111
} else if (element.isSetter) {
115112
parts.add('set-${element.name}');
116113
} else if (element.isConstructor) {
117-
if (element.name.isEmpty) {
114+
if (element.name!.isEmpty) {
118115
parts.add('-constructor');
119116
} else {
120117
parts.add(element.name);
121118
}
122119
} else {
123120
parts.add(
124-
utils.operatorNameToIdentifier(element.name).replaceAll(r'$', '-'));
121+
utils.operatorNameToIdentifier(element.name)!.replaceAll(r'$', '-'));
125122
}
126123
String filename = parts.where((x) => x != null && x != '').join('.');
127124
if (usedFilenames.add(filename)) return filename;
@@ -145,10 +142,10 @@ class _GraphGenerator extends TypeInformationVisitor {
145142
int usedIds = 0;
146143
final api.OutputSink output;
147144
final MemberEntity element;
148-
TypeInformation returnValue;
145+
final TypeInformation returnValue;
149146

150-
_GraphGenerator(this.global, this.element, this.output, this.formatType) {
151-
returnValue = global.inferrer.types.getInferredTypeOfMember(element);
147+
_GraphGenerator(this.global, this.element, this.output, this.formatType)
148+
: returnValue = global.inferrer.types.getInferredTypeOfMember(element) {
152149
getNode(returnValue); // Ensure return value is part of graph.
153150
append('digraph {');
154151
}
@@ -210,7 +207,7 @@ class _GraphGenerator extends TypeInformationVisitor {
210207
/// If [dst] is a record type node, [port] may refer to one of the fields
211208
/// defined in that record (e.g. `obj`, `arg0`, `arg1`, etc)
212209
void addEdge(TypeInformation src, TypeInformation dst,
213-
{String port, String color = 'black'}) {
210+
{String? port, String? color = 'black'}) {
214211
if (isExternal(src) && isExternal(dst)) {
215212
return; // Do not add edges between external nodes.
216213
}
@@ -273,7 +270,7 @@ class _GraphGenerator extends TypeInformationVisitor {
273270
/// [inputs] specify named inputs to the node. If omitted, edges will be
274271
/// based on [node.inputs].
275272
void addNode(TypeInformation node, String text,
276-
{String color = defaultNodeColor, Map<String, TypeInformation> inputs}) {
273+
{String color = defaultNodeColor, Map<String, TypeInformation>? inputs}) {
277274
seen.add(node);
278275
String style = getStyleForNode(node, color);
279276
text = appendDetails(node, text);
@@ -286,15 +283,15 @@ class _GraphGenerator extends TypeInformationVisitor {
286283
String label = '{{$header}|$text|<returnType> $returnType}';
287284
append('$id [shape=record,label="$label",$style]');
288285
for (String key in keys) {
289-
addEdge(inputs[key], node, port: 'a$key');
286+
addEdge(inputs[key]!, node, port: 'a$key');
290287
}
291288
} else {
292289
String label = '{$text|<returnType> $returnType}';
293290
append('$id [shape=record,label="$label",$style]');
294291
// Add assignment edges. Color the edges based on whether they were
295292
// added, removed, temporary, or unchanged.
296-
dynamic originalSet = global.assignmentsBeforeAnalysis[node] ?? const [];
297-
var tracerSet = global.assignmentsBeforeTracing[node] ?? const [];
293+
final originalSet = global.assignmentsBeforeAnalysis[node] ?? const {};
294+
var tracerSet = global.assignmentsBeforeTracing[node] ?? const {};
298295
var currentSet = node.inputs.toSet();
299296
for (TypeInformation assignment in currentSet) {
300297
String color =
@@ -393,12 +390,13 @@ class _GraphGenerator extends TypeInformationVisitor {
393390
Map<String, TypeInformation> inputs) {
394391
String sourceCode = shorten('${info.debugName}');
395392
text = '$text\n$sourceCode';
396-
if (info.arguments != null) {
397-
for (int i = 0; i < info.arguments.positional.length; ++i) {
398-
inputs['arg$i'] = info.arguments.positional[i];
393+
final arguments = info.arguments;
394+
if (arguments != null) {
395+
for (int i = 0; i < arguments.positional.length; ++i) {
396+
inputs['arg$i'] = arguments.positional[i];
399397
}
400-
for (String argName in info.arguments.named.keys) {
401-
inputs[argName] = info.arguments.named[argName];
398+
for (String argName in arguments.named.keys) {
399+
inputs[argName] = arguments.named[argName]!;
402400
}
403401
}
404402
addNode(info, text, color: callColor, inputs: inputs);

pkg/compiler/lib/src/inferrer_experimental/type_graph_dump.dart

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.10
6-
75
library dart2js.inferrer.type_graph_dump;
86

97
import '../../compiler_api.dart' as api;
@@ -70,22 +68,21 @@ class TypeGraphDump {
7068
Map<MemberEntity, List<TypeInformation>> nodes =
7169
<MemberEntity, List<TypeInformation>>{};
7270
for (TypeInformation node in inferrer.types.allTypes) {
73-
if (node.contextMember != null) {
74-
nodes
75-
.putIfAbsent(node.contextMember, () => <TypeInformation>[])
76-
.add(node);
71+
final contextMember = node.contextMember;
72+
if (contextMember != null) {
73+
nodes.putIfAbsent(contextMember, () => <TypeInformation>[]).add(node);
7774
}
7875
}
7976
// Print every group separately.
8077
for (MemberEntity element in nodes.keys) {
81-
api.OutputSink output;
78+
api.OutputSink? output;
8279
try {
8380
String name = filenameFromElement(element);
8481
output = compilerOutput.createOutputSink(
8582
'$outputDir/$name', 'dot', api.OutputType.debug);
8683
_GraphGenerator visitor = _GraphGenerator(
8784
this, element, output, inferrer.abstractValueDomain.getCompactText);
88-
for (TypeInformation node in nodes[element]) {
85+
for (TypeInformation node in nodes[element]!) {
8986
visitor.visit(node);
9087
}
9188
visitor.addMissingNodes();
@@ -106,22 +103,22 @@ class TypeGraphDump {
106103
String filenameFromElement(MemberEntity element) {
107104
// The toString method of elements include characters that are unsuitable
108105
// for URIs and file systems.
109-
List<String> parts = <String>[];
106+
List<String?> parts = [];
110107
parts.add(element.library.canonicalUri.pathSegments.last);
111108
parts.add(element.enclosingClass?.name);
112109
if (element.isGetter) {
113110
parts.add('get-${element.name}');
114111
} else if (element.isSetter) {
115112
parts.add('set-${element.name}');
116113
} else if (element.isConstructor) {
117-
if (element.name.isEmpty) {
114+
if (element.name!.isEmpty) {
118115
parts.add('-constructor');
119116
} else {
120117
parts.add(element.name);
121118
}
122119
} else {
123120
parts.add(
124-
utils.operatorNameToIdentifier(element.name).replaceAll(r'$', '-'));
121+
utils.operatorNameToIdentifier(element.name)!.replaceAll(r'$', '-'));
125122
}
126123
String filename = parts.where((x) => x != null && x != '').join('.');
127124
if (usedFilenames.add(filename)) return filename;
@@ -145,10 +142,10 @@ class _GraphGenerator extends TypeInformationVisitor {
145142
int usedIds = 0;
146143
final api.OutputSink output;
147144
final MemberEntity element;
148-
TypeInformation returnValue;
145+
final TypeInformation returnValue;
149146

150-
_GraphGenerator(this.global, this.element, this.output, this.formatType) {
151-
returnValue = global.inferrer.types.getInferredTypeOfMember(element);
147+
_GraphGenerator(this.global, this.element, this.output, this.formatType)
148+
: returnValue = global.inferrer.types.getInferredTypeOfMember(element) {
152149
getNode(returnValue); // Ensure return value is part of graph.
153150
append('digraph {');
154151
}
@@ -210,7 +207,7 @@ class _GraphGenerator extends TypeInformationVisitor {
210207
/// If [dst] is a record type node, [port] may refer to one of the fields
211208
/// defined in that record (e.g. `obj`, `arg0`, `arg1`, etc)
212209
void addEdge(TypeInformation src, TypeInformation dst,
213-
{String port, String color = 'black'}) {
210+
{String? port, String? color = 'black'}) {
214211
if (isExternal(src) && isExternal(dst)) {
215212
return; // Do not add edges between external nodes.
216213
}
@@ -273,7 +270,7 @@ class _GraphGenerator extends TypeInformationVisitor {
273270
/// [inputs] specify named inputs to the node. If omitted, edges will be
274271
/// based on [node.inputs].
275272
void addNode(TypeInformation node, String text,
276-
{String color = defaultNodeColor, Map<String, TypeInformation> inputs}) {
273+
{String color = defaultNodeColor, Map<String, TypeInformation>? inputs}) {
277274
seen.add(node);
278275
String style = getStyleForNode(node, color);
279276
text = appendDetails(node, text);
@@ -286,15 +283,15 @@ class _GraphGenerator extends TypeInformationVisitor {
286283
String label = '{{$header}|$text|<returnType> $returnType}';
287284
append('$id [shape=record,label="$label",$style]');
288285
for (String key in keys) {
289-
addEdge(inputs[key], node, port: 'a$key');
286+
addEdge(inputs[key]!, node, port: 'a$key');
290287
}
291288
} else {
292289
String label = '{$text|<returnType> $returnType}';
293290
append('$id [shape=record,label="$label",$style]');
294291
// Add assignment edges. Color the edges based on whether they were
295292
// added, removed, temporary, or unchanged.
296-
dynamic originalSet = global.assignmentsBeforeAnalysis[node] ?? const [];
297-
var tracerSet = global.assignmentsBeforeTracing[node] ?? const [];
293+
final originalSet = global.assignmentsBeforeAnalysis[node] ?? const {};
294+
var tracerSet = global.assignmentsBeforeTracing[node] ?? const {};
298295
var currentSet = node.inputs.toSet();
299296
for (TypeInformation assignment in currentSet) {
300297
String color =
@@ -393,12 +390,13 @@ class _GraphGenerator extends TypeInformationVisitor {
393390
Map<String, TypeInformation> inputs) {
394391
String sourceCode = shorten('${info.debugName}');
395392
text = '$text\n$sourceCode';
396-
if (info.arguments != null) {
397-
for (int i = 0; i < info.arguments.positional.length; ++i) {
398-
inputs['arg$i'] = info.arguments.positional[i];
393+
final arguments = info.arguments;
394+
if (arguments != null) {
395+
for (int i = 0; i < arguments.positional.length; ++i) {
396+
inputs['arg$i'] = arguments.positional[i];
399397
}
400-
for (String argName in info.arguments.named.keys) {
401-
inputs[argName] = info.arguments.named[argName];
398+
for (String argName in arguments.named.keys) {
399+
inputs[argName] = arguments.named[argName]!;
402400
}
403401
}
404402
addNode(info, text, color: callColor, inputs: inputs);

0 commit comments

Comments
 (0)