Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning on conversion of anon func with implicit parameters #59522

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this member was just totally redundant in practice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, they have the same meaning. We had HasNames, HasSignature and I was adding yet another one...


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()
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
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();
}
}
}