Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 441dca7

Browse files
jensjohaCommit Bot
authored andcommitted
[CFE][kernel] Use .of instead of .from
This CL replaces most usages of `.from` (e.g. `List<String>.from(variable)`) to use `.of` instead. This is done because code like `List<String> foo = new List<String>.from([null])` is valid and gives no warnings or errors. Using `.of` instead will give an error. Also `.of` appears to be slightly faster. Change-Id: I1b4327be228b77e6a3e9faa283f8ce64f0565608 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/228642 Reviewed-by: Johnni Winther <johnniwinther@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Commit-Queue: Jens Johansen <jensj@google.com>
1 parent 0ff6340 commit 441dca7

23 files changed

+48
-45
lines changed

pkg/front_end/lib/src/fasta/combinator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class CombinatorBuilder {
1010
CombinatorBuilder(this.isShow, this.names, int charOffset, Uri fileUri);
1111

1212
CombinatorBuilder.show(Iterable<String> names, int charOffset, Uri fileUri)
13-
: this(true, new Set<String>.from(names), charOffset, fileUri);
13+
: this(true, new Set<String>.of(names), charOffset, fileUri);
1414

1515
CombinatorBuilder.hide(Iterable<String> names, int charOffset, Uri fileUri)
16-
: this(false, new Set<String>.from(names), charOffset, fileUri);
16+
: this(false, new Set<String>.of(names), charOffset, fileUri);
1717

1818
bool get isHide => !isShow;
1919
}

pkg/front_end/lib/src/fasta/get_dependencies.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ Future<List<Uri>> getDependencies(Uri script,
5454
kernelTarget.setEntryPoints(<Uri>[script]);
5555
dillTarget.buildOutlines();
5656
await kernelTarget.loader.buildOutlines();
57-
return new List<Uri>.from(c.dependencies);
57+
return new List<Uri>.of(c.dependencies);
5858
});
5959
}

