Skip to content

Commit cb33d99

Browse files
author
Julien Couvreur
committed
Fix nullable warning on conversion of anonymous function with implicit parameters
1 parent a73b061 commit cb33d99

22 files changed

+188
-4
lines changed

src/Compilers/CSharp/Portable/Binder/Binder.QueryUnboundLambdaState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public QueryUnboundLambdaState(Binder binder, RangeVariableMap rangeVariableMap,
3030
_bodyFactory = bodyFactory;
3131
}
3232

33+
public override bool HasParameterNames() { return true; }
3334
public override string ParameterName(int index) { return _parameters[index].Name; }
3435
public override bool ParameterIsDiscard(int index) { return false; }
3536
public override bool ParameterIsNullChecked(int index) { return false; }

src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ public TypeWithAnnotations InferReturnType(ConversionsBase conversions, NamedTyp
457457
public TypeWithAnnotations ParameterTypeWithAnnotations(int index) { return Data.ParameterTypeWithAnnotations(index); }
458458
public TypeSymbol ParameterType(int index) { return ParameterTypeWithAnnotations(index).Type; }
459459
public Location ParameterLocation(int index) { return Data.ParameterLocation(index); }
460+
public bool HasParameterNames() { return Data.HasParameterNames(); }
460461
public string ParameterName(int index) { return Data.ParameterName(index); }
461462
public bool ParameterIsDiscard(int index) { return Data.ParameterIsDiscard(index); }
462463
public bool ParameterIsNullChecked(int index) { return Data.ParameterIsNullChecked(index); }
@@ -517,6 +518,7 @@ internal UnboundLambdaState WithCaching(bool includeCache)
517518
public UnboundLambda UnboundLambda => _unboundLambda;
518519

519520
public abstract MessageID MessageID { get; }
521+
public abstract bool HasParameterNames();
520522
public abstract string ParameterName(int index);
521523
public abstract bool ParameterIsDiscard(int index);
522524
public abstract bool ParameterIsNullChecked(int index);
@@ -1409,6 +1411,8 @@ public override SyntaxList<AttributeListSyntax> ParameterAttributes(int index)
14091411
return _parameterAttributes.IsDefault ? default : _parameterAttributes[index];
14101412
}
14111413

1414+
public override bool HasParameterNames() { return !_parameterNames.IsDefault; }
1415+
14121416
public override string ParameterName(int index)
14131417
{
14141418
Debug.Assert(!_parameterNames.IsDefault && 0 <= index && index < _parameterNames.Length);

src/Compilers/CSharp/Portable/CSharpResources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5668,6 +5668,12 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
56685668
<data name="WRN_NullabilityMismatchInParameterTypeOfTargetDelegate_Title" xml:space="preserve">
56695669
<value>Nullability of reference types in type of parameter doesn't match the target delegate (possibly because of nullability attributes).</value>
56705670
</data>
5671+
<data name="WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate" xml:space="preserve">
5672+
<value>Nullability of reference types in type of implicit parameter with index {0} of '{1}' doesn't match the target delegate '{2}' (possibly because of nullability attributes).</value>
5673+
</data>
5674+
<data name="WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate_Title" xml:space="preserve">
5675+
<value>Nullability of reference types in type of implicit parameter doesn't match the target delegate (possibly because of nullability attributes).</value>
5676+
</data>
56715677
<data name="WRN_NullAsNonNullable" xml:space="preserve">
56725678
<value>Cannot convert null literal to non-nullable reference type.</value>
56735679
</data>

src/Compilers/CSharp/Portable/Errors/ErrorCode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ internal enum ErrorCode
17151715
ERR_NullableDirectiveTargetExpected = 8668,
17161716
WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode = 8669,
17171717
WRN_NullReferenceInitializer = 8670,
1718+
WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate = 8671,
17181719

17191720
ERR_MultipleAnalyzerConfigsInSameDir = 8700,
17201721

src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static ErrorFacts()
3434
nullableWarnings.Add(GetId(ErrorCode.WRN_NullabilityMismatchInArgumentForOutput));
3535
nullableWarnings.Add(GetId(ErrorCode.WRN_NullabilityMismatchInReturnTypeOfTargetDelegate));
3636
nullableWarnings.Add(GetId(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate));
37+
nullableWarnings.Add(GetId(ErrorCode.WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate));
3738
nullableWarnings.Add(GetId(ErrorCode.WRN_NullAsNonNullable));
3839
nullableWarnings.Add(GetId(ErrorCode.WRN_NullableValueTypeMayBeNull));
3940
nullableWarnings.Add(GetId(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint));
@@ -434,6 +435,7 @@ internal static int GetWarningLevel(ErrorCode code)
434435
case ErrorCode.WRN_NullabilityMismatchInArgumentForOutput:
435436
case ErrorCode.WRN_NullabilityMismatchInReturnTypeOfTargetDelegate:
436437
case ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate:
438+
case ErrorCode.WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate:
437439
case ErrorCode.WRN_NullAsNonNullable:
438440
case ErrorCode.WRN_NullableValueTypeMayBeNull:
439441
case ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint:

src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7570,10 +7570,20 @@ void reportBadDelegateReturn(BindingDiagnosticBag bag, MethodSymbol targetInvoke
75707570

75717571
void reportBadDelegateParameter(BindingDiagnosticBag bag, MethodSymbol sourceInvokeMethod, MethodSymbol targetInvokeMethod, ParameterSymbol parameterSymbol, bool topLevel, Location location)
75727572
{
7573-
ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate, location,
7574-
unboundLambda.ParameterName(parameterSymbol.Ordinal),
7575-
unboundLambda.MessageID.Localize(),
7576-
delegateType);
7573+
if (unboundLambda.HasParameterNames())
7574+
{
7575+
ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate, location,
7576+
unboundLambda.ParameterName(parameterSymbol.Ordinal),
7577+
unboundLambda.MessageID.Localize(),
7578+
delegateType);
7579+
}
7580+
else
7581+
{
7582+
ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInImplicitParameterTypeOfTargetDelegate, location,
7583+
parameterSymbol.Ordinal,
7584+
unboundLambda.MessageID.Localize(),
7585+
delegateType);
7586+
}
75777587
}
75787588
}
75797589

src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)