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

Await using declarations #31980

Merged
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
36 changes: 34 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,25 @@ private BoundStatement BindUsingDeclarationStatementParts(LocalDeclarationStatem
{
Conversion iDisposableConversion;
MethodSymbol disposeMethod;
bool hasAwait = node.AwaitKeyword != default;
var declarations = BindUsingVariableDeclaration(
this,
diagnostics,
diagnostics.HasAnyErrors(),
node,
node.Declaration,
hasAwait: false,
hasAwait,
out iDisposableConversion,
out disposeMethod);
return new BoundUsingLocalDeclarations(node, disposeMethod, iDisposableConversion, declarations);

AwaitableInfo awaitOpt = null;
bool hasErrors = false;
if (hasAwait)
{
awaitOpt = BindAsyncDisposeAwaiter(node, node.AwaitKeyword, disposeMethod, diagnostics, ref hasErrors);
}

return new BoundUsingLocalDeclarations(node, disposeMethod, iDisposableConversion, awaitOpt, declarations, hasErrors);
}

private BoundStatement BindDeclarationStatementParts(LocalDeclarationStatementSyntax node, DiagnosticBag diagnostics)
Expand Down Expand Up @@ -762,6 +771,29 @@ internal MethodSymbol TryFindDisposePatternMethod(BoundExpression expr, SyntaxNo
return disposeMethod;
}

/// <summary>
/// Binds an awaiter for asynchronous dispose
/// </summary>
/// <param name="node">The syntax node to bind for</param>
/// <param name="awaitKeyword">The await keyword of the syntax</param>
/// <param name="disposeMethodOpt">The dispose method to call, or null to use IAsyncDisposable.DisposeAsync</param>
/// <param name="diagnostics">Populated with any errors</param>
/// <param name="hasErrors">True if errors occurred during binding</param>
/// <returns>An <see cref="AwaitableInfo"/> with the bound information</returns>
protected AwaitableInfo BindAsyncDisposeAwaiter(SyntaxNode node, SyntaxToken awaitKeyword, MethodSymbol disposeMethodOpt, DiagnosticBag diagnostics, ref bool hasErrors)
{
TypeSymbol taskType = disposeMethodOpt is null
? this.Compilation.GetWellKnownType(WellKnownType.System_Threading_Tasks_ValueTask)
Copy link
Member

@jcouv jcouv Dec 21, 2018

Choose a reason for hiding this comment

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

GetWellKnownType [](start = 55, length = 16)

Please add a test where ValueTask is made missing (see MakeTypeMissing test helper).
Also a test where an obsolete or use-site diagnostic is reported. #Closed

: disposeMethodOpt.ReturnType.TypeSymbol;

hasErrors |= ReportUseSiteDiagnostics(taskType, diagnostics, awaitKeyword);

BoundExpression placeholder = new BoundAwaitableValuePlaceholder(node, taskType).MakeCompilerGenerated();
AwaitableInfo awaitOpt = BindAwaitInfo(placeholder, node, awaitKeyword.GetLocation(), diagnostics, ref hasErrors);
return awaitOpt;
}


private TypeSymbolWithAnnotations BindVariableType(CSharpSyntaxNode declarationNode, DiagnosticBag diagnostics, TypeSyntax typeSyntax, ref bool isConst, out bool isVar, out AliasSymbol alias)
{
Debug.Assert(
Expand Down
6 changes: 1 addition & 5 deletions src/Compilers/CSharp/Portable/Binder/UsingStatementBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ internal override BoundStatement BindUsingStatementParts(DiagnosticBag diagnosti

if (hasAwait)
{
TypeSymbol taskType = this.Compilation.GetWellKnownType(WellKnownType.System_Threading_Tasks_ValueTask);
hasErrors |= ReportUseSiteDiagnostics(taskType, diagnostics, _syntax.AwaitKeyword);

var resource = (SyntaxNode)expressionSyntax ?? declarationSyntax;
BoundExpression placeholder = new BoundAwaitableValuePlaceholder(resource, taskType).MakeCompilerGenerated();
awaitOpt = BindAwaitInfo(placeholder, resource, _syntax.AwaitKeyword.GetLocation(), diagnostics, ref hasErrors);
awaitOpt = BindAsyncDisposeAwaiter(resource, _syntax.AwaitKeyword, disposeMethod, diagnostics, ref hasErrors);
}

BoundStatement boundBody = originalBinder.BindPossibleEmbeddedStatement(_syntax.Statement, diagnostics);
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@
<Node Name="BoundUsingLocalDeclarations" Base="BoundMultipleLocalDeclarations">
<Field Name="DisposeMethodOpt" Type="MethodSymbol" Null="Allow"/>
<Field Name="IDisposableConversion" Type="Conversion"/>
<Field Name="AwaitOpt" Type="AwaitableInfo" Null="allow"/>
</Node>

<!--
Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5640,4 +5640,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_GoToBackwardJumpOverUsingVar" xml:space="preserve">
<value>A goto cannot jump to a location before a using declaration within the same block.</value>
</data>
<data name="IDS_FeatureUsingDeclarations" xml:space="preserve">
<value>using declarations</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ internal enum MessageID
IDS_FeatureRangeOperator = MessageBase + 12751,
IDS_FeatureAsyncStreams = MessageBase + 12752,
IDS_Disposable = MessageBase + 12753,
IDS_FeatureUsingDeclarations = MessageBase + 12754
}

// Message IDs may refer to strings that need to be localized.
Expand Down Expand Up @@ -244,6 +245,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureIndexOperator: // semantic check
case MessageID.IDS_FeatureRangeOperator: // semantic check
case MessageID.IDS_FeatureAsyncStreams:
case MessageID.IDS_FeatureUsingDeclarations:
return LanguageVersion.CSharp8;

// C# 7.3 features.
Expand Down
12 changes: 8 additions & 4 deletions src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,26 @@ protected override ImmutableArray<PendingBranch> RemoveReturns()

private static bool HasAwait(PendingBranch pending)
{
if (pending.Branch is null)
var pendingBranch = pending.Branch;
if (pendingBranch is null)
{
return false;
}

BoundKind kind = pending.Branch.Kind;
BoundKind kind = pendingBranch.Kind;
switch (kind)
{
case BoundKind.AwaitExpression:
return true;
case BoundKind.UsingStatement:
var usingStatement = (BoundUsingStatement)pending.Branch;
var usingStatement = (BoundUsingStatement)pendingBranch;
return usingStatement.AwaitOpt != null;
case BoundKind.ForEachStatement:
var foreachStatement = (BoundForEachStatement)pending.Branch;
var foreachStatement = (BoundForEachStatement)pendingBranch;
return foreachStatement.AwaitOpt != null;
case BoundKind.UsingLocalDeclarations:
var localDeclaration = (BoundUsingLocalDeclarations)pendingBranch;
return localDeclaration.AwaitOpt != null;
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,10 @@ public override BoundNode VisitMultipleLocalDeclarations(BoundMultipleLocalDecla

public override BoundNode VisitUsingLocalDeclarations(BoundUsingLocalDeclarations node)
{
if (AwaitUsingAddsPendingBranch && node.AwaitOpt != null)
{
_pendingBranches.Add(new PendingBranch(node, this.State));
}
return VisitMultipleLocalDeclarations(node);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2685,31 +2685,34 @@ public BoundMultipleLocalDeclarations Update(ImmutableArray<BoundLocalDeclaratio

internal sealed partial class BoundUsingLocalDeclarations : BoundMultipleLocalDeclarations
{
public BoundUsingLocalDeclarations(SyntaxNode syntax, MethodSymbol disposeMethodOpt, Conversion iDisposableConversion, ImmutableArray<BoundLocalDeclaration> localDeclarations, bool hasErrors = false)
public BoundUsingLocalDeclarations(SyntaxNode syntax, MethodSymbol disposeMethodOpt, Conversion iDisposableConversion, AwaitableInfo awaitOpt, ImmutableArray<BoundLocalDeclaration> localDeclarations, bool hasErrors = false)
: base(BoundKind.UsingLocalDeclarations, syntax, localDeclarations, hasErrors || localDeclarations.HasErrors())
{

Debug.Assert(!localDeclarations.IsDefault, "Field 'localDeclarations' cannot be null (use Null=\"allow\" in BoundNodes.xml to remove this check)");

this.DisposeMethodOpt = disposeMethodOpt;
this.IDisposableConversion = iDisposableConversion;
this.AwaitOpt = awaitOpt;
}


public MethodSymbol DisposeMethodOpt { get; }

public Conversion IDisposableConversion { get; }

public AwaitableInfo AwaitOpt { get; }

public override BoundNode Accept(BoundTreeVisitor visitor)
{
return visitor.VisitUsingLocalDeclarations(this);
}

public BoundUsingLocalDeclarations Update(MethodSymbol disposeMethodOpt, Conversion iDisposableConversion, ImmutableArray<BoundLocalDeclaration> localDeclarations)
public BoundUsingLocalDeclarations Update(MethodSymbol disposeMethodOpt, Conversion iDisposableConversion, AwaitableInfo awaitOpt, ImmutableArray<BoundLocalDeclaration> localDeclarations)
{
if (disposeMethodOpt != this.DisposeMethodOpt || iDisposableConversion != this.IDisposableConversion || localDeclarations != this.LocalDeclarations)
if (disposeMethodOpt != this.DisposeMethodOpt || iDisposableConversion != this.IDisposableConversion || awaitOpt != this.AwaitOpt || localDeclarations != this.LocalDeclarations)
{
var result = new BoundUsingLocalDeclarations(this.Syntax, disposeMethodOpt, iDisposableConversion, localDeclarations, this.HasErrors);
var result = new BoundUsingLocalDeclarations(this.Syntax, disposeMethodOpt, iDisposableConversion, awaitOpt, localDeclarations, this.HasErrors);
result.WasCompilerGenerated = this.WasCompilerGenerated;
return result;
}
Expand Down Expand Up @@ -9538,7 +9541,7 @@ public override BoundNode VisitMultipleLocalDeclarations(BoundMultipleLocalDecla
public override BoundNode VisitUsingLocalDeclarations(BoundUsingLocalDeclarations node)
{
ImmutableArray<BoundLocalDeclaration> localDeclarations = (ImmutableArray<BoundLocalDeclaration>)this.VisitList(node.LocalDeclarations);
return node.Update(node.DisposeMethodOpt, node.IDisposableConversion, localDeclarations);
return node.Update(node.DisposeMethodOpt, node.IDisposableConversion, node.AwaitOpt, localDeclarations);
}
public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatement node)
{
Expand Down Expand Up @@ -10783,6 +10786,7 @@ public override TreeDumperNode VisitUsingLocalDeclarations(BoundUsingLocalDeclar
{
new TreeDumperNode("disposeMethodOpt", node.DisposeMethodOpt, null),
new TreeDumperNode("iDisposableConversion", node.IDisposableConversion, null),
new TreeDumperNode("awaitOpt", node.AwaitOpt, null),
new TreeDumperNode("localDeclarations", null, from x in node.LocalDeclarations select Visit(x, null))
}
);
Expand Down
Loading