Skip to content

Commit

Permalink
Fix warning on conversion of anon func with implicit parameters (#59522)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Feb 15, 2022
1 parent 07c890c commit f767c70
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributeListSyntax> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1357,7 +1356,6 @@ internal PlainUnboundLambdaState(
_isStatic = isStatic;
}

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

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

Expand Down
12 changes: 8 additions & 4 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155949,5 +155949,26 @@ void M()
Diagnostic(ErrorCode.WRN_NullabilityMismatchInReturnTypeOfTargetDelegate, "d2").WithArguments("ref readonly string? D<string?>.Invoke()", "D<string>").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();
}
}
}

0 comments on commit f767c70

Please sign in to comment.