Skip to content

Commit d735f1f

Browse files
sigmundchcommit-bot@chromium.org
authored andcommitted
(dart2js): enable new-rti by default
This change: * adds the `--use-old-rti` flag to revert to the old behavior * enables the new behavior by default * changes the -rti- builders to run the old rti instead of the new rti * documents the change in CHANGELOG.md I've kept around the logic as `useNewRti` to avoid swapping all the conditions in the compiler. Change-Id: I773ac33b658cb60f72e0b6beef83375abec31bad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127492 Commit-Queue: Sigmund Cherem <sigmund@google.com> Reviewed-by: Stephen Adams <sra@google.com> Reviewed-by: Mayank Patke <fishythefish@google.com>
1 parent 6e7a900 commit d735f1f

File tree

83 files changed

+400
-1228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+400
-1228
lines changed

CHANGELOG.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,58 @@ The Linter was updated to `0.1.106`, which includes:
5454
dependencies by default. Instead they are precompiled on first `pub run`.
5555
Use `pub get --precompile` to get the previous behavior.
5656

57+
#### dart2js
58+
59+
A new representation of runtime types was enabled by default.
60+
61+
This change is part of a long term goal of making runtime checks cheaper and
62+
more flexible for upcoming changes in the language. The new representation
63+
disentangles how types and classes are represented and makes types first-class
64+
to the compiler. This makes it possible to do certain kind of optimizations on
65+
type checks that were not possible before and will enable us to model
66+
non-nullable types in the near future.
67+
68+
This change should not affect the semantics of your application, but it has some
69+
relatively small visible effects that we want to highlight:
70+
71+
* Types are now canonicalized, this fixes a long standing bug that Types could
72+
not be used in switch cases (issue [17207][]).
73+
74+
* Code-size changes may be visible, but the difference is small overall. It is
75+
more visible on smaller apps because the new implementation includes more
76+
helper methods. On large apps we have even seen an overall code-size
77+
reduction.
78+
79+
* Certain checks are a lot faster. This is less noticeable if you are compiling
80+
apps with `-O3` where checks are omitted altogether. Even with `-O3`, the
81+
performance of some `is` checks used by your app may improve.
82+
83+
* When using `-O3` and `-O4` incorrect type annotations could surface as errors.
84+
The old type representation was accidentally lenient on some invalid type
85+
annotations. We have only encountered this issue on programs that were not
86+
tested properly at the js-interop program boundary.
87+
88+
* `Type.toString` has a small change that is rarely visible. For a long time
89+
dart2js has had support to erase unused type variables. Today, when dart2js is
90+
given `--lax-runtime-type-to-string` (currently included in `-O2`, `-O3`, and
91+
`-O4`) and it decides to erase the type variable of a class `Foo<T>`, then it
92+
compiles expressions like `foo.runtimeType.toString()` to print `Foo`. With
93+
the new representation, this will show `Foo<erased>` instead. This change may
94+
be visible in error messages produced by type checks involving erased types.
95+
96+
Because types and classes are represented separately, we will likely reevaluate
97+
restrictions of deferred libraries in the near future. For example, we could
98+
support referring to deferred types because types can be downloaded while
99+
classes are not.
100+
101+
In the unlikely case you run into any issues, please file a bug so we can
102+
investigate. You can temporarily force the old type representation by passing
103+
`--use-old-rti` to dart2js if necessary, but our goal is to delete the old type
104+
representation soon.
105+
106+
107+
[17207]: https://github.com/dart-lang/sdk/issues/17207
108+
57109
## 2.7.0 - 2019-12-11
58110

59111
**Extension methods** -- which we shipped in preview in 2.6.0 -- are no longer

