OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, 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 import 'package:kernel/class_hierarchy.dart'; | |
5 import 'package:kernel/core_types.dart'; | |
6 import 'package:kernel/kernel.dart'; | |
7 import 'package:kernel/transformations/insert_covariance_checks.dart'; | |
8 import 'package:kernel/transformations/insert_type_checks.dart'; | |
9 import 'package:kernel/transformations/mixin_full_resolution.dart'; | |
10 import 'package:kernel/transformations/treeshaker.dart'; | |
11 import 'package:kernel/type_checker.dart'; | |
12 import 'package:path/path.dart' as pathlib; | |
13 | |
14 import 'baseline_tester.dart'; | |
15 | |
16 class StrongModeTest extends TestTarget { | |
17 @override | |
18 List<String> get extraRequiredLibraries => []; | |
19 | |
20 @override | |
21 String get name => 'strong-mode-test'; | |
22 | |
23 @override | |
24 bool get strongMode => true; | |
25 | |
26 @override | |
27 List<String> performModularTransformations(Program program) { | |
28 new MixinFullResolution().transform(program); | |
29 return const <String>[]; | |
30 } | |
31 | |
32 @override | |
33 List<String> performGlobalTransformations(Program program) { | |
34 List<String> errors = <String>[]; | |
35 new InsertTypeChecks().transformProgram(program); | |
36 new InsertCovarianceChecks().transformProgram(program); | |
37 new TestTypeChecker( | |
38 errors, new CoreTypes(program), new ClassHierarchy(program)) | |
39 .checkProgram(program); | |
40 new TreeShaker(program, strongMode: true).transform(program); | |
41 program.accept(new TreeShakerCheck()); | |
42 return errors; | |
43 } | |
44 } | |
45 | |
46 class TestTypeChecker extends TypeChecker { | |
47 final List<String> errors; | |
48 | |
49 TestTypeChecker(this.errors, CoreTypes coreTypes, ClassHierarchy hierarchy) | |
50 : super(coreTypes, hierarchy); | |
51 | |
52 @override | |
53 void checkAssignable(TreeNode where, DartType from, DartType to) { | |
54 if (!environment.isSubtypeOf(from, to)) { | |
55 fail(where, '$from is not a subtype of $to'); | |
56 } | |
57 } | |
58 | |
59 @override | |
60 void fail(TreeNode where, String message) { | |
61 var location = where.location; | |
62 var locationString; | |
63 if (location != null) { | |
64 var file = pathlib.basename(Uri.parse(location.file).path); | |
65 locationString = '($file:${location.line}:${location.column})'; | |
66 } else { | |
67 locationString = '(no location)'; | |
68 } | |
69 errors.add('$message $locationString'); | |
70 } | |
71 } | |
72 | |
73 class TreeShakerCheck extends RecursiveVisitor { | |
74 visitStringLiteral(StringLiteral node) { | |
75 if (node.value.toLowerCase() == 'unused') { | |
76 throw 'Code not tree-shaken at ${node.location}.\n' | |
77 'The string literal $node is used to indicate that the code is ' | |
78 'expected to be removed by strong-mode tree shaking'; | |
79 } | |
80 } | |
81 } | |
82 | |
83 void main() { | |
84 runBaselineTests('strong-mode', new StrongModeTest()); | |
85 } | |
OLD | NEW |