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 out var behavior, including with NotNullWhenTrue/False #26828

Merged
merged 2 commits into from
Jun 1, 2018
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
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private void FailRemainingInferencesAndSetValEscape(ArrayBuilder<DeconstructionV
{
case BoundKind.Local:
var local = (BoundLocal)variable.Single;
if (local.IsDeclaration)
if (local.DeclarationKind != BoundLocalDeclarationKind.None)
{
((SourceLocalSymbol)local.LocalSymbol).SetValEscape(rhsValEscape);
}
Expand Down Expand Up @@ -826,7 +826,7 @@ private BoundExpression BindDeconstructionVariable(

if ((object)declType != null)
{
return new BoundLocal(syntax, localSymbol, isDeclaration: true, constantValueOpt: null, isNullableUnknown: false, type: declType.TypeSymbol, hasErrors: hasErrors);
return new BoundLocal(syntax, localSymbol, BoundLocalDeclarationKind.WithExplicitType, constantValueOpt: null, isNullableUnknown: false, type: declType.TypeSymbol, hasErrors: hasErrors);
}

return new DeconstructionVariablePendingInference(syntax, localSymbol, receiverOpt: null);
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ private BoundExpression BindNonMethod(SimpleNameSyntax node, Symbol symbol, Diag

var constantValueOpt = localSymbol.IsConst && !IsInsideNameof && !type.IsErrorType()
? localSymbol.GetConstantValue(node, this.LocalInProgress, diagnostics) : null;
return new BoundLocal(node, localSymbol, isDeclaration: false, constantValueOpt: constantValueOpt, isNullableUnknown: isNullableUnknown, type: type, hasErrors: isError);
return new BoundLocal(node, localSymbol, BoundLocalDeclarationKind.None, constantValueOpt: constantValueOpt, isNullableUnknown: isNullableUnknown, type: type, hasErrors: isError);
}

case SymbolKind.Parameter:
Expand Down Expand Up @@ -2347,7 +2347,7 @@ private BoundExpression BindOutVariableDeclarationArgument(

CheckRestrictedTypeInAsync(this.ContainingMemberOrLambda, declType.TypeSymbol, diagnostics, typeSyntax);

return new BoundLocal(declarationExpression, localSymbol, isDeclaration: true, constantValueOpt: null, isNullableUnknown: false, type: declType.TypeSymbol);
return new BoundLocal(declarationExpression, localSymbol, BoundLocalDeclarationKind.WithExplicitType, constantValueOpt: null, isNullableUnknown: false, type: declType.TypeSymbol);
}

// Is this a field?
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/BoundTree/BoundExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ public override Symbol ExpressionSymbol
}

public BoundLocal(SyntaxNode syntax, LocalSymbol localSymbol, ConstantValue constantValueOpt, TypeSymbol type, bool hasErrors = false)
: this(syntax, localSymbol, false, constantValueOpt, false, type, hasErrors)
: this(syntax, localSymbol, BoundLocalDeclarationKind.None, constantValueOpt, false, type, hasErrors)
{
}

public BoundLocal Update(LocalSymbol localSymbol, ConstantValue constantValueOpt, TypeSymbol type)
{
return this.Update(localSymbol, this.IsDeclaration, constantValueOpt, this.IsNullableUnknown, type);
return this.Update(localSymbol, this.DeclarationKind, constantValueOpt, this.IsNullableUnknown, type);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// Indicates whether a bound local is also a declaration, and if so was it a declaration with an explicit or an inferred type.
/// Ex:
/// - In `M(x)`, `x` has `LocalDeclarationKind.None`
/// - In `M(out int x)`, `x` has `LocalDeclarationKind.WithExplicitType`
/// - In `M(out var x)`, `x` has `LocalDeclarationKind.WithInferredType`
/// </summary>
internal enum BoundLocalDeclarationKind
{
None = 0,
WithExplicitType,
WithInferredType
}
}
5 changes: 3 additions & 2 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<ValueType Name="NoOpStatementFlavor"/>
<ValueType Name="RefKind"/>
<ValueType Name="BoundTypeOrValueData"/>
<ValueType Name="BoundLocalDeclarationKind"/>

<AbstractNode Name="BoundInitializer" Base="BoundNode"/>

Expand Down Expand Up @@ -1059,7 +1060,7 @@
<!-- Non-null type is required for this node kind -->
<Field Name="Type" Type="TypeSymbol" Override="true" Null="disallow"/>
<Field Name="LocalSymbol" Type="LocalSymbol"/>
<Field Name="IsDeclaration" Type="bool"/>
<Field Name="DeclarationKind" Type="BoundLocalDeclarationKind" Null="NotApplicable"/>
<Field Name="ConstantValueOpt" Type="ConstantValue" Null="allow"/>
<!-- True if LocalSymbol.Type.IsNullable could not be inferred. -->
<Field Name="IsNullableUnknown" Type="bool"/>
Expand Down Expand Up @@ -1728,7 +1729,7 @@
<!-- Special node, used only during nullability flow analysis -->
<!-- PROTOTYPE(NullableReferenceTypes): Derive from BoundValuePlaceholderBase and give specific name. -->
<Node Name="BoundValuePlaceholder" Base="BoundExpression">
<Field Name="Type" Type="TypeSymbol" Override="true" Null="disallow"/>
<Field Name="Type" Type="TypeSymbol" Override="true" Null="allow"/> <!-- We use null Type for placeholders representing out vars -->
Copy link
Contributor

@AlekseyTs AlekseyTs May 29, 2018

Choose a reason for hiding this comment

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

[](start = 72, length = 64)

Is this done only in flow analysis? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this placeholder is only created in NullableWalker.
The usage that is relevant here is in InferMethod.


In reply to: 191560002 [](ancestors = 191560002)

<Field Name="IsNullable" Type="bool?" Null="allow"/>
</Node>
</Tree>
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal BoundExpression SetInferredType(TypeSymbol type, Binder binderOpt, Diag
}

localSymbol.SetTypeSymbol(TypeSymbolWithAnnotations.Create(type));
return new BoundLocal(this.Syntax, localSymbol, isDeclaration: true, constantValueOpt: null, isNullableUnknown: false, type: type, hasErrors: this.HasErrors || inferenceFailed);
return new BoundLocal(this.Syntax, localSymbol, BoundLocalDeclarationKind.WithInferredType, constantValueOpt: null, isNullableUnknown: false, type: type, hasErrors: this.HasErrors || inferenceFailed);

case SymbolKind.Field:
var fieldSymbol = (GlobalExpressionVariable)this.VariableSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override bool Equals(object obj)

internal string GetDebuggerDisplay()
{
return $"ContainingSlot={ContainingSlot}, Symbol={Symbol}";
return $"ContainingSlot={ContainingSlot}, Symbol={Symbol.GetDebuggerDisplay()}";
}
}
}
Expand Down
Loading