Skip to content

Commit

Permalink
[ddc] Migrate shared_command to null safety
Browse files Browse the repository at this point in the history
Update CFE API for ddc to mirror the nullability of fields in
`CompilerOptions`.

Issue: #46617
Change-Id: I08d8cc2c5bb1dba4823cb3a7e1f4a540fb3bfef5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/248181
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
nshahan authored and Commit Bot committed Jun 21, 2022
1 parent 338fb1d commit c53c689
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
51 changes: 21 additions & 30 deletions pkg/dev_compiler/lib/src/compiler/shared_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:io';

import 'package:args/args.dart';
Expand Down Expand Up @@ -84,7 +82,7 @@ class SharedCompilerOptions {
final String multiRootScheme;

/// Path to set multi-root files relative to when generating source-maps.
final String multiRootOutputPath;
final String? multiRootOutputPath;

/// Experimental language features that are enabled/disabled, see
/// [the spec](https://github.com/dart-lang/sdk/blob/master/docs/process/experimental-flags.md)
Expand All @@ -107,8 +105,8 @@ class SharedCompilerOptions {
this.emitFullCompiledKernel = false,
this.summaryModules = const {},
this.moduleFormats = const [],
this.moduleName,
this.multiRootScheme,
required this.moduleName,
this.multiRootScheme = 'org-dartlang-app',
this.multiRootOutputPath,
this.experiments = const {},
this.soundNullSafety = false,
Expand All @@ -132,7 +130,7 @@ class SharedCompilerOptions {
moduleFormats: parseModuleFormatOption(args),
moduleName: _getModuleName(args),
multiRootScheme: args['multi-root-scheme'] as String,
multiRootOutputPath: args['multi-root-output-path'] as String,
multiRootOutputPath: args['multi-root-output-path'] as String?,
experiments: parseExperimentalArguments(
args['enable-experiment'] as List<String>),
soundNullSafety: args['sound-null-safety'] as bool,
Expand All @@ -147,7 +145,7 @@ class SharedCompilerOptions {
moduleName:
args['module-name'] != null ? _getModuleName(args) : 'dart_sdk',
multiRootScheme: args['multi-root-scheme'] as String,
multiRootOutputPath: args['multi-root-output-path'] as String,
multiRootOutputPath: args['multi-root-output-path'] as String?,
experiments: parseExperimentalArguments(
args['enable-experiment'] as List<String>),
soundNullSafety: args['sound-null-safety'] as bool,
Expand Down Expand Up @@ -230,18 +228,15 @@ class SharedCompilerOptions {
}

static String _getModuleName(ArgResults args) {
var moduleName = args['module-name'] as String;
var moduleName = args['module-name'] as String?;
if (moduleName == null) {
var outPaths = args['out'];
var outPath = outPaths is String
? outPaths
: (outPaths as List<String>)
.firstWhere((_) => true, orElse: () => null);

// TODO(jmesserly): fix the debugger console so it's not passing invalid
// options.
if (outPath == null) return null;

var outPaths = args['out'] as List<String>;
if (outPaths.isEmpty) {
throw UnsupportedError(
'No module name provided and unable to synthesize one without any '
'output paths.');
}
var outPath = outPaths.first;
moduleName = p.basenameWithoutExtension(outPath);
}
// TODO(jmesserly): this should probably use sourcePathToUri.
Expand All @@ -260,9 +255,8 @@ class SharedCompilerOptions {
/// allow working with summaries whose physical location is outside of the
/// module root directory.
Map<String, String> _parseCustomSummaryModules(List<String> summaryPaths,
[String moduleRoot, String summaryExt]) {
[String? moduleRoot, String? summaryExt]) {
var pathToModule = <String, String>{};
if (summaryPaths == null) return pathToModule;
for (var summaryPath in summaryPaths) {
var equalSign = summaryPath.indexOf('=');
String modulePath;
Expand Down Expand Up @@ -299,7 +293,7 @@ List<String> filterUnknownArguments(List<String> args, ArgParser parser) {
if (abbreviation != null) {
knownAbbreviations.add(abbreviation);
}
if (option.negatable) {
if (option.negatable != null && option.negatable!) {
knownOptions.add('no-$name');
}
});
Expand Down Expand Up @@ -331,10 +325,7 @@ List<String> filterUnknownArguments(List<String> args, ArgParser parser) {

/// Convert a [source] string to a Uri, where the source may be a
/// dart/file/package URI or a local win/mac/linux path.
///
/// If [source] is null, this will return null.
Uri sourcePathToUri(String source, {bool windows}) {
if (source == null) return null;
Uri sourcePathToUri(String source, {bool? windows}) {
if (windows == null) {
// Running on the web the Platform check will fail, and we can't use
// fromEnvironment because internally it's set to true for dart.library.io.
Expand All @@ -358,7 +349,7 @@ Uri sourcePathToUri(String source, {bool windows}) {
return result;
}

Uri sourcePathToRelativeUri(String source, {bool windows}) {
Uri sourcePathToRelativeUri(String source, {bool? windows}) {
var uri = sourcePathToUri(source, windows: windows);
if (uri.isScheme('file')) {
var uriPath = uri.path;
Expand Down Expand Up @@ -388,8 +379,8 @@ Uri sourcePathToRelativeUri(String source, {bool windows}) {
// TODO(#40251): Remove this logic from dev_compiler itself, push it to the
// invokers of dev_compiler which have more knowledge about how they want
// source paths to look.
Map placeSourceMap(Map sourceMap, String sourceMapPath, String multiRootScheme,
{String multiRootOutputPath, String sourceMapBase}) {
Map placeSourceMap(Map sourceMap, String sourceMapPath, String? multiRootScheme,
{String? multiRootOutputPath, String? sourceMapBase}) {
var map = Map.from(sourceMap);
// Convert to a local file path if it's not.
sourceMapPath = sourcePathToUri(p.absolute(p.fromUri(sourceMapPath))).path;
Expand Down Expand Up @@ -446,15 +437,15 @@ class CompilerResult {
/// Optionally provides the front_end state from the previous compilation,
/// which can be passed to [compile] to potentially speed up the next
/// compilation.
final InitializedCompilerState kernelState;
final InitializedCompilerState? kernelState;

/// The process exit code of the compiler.
final int exitCode;

CompilerResult(this.exitCode, {this.kernelState});

/// Gets the kernel compiler state, if any.
Object get compilerState => kernelState;
Object? get compilerState => kernelState;

/// Whether the program compiled without any fatal errors (equivalent to
/// [exitCode] == 0).
Expand Down
4 changes: 2 additions & 2 deletions pkg/dev_compiler/lib/src/kernel/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Future<CompilerResult> _compile(List<String> args,
compileSdk,
sourcePathToUri(getSdkPath()),
compileSdk ? null : sourcePathToUri(sdkSummaryPath),
sourcePathToUri(packageFile),
packageFile != null ? sourcePathToUri(packageFile) : null,
sourcePathToUri(librarySpecPath),
additionalDills,
DevCompilerTarget(TargetFlags(
Expand Down Expand Up @@ -314,7 +314,7 @@ Future<CompilerResult> _compile(List<String> args,
compileSdk,
sourcePathToUri(getSdkPath()),
compileSdk ? null : sourcePathToUri(sdkSummaryPath),
sourcePathToUri(packageFile),
packageFile != null ? sourcePathToUri(packageFile) : null,
sourcePathToUri(librarySpecPath),
additionalDills,
inputDigests,
Expand Down
18 changes: 9 additions & 9 deletions pkg/front_end/lib/src/api_unstable/ddc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ class DdcResult {
InitializedCompilerState initializeCompiler(
InitializedCompilerState? oldState,
bool compileSdk,
Uri sdkRoot,
Uri sdkSummary,
Uri packagesFile,
Uri librariesSpecificationUri,
Uri? sdkRoot,
Uri? sdkSummary,
Uri? packagesFile,
Uri? librariesSpecificationUri,
List<Uri> additionalDills,
Target target,
{FileSystem? fileSystem,
Expand Down Expand Up @@ -166,14 +166,14 @@ InitializedCompilerState initializeCompiler(
/// Re-uses cached components from [oldState.workerInputCache], and reloads them
/// as necessary based on [workerInputDigests].
Future<InitializedCompilerState> initializeIncrementalCompiler(
InitializedCompilerState oldState,
InitializedCompilerState? oldState,
Set<String> tags,
List<Component> doneAdditionalDills,
bool compileSdk,
Uri sdkRoot,
Uri sdkSummary,
Uri packagesFile,
Uri librariesSpecificationUri,
Uri? sdkRoot,
Uri? sdkSummary,
Uri? packagesFile,
Uri? librariesSpecificationUri,
List<Uri> additionalDills,
Map<Uri, List<int>> workerInputDigests,
Target target,
Expand Down

0 comments on commit c53c689

Please sign in to comment.