diff --git a/src/Compilers/CSharp/Portable/Compilation/AwaitExpressionInfo.cs b/src/Compilers/CSharp/Portable/Compilation/AwaitExpressionInfo.cs index 43ed80212011c..6657db6aab8b7 100644 --- a/src/Compilers/CSharp/Portable/Compilation/AwaitExpressionInfo.cs +++ b/src/Compilers/CSharp/Portable/Compilation/AwaitExpressionInfo.cs @@ -13,13 +13,13 @@ public struct AwaitExpressionInfo : IEquatable { 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) { @@ -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) @@ -41,6 +41,10 @@ public bool Equals(AwaitExpressionInfo other) public override int GetHashCode() { + if (_awaitableInfo is null) + { + return 0; + } return Hash.Combine(GetAwaiterMethod, Hash.Combine(IsCompletedProperty, GetResultMethod.GetHashCode())); } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/AwaitExpressionTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/AwaitExpressionTests.cs index 51d65aad6c776..9bd1f60131de3 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/AwaitExpressionTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/AwaitExpressionTests.cs @@ -54,6 +54,18 @@ public C(Task t) { Assert.Equal("System.Boolean System.Runtime.CompilerServices.TaskAwaiter.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)); diff --git a/src/Compilers/VisualBasic/Test/Semantic/Semantics/AsyncAwait.vb b/src/Compilers/VisualBasic/Test/Semantic/Semantics/AsyncAwait.vb index f90e9e6cf9b42..259315ef4700b 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Semantics/AsyncAwait.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Semantics/AsyncAwait.vb @@ -42,6 +42,15 @@ End Module AssertTheseDiagnostics(compilation, ) End Sub + + Public Sub DefaultAwaitExpressionInfo() + Dim awaitInfo As AwaitExpressionInfo = Nothing + + Assert.Null(awaitInfo.GetAwaiterMethod) + Assert.Null(awaitInfo.IsCompletedProperty) + Assert.Null(awaitInfo.GetResultMethod) + End Sub + Public Sub AwaitableType01() Dim source =