OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Partial test that the closed world computed from [WorldImpact]s derived from |
| 6 // kernel is equivalent to the original computed from resolution. |
| 7 library dart2js.kernel.closed_world_from_dill_test; |
| 8 |
| 9 import 'dart:async'; |
| 10 |
| 11 import 'package:async_helper/async_helper.dart'; |
| 12 import 'package:compiler/src/commandline_options.dart'; |
| 13 import 'package:compiler/src/common.dart'; |
| 14 import 'package:compiler/src/common_elements.dart'; |
| 15 import 'package:compiler/src/compiler.dart'; |
| 16 import 'package:compiler/src/elements/entities.dart'; |
| 17 import 'package:compiler/src/elements/resolution_types.dart'; |
| 18 import 'package:compiler/src/elements/types.dart'; |
| 19 import 'package:compiler/src/enqueue.dart'; |
| 20 import 'package:compiler/src/js_backend/backend_usage.dart'; |
| 21 import 'package:compiler/src/js_backend/native_data.dart'; |
| 22 import 'package:compiler/src/kernel/element_map.dart'; |
| 23 import 'package:compiler/src/kernel/kernel_strategy.dart'; |
| 24 import 'package:compiler/src/universe/world_builder.dart'; |
| 25 import 'package:compiler/src/world.dart'; |
| 26 import 'package:expect/expect.dart'; |
| 27 import '../memory_compiler.dart'; |
| 28 import '../serialization/helper.dart'; |
| 29 import '../serialization/model_test_helper.dart'; |
| 30 import '../serialization/test_helper.dart'; |
| 31 |
| 32 import 'closed_world_test.dart' hide KernelWorkItemBuilder; |
| 33 import 'compiler_helper.dart'; |
| 34 import 'impact_test.dart'; |
| 35 |
| 36 const SOURCE = const { |
| 37 'main.dart': ''' |
| 38 main() { |
| 39 print('Hello World'); |
| 40 } |
| 41 ''' |
| 42 }; |
| 43 |
| 44 main(List<String> args) { |
| 45 asyncTest(() async { |
| 46 await mainInternal(args); |
| 47 }); |
| 48 } |
| 49 |
| 50 enum ResultKind { crashes, errors, warnings, success, failure } |
| 51 |
| 52 Future<ResultKind> mainInternal(List<String> args, |
| 53 {bool skipWarnings: false, bool skipErrors: false}) async { |
| 54 Arguments arguments = new Arguments.from(args); |
| 55 Uri entryPoint; |
| 56 Map<String, String> memorySourceFiles; |
| 57 if (arguments.uri != null) { |
| 58 entryPoint = arguments.uri; |
| 59 memorySourceFiles = const <String, String>{}; |
| 60 } else { |
| 61 entryPoint = Uri.parse('memory:main.dart'); |
| 62 memorySourceFiles = SOURCE; |
| 63 } |
| 64 |
| 65 enableDebugMode(); |
| 66 |
| 67 print('---- analyze-only ------------------------------------------------'); |
| 68 DiagnosticCollector collector = new DiagnosticCollector(); |
| 69 Compiler compiler1 = compilerFor( |
| 70 entryPoint: entryPoint, |
| 71 memorySourceFiles: memorySourceFiles, |
| 72 diagnosticHandler: collector, |
| 73 options: [Flags.analyzeOnly, Flags.enableAssertMessage]); |
| 74 ElementResolutionWorldBuilder.useInstantiationMap = true; |
| 75 compiler1.resolution.retainCachesForTesting = true; |
| 76 await compiler1.run(entryPoint); |
| 77 if (collector.crashes.isNotEmpty) { |
| 78 print('Skipping due to crashes.'); |
| 79 return ResultKind.crashes; |
| 80 } |
| 81 if (collector.errors.isNotEmpty && skipErrors) { |
| 82 print('Skipping due to errors.'); |
| 83 return ResultKind.errors; |
| 84 } |
| 85 if (collector.warnings.isNotEmpty && skipWarnings) { |
| 86 print('Skipping due to warnings.'); |
| 87 return ResultKind.warnings; |
| 88 } |
| 89 Expect.isFalse(compiler1.compilationFailed); |
| 90 ResolutionEnqueuer enqueuer1 = compiler1.enqueuer.resolution; |
| 91 BackendUsage backendUsage1 = compiler1.backend.backendUsage; |
| 92 ClosedWorld closedWorld1 = compiler1.resolutionWorldBuilder.closeWorld(); |
| 93 |
| 94 Compiler compiler2 = |
| 95 await compileWithDill(entryPoint, memorySourceFiles, printSteps: true); |
| 96 |
| 97 KernelFrontEndStrategy frontEndStrategy = compiler2.frontEndStrategy; |
| 98 KernelToElementMap elementMap = frontEndStrategy.elementMap; |
| 99 |
| 100 // TODO(johnniwinter): Change expectancy and continue equivalence testing when |
| 101 // https://codereview.chromium.org/2862223002/ is committed. |
| 102 Expect.isTrue(compiler2.compilationFailed); |
| 103 return ResultKind.failure; |
| 104 |
| 105 KernelEquivalence equivalence = new KernelEquivalence(elementMap); |
| 106 |
| 107 ResolutionEnqueuer enqueuer2 = compiler2.enqueuer.resolution; |
| 108 BackendUsage backendUsage2 = compiler2.backend.backendUsage; |
| 109 ClosedWorld closedWorld2 = compiler2.resolutionWorldBuilder.closeWorld(); |
| 110 checkBackendUsage(backendUsage1, backendUsage2, equivalence); |
| 111 |
| 112 checkResolutionEnqueuers(backendUsage1, backendUsage2, enqueuer1, enqueuer2, |
| 113 elementEquivalence: equivalence.entityEquivalence, |
| 114 typeEquivalence: (ResolutionDartType a, DartType b) { |
| 115 return equivalence.typeEquivalence(unalias(a), b); |
| 116 }, elementFilter: elementFilter, verbose: arguments.verbose); |
| 117 |
| 118 checkClosedWorlds(closedWorld1, closedWorld2, equivalence.entityEquivalence, |
| 119 verbose: arguments.verbose); |
| 120 |
| 121 return ResultKind.success; |
| 122 } |
| 123 |
| 124 void checkNativeBasicData(NativeBasicDataImpl data1, NativeBasicDataImpl data2, |
| 125 KernelEquivalence equivalence) { |
| 126 checkMapEquivalence( |
| 127 data1, |
| 128 data2, |
| 129 'nativeClassTagInfo', |
| 130 data1.nativeClassTagInfo, |
| 131 data2.nativeClassTagInfo, |
| 132 equivalence.entityEquivalence, |
| 133 (a, b) => a == b); |
| 134 // TODO(johnniwinther): Check the remaining properties. |
| 135 } |
| 136 |
| 137 void checkBackendUsage(BackendUsageImpl usage1, BackendUsageImpl usage2, |
| 138 KernelEquivalence equivalence) { |
| 139 checkSetEquivalence( |
| 140 usage1, |
| 141 usage2, |
| 142 'globalClassDependencies', |
| 143 usage1.globalClassDependencies, |
| 144 usage2.globalClassDependencies, |
| 145 equivalence.entityEquivalence); |
| 146 checkSetEquivalence( |
| 147 usage1, |
| 148 usage2, |
| 149 'globalFunctionDependencies', |
| 150 usage1.globalFunctionDependencies, |
| 151 usage2.globalFunctionDependencies, |
| 152 equivalence.entityEquivalence); |
| 153 checkSetEquivalence( |
| 154 usage1, |
| 155 usage2, |
| 156 'helperClassesUsed', |
| 157 usage1.helperClassesUsed, |
| 158 usage2.helperClassesUsed, |
| 159 equivalence.entityEquivalence); |
| 160 checkSetEquivalence( |
| 161 usage1, |
| 162 usage2, |
| 163 'helperFunctionsUsed', |
| 164 usage1.helperFunctionsUsed, |
| 165 usage2.helperFunctionsUsed, |
| 166 equivalence.entityEquivalence); |
| 167 check( |
| 168 usage1, |
| 169 usage2, |
| 170 'needToInitializeIsolateAffinityTag', |
| 171 usage1.needToInitializeIsolateAffinityTag, |
| 172 usage2.needToInitializeIsolateAffinityTag); |
| 173 check( |
| 174 usage1, |
| 175 usage2, |
| 176 'needToInitializeDispatchProperty', |
| 177 usage1.needToInitializeDispatchProperty, |
| 178 usage2.needToInitializeDispatchProperty); |
| 179 check(usage1, usage2, 'requiresPreamble', usage1.requiresPreamble, |
| 180 usage2.requiresPreamble); |
| 181 check(usage1, usage2, 'isInvokeOnUsed', usage1.isInvokeOnUsed, |
| 182 usage2.isInvokeOnUsed); |
| 183 check(usage1, usage2, 'isRuntimeTypeUsed', usage1.isRuntimeTypeUsed, |
| 184 usage2.isRuntimeTypeUsed); |
| 185 check(usage1, usage2, 'isIsolateInUse', usage1.isIsolateInUse, |
| 186 usage2.isIsolateInUse); |
| 187 check(usage1, usage2, 'isFunctionApplyUsed', usage1.isFunctionApplyUsed, |
| 188 usage2.isFunctionApplyUsed); |
| 189 check(usage1, usage2, 'isNoSuchMethodUsed', usage1.isNoSuchMethodUsed, |
| 190 usage2.isNoSuchMethodUsed); |
| 191 } |
| 192 |
| 193 checkElementEnvironment(ElementEnvironment env1, ElementEnvironment env2, |
| 194 KernelEquivalence equivalence) { |
| 195 checkSetEquivalence(env1, env2, 'libraries', env1.libraries, env2.libraries, |
| 196 equivalence.entityEquivalence, |
| 197 onSameElement: (LibraryEntity lib1, LibraryEntity lib2) { |
| 198 List<ClassEntity> classes2 = <ClassEntity>[]; |
| 199 env1.forEachClass(lib1, (ClassEntity cls1) { |
| 200 String className = cls1.name; |
| 201 ClassEntity cls2 = env2.lookupClass(lib2, className); |
| 202 Expect.isNotNull(cls2, 'Missing class $className in $lib2'); |
| 203 check(lib1, lib2, 'class:${className}', cls1, cls2, |
| 204 equivalence.entityEquivalence); |
| 205 |
| 206 check(cls1, cls2, 'superclass', env1.getSuperClass(cls1), |
| 207 env2.getSuperClass(cls2), equivalence.entityEquivalence); |
| 208 |
| 209 Map<MemberEntity, ClassEntity> members1 = <MemberEntity, ClassEntity>{}; |
| 210 Map<MemberEntity, ClassEntity> members2 = <MemberEntity, ClassEntity>{}; |
| 211 env1.forEachClassMember(cls1, |
| 212 (ClassEntity declarer1, MemberEntity member1) { |
| 213 members1[member1] = declarer1; |
| 214 }); |
| 215 env1.forEachClassMember(cls1, |
| 216 (ClassEntity declarer2, MemberEntity member2) { |
| 217 members2[member2] = declarer2; |
| 218 }); |
| 219 checkMapEquivalence(cls1, cls2, 'members', members1, members2, |
| 220 equivalence.entityEquivalence, equivalence.entityEquivalence); |
| 221 |
| 222 classes2.add(cls2); |
| 223 }); |
| 224 env2.forEachClass(lib2, (ClassEntity cls2) { |
| 225 Expect.isTrue(classes2.contains(cls2), "Extra class $cls2 in $lib2"); |
| 226 }); |
| 227 }); |
| 228 |
| 229 // TODO(johnniwinther): Test the remaining properties of [ElementEnvironment]. |
| 230 } |
OLD | NEW |