Files
sdk/tests/language/if_null/assignment_static_test.dart
Robert Nystrom 5bb9e922db Opt multitests out of formatting in language/.
The new formatter supports opting a region of code out from being
formatted. I'm applying this marker to all of the multitests since
those tests are often very sensitive to formatting and easily broken.
This way, anyone touching a multitest (including me when I reformat
the tests) doesn't have to remember to not run the formatter on it.

Unfortunately, this doesn't opt out 100# of the multitests. There are a
handful of multitests that also contain "@dart=" comments and are thus
formatted using the old style where the "// dart format off" marker has
no effect. For those, we'll have to still be careful to not accidentally
format them.

Change-Id: I257d0ee1eb44eee57047be06b8520f0ccc7b56d3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396162
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2024-11-21 10:49:44 +00:00

159 lines
5.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.
// Formatting can break multitests, so don't format them.
// dart format off
// Verify that the static type of a ??= b is the least upper bound of the
// static types of a and b.
import "package:expect/expect.dart";
bad() {
Expect.fail('Should not be executed');
}
/// Actually of type B so that the implicit downcasts below succeed at runtime.
A theA = new B();
B theB = new B();
class A {
String? a;
}
class B extends A {
String? b;
}
class C extends A {
String? c;
}
A? get a => null;
void set a(A? value) {}
B? get b => null;
void set b(B? value) {}
class ClassWithStaticGetters {
static A? get a => null;
static void set a(A? value) {}
static B? get b => null;
static void set b(B? value) {}
}
class ClassWithInstanceGetters {
A? get a => null;
void set a(A? value) {}
B? get b => null;
void set b(B? value) {}
}
class DerivedClass extends ClassWithInstanceGetters {
A? get a => bad();
void set a(A? value) {
bad();
}
B? get b => bad();
void set b(B? value) {
bad();
}
void derivedTest() {
// The static type of super.v ??= e is the LUB of NonNull(typeof(super.v))
// and typeof(e).
(super.a ??= theA).a; //# 01: ok
(super.a ??= theA).b; //# 02: compile-time error
(super.a ??= theB).a; //# 03: ok
(super.a ??= theB).b; //# 04: compile-time error
(super.b ??= theA).a; //# 05: compile-time error
(super.b ??= theA).b; //# 06: compile-time error
// Exactly the same static errors that would be caused by super.v = e
// are also generated in the case of super.v ??= e.
super.b ??= new C(); //# 07: compile-time error
}
}
main() {
new DerivedClass().derivedTest();
// The static type of v ??= e is the LUB of NonNull(typeof(v)) and typeof(e).
(a ??= theA).a; //# 08: ok
(a ??= theA).b; //# 09: compile-time error
(a ??= theB).a; //# 10: ok
(a ??= theB).b; //# 11: compile-time error
(b ??= theA).a; //# 12: compile-time error
(b ??= theA).b; //# 13: compile-time error
// Exactly the same static errors that would be caused by v = e are also
// generated in the case of v ??= e.
b ??= new C(); //# 14: compile-time error
// The static type of C.v ??= e is the LUB of NonNull(typeof(C.v)) and
// typeof(e).
(ClassWithStaticGetters.a ??= theA).a; //# 15: ok
(ClassWithStaticGetters.a ??= theA).b; //# 16: compile-time error
(ClassWithStaticGetters.a ??= theB).a; //# 17: ok
(ClassWithStaticGetters.a ??= theB).b; //# 18: compile-time error
(ClassWithStaticGetters.b ??= theA).a; //# 19: compile-time error
(ClassWithStaticGetters.b ??= theA).b; //# 20: compile-time error
// Exactly the same static errors that would be caused by C.v = e are
// also generated in the case of C.v ??= e.
ClassWithStaticGetters.b ??= new C(); //# 21: compile-time error
// The static type of e1.v ??= e2 is the LUB of NonNull(typeof(e1.v)) and
// typeof(e2).
(new ClassWithInstanceGetters().a ??= theA).a; //# 22: ok
(new ClassWithInstanceGetters().a ??= theA).b; //# 23: compile-time error
(new ClassWithInstanceGetters().a ??= theB).a; //# 24: ok
(new ClassWithInstanceGetters().a ??= theB).b; //# 25: compile-time error
(new ClassWithInstanceGetters().b ??= theA).a; //# 26: compile-time error
(new ClassWithInstanceGetters().b ??= theA).b; //# 27: compile-time error
// Exactly the same static errors that would be caused by e1.v = e2 are
// also generated in the case of e1.v ??= e2.
new ClassWithInstanceGetters().b ??= new C(); //# 28: compile-time error
// The static type of e1[e2] ??= e3 is the LUB of NonNull(typeof(e1[e2])) and
// typeof(e3).
((<A?>[null])[0] ??= theA).a; //# 29: ok
((<A?>[null])[0] ??= theA).b; //# 30: compile-time error
((<A?>[null])[0] ??= theB).a; //# 31: ok
((<A?>[null])[0] ??= theB).b; //# 32: compile-time error
((<B?>[null])[0] ??= theA).a; //# 33: compile-time error
((<B?>[null])[0] ??= theA).b; //# 34: compile-time error
// Exactly the same static errors that would be caused by e1[e2] = e3 are
// also generated in the case of e1[e2] ??= e3.
(<B?>[null])[0] ??= new C(); //# 35: compile-time error
// The static type of e1?.v op= e2 is the static type of e1.v op e2,
// therefore the static type of e1?.v ??= e2 is the static type of
// e1.v ?? e2, which is the LUB of NonNull(typeof(e1?.v)) and typeof(e2).
var c = new ClassWithInstanceGetters() as ClassWithInstanceGetters?;
(c?.a ??= theA)!.a; //# 36: ok
(c?.a ??= theA)!.b; //# 37: compile-time error
(c?.a ??= theB)!.a; //# 38: ok
(c?.a ??= theB)!.b; //# 39: compile-time error
(c?.b ??= theA)!.a; //# 40: compile-time error
(c?.b ??= theA)!.b; //# 41: compile-time error
// Exactly the same static errors that would be caused by e1.v ??= e2 are
// also generated in the case of e1?.v ??= e2.
c?.b ??= new C(); //# 42: compile-time error
}