未验证 提交 9e62637c 编写于 作者: J Julien Couvreur 提交者: GitHub

Adjust type of out var based on parameter state (#36284)

上级 9cdc1fb5
......@@ -3201,23 +3201,29 @@ private void CheckDisallowedNullAssignment(TypeWithState state, TypeWithAnnotati
break;
case RefKind.Out:
{
var lValueType = result.LValueType;
// compute the fictional parameter state
var parameterWithAnnotations = ApplyRValueAnnotations(parameterType, parameterAnnotations);
var parameterWithState = TypeWithState.Create(parameterWithAnnotations, parameterAnnotations);
// Adjust parameter state if MaybeNull or MaybeNullWhen are present (for `var` type and for assignment warnings)
var worstCaseParameterWithState = applyPostConditionsUnconditionally(parameterWithState, parameterAnnotations);
var lValueType = result.LValueType;
if (argument is BoundLocal local && local.DeclarationKind == BoundLocalDeclarationKind.WithInferredType)
{
_variableTypes[local.LocalSymbol] = parameterWithAnnotations;
lValueType = parameterWithAnnotations;
var varType = worstCaseParameterWithState.ToTypeWithAnnotations();
_variableTypes[local.LocalSymbol] = varType;
lValueType = varType;
}
// assign from a fictional value from the parameter to the argument.
var parameterWithState = TypeWithState.Create(parameterWithAnnotations, parameterAnnotations);
// track state by assigning from a fictional value from the parameter to the argument.
var parameterValue = new BoundParameter(argument.Syntax, parameter);
trackNullableStateForAssignment(parameterValue, lValueType, MakeSlot(argument), parameterWithState, argument.IsSuppressed, parameterAnnotations);
// check whether parameter would unsafely let a null out in the worse case
// report warnings if parameter would unsafely let a null out in the worst case
if (!argument.IsSuppressed)
{
ReportNullableAssignmentIfNecessary(parameterValue, lValueType, applyPostConditionsUnconditionally(parameterWithState, parameterAnnotations), UseLegacyWarnings(argument, result.LValueType));
ReportNullableAssignmentIfNecessary(parameterValue, lValueType, worstCaseParameterWithState, UseLegacyWarnings(argument, result.LValueType));
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
if (!_conversions.HasIdentityOrImplicitReferenceConversion(parameterType.Type, lValueType.Type, ref useSiteDiagnostics))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册