Files
sdk/tests/standalone_2/check_null_cha_test.dart
T
Aart Bik ac73b8e198 [vm/compiler] Improved type analysis for check class.
Rationale:
Improves the analysis if instance calls need checks
(check class or check null) combined with CHA.

History: revert^2 of original
https://dart-review.googlesource.com/c/sdk/+/65220
https://dart-review.googlesource.com/c/sdk/+/64440

Bug: https://github.com/dart-lang/sdk/issues/33664
Change-Id: I21ea857b68ba136d2bd4c9714ab557401ae7c7b8
Reviewed-on: https://dart-review.googlesource.com/66023
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Aart Bik <ajcbik@google.com>
2018-07-20 23:26:45 +00:00

59 lines
1.5 KiB
Dart

// Copyright (c) 2018, 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.
import "package:expect/expect.dart";
// A class the has a getter also provided by Object
// (higher in the class hierarchy) and thus also the
// Null class (besides X in the class hierarchy).
class X {
int hashCode;
X() {
hashCode = 1;
}
}
// Use this getter on X receiver.
int hashMe(X x) {
int d = 0;
for (int i = 0; i < 10; i++) {
d += x.hashCode;
}
return d;
}
// Use this getter on Null class receiver.
// Only possible value is null.
int hashNull(Null x) {
int d = 0;
for (int i = 0; i < 10; i++) {
d += x.hashCode;
}
return d;
}
main() {
// Warm up the JIT with just an X object. Having a single receiver
// of type X with nothing below in the hierarchy that overrides
// hashCode could tempt the JIT to inline the getter with CHA
// that deopts when X is subclassed in the future.
X x = new X();
for (int i = 0; i < 1000; i++) {
Expect.equals(10, hashMe(x));
}
// However, this is a special case that also works on null
// (calling Object's hashCode). So this should not throw an
// exception. Had we inlined, this would have hit the null
// check and thrown an exception.
Expect.notEquals(0, hashMe(null));
// Also warm up the JIT on a direct Null receiver.
int d = 0;
for (int i = 0; i < 1000; i++) {
d += hashNull(null);
}
Expect.notEquals(0, d);
}