Report PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT

Mostly to don't crash when running co19 tests.

Change-Id: I62838d04872642b2e3a9ed01a1be120aec1c0608
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273081
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-12-02 02:58:16 +00:00 committed by Commit Queue
parent 3daee0282b
commit 73721f7654
7 changed files with 148 additions and 6 deletions

View file

@ -891,6 +891,8 @@ CompileTimeErrorCode.PART_OF_NON_PART:
status: needsEvaluation
CompileTimeErrorCode.PART_OF_UNNAMED_LIBRARY:
status: needsEvaluation
CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT:
status: needsEvaluation
CompileTimeErrorCode.POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT:
status: needsFix
since: 2.17

View file

@ -114,12 +114,17 @@ class SharedTypeAnalyzerErrors
}
@override
void patternTypeMismatchInIrrefutableContext(
{required AstNode pattern,
required AstNode context,
required DartType matchedType,
required DartType requiredType}) {
throw UnimplementedError('TODO(paulberry)');
void patternTypeMismatchInIrrefutableContext({
required covariant DartPatternImpl pattern,
required AstNode context,
required DartType matchedType,
required DartType requiredType,
}) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
pattern,
[matchedType, requiredType],
);
}
@override

View file

@ -3673,6 +3673,19 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);
/// Parameters:
/// 0: the matched type
/// 1: the required type
static const CompileTimeErrorCode
PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT = CompileTimeErrorCode(
'PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT',
"The matched value of type '{0}' isn't assignable to the required type "
"'{1}'.",
correctionMessage:
"Try changing the required type of the pattern, or the matched value "
"type.",
);
/// No parameters.
static const CompileTimeErrorCode
POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT =

View file

@ -373,6 +373,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.PART_OF_DIFFERENT_LIBRARY,
CompileTimeErrorCode.PART_OF_NON_PART,
CompileTimeErrorCode.PART_OF_UNNAMED_LIBRARY,
CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
CompileTimeErrorCode
.POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT,
CompileTimeErrorCode.PREFIX_COLLIDES_WITH_TOP_LEVEL_MEMBER,

View file

@ -11166,6 +11166,13 @@ CompileTimeErrorCode:
%uri="lib/part_file.dart"
part of 'test.dart';
```
PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT:
problemMessage: "The matched value of type '{0}' isn't assignable to the required type '{1}'."
correctionMessage: "Try changing the required type of the pattern, or the matched value type."
comment: |-
Parameters:
0: the matched type
1: the required type
POSITIONAL_SUPER_FORMAL_PARAMETER_WITH_POSITIONAL_ARGUMENT:
problemMessage: Positional super parameters can't be used when the super constructor invocation has a positional argument.
correctionMessage: Try making all the positional parameters passed to the super constructor be either all super parameters or all normal parameters.

View file

@ -0,0 +1,111 @@
// Copyright (c) 2022, 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.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(PatternTypeMismatchInIrrefutableContextTest);
});
}
@reflectiveTest
class PatternTypeMismatchInIrrefutableContextTest
extends PubPackageResolutionTest {
test_listPattern_differentList() async {
await assertErrorsInCode(r'''
void f(List<Object> x) {
var <int>[a] = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
31, 8),
]);
}
test_listPattern_notList() async {
await assertErrorsInCode(r'''
void f(Object x) {
var [a] = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
25, 3),
]);
}
test_mapPattern_notMap() async {
await assertErrorsInCode(r'''
void f(Object x) {
var <int, String>{0: a} = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
25, 19),
]);
}
test_objectPattern_differentClass() async {
await assertErrorsInCode(r'''
void f(Object x) {
var String(length: a) = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
25, 17),
]);
}
test_recordPattern_notRecord() async {
await assertErrorsInCode(r'''
void f(Object x) {
var (a,) = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
25, 4),
]);
}
test_recordPattern_record_differentShape() async {
await assertErrorsInCode(r'''
void f(({int foo}) x) {
var (a,) = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
30, 4),
]);
}
test_variablePattern_assignable_fromDynamic() async {
await assertNoErrorsInCode(r'''
void f(dynamic x) {
var (int a) = x;
}
''');
}
test_variablePattern_assignable_fromSubtype() async {
await assertNoErrorsInCode(r'''
void f(int x) {
var (num a) = x;
}
''');
}
test_variablePattern_notAssignable_fromSupertype() async {
await assertErrorsInCode(r'''
void f(num x) {
var (int a) = x;
}
''', [
error(CompileTimeErrorCode.PATTERN_TYPE_MISMATCH_IN_IRREFUTABLE_CONTEXT,
23, 5),
]);
}
}

View file

@ -605,6 +605,8 @@ import 'packed_annotation_alignment_test.dart' as packed_annotation_alignment;
import 'packed_annotation_test.dart' as packed_annotation;
import 'part_of_different_library_test.dart' as part_of_different_library;
import 'part_of_non_part_test.dart' as part_of_non_part;
import 'pattern_type_mismatch_in_irrefutable_context_test.dart'
as pattern_type_mismatch_in_irrefutable_context;
import 'positional_super_formal_parameter_with_positional_argument_test.dart'
as positional_super_formal_parameter_with_positional_argument;
import 'prefix_collides_with_top_level_member_test.dart'
@ -1213,6 +1215,7 @@ main() {
packed_annotation_alignment.main();
part_of_different_library.main();
part_of_non_part.main();
pattern_type_mismatch_in_irrefutable_context.main();
positional_super_formal_parameter_with_positional_argument.main();
prefix_collides_with_top_level_member.main();
prefix_identifier_not_followed_by_dot.main();