Skip to content

Commit bbb449d

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 40931. Update inference constraint for T? to be non-nullable.
Bug: #40931 Change-Id: I483c3febad236b366f911f38339c712dd928cfcf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138790 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent f1369d7 commit bbb449d

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

pkg/analyzer/lib/src/generated/type_system.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ class GenericInferrer {
649649
var constraints = this.constraints[t2.element];
650650
if (constraints != null) {
651651
if (!identical(t1, UnknownInferredType.instance)) {
652+
if (t2.nullabilitySuffix == NullabilitySuffix.question) {
653+
t1 = _typeSystem.promoteToNonNull(t1);
654+
}
652655
var constraint = _TypeConstraint(origin, t2.element, lower: t1);
653656
constraints.add(constraint);
654657
_undoBuffer.add(constraint);

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,9 @@ class MethodInvocationResolutionWithNnbdTest extends DriverResolutionTest {
17021702
[EnableString.non_nullable],
17031703
);
17041704

1705+
@override
1706+
bool get typeToStringWithNullability => true;
1707+
17051708
test_hasReceiver_deferredImportPrefix_loadLibrary_optIn_fromOptOut() async {
17061709
newFile('/test/lib/a.dart', content: r'''
17071710
class A {}
@@ -1724,7 +1727,7 @@ main() {
17241727
assertMethodInvocation(
17251728
invocation,
17261729
import.importedLibrary.loadLibraryFunction,
1727-
'Future<dynamic> Function()',
1730+
'Future<dynamic>* Function()*',
17281731
);
17291732
}
17301733

@@ -1834,6 +1837,29 @@ main(A? a) {
18341837
);
18351838
}
18361839

1840+
test_hasReceiver_interfaceTypeQ_defined_extensionQ2() async {
1841+
await assertNoErrorsInCode(r'''
1842+
extension E<T> on T? {
1843+
T foo() => throw 0;
1844+
}
1845+
1846+
main(int? a) {
1847+
a.foo();
1848+
}
1849+
''');
1850+
1851+
assertMethodInvocation2(
1852+
findNode.methodInvocation('a.foo()'),
1853+
element: elementMatcher(
1854+
findElement.method('foo', of: 'E'),
1855+
substitution: {'T': 'int'},
1856+
),
1857+
typeArgumentTypes: [],
1858+
invokeType: 'int Function()',
1859+
type: 'int',
1860+
);
1861+
}
1862+
18371863
test_hasReceiver_interfaceTypeQ_notDefined() async {
18381864
await assertErrorsInCode(r'''
18391865
class A {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ mixin ResolutionTest implements ResourceProviderMixin {
454454

455455
void assertMethodInvocation2(
456456
MethodInvocation node, {
457-
@required ExecutableElement element,
457+
@required Object element,
458458
@required List<String> typeArgumentTypes,
459459
@required String invokeType,
460460
@required String type,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// SharedOptions=--enable-experiment=non-nullable
6+
7+
main() {}
8+
9+
extension E<T> on T? {
10+
T foo() => throw 0;
11+
}
12+
13+
void f(int? a) {
14+
a.foo();
15+
}

0 commit comments

Comments
 (0)