[dart2js] Update MethodTypeInformation to narrow the return type of

_lateReadCheck.

Change-Id: If76a5b34cfd9da9721bfa0d8642b018a4af131dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213901
Reviewed-by: Stephen Adams <sra@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
This commit is contained in:
Mayank Patke 2021-09-21 01:59:45 +00:00 committed by commit-bot@chromium.org
parent 9872fd192e
commit 35421c5d1a
2 changed files with 39 additions and 0 deletions

View file

@ -625,6 +625,9 @@ class MethodTypeInformation extends MemberTypeInformation {
@override
AbstractValue _potentiallyNarrowType(
AbstractValue mask, InferrerEngine inferrer) {
if (inferrer.commonElements.isLateReadCheck(_method)) {
mask = inferrer.abstractValueDomain.excludeLateSentinel(mask);
}
return _narrowType(inferrer.closedWorld, mask, _type.returnType);
}

View file

@ -0,0 +1,36 @@
// Copyright (c) 2021, 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 'dart:async';
import 'package:async_helper/async_helper.dart';
import '../helpers/compiler_helper.dart';
const String TEST = r"""
class Foo {
late int x;
}
int foo() {
final foo = Foo();
foo.x = 40;
return foo.x + 2;
// present: '+ 2'
// absent: 'add'
}
""";
Future check(String test) {
return compile(test,
entry: 'foo',
check: checkerForAbsentPresent(test),
disableTypeInference: false,
disableInlining: false,
soundNullSafety: true);
}
void main() {
asyncTest(() async {
await check(TEST);
});
}