pkg/front_end/lib/src/fasta/incremental_compiler.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
394394
// Compute which libraries to output and which (previous) errors/warnings
395395
// we have to reissue. In the process do some cleanup too.
396396
List<Library> compiledLibraries =
397-
new List<Library>.from(currentKernelTarget.loader.libraries);
397+
new List<Library>.of(currentKernelTarget.loader.libraries);
398398
Map<Uri, Source> uriToSource = componentWithDill!.uriToSource;
399399
_experimentalCompilationPostCompilePatchup(
400400
experimentalInvalidation, compiledLibraries, uriToSource);
@@ -2047,7 +2047,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
20472047
IncrementalKernelTarget? lastGoodKernelTarget = this._lastGoodKernelTarget;
20482048
if (lastGoodKernelTarget != null) {
20492049
Set<Uri> uris =
2050-
new Set<Uri>.from(lastGoodKernelTarget.loader.libraryImportUris);
2050+
new Set<Uri>.of(lastGoodKernelTarget.loader.libraryImportUris);
20512051
uris.removeAll(_dillLoadedData!.loader.libraryImportUris);
20522052
if (_previousSourceBuilders != null) {
20532053
for (Library library in _previousSourceBuilders!) {
@@ -2578,7 +2578,7 @@ class _ComponentProblems {
25782578

25792579
// Save any new component-problems.
25802580
_addProblemsAsJson(componentWithDill.problemsAsJson);
2581-
return new List<String>.from(issuedProblems);
2581+
return new List<String>.of(issuedProblems);
25822582
}
25832583

25842584
void saveComponentProblems(Component component) {

pkg/front_end/lib/src/fasta/incremental_serializer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class IncrementalSerializer {
175175
}
176176

177177
bool isSelfContained(Component component) {
178-
Set<Library> got = new Set<Library>.from(component.libraries);
178+
Set<Library> got = new Set<Library>.of(component.libraries);
179179
for (Library lib in component.libraries) {
180180
for (LibraryDependency dependency in lib.dependencies) {
181181
if (!got.contains(dependency.targetLibrary)) {

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
19011901
}
19021902
List<Object?>? argumentsOriginalOrder;
19031903
if (libraryBuilder.enableNamedArgumentsAnywhereInLibrary) {
1904-
argumentsOriginalOrder = new List<Object?>.from(arguments);
1904+
argumentsOriginalOrder = new List<Object?>.of(arguments);
19051905
}
19061906
int firstNamedArgumentIndex = arguments.length;
19071907
int positionalCount = 0;
@@ -1951,6 +1951,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
19511951
assert(
19521952
positionalIndex == positional.length && namedIndex == named.length);
19531953
} else {
1954+
// arguments have non-null Expression entries after the initial loop.
19541955
positional = new List<Expression>.from(
19551956
arguments.getRange(0, firstNamedArgumentIndex));
19561957
named = new List<NamedExpression>.from(
@@ -1962,6 +1963,8 @@ class BodyBuilder extends ScopeListener<JumpTarget>
19621963
} else {
19631964
// TODO(kmillikin): Find a way to avoid allocating a second list in the
19641965
// case where there were no named arguments, which is a common one.
1966+
1967+
// arguments have non-null Expression entries after the initial loop.
19651968
push(forest.createArguments(
19661969
beginToken.offset, new List<Expression>.from(arguments),
19671970
argumentsOriginalOrder: argumentsOriginalOrder));
@@ -4885,8 +4888,8 @@ class BodyBuilder extends ScopeListener<JumpTarget>
48854888
}
48864889
List<NamedExpression> named = forest.argumentsNamed(arguments);
48874890
if (named.isNotEmpty) {
4888-
Set<String> parameterNames =
4889-
new Set.from(function.namedParameters.map((a) => a.name));
4891+
Set<String?> parameterNames =
4892+
new Set.of(function.namedParameters.map((a) => a.name));
48904893
for (NamedExpression argument in named) {
48914894
if (!parameterNames.contains(argument.name)) {
48924895
return fasta.templateNoSuchNamedParameter
@@ -4897,7 +4900,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
48974900
}
48984901
if (function.namedParameters.isNotEmpty) {
48994902
if (libraryBuilder.isNonNullableByDefault) {
4900-
Set<String> argumentNames = new Set.from(named.map((a) => a.name));
4903+
Set<String> argumentNames = new Set.of(named.map((a) => a.name));
49014904
for (VariableDeclaration parameter in function.namedParameters) {
49024905
if (parameter.isRequired && !argumentNames.contains(parameter.name)) {
49034906
return fasta.templateValueForRequiredParameterNotProvidedError
@@ -4958,7 +4961,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
49584961
List<NamedExpression> named = forest.argumentsNamed(arguments);
49594962
if (named.isNotEmpty) {
49604963
Set<String> names =
4961-
new Set.from(function.namedParameters.map((a) => a.name));
4964+
new Set.of(function.namedParameters.map((a) => a.name));
49624965
for (NamedExpression argument in named) {
49634966
if (!names.contains(argument.name)) {
49644967
return fasta.templateNoSuchNamedParameter
@@ -4969,7 +4972,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
49694972
}
49704973
if (function.namedParameters.isNotEmpty) {
49714974
if (libraryBuilder.isNonNullableByDefault) {
4972-
Set<String> argumentNames = new Set.from(named.map((a) => a.name));
4975+
Set<String> argumentNames = new Set.of(named.map((a) => a.name));
49734976
for (NamedType parameter in function.namedParameters) {
49744977
if (parameter.isRequired && !argumentNames.contains(parameter.name)) {
49754978
return fasta.templateValueForRequiredParameterNotProvidedError
@@ -6053,7 +6056,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
60536056
noLocation,
60546057
noLocation,
60556058
// New list because the declarations are not a growable list.
6056-
new List<Statement>.from(
6059+
new List<Statement>.of(
60576060
forest.variablesDeclarationExtractDeclarations(lvalue)));
60586061
} else {
60596062
effects = forest.createExpressionStatement(
@@ -7147,7 +7150,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
71477150
Arguments? arguments, Expression expression) {
71487151
if (arguments == null) return expression;
71497152
List<Expression> expressions =
7150-
new List<Expression>.from(forest.argumentsPositional(arguments));
7153+
new List<Expression>.of(forest.argumentsPositional(arguments));
71517154
for (NamedExpression named in forest.argumentsNamed(arguments)) {
71527155
expressions.add(named.value);
71537156
}

pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class ConstantWeakener extends ComputeOnceConstantVisitor<Constant?> {
286286
Reference reference = entry.key;
287287
Constant? value = visitConstant(entry.value);
288288
if (value != null) {
289-
fieldValues ??= new Map<Reference, Constant>.from(node.fieldValues);
289+
fieldValues ??= new Map<Reference, Constant>.of(node.fieldValues);
290290
fieldValues[reference] = value;
291291
}
292292
}

pkg/front_end/lib/src/fasta/kernel/forest.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Forest {
374374
for (int i = 0; i < statements.length; i++) {
375375
Statement statement = statements[i];
376376
if (statement is _VariablesDeclaration) {
377-
copy ??= new List<Statement>.from(statements.getRange(0, i));
377+
copy ??= new List<Statement>.of(statements.getRange(0, i));
378378
copy.addAll(statement.declarations);
379379
} else if (copy != null) {
380380
copy.add(statement);
@@ -592,7 +592,7 @@ class Forest {
592592
Statement wrapVariables(Statement statement) {
593593
if (statement is _VariablesDeclaration) {
594594
return new Block(
595-
new List<Statement>.from(statement.declarations, growable: true))
595+
new List<Statement>.of(statement.declarations, growable: true))
596596
..fileOffset = statement.fileOffset;
597597
} else if (statement is VariableDeclaration) {
598598
return new Block(<Statement>[statement])

pkg/front_end/lib/src/fasta/kernel/hierarchy/members_node.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2352,7 +2352,7 @@ class ClassMembersNodeBuilder {
23522352
}
23532353
}
23542354
if (contextMap.isEmpty) return;
2355-
List<String> names = new List<String>.from(contextMap.keys)..sort();
2355+
List<String> names = new List<String>.of(contextMap.keys)..sort();
23562356
List<LocatedMessage> context = <LocatedMessage>[];
23572357
for (int i = 0; i < names.length; i++) {
23582358
context.add(contextMap[names[i]]!);

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class KernelTarget extends TargetImplementation {
412412
installSyntheticConstructors(sourceClassBuilders);
413413
loader.resolveConstructors();
414414
component =
415-
link(new List<Library>.from(loader.libraries), nameRoot: nameRoot);
415+
link(new List<Library>.of(loader.libraries), nameRoot: nameRoot);
416416
computeCoreTypes();
417417
loader.buildClassHierarchy(sourceClassBuilders, objectClassBuilder);
418418
loader.checkSupertypes(sourceClassBuilders, enumClass);

pkg/front_end/lib/src/fasta/source/source_class_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,10 +2382,10 @@ class SourceClassBuilder extends ClassBuilderImpl
23822382
}
23832383

23842384
List<VariableDeclaration> sortedFromDeclared =
2385-
new List.from(declaredFunction.namedParameters)
2385+
new List.of(declaredFunction.namedParameters)
23862386
..sort(compareNamedParameters);
23872387
List<VariableDeclaration> sortedFromInterface =
2388-
new List.from(interfaceFunction.namedParameters)
2388+
new List.of(interfaceFunction.namedParameters)
23892389
..sort(compareNamedParameters);
23902390
Iterator<VariableDeclaration> declaredNamedParameters =
23912391
sortedFromDeclared.iterator;

0 commit comments

Comments
 (0)