Fix context for null-aware extension method invocations to match analyzer.

This looks like it was an oversight--previous to this change, there
was zero test coverage for this case.

Change-Id: Icba1381c389af37ab4fe38f345cc78fe6c01d7f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353226
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Paul Berry 2024-02-21 13:13:07 +00:00 committed by Commit Queue
parent e86b08e5bb
commit fa38f392fa
2 changed files with 39 additions and 1 deletions

View file

@ -5006,7 +5006,7 @@ class InferenceVisitorImpl extends InferenceVisitorBase
node.variable.type, "?.", node.variable.fileOffset);
NullAwareGuard nullAwareGuard = createNullAwareGuard(node.variable);
ExpressionInferenceResult expressionResult =
inferExpression(node.expression, const UnknownType());
inferExpression(node.expression, typeContext);
return createNullAwareExpressionInferenceResult(
expressionResult.inferredType,
expressionResult.expression,

View file

@ -0,0 +1,38 @@
// Copyright (c) 2024, 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.
/// Tests that the type inference for generic extension method invocations
/// properly accounts for the downward inference context.
import '../static_type_helper.dart';
extension E on Object? {
T f<T>(List<T> t) => t.first;
}
class C {
T g<T>(List<T> t) => t.first;
}
main() {
var string = '';
context<int>(
string.f(contextType([1])..expectStaticType<Exactly<List<int>>>()));
context<int>(
E(string).f(contextType([1])..expectStaticType<Exactly<List<int>>>()));
var nullableString = '' as String?;
context<int?>(nullableString
?.f(contextType([1])..expectStaticType<Exactly<List<int?>>>()));
context<int?>(E(nullableString)
?.f(contextType([1])..expectStaticType<Exactly<List<int?>>>()));
// And just to verify that the expectations above are reasonable, repeat the
// same thing with an ordinary class:
var c = C();
context<int>(c.g(contextType([1])..expectStaticType<Exactly<List<int>>>()));
var nullableC = C() as C?;
context<int?>(
nullableC?.g(contextType([1])..expectStaticType<Exactly<List<int?>>>()));
}