-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hasOnlyNonDeferredImportPathsToConstant to add deferred constants
Change-Id: Ifd8b733c526fe4e7ae837a7d17c682e91717feb5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98013 Commit-Queue: Johnni Winther <johnniwinther@google.com> Reviewed-by: Sigmund Cherem <sigmund@google.com>
- Loading branch information
1 parent
32a5fa0
commit 10abacb
Showing
10 changed files
with
328 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/compiler/dart2js/deferred/constant_emission_test_helper.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
|
||
// Test that the additional runtime type support is output to the right | ||
// Files when using deferred loading. | ||
|
||
import 'package:compiler/compiler_new.dart'; | ||
import 'package:compiler/src/commandline_options.dart'; | ||
import 'package:compiler/src/compiler.dart'; | ||
import 'package:compiler/src/constants/values.dart'; | ||
import 'package:compiler/src/deferred_load.dart'; | ||
import 'package:compiler/src/elements/entities.dart'; | ||
import 'package:compiler/src/js_emitter/model.dart'; | ||
import 'package:compiler/src/util/util.dart'; | ||
import 'package:expect/expect.dart'; | ||
import '../helpers/memory_compiler.dart'; | ||
import '../helpers/output_collector.dart'; | ||
import '../helpers/program_lookup.dart'; | ||
|
||
class OutputUnitDescriptor { | ||
final String uri; | ||
final String member; | ||
final String name; | ||
|
||
const OutputUnitDescriptor(this.uri, this.member, this.name); | ||
} | ||
|
||
run(Map<String, String> sourceFiles, List<OutputUnitDescriptor> outputUnits, | ||
Map<String, Set<String>> expectedOutputUnits, | ||
{bool useCFEConstants: false}) async { | ||
OutputCollector collector = new OutputCollector(); | ||
CompilationResult result = await runCompiler( | ||
memorySourceFiles: sourceFiles, | ||
outputProvider: collector, | ||
options: useCFEConstants | ||
? ['${Flags.enableLanguageExperiments}=constant-update-2018'] | ||
: ['${Flags.enableLanguageExperiments}=no-constant-update-2018']); | ||
Compiler compiler = result.compiler; | ||
ProgramLookup lookup = new ProgramLookup(compiler); | ||
var closedWorld = compiler.backendClosedWorldForTesting; | ||
var elementEnvironment = closedWorld.elementEnvironment; | ||
|
||
LibraryEntity lookupLibrary(name) { | ||
return elementEnvironment.lookupLibrary(Uri.parse(name)); | ||
} | ||
|
||
OutputUnit Function(MemberEntity) outputUnitForMember = | ||
closedWorld.outputUnitData.outputUnitForMember; | ||
|
||
Map<String, Fragment> fragments = {}; | ||
fragments['main'] = lookup.program.mainFragment; | ||
|
||
for (OutputUnitDescriptor descriptor in outputUnits) { | ||
LibraryEntity library = lookupLibrary(descriptor.uri); | ||
MemberEntity member = | ||
elementEnvironment.lookupLibraryMember(library, descriptor.member); | ||
OutputUnit outputUnit = outputUnitForMember(member); | ||
fragments[descriptor.name] = lookup.getFragment(outputUnit); | ||
} | ||
|
||
Map<String, Set<String>> actualOutputUnits = {}; | ||
|
||
bool errorsFound = false; | ||
|
||
void processFragment(String fragmentName, Fragment fragment) { | ||
for (Constant constant in fragment.constants) { | ||
String text = constant.value.toStructuredText(); | ||
Set<String> expectedConstantUnit = expectedOutputUnits[text]; | ||
if (expectedConstantUnit == null) { | ||
if (constant.value is DeferredGlobalConstantValue) { | ||
print('ERROR: No expectancy for $constant found in $fragmentName'); | ||
errorsFound = true; | ||
} | ||
} else { | ||
(actualOutputUnits[text] ??= <String>{}).add(fragmentName); | ||
} | ||
} | ||
} | ||
|
||
fragments.forEach(processFragment); | ||
|
||
expectedOutputUnits.forEach((String constant, Set<String> expectedSet) { | ||
Set<String> actualSet = actualOutputUnits[constant] ?? const <String>{}; | ||
if (!equalSets(expectedSet, actualSet)) { | ||
print("ERROR: Constant $constant found in $actualSet, expected " | ||
"$expectedSet"); | ||
errorsFound = true; | ||
} | ||
}); | ||
|
||
Expect.isFalse(errorsFound, "Errors found."); | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/compiler/dart2js/deferred/deferred_constant3_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
|
||
// Test that the additional runtime type support is output to the right | ||
// Files when using deferred loading. | ||
|
||
import 'package:async_helper/async_helper.dart'; | ||
import 'constant_emission_test_helper.dart'; | ||
|
||
void main() { | ||
runTest({bool useCFEConstants: false}) async { | ||
Map<String, Set<String>> expectedOutputUnits = { | ||
'ConstructedConstant(C(x=IntConstant(1)))': {'main'}, | ||
'DeferredGlobalConstant(ConstructedConstant(C(x=IntConstant(1))))': | ||
// With CFE constants, the references are inlined, so the constant | ||
// only occurs in main. | ||
useCFEConstants ? {} : {'lib2'}, | ||
'ConstructedConstant(C(x=IntConstant(2)))': {'lib1'}, | ||
'DeferredGlobalConstant(ConstructedConstant(C(x=IntConstant(2))))': { | ||
'lib1' | ||
}, | ||
'ConstructedConstant(C(x=IntConstant(3)))': {'lib1'}, | ||
'ConstructedConstant(C(x=IntConstant(4)))': {'lib2'}, | ||
'DeferredGlobalConstant(ConstructedConstant(C(x=IntConstant(4))))': { | ||
'lib2' | ||
}, | ||
'ConstructedConstant(C(x=IntConstant(5)))': {'lib2'}, | ||
}; | ||
await run( | ||
MEMORY_SOURCE_FILES, | ||
const [ | ||
OutputUnitDescriptor('memory:lib1.dart', 'm1', 'lib1'), | ||
OutputUnitDescriptor('memory:lib2.dart', 'm2', 'lib2'), | ||
], | ||
expectedOutputUnits, | ||
useCFEConstants: useCFEConstants); | ||
} | ||
|
||
asyncTest(() async { | ||
print('--test from kernel------------------------------------------------'); | ||
await runTest(); | ||
print('--test from kernel with CFE constants-----------------------------'); | ||
await runTest(useCFEConstants: true); | ||
}); | ||
} | ||
|
||
// Make sure that deferred constants are not inlined into the main hunk. | ||
const Map<String, String> MEMORY_SOURCE_FILES = const { | ||
"main.dart": r""" | ||
import 'c.dart'; | ||
import 'lib1.dart' deferred as l1; | ||
const c1 = const C(1); | ||
main() async { | ||
print(c1.x); | ||
await l1.loadLibrary(); | ||
l1.m1(); | ||
print(l1.c2); | ||
} | ||
""", | ||
"lib1.dart": """ | ||
import 'c.dart'; | ||
import 'lib2.dart' deferred as l2; | ||
const c2 = const C(2); | ||
const c3 = const C(3); | ||
m1() async { | ||
print(c2); | ||
print(c3); | ||
await l2.loadLibrary(); | ||
l2.m2(); | ||
print(l2.c3); | ||
print(l2.c4); | ||
} | ||
""", | ||
"lib2.dart": """ | ||
import 'c.dart'; | ||
const c3 = const C(1); | ||
const c4 = const C(4); | ||
const c5 = const C(5); | ||
m2() async { | ||
print(c3); | ||
print(c4); | ||
print(c5); | ||
} | ||
""", | ||
"c.dart": """ | ||
class C { const C(this.x); final x; } | ||
""", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.