e933e91aaa
This is a reland of 6a1c54ec30
Original change's description:
> Flow analysis: promote to non-nullable on initialization
>
> When flow analysis encounters a variable declaration of the form `T? x
> = expr;`, if the type of `expr` is `T`, then the variable is
> immediately promoted to type `T`.
>
> Fixes #43099.
>
> Change-Id: Ia206fe0d50e2fdd9bdf637e13c85633d8490dbcc
> Bug: https://github.com/dart-lang/sdk/issues/43099
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/163841
> Commit-Queue: Paul Berry <paulberry@google.com>
> Reviewed-by: Bob Nystrom <rnystrom@google.com>
> Reviewed-by: Johnni Winther <johnniwinther@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Bug: https://github.com/dart-lang/sdk/issues/43099
Change-Id: I7530bb0f7c24674a7b500558b89d50b35e045aca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166305
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
104 lines
2.0 KiB
Dart
104 lines
2.0 KiB
Dart
// Copyright (c) 2015, 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.
|
|
//
|
|
// VMOptions=--optimization_counter_threshold=5
|
|
//
|
|
// Basic null-aware operator test that invokes the optimizing compiler.
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
class C {
|
|
C(this.f);
|
|
var f;
|
|
m(a) => a;
|
|
}
|
|
|
|
bomb() {
|
|
Expect.fail('Should not be executed');
|
|
return 100;
|
|
}
|
|
|
|
getNull() => null;
|
|
|
|
test() {
|
|
var c;
|
|
var d = new C(5) as C?;
|
|
Expect.equals(null, c?.m(bomb()));
|
|
Expect.equals(null, getNull()?.anything(bomb()));
|
|
Expect.equals(1, d?.m(1));
|
|
|
|
var c2 = new C(1) as C?;
|
|
Expect.equals(1, c2?.f);
|
|
Expect.equals(null, c?.v);
|
|
Expect.equals(10, c ?? 10);
|
|
Expect.equals(d, d ?? bomb());
|
|
var list = [[3]] as List<List<int>>?;
|
|
Expect.equals(
|
|
3,
|
|
list?.expand((i) => i).toList()[0]);
|
|
Expect.equals(null, (null as dynamic)?.expand((i) => i)?.toList());
|
|
|
|
var e;
|
|
// The assignment to e is not executed since d != null.
|
|
d ??= e ??= new C(100);
|
|
Expect.equals(null, e);
|
|
e ??= new C(100);
|
|
Expect.equals(100, e?.f);
|
|
e?.f ??= 200;
|
|
Expect.equals(100, e?.f);
|
|
|
|
e.f = null;
|
|
e?.f ??= 200;
|
|
Expect.equals(200, e?.f);
|
|
|
|
c?.f ??= 400;
|
|
Expect.equals(null, c?.f);
|
|
Expect.equals(null, c?.f++);
|
|
e?.f++;
|
|
Expect.equals(201, e.f);
|
|
|
|
var five = 5 as int?;
|
|
var x = five ?? bomb();
|
|
}
|
|
|
|
// Check that instructions without result do not crash.
|
|
test2() {
|
|
var c;
|
|
c?.v;
|
|
c?.m(bomb());
|
|
}
|
|
|
|
class Bar {
|
|
String? s;
|
|
}
|
|
|
|
class Foo {
|
|
Bar? _bar;
|
|
String? str;
|
|
|
|
Foo(this._bar) : str = _bar?.s;
|
|
}
|
|
|
|
// Check that ?? isn't incorrectly optimized as non-nullable
|
|
// (DDC regression test)
|
|
test3() {
|
|
List? n = null;
|
|
var func = n?.add;
|
|
var result = func ?? 1;
|
|
Expect.equals(result, 1);
|
|
|
|
String? x = null;
|
|
Expect.equals(x?.split(',') ?? 'hello', 'hello');
|
|
}
|
|
|
|
main() {
|
|
for (int i = 0; i < 10; i++) {
|
|
test();
|
|
test2();
|
|
test3();
|
|
}
|
|
|
|
Expect.equals(null, new Foo(new Bar()).str);
|
|
}
|