Skip to content

Commit b67f61a

Browse files
kallentuCommit Queue
authored andcommitted
[tests] Additional tests for ?? with enum shorthands.
Tests for `??` where context is from LHS and warnings when LHS is not nullable. Context: https://dart-review.googlesource.com/c/sdk/+/399520/comments/ee1b4e32_2a0ed6e7 Bug: #57038 Change-Id: I563d54b447e90f97fbea1a972f2e06c2cc23c8fe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401140 Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent e1be04c commit b67f61a

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Errors with `??` and enum shorthands with constructors.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
void constructorClassTest() {
12+
ConstructorClass ctor = ConstructorClass(1);
13+
14+
// Warning when LHS is not able to be `null`.
15+
ConstructorClass notNullable = .new(1) ?? ctor;
16+
// ^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
20+
ConstructorClass notNullableRegular = .regular(1) ?? ctor;
21+
// ^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
25+
ConstructorClass notNullableNamed = .named(x: 1) ?? ctor;
26+
// ^
27+
// [analyzer] unspecified
28+
// [cfe] unspecified
29+
30+
ConstructorClass notNullableOptional = .optional(1) ?? ctor;
31+
// ^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
}
35+
36+
void constructorExtTest() {
37+
ConstructorExt ctorExt = ConstructorExt(1);
38+
39+
// Warning when LHS is not able to be `null`.
40+
ConstructorExt notNullableExt = .new(1) ?? ctorExt;
41+
// ^
42+
// [analyzer] unspecified
43+
// [cfe] unspecified
44+
45+
ConstructorExt notNullableRegularExt = .regular(1) ?? ctorExt;
46+
// ^
47+
// [analyzer] unspecified
48+
// [cfe] unspecified
49+
50+
ConstructorExt notNullableNamedExt = .named(x: 1) ?? ctorExt;
51+
// ^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
55+
ConstructorExt notNullableOptionalExt = .optional(1) ?? ctorExt;
56+
// ^
57+
// [analyzer] unspecified
58+
// [cfe] unspecified
59+
}

tests/language/enum_shorthands/constructor/constructor_if_null_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ ConstructorClass ctorTest(ConstructorClass? ctor) => ctor ?? .new(1);
1414

1515
ConstructorClass ctorRegularTest(ConstructorClass? ctor) => ctor ?? .regular(1);
1616

17+
void noContextLHSContext(ConstructorClass? ctor) {
18+
ctor ?? .new(1);
19+
ctor ?? .regular(1);
20+
}
21+
1722
ConstructorExt ctorExtTest(ConstructorExt? ctor) => ctor ?? .new(1);
1823

1924
ConstructorExt ctorExtRegularTest(ConstructorExt? ctor) => ctor ?? .regular(1);
2025

