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 2 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 @@ -30,6 +30,7 @@ public QueryUnboundLambdaState(Binder binder, RangeVariableMap rangeVariableMap,
_bodyFactory = bodyFactory;
}

public override bool HasParameterNames() { return true; }
public override string ParameterName(int index) { return _parameters[index].Name; }
public override bool ParameterIsDiscard(int index) { return false; }
public override bool ParameterIsNullChecked(int index) { return false; }
Expand Down
4 changes: 4 additions & 0 deletions src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public TypeWithAnnotations InferReturnType(ConversionsBase conversions, NamedTyp
public TypeWithAnnotations ParameterTypeWithAnnotations(int index) { return Data.ParameterTypeWithAnnotations(index); }
public TypeSymbol ParameterType(int index) { return ParameterTypeWithAnnotations(index).Type; }
public Location ParameterLocation(int index) { return Data.ParameterLocation(index); }
public bool HasParameterNames() { return Data.HasParameterNames(); }
public string ParameterName(int index) { return Data.ParameterName(index); }
public bool ParameterIsDiscard(int index) { return Data.ParameterIsDiscard(index); }
public bool ParameterIsNullChecked(int index) { return Data.ParameterIsNullChecked(index); }
Expand Down Expand Up @@ -517,6 +518,7 @@ internal UnboundLambdaState WithCaching(bool includeCache)
public UnboundLambda UnboundLambda => _unboundLambda;

public abstract MessageID MessageID { get; }
public abstract bool HasParameterNames();
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
public abstract string ParameterName(int index);
public abstract bool ParameterIsDiscard(int index);
public abstract bool ParameterIsNullChecked(int index);
Expand Down Expand Up @@ -1409,6 +1411,8 @@ public override SyntaxList<AttributeListSyntax> ParameterAttributes(int index)
return _parameterAttributes.IsDefault ? default : _parameterAttributes[index];
}

public override bool HasParameterNames() { return !_parameterNames.IsDefault; }

public override string ParameterName(int index)
{
Debug.Assert(!_parameterNames.IsDefault && 0 <= index && index < _parameterNames.Length);
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.HasParameterNames())
{
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 ParameterNameCrash()
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();
}
}
}