Skip to content

Commit f767c70

Browse files
authored
Fix warning on conversion of anon func with implicit parameters (#59522)
1 parent 07c890c commit f767c70

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public QueryUnboundLambdaState(Binder binder, RangeVariableMap rangeVariableMap,
3434
public override bool ParameterIsDiscard(int index) { return false; }
3535
public override bool ParameterIsNullChecked(int index) { return false; }
3636
public override SyntaxList<AttributeListSyntax> ParameterAttributes(int index) => default;
37-
public override bool HasNames { get { return true; } }
3837
public override bool HasSignature { get { return true; } }
3938

4039
public override bool HasExplicitReturnType(out RefKind refKind, out TypeWithAnnotations returnType)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ private UnboundLambda BindAnonymousFunction(AnonymousFunctionExpressionSyntax sy
335335
// are reported now.
336336
ModifierUtils.ToDeclarationModifiers(syntax.Modifiers, diagnostics.DiagnosticBag ?? new DiagnosticBag());
337337

338-
if (data.HasNames)
338+
if (data.HasSignature)
339339
{
340340
var binder = new LocalScopeBinder(this);
341341
bool allowShadowingNames = binder.Compilation.IsFeatureEnabled(MessageID.IDS_FeatureNameShadowingInNestedFunctions);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ internal UnboundLambdaState WithCaching(bool includeCache)
526526
public abstract bool HasExplicitlyTypedParameterList { get; }
527527
public abstract int ParameterCount { get; }
528528
public abstract bool IsAsync { get; }
529-
public abstract bool HasNames { get; }
530529
public abstract bool IsStatic { get; }
531530
public abstract Location ParameterLocation(int index);
532531
public abstract TypeWithAnnotations ParameterTypeWithAnnotations(int index);
@@ -1357,7 +1356,6 @@ internal PlainUnboundLambdaState(
13571356
_isStatic = isStatic;
13581357
}
13591358

1360-
public override bool HasNames { get { return !_parameterNames.IsDefault; } }
13611359

13621360
public override bool HasSignature { get { return !_parameterNames.IsDefault; } }
13631361

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7570,10 +7570,14 @@ 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+
// For anonymous functions with implicit parameters, no need to report this since the parameters can't be referenced
7574+
if (unboundLambda.HasSignature)
7575+
{
7576+
ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate, location,
7577+
unboundLambda.ParameterName(parameterSymbol.Ordinal),
7578+
unboundLambda.MessageID.Localize(),
7579+
delegateType);
7580+
}
75777581
}
75787582
}
75797583

src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155949,5 +155949,26 @@ void M()
155949155949
Diagnostic(ErrorCode.WRN_NullabilityMismatchInReturnTypeOfTargetDelegate, "d2").WithArguments("ref readonly string? D<string?>.Invoke()", "D<string>").WithLocation(9, 28)
155950155950
);
155951155951
}
155952+
155953+
[Fact, WorkItem(59336, "https://github.com/dotnet/roslyn/issues/59336")]
155954+
public void DelegateConversionWithImplicitParameters()
155955+
{
155956+
var source = @"
155957+
#nullable enable
155958+
155959+
public delegate void MyDelegate([System.Diagnostics.CodeAnalysis.MaybeNull] object first, [System.Diagnostics.CodeAnalysis.MaybeNull] object second);
155960+
155961+
class C
155962+
{
155963+
public event MyDelegate? MyEvent;
155964+
void M()
155965+
{
155966+
MyEvent += delegate { };
155967+
MyEvent?.Invoke(new object(), new object());
155968+
}
155969+
}";
155970+
var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition });
155971+
comp.VerifyDiagnostics();
155972+
}
155952155973
}
155953155974
}

0 commit comments

Comments
 (0)