pkg/compiler/lib/src/commandline_options.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Flags {
7777
static const String useContentSecurityPolicy = '--csp';
7878
static const String useMultiSourceInfo = '--use-multi-source-info';
7979
static const String useNewSourceInfo = '--use-new-source-info';
80+
static const String useOldRti = '--use-old-rti';
8081
static const String verbose = '--verbose';
8182
static const String progress = '--show-internal-progress';
8283
static const String version = '--version';

pkg/compiler/lib/src/common_elements.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,10 +1497,9 @@ class CommonElementsImpl
14971497

14981498
ClassEntity _typeLiteralClass;
14991499
@override
1500-
ClassEntity get typeLiteralClass =>
1501-
_typeLiteralClass ??= _options.experimentNewRti
1502-
? _findRtiClass('_Type')
1503-
: _findHelperClass('TypeImpl');
1500+
ClassEntity get typeLiteralClass => _typeLiteralClass ??= _options.useNewRti
1501+
? _findRtiClass('_Type')
1502+
: _findHelperClass('TypeImpl');
15041503

15051504
ClassEntity _constMapLiteralClass;
15061505
@override
@@ -1787,7 +1786,7 @@ class CommonElementsImpl
17871786
_findHelperFunction('throwNoSuchMethod');
17881787

17891788
@override
1790-
FunctionEntity get createRuntimeType => _options.experimentNewRti
1789+
FunctionEntity get createRuntimeType => _options.useNewRti
17911790
? _findRtiFunction('createRuntimeType')
17921791
: _findHelperFunction('createRuntimeType');
17931792

pkg/compiler/lib/src/dart2js.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ Future<api.CompilationResult> compile(List<String> argv,
468468
new OptionHandler(Flags.generateCodeWithCompileTimeErrors, ignoreOption),
469469
new OptionHandler(Flags.useMultiSourceInfo, passThrough),
470470
new OptionHandler(Flags.useNewSourceInfo, passThrough),
471+
new OptionHandler(Flags.useOldRti, passThrough),
471472
new OptionHandler(Flags.testMode, passThrough),
472473

473474
// Experimental features.
@@ -482,7 +483,7 @@ Future<api.CompilationResult> compile(List<String> argv,
482483
new OptionHandler(Flags.experimentStartupFunctions, passThrough),
483484
new OptionHandler(Flags.experimentToBoolean, passThrough),
484485
new OptionHandler(Flags.experimentCallInstrumentation, passThrough),
485-
new OptionHandler(Flags.experimentNewRti, passThrough),
486+
new OptionHandler(Flags.experimentNewRti, ignoreOption),
486487

487488
// The following three options must come last.
488489
new OptionHandler('-D.+=.*', addInEnvironment),

pkg/compiler/lib/src/js_backend/backend_impact.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class BackendImpacts {
168168
BackendImpact get typeVariableBoundCheck {
169169
return _typeVariableBoundCheck ??= new BackendImpact(staticUses: [
170170
_commonElements.throwTypeError,
171-
if (_options.experimentNewRti)
171+
if (_options.useNewRti)
172172
_commonElements.checkTypeBound
173173
else
174174
_commonElements.assertIsSubtype,
@@ -435,7 +435,7 @@ class BackendImpacts {
435435
_commonElements.typeLiteralClass
436436
], staticUses: [
437437
_commonElements.createRuntimeType,
438-
if (_options.experimentNewRti) _commonElements.typeLiteralMaker,
438+
if (_options.useNewRti) _commonElements.typeLiteralMaker,
439439
]);
440440
}
441441

@@ -785,7 +785,7 @@ class BackendImpacts {
785785
_genericInstantiation[typeArgumentCount] ??=
786786
new BackendImpact(staticUses: [
787787
_commonElements.getInstantiateFunction(typeArgumentCount),
788-
..._options.experimentNewRti
788+
..._options.useNewRti
789789
? [
790790
_commonElements.instantiatedGenericFunctionTypeNewRti,
791791
_commonElements.closureFunctionType
@@ -802,7 +802,7 @@ class BackendImpacts {
802802
BackendImpact _newRtiImpact;
803803

804804
// TODO(sra): Split into refined impacts.
805-
BackendImpact get newRtiImpact => _newRtiImpact ??= _options.experimentNewRti
805+
BackendImpact get newRtiImpact => _newRtiImpact ??= _options.useNewRti
806806
? BackendImpact(staticUses: [
807807
_commonElements.findType,
808808
_commonElements.instanceType,

pkg/compiler/lib/src/js_backend/codegen_listener.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CodegenEnqueuerListener extends EnqueuerListener {
122122
}
123123

124124
// TODO(fishythefish): Avoid registering unnecessary impacts.
125-
if (_options.experimentNewRti && !_isNewRtiUsed) {
125+
if (_options.useNewRti && !_isNewRtiUsed) {
126126
WorldImpactBuilderImpl newRtiImpact = new WorldImpactBuilderImpl();
127127
newRtiImpact.registerStaticUse(StaticUse.staticInvoke(
128128
_commonElements.rtiAddRulesMethod, CallStructure.TWO_ARGS));
@@ -189,7 +189,7 @@ class CodegenEnqueuerListener extends EnqueuerListener {
189189
_elementEnvironment.getThisType(_commonElements
190190
.getInstantiationClass(constant.typeArguments.length))));
191191

192-
if (_options.experimentNewRti) {
192+
if (_options.useNewRti) {
193193
impactBuilder.registerStaticUse(StaticUse.staticInvoke(
194194
_commonElements.instantiatedGenericFunctionTypeNewRti,
195195
CallStructure.TWO_ARGS));

pkg/compiler/lib/src/js_backend/constant_emitter.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class ConstantEmitter extends ModularConstantEmitter {
238238
.toList(growable: false);
239239
jsAst.ArrayInitializer array = new jsAst.ArrayInitializer(elements);
240240
jsAst.Expression value = _makeConstantList(array);
241-
if (_options.experimentNewRti) {
241+
if (_options.useNewRti) {
242242
return maybeAddListTypeArgumentsNewRti(constant, constant.type, value);
243243
} else {
244244
return maybeAddTypeArguments(constant, constant.type, value);
@@ -261,7 +261,7 @@ class ConstantEmitter extends ModularConstantEmitter {
261261
];
262262

263263
if (_rtiNeed.classNeedsTypeArguments(classElement)) {
264-
if (_options.experimentNewRti) {
264+
if (_options.useNewRti) {
265265
arguments.add(_reifiedTypeNewRti(sourceType));
266266
} else {
267267
arguments
@@ -352,7 +352,7 @@ class ConstantEmitter extends ModularConstantEmitter {
352352
}
353353

354354
if (_rtiNeed.classNeedsTypeArguments(classElement)) {
355-
if (_options.experimentNewRti) {
355+
if (_options.useNewRti) {
356356
arguments.add(_reifiedTypeNewRti(constant.type));
357357
} else {
358358
arguments
@@ -373,7 +373,7 @@ class ConstantEmitter extends ModularConstantEmitter {
373373
jsAst.Expression visitType(TypeConstantValue constant, [_]) {
374374
DartType type = constant.representedType.unaliased;
375375

376-
if (_options.experimentNewRti) {
376+
if (_options.useNewRti) {
377377
assert(!type.containsTypeVariables);
378378

379379
jsAst.Expression recipe = _rtiRecipeEncoder.encodeGroundRecipe(
@@ -428,7 +428,7 @@ class ConstantEmitter extends ModularConstantEmitter {
428428
}
429429
});
430430
if (_rtiNeed.classNeedsTypeArguments(constant.type.element)) {
431-
if (_options.experimentNewRti) {
431+
if (_options.useNewRti) {
432432
fields.add(_reifiedTypeNewRti(constant.type));
433433
} else {
434434
fields
@@ -446,7 +446,7 @@ class ConstantEmitter extends ModularConstantEmitter {
446446
List<jsAst.Expression> fields = <jsAst.Expression>[
447447
_constantReferenceGenerator(constant.function)
448448
];
449-
if (_options.experimentNewRti) {
449+
if (_options.useNewRti) {
450450
fields
451451
.add(_reifiedTypeNewRti(InterfaceType(cls, constant.typeArguments)));
452452
} else {
@@ -502,7 +502,7 @@ class ConstantEmitter extends ModularConstantEmitter {
502502
}
503503

504504
jsAst.Expression _reifiedTypeNewRti(DartType type) {
505-
assert(_options.experimentNewRti);
505+
assert(_options.useNewRti);
506506
assert(!type.containsTypeVariables);
507507
return TypeReference(TypeExpressionRecipe(type))..forConstant = true;
508508
}

pkg/compiler/lib/src/js_backend/resolution_listener.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class ResolutionEnqueuerListener extends EnqueuerListener {
443443
_registerBackendImpact(impactBuilder, _impacts.traceHelper);
444444
}
445445

446-
if (_options.experimentNewRti) {
446+
if (_options.useNewRti) {
447447
_registerBackendImpact(impactBuilder, _impacts.rtiAddRules);
448448
}
449449

pkg/compiler/lib/src/js_emitter/instantiation_stub_generator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class InstantiationStubGenerator {
8181
parameters.add(new jsAst.Parameter(jsName));
8282
}
8383

84-
if (_options.experimentNewRti) {
84+
if (_options.useNewRti) {
8585
for (int i = 0; i < targetSelector.typeArgumentCount; i++) {
8686
arguments.add(js('this.#.#[#]', [
8787
_namer.rtiFieldJsName,
@@ -122,7 +122,7 @@ class InstantiationStubGenerator {
122122
jsAst.Name operatorSignature =
123123
_namer.asName(_namer.fixedNames.operatorSignature);
124124

125-
jsAst.Fun function = _options.experimentNewRti
125+
jsAst.Fun function = _options.useNewRti
126126
? _generateSignatureNewRti(functionField)
127127
: _generateSignatureLegacy(functionField);
128128

pkg/compiler/lib/src/js_emitter/metadata_collector.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class MetadataCollector implements jsAst.TokenFinalizer {
203203
jsAst.Expression addTypeInOutputUnit(DartType type, OutputUnit outputUnit) {
204204
_typesMap[outputUnit] ??= new Map<DartType, _BoundMetadataEntry>();
205205
return _typesMap[outputUnit].putIfAbsent(type, () {
206-
if (_options.experimentNewRti) {
206+
if (_options.useNewRti) {
207207
return new _BoundMetadataEntry(_computeTypeRepresentationNewRti(type));
208208
} else {
209209
return new _BoundMetadataEntry(_computeTypeRepresentation(type));

0 commit comments

Comments
 (0)