Skip to content

Commit 43d68f9

Browse files
natebiggsCommit Bot
authored and
Commit Bot
committed
[dart2js] Migrate load_kernel.dart to nnbd
Change-Id: If3e8925d5e6888f6c888ca6c81772a3e83f94bad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247285 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Joshua Litt <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent 62e8e9d commit 43d68f9

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

pkg/compiler/lib/src/phase/load_kernel.dart

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
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
import 'dart:async';
86

7+
import 'package:collection/collection.dart';
98
import 'package:front_end/src/fasta/kernel/utils.dart';
109
import 'package:kernel/ast.dart' as ir;
1110
import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder;
@@ -49,7 +48,7 @@ class Output {
4948
/// The [Uri] of the root library containing main.
5049
/// Note: rootLibraryUri will be null for some modules, for example in the
5150
/// case of dependent libraries processed modularly.
52-
final Uri rootLibraryUri;
51+
final Uri? rootLibraryUri;
5352

5453
/// Returns the [Uri]s of all libraries that have been loaded that are
5554
/// reachable from the [rootLibraryUri].
@@ -75,25 +74,25 @@ class Output {
7574
}
7675

7776
Library _findEntryLibrary(Component component, Uri entryUri) {
78-
var entryLibrary = component.libraries
79-
.firstWhere((l) => l.fileUri == entryUri, orElse: () => null);
77+
final entryLibrary =
78+
component.libraries.firstWhereOrNull((l) => l.fileUri == entryUri);
8079
if (entryLibrary == null) {
8180
throw ArgumentError('Entry uri $entryUri not found in dill.');
8281
}
8382
return entryLibrary;
8483
}
8584

8685
ir.Reference _findMainMethod(Library entryLibrary) {
87-
var mainMethod = entryLibrary.procedures
88-
.firstWhere((p) => p.name.text == 'main', orElse: () => null);
86+
var mainMethod =
87+
entryLibrary.procedures.firstWhereOrNull((p) => p.name.text == 'main');
8988

9089
// In some cases, a main method is defined in another file, and then
9190
// exported. In these cases, we search for the main method in
9291
// [additionalExports].
93-
ir.Reference mainMethodReference;
92+
ir.Reference? mainMethodReference;
9493
if (mainMethod == null) {
9594
mainMethodReference = entryLibrary.additionalExports
96-
.firstWhere((p) => p.canonicalName.name == 'main', orElse: () => null);
95+
.firstWhereOrNull((p) => p.canonicalName?.name == 'main');
9796
} else {
9897
mainMethodReference = mainMethod.reference;
9998
}
@@ -121,8 +120,8 @@ void _validateNullSafetyMode(CompilerOptions options) {
121120
}
122121

123122
class _LoadFromKernelResult {
124-
final ir.Component component;
125-
final Library entryLibrary;
123+
final ir.Component? component;
124+
final Library? entryLibrary;
126125
final List<Uri> moduleLibraries;
127126

128127
_LoadFromKernelResult(
@@ -135,8 +134,8 @@ void _doGlobalTransforms(Component component) {
135134

136135
Future<_LoadFromKernelResult> _loadFromKernel(CompilerOptions options,
137136
api.CompilerInput compilerInput, String targetName) async {
138-
Library entryLibrary;
139-
var resolvedUri = options.compilationTarget;
137+
Library? entryLibrary;
138+
var resolvedUri = options.compilationTarget!;
140139
ir.Component component = ir.Component();
141140
List<Uri> moduleLibraries = [];
142141

@@ -171,24 +170,24 @@ Future<_LoadFromKernelResult> _loadFromKernel(CompilerOptions options,
171170
options.modularMode || options.hasModularAnalysisInputs;
172171
if (options.platformBinaries != null && !platformBinariesIncluded) {
173172
var platformUri = options.platformBinaries
174-
.resolve(_getPlatformFilename(options, targetName));
173+
?.resolve(_getPlatformFilename(options, targetName));
175174
// Modular analysis can be run on the sdk by providing directly the
176175
// path to the platform.dill file. In that case, we do not load the
177176
// platform file implicitly.
178177
// TODO(joshualitt): Change how we detect this case so it is less
179178
// brittle.
180-
if (platformUri != resolvedUri) await read(platformUri);
179+
if (platformUri != resolvedUri) await read(platformUri!);
181180
}
182181

183182
// Concatenate dills.
184183
if (options.dillDependencies != null) {
185-
for (Uri dependency in options.dillDependencies) {
184+
for (Uri dependency in options.dillDependencies!) {
186185
await read(dependency);
187186
}
188187
}
189188

190189
if (options.entryUri != null) {
191-
entryLibrary = _findEntryLibrary(component, options.entryUri);
190+
entryLibrary = _findEntryLibrary(component, options.entryUri!);
192191
var mainMethod = _findMainMethod(entryLibrary);
193192
component.setMainMethodAndMode(mainMethod, true, component.mode);
194193
}
@@ -201,7 +200,7 @@ Future<_LoadFromKernelResult> _loadFromKernel(CompilerOptions options,
201200
}
202201

203202
class _LoadFromSourceResult {
204-
final ir.Component component;
203+
final ir.Component? component;
205204
final fe.InitializedCompilerState initializedCompilerState;
206205
final List<Uri> moduleLibraries;
207206

@@ -217,7 +216,7 @@ Future<_LoadFromSourceResult> _loadFromSource(
217216
String targetName) async {
218217
bool verbose = false;
219218
bool cfeConstants = options.features.cfeConstants.isEnabled;
220-
Map<String, String> environment = cfeConstants ? options.environment : null;
219+
Map<String, String>? environment = cfeConstants ? options.environment : null;
221220
Target target = Dart2jsTarget(targetName, TargetFlags(),
222221
options: options,
223222
canPerformGlobalTransforms: true,
@@ -237,7 +236,7 @@ Future<_LoadFromSourceResult> _loadFromSource(
237236
List<Uri> sources = [];
238237
if (options.sources != null) {
239238
isModularCompile = true;
240-
sources.addAll(options.sources);
239+
sources.addAll(options.sources!);
241240
} else {
242241
fe.CompilerOptions feOptions = fe.CompilerOptions()
243242
..target = target
@@ -249,22 +248,22 @@ Future<_LoadFromSourceResult> _loadFromSource(
249248
..fileSystem = fileSystem
250249
..onDiagnostic = onDiagnostic
251250
..verbosity = verbosity;
252-
Uri resolvedUri = options.compilationTarget;
251+
Uri resolvedUri = options.compilationTarget!;
253252
bool isLegacy =
254253
await fe.uriUsesLegacyLanguageVersion(resolvedUri, feOptions);
255254
_inferNullSafetyMode(options, !isLegacy);
256-
sources.add(options.compilationTarget);
255+
sources.add(options.compilationTarget!);
257256
}
258257

259258
// If we are performing a modular compile, we expect the platform binary to be
260259
// supplied along with other dill dependencies.
261260
List<Uri> dependencies = [];
262261
if (options.platformBinaries != null && !isModularCompile) {
263-
dependencies.add(options.platformBinaries
262+
dependencies.add(options.platformBinaries!
264263
.resolve(_getPlatformFilename(options, targetName)));
265264
}
266265
if (options.dillDependencies != null) {
267-
dependencies.addAll(options.dillDependencies);
266+
dependencies.addAll(options.dillDependencies!);
268267
}
269268

270269
initializedCompilerState = fe.initializeCompiler(
@@ -279,14 +278,14 @@ Future<_LoadFromSourceResult> _loadFromSource(
279278
options.useLegacySubtyping ? fe.NnbdMode.Weak : fe.NnbdMode.Strong,
280279
invocationModes: options.cfeInvocationModes,
281280
verbosity: verbosity);
282-
ir.Component component = await fe.compile(initializedCompilerState, verbose,
281+
ir.Component? component = await fe.compile(initializedCompilerState, verbose,
283282
fileSystem, onDiagnostic, sources, isModularCompile);
284283
_validateNullSafetyMode(options);
285284

286285
// We have to compute canonical names on the component here to avoid missing
287286
// canonical names downstream.
288287
if (isModularCompile) {
289-
component.computeCanonicalNames();
288+
component?.computeCanonicalNames();
290289
}
291290
return _LoadFromSourceResult(
292291
component, initializedCompilerState, isModularCompile ? sources : []);
@@ -295,11 +294,11 @@ Future<_LoadFromSourceResult> _loadFromSource(
295294
Output _createOutput(
296295
CompilerOptions options,
297296
DiagnosticReporter reporter,
298-
Library entryLibrary,
297+
Library? entryLibrary,
299298
ir.Component component,
300299
List<Uri> moduleLibraries,
301300
fe.InitializedCompilerState initializedCompilerState) {
302-
Uri rootLibraryUri = null;
301+
Uri? rootLibraryUri = null;
303302
Iterable<ir.Library> libraries = component.libraries;
304303
if (!options.modularMode) {
305304
// For non-modular builds we should always have a [mainMethod] at this
@@ -317,7 +316,7 @@ Output _createOutput(
317316
// NOTE: Under some circumstances, the [entryLibrary] exports the
318317
// [mainMethod] from another library, and thus the [enclosingLibrary] of
319318
// the [mainMethod] may not be the same as the [entryLibrary].
320-
var root = entryLibrary ?? component.mainMethod.enclosingLibrary;
319+
var root = entryLibrary ?? component.mainMethod!.enclosingLibrary;
321320
rootLibraryUri = root.importUri;
322321

323322
// Filter unreachable libraries: [Component] was built by linking in the
@@ -360,15 +359,15 @@ Output _createOutput(
360359
}
361360

362361
/// Loads an entire Kernel [Component] from a file on disk.
363-
Future<Output> run(Input input) async {
362+
Future<Output?> run(Input input) async {
364363
CompilerOptions options = input.options;
365364
api.CompilerInput compilerInput = input.compilerInput;
366365
DiagnosticReporter reporter = input.reporter;
367366

368367
String targetName = options.compileForServer ? "dart2js_server" : "dart2js";
369368

370-
Library entryLibrary;
371-
ir.Component component;
369+
Library? entryLibrary;
370+
ir.Component? component;
372371
List<Uri> moduleLibraries = const [];
373372
fe.InitializedCompilerState initializedCompilerState =
374373
input.initializedCompilerState;

pkg/front_end/lib/src/api_unstable/dart2js.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ export 'compiler_state.dart' show InitializedCompilerState;
125125
InitializedCompilerState initializeCompiler(
126126
InitializedCompilerState? oldState,
127127
Target target,
128-
Uri librariesSpecificationUri,
128+
Uri? librariesSpecificationUri,
129129
List<Uri> additionalDills,
130-
Uri packagesFileUri,
130+
Uri? packagesFileUri,
131131
{required Map<ExperimentalFlag, bool> explicitExperimentalFlags,
132132
Map<String, String>? environmentDefines,
133133
bool verify: false,

0 commit comments

Comments
 (0)