26+
void noContextLHSContextExt(ConstructorExt? ctor) {
27+
ctor ?? .new(1);
28+
ctor ?? .regular(1);
29+
}
30+
2131
void main() {
2232
// Class
2333
var ctorDefault = ConstructorClass(2);
@@ -28,6 +38,9 @@ void main() {
2838
Expect.isNotNull(ctorRegularTest(null));
2939
Expect.equals(ctorRegularTest(ctorDefault), ctorDefault);
3040

41+
noContextLHSContext(null);
42+
noContextLHSContext(ctorDefault);
43+
3144
// Extension type
3245
var ctorExtDefault = ConstructorExt(2);
3346

@@ -36,4 +49,7 @@ void main() {
3649

3750
Expect.isNotNull(ctorExtRegularTest(null));
3851
Expect.equals(ctorExtRegularTest(ctorExtDefault), ctorExtDefault);
52+
53+
noContextLHSContextExt(null);
54+
noContextLHSContextExt(ctorExtDefault);
3955
}

tests/language/enum_shorthands/enum_shorthand_helper.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum Color { red, green, blue }
1111
class Integer {
1212
static Integer get one => Integer(1);
1313
static Integer get two => Integer(2);
14+
static Integer? get nullable => null;
1415
static const Integer constOne = const Integer._(1);
1516
static const Integer constTwo = const Integer._(2);
1617
final int integer;
@@ -21,6 +22,7 @@ class Integer {
2122
extension type IntegerExt(int integer) {
2223
static IntegerExt get one => IntegerExt(1);
2324
static IntegerExt get two => IntegerExt(2);
25+
static IntegerExt? get nullable => null;
2426
static const IntegerExt constOne = const IntegerExt._(1);
2527
static const IntegerExt constTwo = const IntegerExt._(2);
2628
const IntegerExt._(this.integer);
@@ -29,6 +31,7 @@ extension type IntegerExt(int integer) {
2931
mixin IntegerMixin on Integer {
3032
static IntegerMixin get mixinOne => _IntegerWithMixin(1);
3133
static IntegerMixin get mixinTwo => _IntegerWithMixin(2);
34+
static IntegerMixin? get mixinNullable => null;
3235
static const IntegerMixin mixinConstOne = const _IntegerWithMixin._(1);
3336
static const IntegerMixin mixinConstTwo = const _IntegerWithMixin._(2);
3437
}
@@ -68,13 +71,17 @@ extension type ConstructorExt(int? x) {
6871

6972
class StaticMember<T> {
7073
static StaticMember<int> member() => StaticMember(1);
74+
static StaticMember<int>? memberNullable() => null;
7175
static StaticMember<U> memberType<U, V>(U u) => StaticMember(u);
76+
static StaticMember<U>? memberTypeNullable<U, V>(U u) => null;
7277

7378
final T t;
7479
StaticMember(this.t);
7580
}
7681

7782
extension type StaticMemberExt<T>(T x) {
7883
static StaticMemberExt<int> member() => StaticMemberExt(1);
84+
static StaticMemberExt<int>? memberNullable() => null;
7985
static StaticMemberExt<U> memberType<U, V>(U u) => StaticMemberExt(u);
86+
static StaticMemberExt<U>? memberTypeNullable<U, V>(U u) => null;
8087
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Errors with `??` and enum shorthands with static members.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
void memberTest() {
12+
StaticMember member = StaticMember.member();
13+
14+
// Warning when LHS is not able to be `null`.
15+
StaticMember memberLocal = .member() ?? member;
16+
// ^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
20+
StaticMember memberType = .memberType<String, int>("s") ?? member;
21+
// ^
22+
// [analyzer] unspecified
23+
// [cfe] unspecified
24+
}
25+
26+
void memberExtTest() {
27+
StaticMemberExt<int> member = StaticMemberExt.member();
28+
29+
// Warning when LHS is not able to be `null`.
30+
StaticMemberExt memberLocal = .member() ?? member;
31+
// ^
32+
// [analyzer] unspecified
33+
// [cfe] unspecified
34+
35+
StaticMemberExt memberType = .memberType<String, int>("s") ?? member;
36+
// ^
37+
// [analyzer] unspecified
38+
// [cfe] unspecified
39+
}

tests/language/enum_shorthands/member/static_method_if_null_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ StaticMember memberTest(StaticMember? member) => member ?? .member();
1414

1515
StaticMember memberTypeTest(StaticMember? member) => member ?? .memberType<String, int>("s");
1616

17+
void noContextLHSContext(StaticMember? member) {
18+
member ?? .member();
19+
member ?? .memberType<String, int>("s");
20+
}
21+
1722
StaticMemberExt memberExtTest(StaticMemberExt? member) => member ?? .member();
1823

1924
StaticMemberExt memberExtTypeTest(StaticMemberExt? member) => member ?? .memberType<String, int>("s");
2025

26+
void noContextLHSContextExt(StaticMemberExt? member) {
27+
member ?? .member();
28+
member ?? .memberType<String, int>("s");
29+
}
30+
2131
void main() {
2232
// Class
2333
var memberDefault = StaticMember.memberType<int, int>(100);
@@ -28,6 +38,12 @@ void main() {
2838
Expect.isNotNull(memberTypeTest(null));
2939
Expect.equals(memberTypeTest(memberDefault), memberDefault);
3040

41+
noContextLHSContext(null);
42+
noContextLHSContext(memberDefault);
43+
44+
StaticMember possiblyNullable = .memberNullable() ?? memberDefault;
45+
StaticMember possiblyNullableWithType = .memberTypeNullable<String, int>("s") ?? memberDefault;
46+
3147
// Extension type
3248
var memberExtDefault = StaticMemberExt.memberType(100);
3349

@@ -36,4 +52,10 @@ void main() {
3652

3753
Expect.isNotNull(memberExtTypeTest(null));
3854
Expect.equals(memberExtTypeTest(memberExtDefault), memberExtDefault);
55+
56+
noContextLHSContextExt(null);
57+
noContextLHSContextExt(memberExtDefault);
58+
59+
StaticMemberExt possiblyNullableExt = .memberNullable() ?? memberExtDefault;
60+
StaticMemberExt possiblyNullableExtWithType = .memberTypeNullable<String, int>("s") ?? memberExtDefault;
3961
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Errors with `??` and enum shorthands with static properties or enums.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
void test() {
12+
// Warning when LHS is not able to be `null`.
13+
Color colorLocal = .blue ?? Color.red;
14+
// ^
15+
// [analyzer] unspecified
16+
// [cfe] unspecified
17+
18+
Integer integerLocal = .one ?? Integer.two;
19+
// ^
20+
// [analyzer] unspecified
21+
// [cfe] unspecified
22+
23+
IntegerMixin integerMixinLocal = .mixinOne ?? IntegerMixin.mixinTwo;
24+
// ^
25+
// [analyzer] unspecified
26+
// [cfe] unspecified
27+
}

tests/language/enum_shorthands/simple/simple_identifier_if_null_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,62 @@ import 'package:expect/expect.dart';
1111

1212
Color colorTest(Color? color) => color ?? .blue;
1313

14+
void noContextLHSContextColor(Color? color) {
15+
color ?? .blue;
16+
}
17+
1418
Integer integerTest(Integer? integer) => integer ?? .one;
1519

20+
void noContextLHSContextInteger(Integer? integer) {
21+
integer ?? .one;
22+
}
23+
1624
IntegerExt integerExtTest(IntegerExt? integer) => integer ?? .one;
1725

26+
void noContextLHSContextIntegerExt(IntegerExt? integer) {
27+
integer ?? .one;
28+
}
29+
1830
IntegerMixin integerMixinTest(IntegerMixin? integer) =>
1931
integer ?? .mixinOne;
2032

33+
void noContextLHSContextIntegerMixin(IntegerMixin? integer) {
34+
integer ?? .mixinOne;
35+
}
36+
2137
void main() {
2238
// Enum
2339
Expect.equals(colorTest(null), Color.blue);
2440
Expect.equals(colorTest(Color.red), Color.red);
2541

42+
noContextLHSContextColor(null);
43+
noContextLHSContextColor(Color.red);
44+
2645
// Class
2746
Expect.equals(integerTest(null), Integer.one);
2847
Expect.equals(integerTest(Integer.two), Integer.two);
2948

49+
noContextLHSContextInteger(null);
50+
noContextLHSContextInteger(Integer.one);
51+
52+
Integer possiblyNullableInteger = .nullable ?? Integer.one;
53+
3054
// Extension type
3155
Expect.equals(integerExtTest(null), IntegerExt.one);
3256
Expect.equals(integerExtTest(IntegerExt.two), IntegerExt.two);
3357

58+
noContextLHSContextIntegerExt(null);
59+
noContextLHSContextIntegerExt(IntegerExt.one);
60+
61+
IntegerExt possiblyNullableIntegerExt = .nullable ?? IntegerExt.one;
62+
IntegerExt possiblyNullableIntegerExt2 = .one ?? IntegerExt.one;
63+
3464
// Mixin
3565
Expect.equals(integerMixinTest(null), IntegerMixin.mixinOne);
3666
Expect.equals(integerMixinTest(IntegerMixin.mixinTwo), IntegerMixin.mixinTwo);
67+
68+
noContextLHSContextIntegerMixin(null);
69+
noContextLHSContextIntegerMixin(IntegerMixin.mixinOne);
70+
71+
IntegerMixin possiblyNullableIntegerMixin = .mixinNullable ?? IntegerMixin.mixinOne;
3772
}

0 commit comments

Comments
 (0)