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

Add null checks in AwaitExpressionInfo #32062

Merged
merged 3 commits into from
Jan 1, 2019
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
14 changes: 9 additions & 5 deletions src/Compilers/CSharp/Portable/Compilation/AwaitExpressionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public struct AwaitExpressionInfo : IEquatable<AwaitExpressionInfo>
{
private readonly AwaitableInfo _awaitableInfo;

public IMethodSymbol GetAwaiterMethod => _awaitableInfo.GetAwaiter;
public IMethodSymbol GetAwaiterMethod => _awaitableInfo?.GetAwaiter;

public IPropertySymbol IsCompletedProperty => _awaitableInfo.IsCompleted;
public IPropertySymbol IsCompletedProperty => _awaitableInfo?.IsCompleted;

public IMethodSymbol GetResultMethod => _awaitableInfo.GetResult;
public IMethodSymbol GetResultMethod => _awaitableInfo?.GetResult;

public bool IsDynamic => _awaitableInfo.IsDynamic;
public bool IsDynamic => _awaitableInfo?.IsDynamic == true;

internal AwaitExpressionInfo(AwaitableInfo awaitableInfo)
{
Expand All @@ -29,7 +29,7 @@ internal AwaitExpressionInfo(AwaitableInfo awaitableInfo)

public override bool Equals(object obj)
{
return obj is AwaitExpressionInfo && Equals((AwaitExpressionInfo)obj);
return obj is AwaitExpressionInfo otherAwait && Equals(otherAwait);
}

public bool Equals(AwaitExpressionInfo other)
Expand All @@ -41,6 +41,10 @@ public bool Equals(AwaitExpressionInfo other)

public override int GetHashCode()
{
if (_awaitableInfo is null)
{
return 0;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: while you're here could change the Equals implementation to use pattern matching and avoid the double cast.

return Hash.Combine(GetAwaiterMethod, Hash.Combine(IsCompletedProperty, GetResultMethod.GetHashCode()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public C(Task<int> t) {
Assert.Equal("System.Boolean System.Runtime.CompilerServices.TaskAwaiter<System.Int32>.IsCompleted { get; }", info.IsCompletedProperty.ToTestDisplayString());
}

[Fact]
[WorkItem(744146, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/744146")]
public void DefaultAwaitExpressionInfo()
{
AwaitExpressionInfo info = default;
Assert.Null(info.GetAwaiterMethod);
Assert.Null(info.GetResultMethod);
Assert.Null(info.IsCompletedProperty);
Assert.False(info.IsDynamic);
Assert.Equal(0, info.GetHashCode());
}

private AwaitExpressionInfo GetAwaitExpressionInfo(string text, out CSharpCompilation compilation, params DiagnosticDescription[] diagnostics)
{
var tree = Parse(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ End Module
AssertTheseDiagnostics(compilation, <expected></expected>)
End Sub

<Fact, WorkItem(744146, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/744146")>
Public Sub DefaultAwaitExpressionInfo()
Dim awaitInfo As AwaitExpressionInfo = Nothing

Assert.Null(awaitInfo.GetAwaiterMethod)
Assert.Null(awaitInfo.IsCompletedProperty)
Assert.Null(awaitInfo.GetResultMethod)
End Sub

<Fact()>
Public Sub AwaitableType01()
Dim source =
Expand Down