Skip to content

Commit 35b7f2d

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Update to expect either 'dynamic' or a subtype in irrefutable context.
dart-lang/language#2926 Change-Id: Ic9fd0b097ca70c15a184269a10e8a87eb34bb79d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289440 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 9ce261f commit 35b7f2d

File tree

2 files changed

+94
-34
lines changed

2 files changed

+94
-34
lines changed

pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ mixin TypeAnalyzer<
359359
Type matchedType = flow.getMatchedValueType();
360360
Error? patternTypeMismatchInIrrefutableContextError;
361361
if (irrefutableContext != null &&
362-
!operations.isAssignableTo(matchedType, variableDeclaredType)) {
362+
!operations.isDynamic(matchedType) &&
363+
!operations.isSubtypeOf(matchedType, variableDeclaredType)) {
363364
patternTypeMismatchInIrrefutableContextError =
364365
errors.patternTypeMismatchInIrrefutableContext(
365366
pattern: node,
@@ -512,7 +513,8 @@ mixin TypeAnalyzer<
512513
Node? irrefutableContext = context.irrefutableContext;
513514
Error? patternTypeMismatchInIrrefutableContextError;
514515
if (irrefutableContext != null &&
515-
!operations.isAssignableTo(matchedType, staticType)) {
516+
!operations.isDynamic(matchedType) &&
517+
!operations.isSubtypeOf(matchedType, staticType)) {
516518
patternTypeMismatchInIrrefutableContextError =
517519
errors.patternTypeMismatchInIrrefutableContext(
518520
pattern: node,

pkg/analyzer/test/src/diagnostics/pattern_type_mismatch_in_irrefutable_context_test.dart

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,96 @@ main() {
1616
@reflectiveTest
1717
class PatternTypeMismatchInIrrefutableContextTest
1818
extends PubPackageResolutionTest {
19+
test_assignedVariablePattern_recordDestruction_hasCall() async {
20+
await assertErrorsInCode(r'''
21+
void f(int Function(int) a, (A,) x) {
22+
(a) = x;
23+
}
24+
25+
class A {
26+
int call(int x) => x;
27+
}
28+
''', [
29+
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
30+
41, 1),
31+
]);
32+
}
33+
34+
test_assignedVariablePattern_valueDynamic() async {
35+
await assertNoErrorsInCode(r'''
36+
void f(int a, dynamic x) {
37+
(a) = x;
38+
}
39+
''');
40+
}
41+
42+
test_assignedVariablePattern_valueSubtype() async {
43+
await assertNoErrorsInCode(r'''
44+
void f(num a, int x) {
45+
(a) = x;
46+
}
47+
''');
48+
}
49+
50+
test_assignedVariablePattern_valueSupertype() async {
51+
await assertErrorsInCode(r'''
52+
void f(int a, num x) {
53+
(a) = x;
54+
}
55+
''', [
56+
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
57+
26, 1),
58+
]);
59+
}
60+
61+
test_declaredVariablePattern_recordDestruction_hasCall() async {
62+
await assertErrorsInCode(r'''
63+
void f((A,) x) {
64+
var (int Function(int) v,) = x;
65+
}
66+
67+
class A {
68+
int call(int x) => x;
69+
}
70+
''', [
71+
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
72+
24, 19),
73+
error(HintCode.UNUSED_LOCAL_VARIABLE, 42, 1),
74+
]);
75+
}
76+
77+
test_declaredVariablePattern_valueDynamic() async {
78+
await assertErrorsInCode(r'''
79+
void f(dynamic x) {
80+
var (int a) = x;
81+
}
82+
''', [
83+
error(HintCode.UNUSED_LOCAL_VARIABLE, 31, 1),
84+
]);
85+
}
86+
87+
test_declaredVariablePattern_valueSubtype() async {
88+
await assertErrorsInCode(r'''
89+
void f(int x) {
90+
var (num a) = x;
91+
}
92+
''', [
93+
error(HintCode.UNUSED_LOCAL_VARIABLE, 27, 1),
94+
]);
95+
}
96+
97+
test_declaredVariablePattern_valueSupertype() async {
98+
await assertErrorsInCode(r'''
99+
void f(num x) {
100+
var (int a) = x;
101+
}
102+
''', [
103+
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
104+
23, 5),
105+
error(HintCode.UNUSED_LOCAL_VARIABLE, 27, 1),
106+
]);
107+
}
108+
19109
test_listPattern_differentList() async {
20110
await assertErrorsInCode(r'''
21111
void f(List<Object> x) {
@@ -98,36 +188,4 @@ void f(({int foo}) x) {
98188
error(HintCode.UNUSED_LOCAL_VARIABLE, 31, 1),
99189
]);
100190
}
101-
102-
test_variablePattern_assignable_fromDynamic() async {
103-
await assertErrorsInCode(r'''
104-
void f(dynamic x) {
105-
var (int a) = x;
106-
}
107-
''', [
108-
error(HintCode.UNUSED_LOCAL_VARIABLE, 31, 1),
109-
]);
110-
}
111-
112-
test_variablePattern_assignable_fromSubtype() async {
113-
await assertErrorsInCode(r'''
114-
void f(int x) {
115-
var (num a) = x;
116-
}
117-
''', [
118-
error(HintCode.UNUSED_LOCAL_VARIABLE, 27, 1),
119-
]);
120-
}
121-
122-
test_variablePattern_notAssignable_fromSupertype() async {
123-
await assertErrorsInCode(r'''
124-
void f(num x) {
125-
var (int a) = x;
126-
}
127-
''', [
128-
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
129-
23, 5),
130-
error(HintCode.UNUSED_LOCAL_VARIABLE, 27, 1),
131-
]);
132-
}
133191
}

0 commit comments

Comments
 (0)