diff --git a/src/Compilers/CSharp/Portable/Binder/Binder.QueryUnboundLambdaState.cs b/src/Compilers/CSharp/Portable/Binder/Binder.QueryUnboundLambdaState.cs index a65b7103def98..4f5f4e34a4531 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder.QueryUnboundLambdaState.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder.QueryUnboundLambdaState.cs @@ -34,7 +34,6 @@ public QueryUnboundLambdaState(Binder binder, RangeVariableMap rangeVariableMap, public override bool ParameterIsDiscard(int index) { return false; } public override bool ParameterIsNullChecked(int index) { return false; } public override SyntaxList ParameterAttributes(int index) => default; - public override bool HasNames { get { return true; } } public override bool HasSignature { get { return true; } } public override bool HasExplicitReturnType(out RefKind refKind, out TypeWithAnnotations returnType) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs index 68b5133be2f19..83e729bc1f0e8 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs @@ -335,7 +335,7 @@ private UnboundLambda BindAnonymousFunction(AnonymousFunctionExpressionSyntax sy // are reported now. ModifierUtils.ToDeclarationModifiers(syntax.Modifiers, diagnostics.DiagnosticBag ?? new DiagnosticBag()); - if (data.HasNames) + if (data.HasSignature) { var binder = new LocalScopeBinder(this); bool allowShadowingNames = binder.Compilation.IsFeatureEnabled(MessageID.IDS_FeatureNameShadowingInNestedFunctions); diff --git a/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs b/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs index 76baf682e82d8..fce66b2e44abf 100644 --- a/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs +++ b/src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs @@ -526,7 +526,6 @@ internal UnboundLambdaState WithCaching(bool includeCache) public abstract bool HasExplicitlyTypedParameterList { get; } public abstract int ParameterCount { get; } public abstract bool IsAsync { get; } - public abstract bool HasNames { get; } public abstract bool IsStatic { get; } public abstract Location ParameterLocation(int index); public abstract TypeWithAnnotations ParameterTypeWithAnnotations(int index); @@ -1357,7 +1356,6 @@ internal PlainUnboundLambdaState( _isStatic = isStatic; } - public override bool HasNames { get { return !_parameterNames.IsDefault; } } public override bool HasSignature { get { return !_parameterNames.IsDefault; } } diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index 6ff752fbfba82..d052ecd9e89cf 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -7570,10 +7570,14 @@ void reportBadDelegateReturn(BindingDiagnosticBag bag, MethodSymbol targetInvoke void reportBadDelegateParameter(BindingDiagnosticBag bag, MethodSymbol sourceInvokeMethod, MethodSymbol targetInvokeMethod, ParameterSymbol parameterSymbol, bool topLevel, Location location) { - ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate, location, - unboundLambda.ParameterName(parameterSymbol.Ordinal), - unboundLambda.MessageID.Localize(), - delegateType); + // For anonymous functions with implicit parameters, no need to report this since the parameters can't be referenced + if (unboundLambda.HasSignature) + { + ReportDiagnostic(ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate, location, + unboundLambda.ParameterName(parameterSymbol.Ordinal), + unboundLambda.MessageID.Localize(), + delegateType); + } } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index 6456143bb87a9..01f4c859d344b 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -155949,5 +155949,26 @@ void M() Diagnostic(ErrorCode.WRN_NullabilityMismatchInReturnTypeOfTargetDelegate, "d2").WithArguments("ref readonly string? D.Invoke()", "D").WithLocation(9, 28) ); } + + [Fact, WorkItem(59336, "https://github.com/dotnet/roslyn/issues/59336")] + public void DelegateConversionWithImplicitParameters() + { + var source = @" +#nullable enable + +public delegate void MyDelegate([System.Diagnostics.CodeAnalysis.MaybeNull] object first, [System.Diagnostics.CodeAnalysis.MaybeNull] object second); + +class C +{ + public event MyDelegate? MyEvent; + void M() + { + MyEvent += delegate { }; + MyEvent?.Invoke(new object(), new object()); + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics(); + } } }