1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-03 00:08:46 +00:00

Fix pattern context type schema for cast patterns.

According to the patterns spec, the pattern context type schema for a
cast pattern should be `_`. What was actually implemented was
`Object?`.

This is unlikely to make a difference in practice, since (a) cast
patterns are unlikely to be used in circumstances where the pattern
context type schema makes a difference, and (b) it takes some effort
to come up with expressions whose type inference behavior is differenc
between a schema of `Object?` and a schema of `_`.

Change-Id: I695752c8c163621a34faaa8d62b2b076c8152eb0
Bug: https://github.com/dart-lang/sdk/issues/54640
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346383
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Paul Berry 2024-01-31 17:23:20 +00:00 committed by Commit Queue
parent 2958754ed8
commit 3636c8a08b
5 changed files with 15 additions and 13 deletions

View File

@ -1,5 +1,14 @@
## 3.4.0
### Language
- **Breaking Change** [#54640][]: The pattern context type schema for
cast patterns has been changed from `Object?` to `_` (the unknown
type), to align with the specification. This change is not expected
to make any difference in practice.
[#54640]: https://github.com/dart-lang/sdk/issues/54640
### Tools
#### Pub

View File

@ -387,8 +387,7 @@ mixin TypeAnalyzer<
/// Computes the type schema for a cast pattern.
///
/// Stack effect: none.
TypeSchema analyzeCastPatternSchema() =>
operations.typeToSchema(operations.objectQuestionType);
TypeSchema analyzeCastPatternSchema() => operations.unknownType;
/// Analyzes a constant pattern. [node] is the pattern itself, and
/// [expression] is the constant expression. Depending on the client's

View File

@ -1876,13 +1876,9 @@ main() {
test('Type schema', () {
var x = Var('x');
h.run([
ifCase(
expr('num'),
x.pattern().as_('int'),
[],
).checkIR('ifCase(expr(num), castPattern(varPattern(x, '
'matchedType: int, staticType: int), int, matchedType: num), '
'variables(x), true, block(), noop)'),
match(x.pattern().as_('int'), expr('num').checkSchema('?')).checkIR(
'match(expr(num), castPattern(varPattern(x, '
'matchedType: int, staticType: int), int, matchedType: num))'),
]);
});

View File

@ -102,7 +102,7 @@ PatternVariableDeclaration
token: x
staticElement: self::@function::f::@parameter::x
staticType: dynamic
patternTypeSchema: Object?
patternTypeSchema: _
''');
}
}

View File

@ -62,9 +62,7 @@ test() {
// - Cast: The context type schema is `_`.
{
// TODO(paulberry): uncomment this after
// https://github.com/dart-lang/sdk/issues/54640 is addressed.
// var [_ as Object] = [1]..expectStaticType<Exactly<List<int>>>();
var [_ as Object] = [1]..expectStaticType<Exactly<List<int>>>();
}
// - Parenthesized: The context type schema of the inner subpattern.