Skip to content

Commit 4da6cde

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[analyzer] Enable implicit constness in null-aware elements
Part of #56989 Change-Id: I7e940bd3c7f62e183b5425178613c5506fdc0fc2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397902 Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 630dcee commit 4da6cde

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6202,6 +6202,7 @@ sealed class ExpressionImpl extends AstNodeImpl
62026202
case ForElement():
62036203
case MapLiteralEntry():
62046204
case SpreadElement():
6205+
case NullAwareElement():
62056206
case VariableDeclaration():
62066207
break;
62076208
default:

pkg/analyzer/test/src/dart/resolution/constant_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ main() {}
6767
expect(value.getField('(super)')!.getField('f')!.toIntValue(), 42);
6868
}
6969

70+
test_constList_withNullAwareElement() async {
71+
await assertErrorsInCode(r'''
72+
class A {
73+
const A();
74+
foo() {
75+
return const [?A()];
76+
}
77+
}
78+
''', [
79+
error(StaticWarningCode.INVALID_NULL_AWARE_ELEMENT, 51, 1),
80+
]);
81+
assertType(findNode.listLiteral('const ['), 'List<A>');
82+
}
83+
84+
test_constMap_withNullAwareKey() async {
85+
await assertErrorsInCode(r'''
86+
class A {
87+
const A();
88+
foo() {
89+
return const {?A(): 0};
90+
}
91+
}
92+
''', [
93+
error(StaticWarningCode.INVALID_NULL_AWARE_MAP_ENTRY_KEY, 51, 1),
94+
]);
95+
assertType(findNode.setOrMapLiteral('const {'), 'Map<A, int>');
96+
}
97+
98+
test_constMap_withNullAwareValue() async {
99+
await assertErrorsInCode(r'''
100+
class A {
101+
const A();
102+
foo() {
103+
return const {0: ?A()};
104+
}
105+
}
106+
''', [
107+
error(StaticWarningCode.INVALID_NULL_AWARE_MAP_ENTRY_VALUE, 54, 1),
108+
]);
109+
assertType(findNode.setOrMapLiteral('const {'), 'Map<int, A>');
110+
}
111+
70112
test_constNotInitialized() async {
71113
await assertErrorsInCode(r'''
72114
class B {
@@ -82,6 +124,20 @@ class C extends B {
82124
]);
83125
}
84126

127+
test_constSet_withNullAwareElement() async {
128+
await assertErrorsInCode(r'''
129+
class A {
130+
const A();
131+
foo() {
132+
return const {?A()};
133+
}
134+
}
135+
''', [
136+
error(StaticWarningCode.INVALID_NULL_AWARE_ELEMENT, 51, 1),
137+
]);
138+
assertType(findNode.setOrMapLiteral('const {'), 'Set<A>');
139+
}
140+
85141
test_context_eliminateTypeVariables() async {
86142
await assertNoErrorsInCode(r'''
87143
class A<T> {

0 commit comments

Comments
 (0)