Fix context for the RHS of the invocation of an extension setter to match analyzer.

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

Change-Id: I4301a3ba90aedce3b0fcd901649c370cba522f4e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353280
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Paul Berry 2024-02-21 15:10:18 +00:00 committed by Commit Queue
parent 2da70a8d9e
commit 4d1c8b09cb
2 changed files with 49 additions and 1 deletions

View file

@ -1101,7 +1101,7 @@ class InferenceVisitorImpl extends InferenceVisitorBase
DartType valueType = target.getSetterType(this);
ExpressionInferenceResult valueResult =
inferExpression(node.value, const UnknownType(), isVoidAllowed: false);
inferExpression(node.value, valueType, isVoidAllowed: false);
valueResult = ensureAssignableResult(valueType, valueResult);
Expression value = valueResult.expression;

View file

@ -0,0 +1,48 @@
// 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 type inference supplies the proper context for the right hand
/// side of a property set that refers to an extension method.
import '../static_type_helper.dart';
extension E on Object? {
set s1(int i) {}
set s2(int? i) {}
}
class C {
set s3(int i) {}
set s4(int? i) {}
}
main() {
var string = '';
context<num>(string.s1 = contextType(1)..expectStaticType<Exactly<int>>());
context<num>(E(string).s1 = contextType(1)..expectStaticType<Exactly<int>>());
context<num?>(string.s2 = contextType(1)..expectStaticType<Exactly<int?>>());
context<num?>(
E(string).s2 = contextType(1)..expectStaticType<Exactly<int?>>());
var nullableString = '' as String?;
context<num?>(
nullableString?.s1 = contextType(1)..expectStaticType<Exactly<int>>());
context<num?>(
E(nullableString)?.s1 = contextType(1)..expectStaticType<Exactly<int>>());
context<num?>(
nullableString?.s2 = contextType(1)..expectStaticType<Exactly<int?>>());
context<num?>(E(nullableString)?.s2 = contextType(1)
..expectStaticType<Exactly<int?>>());
// And just to verify that the expectations above are reasonable, repeat the
// same thing with an ordinary class:
var c = C();
context<num>(c.s3 = contextType(1)..expectStaticType<Exactly<int>>());
context<num?>(c.s4 = contextType(1)..expectStaticType<Exactly<int?>>());
var nullableC = C() as C?;
context<num?>(
nullableC?.s3 = contextType(1)..expectStaticType<Exactly<int>>());
context<num?>(
nullableC?.s4 = contextType(1)..expectStaticType<Exactly<int?>>());
}