blob: 6e73e75a93662e2431ee24091c5746bf8fc879f1 [file] [log] [blame]
// 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.
// Verify semantics of the ?. operator when it appears on the LHS of an
// assignment.
import "package:expect/expect.dart";
import "conditional_access_helper.dart" as h;
bad() {
Expect.fail('Should not be executed');
}
class B {}
class C extends B {
num v;
C(this.v);
static late int staticInt;
}
class D {
E v;
D(this.v);
static late E staticE;
}
class E {
G operator +(int i) => new I();
}
class F {}
class G extends E implements F {}
class H {}
class I extends G implements H {}
C? nullC() => null;
main() {
// e1?.v = e2 is equivalent to ((x) => x == null ? null : x.v = e2)(e1).
Expect.equals(null, nullC()?.v = bad());
{
C? c = new C(1) as dynamic;
Expect.equals(2, c?.v = 2);
Expect.equals(2, c!.v);
}
// C?.v = e2 is equivalent to C.v = e2.
C.staticInt = 1;
Expect.equals(2, C?.staticInt = 2);
Expect.equals(2, C.staticInt);
h.C.staticInt = 1;
Expect.equals(2, h.C?.staticInt = 2);
Expect.equals(2, h.C.staticInt);
// The static type of e1?.v = e2 is the static type of e2.
{
D? d = new D(new E()) as dynamic;
G g = new G();
F? f = (d?.v = g);
Expect.identical(f, g);
}
{
D.staticE = new E();
G g = new G();
F? f = (D?.staticE = g);
Expect.identical(f, g);
}
h.D.staticE = new h.E();
h.G g = new h.G();
h.F? f = (h.D?.staticE = g);
Expect.identical(f, g);
// Exactly the same errors that would be caused by e1.v = e2 are
// also generated in the case of e1?.v = e2.
// e1?.v op= e2 is equivalent to ((x) => x?.v = x.v op e2)(e1).
Expect.equals(null, nullC()?.v += bad());
{
C? c = new C(1) as dynamic;
Expect.equals(3, c?.v += 2);
Expect.equals(3, c!.v);
}
// C?.v op= e2 is equivalent to C.v op= e2.
C.staticInt = 1;
Expect.equals(3, C?.staticInt += 2);
Expect.equals(3, C?.staticInt);
// The static type of e1?.v op= e2 is the static type of e1.v op e2.
{
D? d = new D(new E()) as dynamic;
F? f = (d?.v += 1);
Expect.identical(d!.v, f);
}
{
D.staticE = new E();
F? f = (D?.staticE += 1);
Expect.identical(D.staticE, f);
}
{
h.D.staticE = new h.E();
h.F? f = (h.D?.staticE += 1);
Expect.identical(h.D.staticE, f);
}
}