diff --git a/docs/features/outvar.md b/docs/features/outvar.md index 3327be1b6afb6..3ec88d4e383b6 100644 --- a/docs/features/outvar.md +++ b/docs/features/outvar.md @@ -15,10 +15,10 @@ You may use the contextual keyword `var` for the variable's type. The scope will be the same as for a *pattern-variable* introduced via pattern-matching. According to Language Specification (section 7.6.7 Element access) -The argument-list of an element-access is not allowed to contain ref or out arguments. -However, due to backward compatibility, compiler overlooks this restriction during parsing -and even ignores out/ref modifiers in element access during binding. -We will enforce that language rule for out variables declarations at the syntax level. +the argument-list of an element-access (indexing expression) +does not contain ref or out arguments. +However, they are permitted by the compiler for various scenarios, for example indexers +declared in metadata that accept `out`. Within the scope of a local variable introduced by a local-variable-declaration, it is a compile-time error to refer to that local variable in a textual position @@ -27,10 +27,23 @@ that precedes its declaration. It is also an error to reference implicitly-typed (§8.5.1) out variable in the same argument list that immediately contains its declaration. -For the purposes of overload resolution (see sections 7.5.3.2 Better function member and 7.5.3.3 Better conversion from expression), -neither conversion is considered better when corresponding argument is an implicitly-typed out variable declaration. -Once overload resolution succeeds, the type of implicitly-typed out variable is set to be equal to the type of the -corresponding parameter in the signature of the method. +Overload resolution is modified as follows: + +We add a new conversion: + +> There is a *conversion from expression* from an implicitly-typed out variable declaration to every type. + +Also + +> The type of an explicitly-typed out variable argument is the declared type. + +and + +> An implicitly-typed out variable argument has no type. + +Neither conversion from expression is better when the argument is an implicitly-typed out variable declaration. (this needs to be woven into the form of the specification) + +The type of an implicitly-typed out variable is the type of the corresponding parameter in the signature of the method. The new syntax node `DeclarationExpressionSyntax` is added to represent the declaration in an out var argument. diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs index 6cb4c4db7e3d2..2b05bce5e1bbe 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs @@ -725,7 +725,7 @@ private BoundExpression BindDeconstructionDeclarationVariable( } // Is this a field? - SourceMemberFieldSymbolFromDesignation field = LookupDeclaredField(designation); + GlobalExpressionVariable field = LookupDeclaredField(designation); if ((object)field == null) { diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs index 06ea5f338d14d..ddb86b677083f 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs @@ -2130,7 +2130,7 @@ private BoundExpression BindOutVariableArgument(DeclarationExpressionSyntax decl } // Is this a field? - SourceMemberFieldSymbolFromDesignation expressionVariableField = LookupDeclaredField(declarationExpression.VariableDesignation()); + GlobalExpressionVariable expressionVariableField = LookupDeclaredField(declarationExpression.VariableDesignation()); if ((object)expressionVariableField == null) { @@ -2158,14 +2158,24 @@ private BoundExpression BindOutVariableArgument(DeclarationExpressionSyntax decl expressionVariableField, null, LookupResultKind.Viable, fieldType); } - internal SourceMemberFieldSymbolFromDesignation LookupDeclaredField(SingleVariableDesignationSyntax variableDesignator) + internal GlobalExpressionVariable LookupDeclaredField(SingleVariableDesignationSyntax variableDesignator) { - foreach (Symbol member in ContainingType?.GetMembers(variableDesignator.Identifier.ValueText) ?? ImmutableArray.Empty) + return LookupDeclaredField(variableDesignator, variableDesignator.Identifier.ValueText); + } + + internal GlobalExpressionVariable LookupDeclaredField(DeclarationPatternSyntax variable) + { + return LookupDeclaredField(variable, variable.Identifier.ValueText); + } + + internal GlobalExpressionVariable LookupDeclaredField(SyntaxNode node, string identifier) + { + foreach (Symbol member in ContainingType?.GetMembers(identifier) ?? ImmutableArray.Empty) { - SourceMemberFieldSymbolFromDesignation field; + GlobalExpressionVariable field; if (member.Kind == SymbolKind.Field && - (field = member as SourceMemberFieldSymbolFromDesignation)?.SyntaxTree == variableDesignator.SyntaxTree && - field.SyntaxNode == variableDesignator) + (field = member as GlobalExpressionVariable)?.SyntaxTree == node.SyntaxTree && + field.SyntaxNode == node) { return field; } diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index f2e6cef6559d1..453b4979cf3da 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -249,11 +249,6 @@ private BoundPattern BindDeclarationPattern( { Debug.Assert(operand != null && operandType != (object)null); - if (InConstructorInitializer || InFieldInitializer) - { - Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, node); - } - var typeSyntax = node.Type; var identifier = node.Identifier; @@ -279,39 +274,47 @@ private BoundPattern BindDeclarationPattern( else { hasErrors |= CheckValidPatternType(typeSyntax, operand, operandType, declType, - isVar: isVar, patternTypeWasInSource: true, diagnostics: diagnostics); + isVar: isVar, patternTypeWasInSource: true, diagnostics: diagnostics); } SourceLocalSymbol localSymbol = this.LookupLocal(identifier); - // In error scenarios with misplaced code, it is possible we can't bind the local declaration. - // This occurs through the semantic model. In that case concoct a plausible result. - if (localSymbol == (object)null) + if (localSymbol != (object)null) { - localSymbol = SourceLocalSymbol.MakeLocal( - ContainingMemberOrLambda, - this, - false, // do not allow ref - typeSyntax, - identifier, - LocalDeclarationKind.PatternVariable); - } + if (InConstructorInitializer || InFieldInitializer) + { + Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, node); + } - localSymbol.SetType(declType); + localSymbol.SetType(declType); - // Check for variable declaration errors. - hasErrors |= localSymbol.ScopeBinder.ValidateDeclarationNameConflictsInScope(localSymbol, diagnostics); + // Check for variable declaration errors. + hasErrors |= localSymbol.ScopeBinder.ValidateDeclarationNameConflictsInScope(localSymbol, diagnostics); - if (this.ContainingMemberOrLambda.Kind == SymbolKind.Method - && ((MethodSymbol)this.ContainingMemberOrLambda).IsAsync - && declType.IsRestrictedType() - && !hasErrors) + if (this.ContainingMemberOrLambda.Kind == SymbolKind.Method + && ((MethodSymbol)this.ContainingMemberOrLambda).IsAsync + && declType.IsRestrictedType() + && !hasErrors) + { + Error(diagnostics, ErrorCode.ERR_BadSpecialByRefLocal, typeSyntax, declType); + hasErrors = true; + } + + return new BoundDeclarationPattern(node, localSymbol, boundDeclType, isVar, hasErrors); + } + else { - Error(diagnostics, ErrorCode.ERR_BadSpecialByRefLocal, typeSyntax, declType); - hasErrors = true; + // We should have the right binder in the chain for a script or interactive, so we use the field for the pattern. + Debug.Assert(node.SyntaxTree.Options.Kind != SourceCodeKind.Regular); + GlobalExpressionVariable expressionVariableField = LookupDeclaredField(node); + DiagnosticBag tempDiagnostics = DiagnosticBag.GetInstance(); + expressionVariableField.SetType(declType, tempDiagnostics); + tempDiagnostics.Free(); + BoundExpression receiver = SynthesizeReceiver(node, expressionVariableField, diagnostics); + var variableAccess = new BoundFieldAccess(node, receiver, expressionVariableField, null, hasErrors); + return new BoundDeclarationPattern(node, expressionVariableField, variableAccess, boundDeclType, isVar, hasErrors); } - - return new BoundDeclarationPattern(node, localSymbol, boundDeclType, isVar, hasErrors); } + } } diff --git a/src/Compilers/CSharp/Portable/Binder/ExpressionVariableFinder.cs b/src/Compilers/CSharp/Portable/Binder/ExpressionVariableFinder.cs index b3186841dcb7e..31f531db1cb85 100644 --- a/src/Compilers/CSharp/Portable/Binder/ExpressionVariableFinder.cs +++ b/src/Compilers/CSharp/Portable/Binder/ExpressionVariableFinder.cs @@ -183,7 +183,12 @@ public override void VisitSwitchStatement(SwitchStatementSyntax node) public override void VisitDeclarationPattern(DeclarationPatternSyntax node) { - _localsBuilder.Add(MakePatternVariable(node, _nodeToBind)); + var variable = MakePatternVariable(node, _nodeToBind); + if ((object)variable != null) + { + _localsBuilder.Add(variable); + } + base.VisitDeclarationPattern(node); } @@ -307,6 +312,15 @@ internal static void FindExpressionVariables( protected override LocalSymbol MakePatternVariable(DeclarationPatternSyntax node, SyntaxNode nodeToBind) { + NamedTypeSymbol container = _scopeBinder.ContainingType; + + if ((object)container != null && container.IsScriptClass && + (object)_scopeBinder.LookupDeclaredField(node) != null) + { + // This is a field declaration + return null; + } + return SourceLocalSymbol.MakeLocalSymbolWithEnclosingContext( _scopeBinder.ContainingMemberOrLambda, scopeBinder: _scopeBinder, @@ -382,13 +396,19 @@ internal static void FindExpressionVariables( protected override Symbol MakePatternVariable(DeclarationPatternSyntax node, SyntaxNode nodeToBind) { - throw ExceptionUtilities.Unreachable; + return GlobalExpressionVariable.Create( + _containingType, _modifiers, node.Type, + node.Identifier.ValueText, node, node.Identifier.GetLocation(), + _containingFieldOpt, nodeToBind); } protected override Symbol MakeOutVariable(DeclarationExpressionSyntax node, BaseArgumentListSyntax argumentListSyntax, SyntaxNode nodeToBind) { - return SourceMemberFieldSymbolFromDesignation.Create(_containingType, node.VariableDesignation(), node.Type(), - _modifiers, _containingFieldOpt, nodeToBind); + var designation = node.VariableDesignation(); + return GlobalExpressionVariable.Create( + _containingType, _modifiers, node.Type(), + designation.Identifier.ValueText, designation, designation.Identifier.GetLocation(), + _containingFieldOpt, nodeToBind); } #region pool diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs index 381dfbd83e126..fa72b802419c3 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/OverloadResolution.cs @@ -1229,26 +1229,16 @@ private BetterResult BetterFunctionMember( bool okToDowngradeToNeither; BetterResult r; - if (argumentKind == BoundKind.OutVariablePendingInference || argumentKind == BoundKind.OutDeconstructVarPendingInference) - { - // If argument is an out variable that needs type inference, - // neither candidate is better in this argument. - r = BetterResult.Neither; - okToDowngradeToNeither = false; - } - else - { - r = BetterConversionFromExpression(arguments[i], - type1, - m1.Result.ConversionForArg(i), - refKind1, - type2, - m2.Result.ConversionForArg(i), - refKind2, - considerRefKinds, - ref useSiteDiagnostics, - out okToDowngradeToNeither); - } + r = BetterConversionFromExpression(arguments[i], + type1, + m1.Result.ConversionForArg(i), + refKind1, + type2, + m2.Result.ConversionForArg(i), + refKind2, + considerRefKinds, + ref useSiteDiagnostics, + out okToDowngradeToNeither); var type1Normalized = type1.NormalizeTaskTypes(Compilation); var type2Normalized = type2.NormalizeTaskTypes(Compilation); @@ -1802,6 +1792,14 @@ private BetterResult BetterConversionFromExpression(BoundExpression node, TypeSy var lambdaOpt = node as UnboundLambda; + var nodeKind = node.Kind; + if (nodeKind == BoundKind.OutVariablePendingInference || nodeKind == BoundKind.OutDeconstructVarPendingInference) + { + // Neither conversion from expression is better when the argument is an implicitly-typed out variable declaration. + okToDowngradeToNeither = false; + return BetterResult.Neither; + } + // Given an implicit conversion C1 that converts from an expression E to a type T1, // and an implicit conversion C2 that converts from an expression E to a type T2, // C1 is a better conversion than C2 if E does not exactly match T2 and one of the following holds: diff --git a/src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml b/src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml index dea39f4afa270..1663eccadc149 100644 --- a/src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml +++ b/src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml @@ -1562,12 +1562,16 @@ - + + + from the Symbol to facilitate lowerings in which the variable is no longer a simple + variable access (e.g. in the expression evaluator). It is expected to be logically side-effect + free. The necessity of this member is a consequence of a design issue documented in + https://github.com/dotnet/roslyn/issues/13960 . When that is fixed this field can be + removed. --> diff --git a/src/Compilers/CSharp/Portable/BoundTree/DecisionTree.cs b/src/Compilers/CSharp/Portable/BoundTree/DecisionTree.cs index e7d386c756114..72969c12bf2a9 100644 --- a/src/Compilers/CSharp/Portable/BoundTree/DecisionTree.cs +++ b/src/Compilers/CSharp/Portable/BoundTree/DecisionTree.cs @@ -210,7 +210,7 @@ internal DecisionTree ComputeDecisionTree() if (defaultLabel != null) { - Add(result, (e, t) => new DecisionTree.Guarded(_switchStatement.Expression, _switchStatement.Expression.Type, default(ImmutableArray>), defaultSection, null, defaultLabel)); + Add(result, (e, t) => new DecisionTree.Guarded(_switchStatement.Expression, _switchStatement.Expression.Type, default(ImmutableArray>), defaultSection, null, defaultLabel)); } return result; @@ -230,13 +230,13 @@ private void AddToDecisionTree(DecisionTree decisionTree, BoundPatternSwitchLabe case BoundKind.ConstantPattern: { var constantPattern = (BoundConstantPattern)pattern; - AddByValue(decisionTree, constantPattern.Value, (e, t) => new DecisionTree.Guarded(e, t, default(ImmutableArray>), _section, guard, label)); + AddByValue(decisionTree, constantPattern.Value, (e, t) => new DecisionTree.Guarded(e, t, default(ImmutableArray>), _section, guard, label)); break; } case BoundKind.DeclarationPattern: { var declarationPattern = (BoundDeclarationPattern)pattern; - DecisionMaker maker = (e, t) => new DecisionTree.Guarded(e, t, ImmutableArray.Create(new KeyValuePair(e, declarationPattern.LocalSymbol)), _section, guard, label); + DecisionMaker maker = (e, t) => new DecisionTree.Guarded(e, t, ImmutableArray.Create(new KeyValuePair(e, declarationPattern.VariableAccess)), _section, guard, label); if (declarationPattern.IsVar) { Add(decisionTree, maker); @@ -1098,7 +1098,7 @@ public class Guarded : DecisionTree { // A sequence of bindings to be assigned before evaluation of the guard or jump to the label. // Each one contains the source of the assignment and the destination of the assignment, in that order. - public readonly ImmutableArray> Bindings; + public readonly ImmutableArray> Bindings; public readonly BoundPatternSwitchSection Section; public readonly BoundExpression Guard; public readonly BoundPatternSwitchLabel Label; @@ -1107,7 +1107,7 @@ public class Guarded : DecisionTree public Guarded( BoundExpression expression, TypeSymbol type, - ImmutableArray> bindings, + ImmutableArray> bindings, BoundPatternSwitchSection section, BoundExpression guard, BoundPatternSwitchLabel label) diff --git a/src/Compilers/CSharp/Portable/BoundTree/DeconstructionVariablePendingInference.cs b/src/Compilers/CSharp/Portable/BoundTree/DeconstructionVariablePendingInference.cs index 5b8d27aa9321a..4a39aec269b91 100644 --- a/src/Compilers/CSharp/Portable/BoundTree/DeconstructionVariablePendingInference.cs +++ b/src/Compilers/CSharp/Portable/BoundTree/DeconstructionVariablePendingInference.cs @@ -33,7 +33,7 @@ public BoundExpression SetInferredType(TypeSymbol type, Binder binderOpt, Diagno return new BoundLocal(this.Syntax, local, constantValueOpt: null, type: type, hasErrors: this.HasErrors || inferenceFailed); case SymbolKind.Field: - var field = (SourceMemberFieldSymbolFromDesignation)this.VariableSymbol; + var field = (GlobalExpressionVariable)this.VariableSymbol; var inferenceDiagnostics = DiagnosticBag.GetInstance(); if (inferenceFailed) { diff --git a/src/Compilers/CSharp/Portable/BoundTree/OutVariablePendingInference.cs b/src/Compilers/CSharp/Portable/BoundTree/OutVariablePendingInference.cs index 91a8f2e8ad47a..8042224b2c512 100644 --- a/src/Compilers/CSharp/Portable/BoundTree/OutVariablePendingInference.cs +++ b/src/Compilers/CSharp/Portable/BoundTree/OutVariablePendingInference.cs @@ -52,7 +52,7 @@ private BoundExpression SetInferredType(TypeSymbol type, Binder binderOpt, Diagn return new BoundLocal(this.Syntax, localSymbol, constantValueOpt: null, type: type, hasErrors: this.HasErrors || inferenceFailed); case SymbolKind.Field: - var fieldSymbol = (SourceMemberFieldSymbolFromDesignation)this.VariableSymbol; + var fieldSymbol = (GlobalExpressionVariable)this.VariableSymbol; var inferenceDiagnostics = DiagnosticBag.GetInstance(); if (inferenceFailed) diff --git a/src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj b/src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj index 808f5f1444d1a..de8183202d04e 100644 --- a/src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj +++ b/src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj @@ -523,7 +523,7 @@ - + diff --git a/src/Compilers/CSharp/Portable/CSharpExtensions.cs b/src/Compilers/CSharp/Portable/CSharpExtensions.cs index 039b46209c35a..1651470eefa6d 100644 --- a/src/Compilers/CSharp/Portable/CSharpExtensions.cs +++ b/src/Compilers/CSharp/Portable/CSharpExtensions.cs @@ -1236,7 +1236,7 @@ public static Conversion ClassifyConversion(this SemanticModel semanticModel, in /// /// Given a declaration pattern syntax, get the corresponding symbol. /// - public static ILocalSymbol GetDeclaredSymbol(this SemanticModel semanticModel, DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) + public static ISymbol GetDeclaredSymbol(this SemanticModel semanticModel, DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) { var csmodel = semanticModel as CSharpSemanticModel; return csmodel?.GetDeclaredSymbol(declarationSyntax, cancellationToken); diff --git a/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs b/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs index 7ff06b0b1767a..d63393cbf0821 100644 --- a/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs +++ b/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs @@ -2710,7 +2710,7 @@ internal Conversion ClassifyConversionForCast(int position, ExpressionSyntax exp /// The syntax node that declares a variable. /// The cancellation token. /// The symbol that was declared. - public abstract ILocalSymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)); + public abstract ISymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)); /// /// Given a labeled statement syntax, get the corresponding label symbol. diff --git a/src/Compilers/CSharp/Portable/Compilation/MemberSemanticModel.cs b/src/Compilers/CSharp/Portable/Compilation/MemberSemanticModel.cs index 0f944c092df29..308be1fb19d1a 100644 --- a/src/Compilers/CSharp/Portable/Compilation/MemberSemanticModel.cs +++ b/src/Compilers/CSharp/Portable/Compilation/MemberSemanticModel.cs @@ -591,7 +591,7 @@ private LocalSymbol GetDeclaredLocal(CSharpSyntaxNode declarationSyntax, SyntaxT return null; } - public override ILocalSymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) + public override ISymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) { CheckSyntaxNode(declarationSyntax); return GetDeclaredLocal(declarationSyntax, declarationSyntax.Identifier); diff --git a/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs b/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs index 215072664ab7f..794b679b860f7 100644 --- a/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs +++ b/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs @@ -1675,10 +1675,11 @@ private Symbol GetDeclaredMember(NamespaceOrTypeSymbol container, TextSpan decla /// The syntax node that declares a variable. /// The cancellation token. /// The symbol that was declared. - public override ILocalSymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) + public override ISymbol GetDeclaredSymbol(DeclarationPatternSyntax declarationSyntax, CancellationToken cancellationToken = default(CancellationToken)) { var memberModel = this.GetMemberModel(declarationSyntax); - return memberModel == null ? null : memberModel.GetDeclaredSymbol(declarationSyntax, cancellationToken); + return memberModel?.GetDeclaredSymbol(declarationSyntax, cancellationToken) ?? + GetEnclosingBinder(declarationSyntax.Position)?.LookupDeclaredField(declarationSyntax); } /// diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowPass.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowPass.cs index 890820fbd07b9..735723898c49a 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowPass.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowPass.cs @@ -1157,11 +1157,17 @@ protected virtual void AssignImpl(BoundNode node, BoundExpression value, RefKind { case BoundKind.DeclarationPattern: { - var local = (BoundDeclarationPattern)node; - LocalSymbol symbol = local.LocalSymbol; - int slot = GetOrCreateSlot(symbol); - SetSlotState(slot, assigned: written || !this.State.Reachable); - if (written) NoteWrite(symbol, value, read); + var pattern = (BoundDeclarationPattern)node; + var symbol = pattern.Variable as LocalSymbol; + if ((object)symbol != null) + { + // we do not track definite assignment for pattern variables when they are + // promoted to fields for top-level code in scripts and interactive + int slot = GetOrCreateSlot(symbol); + SetSlotState(slot, assigned: written || !this.State.Reachable); + } + + if (written) NoteWrite(pattern.VariableAccess, value, read); break; } @@ -1556,16 +1562,13 @@ protected override void VisitGuardedPattern(DecisionTree.Guarded guarded) private void CreateSlots(BoundPattern pattern) { - switch (pattern.Kind) + if (pattern.Kind == BoundKind.DeclarationPattern) { - case BoundKind.DeclarationPattern: - { - int slot = GetOrCreateSlot(((BoundDeclarationPattern)pattern).LocalSymbol); - break; - } - case BoundKind.ConstantPattern: - default: - break; + var local = ((BoundDeclarationPattern)pattern).Variable as LocalSymbol; + if ((object)local != null) + { + int slot = GetOrCreateSlot(local); + } } } diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowsOutWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowsOutWalker.cs index 60a9f0a8b8971..1f52183ff85cb 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowsOutWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/DataFlowsOutWalker.cs @@ -106,7 +106,7 @@ private Symbol GetNodeSymbol(BoundNode node) { case BoundKind.DeclarationPattern: { - return ((BoundDeclarationPattern)node).LocalSymbol; + return ((BoundDeclarationPattern)node).Variable as LocalSymbol; } case BoundKind.FieldAccess: diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs index 71cf5ec906992..9b4e6bcf0fad5 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs @@ -77,7 +77,12 @@ private void NoteDeclaredPatternVariables(BoundPattern pattern) if (IsInside && pattern.Kind == BoundKind.DeclarationPattern) { var decl = (BoundDeclarationPattern)pattern; - _variablesDeclared.Add(decl.LocalSymbol); + if (decl.Variable.Kind == SymbolKind.Local) + { + // Because this API only returns local symbols and parameters, + // we exclude pattern variables that have become fields in scripts. + _variablesDeclared.Add(decl.Variable); + } } } diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_PatternSwitchStatement.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_PatternSwitchStatement.cs index f7982e3ec5cd7..5e91640667b5b 100644 --- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_PatternSwitchStatement.cs +++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_PatternSwitchStatement.cs @@ -309,18 +309,19 @@ private void LowerDecisionTree(DecisionTree.Guarded guarded) } } - private void AddBindings(ArrayBuilder sectionBuilder, ImmutableArray> bindings) + private void AddBindings(ArrayBuilder sectionBuilder, ImmutableArray> bindings) { - if (bindings.IsDefaultOrEmpty) + if (!bindings.IsDefaultOrEmpty) { - return; - } - - foreach (var kv in bindings) - { - var source = kv.Key; - var dest = kv.Value; - sectionBuilder.Add(_factory.Assignment(_factory.Local(dest), source)); + foreach (var kv in bindings) + { + var source = kv.Key; + var dest = kv.Value; + var rewriter = this.LocalRewriter; + sectionBuilder.Add(_factory.ExpressionStatement( + rewriter.MakeStaticAssignmentOperator( + _factory.Syntax, rewriter.VisitExpression(dest), rewriter.VisitExpression(source), RefKind.None, dest.Type, false))); + } } } diff --git a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs index b15431d27b189..c540b749f2d19 100644 --- a/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_Patterns.cs @@ -52,12 +52,12 @@ private BoundExpression LowerConstantPattern(BoundConstantPattern pattern, Bound private BoundExpression LowerDeclarationPattern(BoundDeclarationPattern pattern, BoundExpression input) { - Debug.Assert(pattern.IsVar || pattern.LocalSymbol.Type == pattern.DeclaredType.Type); + Debug.Assert(pattern.Variable.GetTypeOrReturnType() == pattern.DeclaredType.Type); var variableAccess = VisitExpression(pattern.VariableAccess); if (pattern.IsVar) { - Debug.Assert(input.Type == pattern.LocalSymbol.Type); + Debug.Assert(input.Type == pattern.Variable.GetTypeOrReturnType()); var assignment = _factory.AssignmentExpression(variableAccess, input); var result = _factory.Literal(true); return _factory.MakeSequence(assignment, result); diff --git a/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt b/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt index 4a0c62e41bd67..67c432ec88828 100644 --- a/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt +++ b/src/Compilers/CSharp/Portable/PublicAPI.Unshipped.txt @@ -293,7 +293,7 @@ override Microsoft.CodeAnalysis.CSharp.Syntax.VariableComponentAssignmentSyntax. override Microsoft.CodeAnalysis.CSharp.Syntax.VariableComponentAssignmentSyntax.Accept(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor visitor) -> TResult override Microsoft.CodeAnalysis.CSharp.Syntax.WhenClauseSyntax.Accept(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor visitor) -> void override Microsoft.CodeAnalysis.CSharp.Syntax.WhenClauseSyntax.Accept(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor visitor) -> TResult -static Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetDeclaredSymbol(this Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.CSharp.Syntax.DeclarationPatternSyntax declarationSyntax, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.ILocalSymbol +static Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetDeclaredSymbol(this Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.CSharp.Syntax.DeclarationPatternSyntax declarationSyntax, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.ISymbol static Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetDeclaredSymbol(this Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.CSharp.Syntax.SingleVariableDesignationSyntax designationSyntax, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> Microsoft.CodeAnalysis.ISymbol static Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetForEachStatementInfo(this Microsoft.CodeAnalysis.SemanticModel semanticModel, Microsoft.CodeAnalysis.CSharp.Syntax.CommonForEachStatementSyntax forEachStatement) -> Microsoft.CodeAnalysis.CSharp.ForEachStatementInfo static Microsoft.CodeAnalysis.CSharp.SyntaxFactory.AccessorDeclaration(Microsoft.CodeAnalysis.CSharp.SyntaxKind kind) -> Microsoft.CodeAnalysis.CSharp.Syntax.AccessorDeclarationSyntax diff --git a/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs index 596a33e64c995..322c3f3cfcc7f 100644 --- a/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs @@ -453,48 +453,6 @@ internal static ImmutableArray GetExplicitInterfaceImplementations(this } } - internal static TypeSymbol GetTypeOrReturnType(this Symbol member) - { - RefKind refKind; - TypeSymbol returnType; - ImmutableArray returnTypeCustomModifiers; - GetTypeOrReturnType(member, out refKind, out returnType, out returnTypeCustomModifiers); - return returnType; - } - - internal static void GetTypeOrReturnType(this Symbol member, out RefKind refKind, out TypeSymbol returnType, out ImmutableArray returnTypeCustomModifiers) - { - switch (member.Kind) - { - case SymbolKind.Field: - FieldSymbol field = (FieldSymbol)member; - refKind = RefKind.None; - returnType = field.Type; - returnTypeCustomModifiers = field.CustomModifiers; - break; - case SymbolKind.Method: - MethodSymbol method = (MethodSymbol)member; - refKind = method.RefKind; - returnType = method.ReturnType; - returnTypeCustomModifiers = method.ReturnTypeCustomModifiers; - break; - case SymbolKind.Property: - PropertySymbol property = (PropertySymbol)member; - refKind = property.RefKind; - returnType = property.Type; - returnTypeCustomModifiers = property.TypeCustomModifiers; - break; - case SymbolKind.Event: - EventSymbol @event = (EventSymbol)member; - refKind = RefKind.None; - returnType = @event.Type; - returnTypeCustomModifiers = ImmutableArray.Empty; - break; - default: - throw ExceptionUtilities.UnexpectedValue(member.Kind); - } - } - internal static Symbol GetOverriddenMember(this Symbol member) { switch (member.Kind) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbolFromDesignation.cs b/src/Compilers/CSharp/Portable/Symbols/Source/GlobalExpressionVariable.cs similarity index 75% rename from src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbolFromDesignation.cs rename to src/Compilers/CSharp/Portable/Symbols/Source/GlobalExpressionVariable.cs index e33de8b313764..b7b4b67711a3d 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbolFromDesignation.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/GlobalExpressionVariable.cs @@ -10,66 +10,54 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols { /// - /// Represents expression variables declared in a global statement. + /// Represents expression and deconstruction variables declared in a global statement. /// - internal class SourceMemberFieldSymbolFromDesignation : SourceMemberFieldSymbol + internal class GlobalExpressionVariable : SourceMemberFieldSymbol { private TypeSymbol _lazyType; private SyntaxReference _typeSyntax; - internal SourceMemberFieldSymbolFromDesignation( + internal GlobalExpressionVariable( SourceMemberContainerTypeSymbol containingType, - SingleVariableDesignationSyntax designation, + DeclarationModifiers modifiers, TypeSyntax typeSyntax, - DeclarationModifiers modifiers) - : base(containingType, modifiers, designation.Identifier.ValueText, designation.GetReference(), designation.Identifier.GetLocation()) + string name, + SyntaxReference syntax, + Location location) + : base(containingType, modifiers, name, syntax, location) { Debug.Assert(DeclaredAccessibility == Accessibility.Private); _typeSyntax = typeSyntax.GetReference(); } - internal static SourceMemberFieldSymbolFromDesignation Create( + internal static GlobalExpressionVariable Create( SourceMemberContainerTypeSymbol containingType, - SingleVariableDesignationSyntax designation, - TypeSyntax typeSyntax, DeclarationModifiers modifiers, + TypeSyntax typeSyntax, + string name, + SyntaxNode syntax, + Location location, FieldSymbol containingFieldOpt, SyntaxNode nodeToBind) { Debug.Assert(nodeToBind.Kind() == SyntaxKind.VariableDeclarator || nodeToBind is ExpressionSyntax || nodeToBind.Kind() == SyntaxKind.VariableComponentAssignment); - + var syntaxReference = syntax.GetReference(); return typeSyntax.IsVar - ? new SourceMemberFieldSymbolFromDesignationWithEnclosingContext(containingType, designation, typeSyntax, modifiers, containingFieldOpt, nodeToBind) - : new SourceMemberFieldSymbolFromDesignation(containingType, designation, typeSyntax, modifiers); - } - - protected override SyntaxList AttributeDeclarationSyntaxList - { - get - { - return default(SyntaxList); - } + ? new InferrableGlobalExpressionVariable(containingType, modifiers, typeSyntax, name, syntaxReference, location, containingFieldOpt, nodeToBind) + : new GlobalExpressionVariable(containingType, modifiers, typeSyntax, name, syntaxReference, location); } - public SingleVariableDesignationSyntax VariableDesignation - { - get - { - return (SingleVariableDesignationSyntax)this.SyntaxNode; - } - } + protected override SyntaxList AttributeDeclarationSyntaxList => default(SyntaxList); protected override TypeSyntax TypeSyntax => (TypeSyntax)_typeSyntax.GetSyntax(); - - protected override SyntaxTokenList ModifiersTokenList - { - get - { - return default(SyntaxTokenList); - } - } + protected override SyntaxTokenList ModifiersTokenList => default(SyntaxTokenList); + public override bool HasInitializer => false; + protected override ConstantValue MakeConstantValue( + HashSet dependencies, + bool earlyDecodingWellKnownAttributes, + DiagnosticBag diagnostics) => null; internal override TypeSymbol GetFieldType(ConsList fieldsBeingBound) { @@ -80,7 +68,6 @@ internal override TypeSymbol GetFieldType(ConsList fieldsBeingBound return _lazyType; } - var designation = VariableDesignation; var typeSyntax = TypeSyntax; var compilation = this.DeclaringCompilation; @@ -152,32 +139,21 @@ protected virtual void InferFieldType(ConsList fieldsBeingBound, Bi throw ExceptionUtilities.Unreachable; } - protected override ConstantValue MakeConstantValue(HashSet dependencies, bool earlyDecodingWellKnownAttributes, DiagnosticBag diagnostics) - { - return null; - } - - public override bool HasInitializer - { - get - { - return false; - } - } - - private class SourceMemberFieldSymbolFromDesignationWithEnclosingContext : SourceMemberFieldSymbolFromDesignation + private class InferrableGlobalExpressionVariable : GlobalExpressionVariable { private readonly FieldSymbol _containingFieldOpt; private readonly SyntaxReference _nodeToBind; - internal SourceMemberFieldSymbolFromDesignationWithEnclosingContext( + internal InferrableGlobalExpressionVariable( SourceMemberContainerTypeSymbol containingType, - SingleVariableDesignationSyntax designation, - TypeSyntax typeSyntax, DeclarationModifiers modifiers, + TypeSyntax typeSyntax, + string name, + SyntaxReference syntax, + Location location, FieldSymbol containingFieldOpt, SyntaxNode nodeToBind) - : base(containingType, designation, typeSyntax, modifiers) + : base(containingType, modifiers, typeSyntax, name, syntax, location) { Debug.Assert(nodeToBind.Kind() == SyntaxKind.VariableDeclarator || nodeToBind is ExpressionSyntax @@ -227,4 +203,4 @@ protected override void InferFieldType(ConsList fieldsBeingBound, B } } } -} \ No newline at end of file +} diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFixedFieldSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFixedFieldSymbol.cs index 90e875c2915b6..731ac077550f5 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceFixedFieldSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceFixedFieldSymbol.cs @@ -55,8 +55,7 @@ public sealed override int FixedSize DiagnosticBag diagnostics = DiagnosticBag.GetInstance(); int size = 0; - VariableDeclaratorSyntax declarator = this.VariableDeclaratorNode; - + VariableDeclaratorSyntax declarator = VariableDeclaratorNode; if (declarator.ArgumentList == null) { // Diagnostic reported by parser. diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs index d6b54cb98dfb8..6ffdc1fed2ccd 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs @@ -3245,11 +3245,17 @@ private void CollectFieldsFromGlobalDeconstruction(ArrayBuilder builder, switch (designation.Kind()) { case SyntaxKind.SingleVariableDesignation: - var field = SourceMemberFieldSymbolFromDesignation.Create(this, (SingleVariableDesignationSyntax)designation, - type, DeclarationModifiers.Private, null, assignment); - + var single = (SingleVariableDesignationSyntax)designation; + var field = GlobalExpressionVariable.Create( + containingType: this, + modifiers: DeclarationModifiers.Private, + typeSyntax: type, + name: single.Identifier.ValueText, + syntax: designation, + location: designation.Location, + containingFieldOpt: null, + nodeToBind: assignment); builder.Add(field); - break; case SyntaxKind.ParenthesizedVariableDesignation: diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbol.cs index bec22be20c13d..2ccd95b0ed4a7 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberFieldSymbol.cs @@ -19,7 +19,7 @@ internal abstract class SourceMemberFieldSymbol : SourceFieldSymbolWithSyntaxRef internal SourceMemberFieldSymbol( SourceMemberContainerTypeSymbol containingType, DeclarationModifiers modifiers, - string name, + string name, SyntaxReference syntax, Location location) : base(containingType, name, syntax, location) @@ -323,7 +323,7 @@ public sealed override bool HasInitializer get { return _hasInitializer; } } - public VariableDeclaratorSyntax VariableDeclaratorNode + protected VariableDeclaratorSyntax VariableDeclaratorNode { get { diff --git a/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs index a500096448f59..bbb025a51eaaf 100644 --- a/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs @@ -331,5 +331,53 @@ internal static ImmutableArray ToTypes(this ImmutableArray !a.CustomModifiers.IsDefaultOrEmpty); return typesWithModifiers.SelectAsArray(a => a.Type); } + + internal static TypeSymbol GetTypeOrReturnType(this Symbol symbol) + { + RefKind refKind; + TypeSymbol returnType; + ImmutableArray returnTypeCustomModifiers; + GetTypeOrReturnType(symbol, out refKind, out returnType, out returnTypeCustomModifiers); + return returnType; + } + + internal static void GetTypeOrReturnType(this Symbol symbol, out RefKind refKind, out TypeSymbol returnType, out ImmutableArray returnTypeCustomModifiers) + { + switch (symbol.Kind) + { + case SymbolKind.Field: + FieldSymbol field = (FieldSymbol)symbol; + refKind = RefKind.None; + returnType = field.Type; + returnTypeCustomModifiers = field.CustomModifiers; + break; + case SymbolKind.Method: + MethodSymbol method = (MethodSymbol)symbol; + refKind = method.RefKind; + returnType = method.ReturnType; + returnTypeCustomModifiers = method.ReturnTypeCustomModifiers; + break; + case SymbolKind.Property: + PropertySymbol property = (PropertySymbol)symbol; + refKind = property.RefKind; + returnType = property.Type; + returnTypeCustomModifiers = property.TypeCustomModifiers; + break; + case SymbolKind.Event: + EventSymbol @event = (EventSymbol)symbol; + refKind = RefKind.None; + returnType = @event.Type; + returnTypeCustomModifiers = ImmutableArray.Empty; + break; + case SymbolKind.Local: + LocalSymbol local = (LocalSymbol)symbol; + refKind = local.RefKind; + returnType = local.Type; + returnTypeCustomModifiers = ImmutableArray.Empty; + break; + default: + throw ExceptionUtilities.UnexpectedValue(symbol.Kind); + } + } } } diff --git a/src/Compilers/CSharp/Test/Semantic/CSharpCompilerSemanticTest.csproj b/src/Compilers/CSharp/Test/Semantic/CSharpCompilerSemanticTest.csproj index 747c5ccadffc6..4cc1cab81e4b5 100644 --- a/src/Compilers/CSharp/Test/Semantic/CSharpCompilerSemanticTest.csproj +++ b/src/Compilers/CSharp/Test/Semantic/CSharpCompilerSemanticTest.csproj @@ -72,6 +72,7 @@ + @@ -98,7 +99,9 @@ + + @@ -155,4 +158,4 @@ - + \ No newline at end of file diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs new file mode 100644 index 0000000000000..ae10e9d4d6583 --- /dev/null +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs @@ -0,0 +1,276 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis.CSharp.Symbols; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Microsoft.CodeAnalysis.Test.Utilities; +using Roslyn.Test.Utilities; +using Xunit; +using Roslyn.Utilities; + +namespace Microsoft.CodeAnalysis.CSharp.UnitTests +{ + public class PatternMatchingTestBase : CSharpTestBase + { + #region helpers + protected IEnumerable GetPatternDeclarations(SyntaxTree tree, string v) + { + return GetPatternDeclarations(tree).Where(p => p.Identifier.ValueText == v); + } + + protected IEnumerable GetPatternDeclarations(SyntaxTree tree) + { + return tree.GetRoot().DescendantNodes().OfType(); + } + + protected static IEnumerable GetReferences(SyntaxTree tree, string name) + { + return tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == name); + } + + + protected static void VerifyModelForDeclarationPattern(SemanticModel model, DeclarationPatternSyntax decl, params IdentifierNameSyntax[] references) + { + VerifyModelForDeclarationPattern(model, decl, false, references); + } + + protected static void VerifyModelForDeclarationPattern( + SemanticModel model, + DeclarationPatternSyntax decl, + bool isShadowed, + params IdentifierNameSyntax[] references) + { + var symbol = model.GetDeclaredSymbol(decl); + Assert.Equal(decl.Identifier.ValueText, symbol.Name); + Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax()); + Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); + Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl)); + + if (isShadowed) + { + Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); + } + else + { + Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); + } + + Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText)); + + Assert.True(SyntaxFacts.IsInNamespaceOrTypeContext(decl.Type)); + Assert.True(SyntaxFacts.IsInTypeOnlyContext(decl.Type)); + + var type = ((LocalSymbol)symbol).Type; + if (!decl.Type.IsVar || !type.IsErrorType()) + { + Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol); + } + + foreach (var reference in references) + { + Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol); + Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single()); + Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText)); + } + } + + protected static void VerifyModelForDeclarationPatternDuplicateInSameScope(SemanticModel model, DeclarationPatternSyntax decl) + { + var symbol = model.GetDeclaredSymbol(decl); + Assert.Equal(decl.Identifier.ValueText, symbol.Name); + Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax()); + Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); + Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl)); + Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); + Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText)); + + var type = ((LocalSymbol)symbol).Type; + if (!decl.Type.IsVar || !type.IsErrorType()) + { + Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol); + } + } + + protected static void VerifyNotAPatternLocal(SemanticModel model, IdentifierNameSyntax reference) + { + var symbol = model.GetSymbolInfo(reference).Symbol; + + if (symbol.Kind == SymbolKind.Local) + { + Assert.NotEqual(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); + } + + Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Single()); + Assert.True(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText)); + } + + protected static void VerifyNotInScope(SemanticModel model, IdentifierNameSyntax reference) + { + Assert.Null(model.GetSymbolInfo(reference).Symbol); + Assert.False(model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Any()); + Assert.False(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText)); + } + + protected static void VerifyModelForDeclarationField( + SemanticModel model, + DeclarationPatternSyntax decl, + params IdentifierNameSyntax[] references) + { + VerifyModelForDeclarationField(model, decl, false, references); + } + + protected static void VerifyModelForDeclarationFieldDuplicate( + SemanticModel model, + DeclarationPatternSyntax decl, + params IdentifierNameSyntax[] references) + { + VerifyModelForDeclarationField(model, decl, true, references); + } + + protected static void VerifyModelForDeclarationField( + SemanticModel model, + DeclarationPatternSyntax decl, + bool duplicate, + params IdentifierNameSyntax[] references) + { + var symbol = model.GetDeclaredSymbol(decl); + Assert.Equal(decl.Identifier.ValueText, symbol.Name); + Assert.Equal(SymbolKind.Field, symbol.Kind); + Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax()); + Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl)); + + var symbols = model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText); + var names = model.LookupNames(decl.SpanStart); + + if (duplicate) + { + Assert.True(symbols.Count() > 1); + Assert.Contains(symbol, symbols); + } + else + { + Assert.Same(symbol, symbols.Single()); + } + + Assert.Contains(decl.Identifier.ValueText, names); + + var local = (FieldSymbol)symbol; + var typeSyntax = decl.Type; + + Assert.True(SyntaxFacts.IsInNamespaceOrTypeContext(typeSyntax)); + Assert.True(SyntaxFacts.IsInTypeOnlyContext(typeSyntax)); + + if (typeSyntax.IsVar && local.Type.IsErrorType()) + { + Assert.Null(model.GetSymbolInfo(typeSyntax).Symbol); + } + else + { + Assert.Equal(local.Type, model.GetSymbolInfo(typeSyntax).Symbol); + } + + var declarator = decl.Ancestors().OfType().FirstOrDefault(); + var inFieldDeclaratorArgumentlist = declarator != null && declarator.Parent.Parent.Kind() != SyntaxKind.LocalDeclarationStatement && + (declarator.ArgumentList?.Contains(decl)).GetValueOrDefault(); + + // this is a declaration site, not a use site. + Assert.Null(model.GetSymbolInfo(decl).Symbol); + Assert.Null(model.GetSymbolInfo(decl).Symbol); + + foreach (var reference in references) + { + var referenceInfo = model.GetSymbolInfo(reference); + symbols = model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText); + + if (duplicate) + { + Assert.Null(referenceInfo.Symbol); + Assert.Contains(symbol, referenceInfo.CandidateSymbols); + Assert.True(symbols.Count() > 1); + Assert.Contains(symbol, symbols); + } + else + { + Assert.Same(symbol, referenceInfo.Symbol); + Assert.Same(symbol, symbols.Single()); + Assert.Equal(local.Type, model.GetTypeInfo(reference).Type); + } + + Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText)); + } + + if (!inFieldDeclaratorArgumentlist) + { + var dataFlowParent = decl.FirstAncestorOrSelf(); + + if (model.IsSpeculativeSemanticModel) + { + Assert.Throws(() => model.AnalyzeDataFlow(dataFlowParent)); + } + else + { + var dataFlow = model.AnalyzeDataFlow(dataFlowParent); + + if (dataFlow.Succeeded) + { + Assert.False(dataFlow.VariablesDeclared.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.AlwaysAssigned.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.WrittenInside.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.DataFlowsIn.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.ReadInside.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.DataFlowsOut.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.ReadOutside.Contains(symbol, ReferenceEqualityComparer.Instance)); + Assert.False(dataFlow.WrittenOutside.Contains(symbol, ReferenceEqualityComparer.Instance)); + } + } + } + } + + protected static void AssertContainedInDeclaratorArguments(DeclarationPatternSyntax decl) + { + Assert.True(decl.Ancestors().OfType().First().ArgumentList.Contains(decl)); + } + + protected static void AssertContainedInDeclaratorArguments(params DeclarationPatternSyntax[] decls) + { + foreach (var decl in decls) + { + AssertContainedInDeclaratorArguments(decl); + } + } + + protected static void VerifyModelNotSupported( + SemanticModel model, + DeclarationPatternSyntax decl, + params IdentifierNameSyntax[] references) + { + Assert.Null(model.GetDeclaredSymbol(decl)); + var identifierText = decl.Identifier.ValueText; + Assert.False(model.LookupSymbols(decl.SpanStart, name: identifierText).Any()); + + Assert.False(model.LookupNames(decl.SpanStart).Contains(identifierText)); + Assert.Null(model.GetSymbolInfo(decl.Type).Symbol); + + Assert.Null(model.GetSymbolInfo(decl).Symbol); + Assert.Null(model.GetTypeInfo(decl).Type); + Assert.Null(model.GetDeclaredSymbol(decl)); + VerifyModelNotSupported(model, references); + } + + protected static void VerifyModelNotSupported(SemanticModel model, params IdentifierNameSyntax[] references) + { + foreach (var reference in references) + { + Assert.Null(model.GetSymbolInfo(reference).Symbol); + Assert.False(model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Any()); + Assert.DoesNotContain(reference.Identifier.ValueText, model.LookupNames(reference.SpanStart)); + Assert.True(((TypeSymbol)model.GetTypeInfo(reference).Type).IsErrorType()); + } + } + + #endregion helpers + } +} diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs index 4df0c52a44b72..24c1d2589334a 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests { [CompilerTrait(CompilerFeature.Patterns)] - public class PatternMatchingTests : CSharpTestBase + public class PatternMatchingTests : PatternMatchingTestBase { [Fact] public void DemoModes() @@ -665,7 +665,7 @@ public static void M1(ref int x) {} } [Fact] - public void ScopeOfPatternVariables_ExpressionStatement_01() + public void If_01() { var source = @" @@ -673,262 +673,199 @@ public class X { public static void Main() { + Test(1); + Test(2); } - void Dummy(params object[] x) {} - - void Test1() + public static void Test(int val) { - Dummy(true is var x1, x1); + if (Dummy(val == 1, val is var x1, x1)) { - Dummy(true is var x1, x1); + System.Console.WriteLine(""true""); + System.Console.WriteLine(x1); + } + else + { + System.Console.WriteLine(""false""); + System.Console.WriteLine(x1); } - Dummy(true is var x1, x1); - } - - void Test2() - { - Dummy(x2, true is var x2); - } - - void Test3(int x3) - { - Dummy(true is var x3, x3); - } - void Test4() - { - var x4 = 11; - Dummy(x4); - Dummy(true is var x4, x4); + System.Console.WriteLine(x1); } - void Test5() + static bool Dummy(bool x, object y, object z) { - Dummy(true is var x5, x5); - var x5 = 11; - Dummy(x5); + System.Console.WriteLine(z); + return x; } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"1 +true +1 +1 +2 +false +2 +2"); - //void Test6() - //{ - // let x6 = 11; - // Dummy(x6); - // Dummy(true is var x6, x6); - //} - - //void Test7() - //{ - // Dummy(true is var x7, x7); - // let x7 = 11; - // Dummy(x7); - //} + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); - void Test8() - { - Dummy(true is var x8, x8, false is var x8, x8); - } + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(4, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); + } - void Test9(bool y9) + [Fact] + public void If_02() + { + var source = +@" +public class X +{ + public static void Main() { - if (y9) - Dummy(true is var x9, x9); - } + bool f = true; - System.Action Test10(bool y10) - { - return () => - { - if (y10) - Dummy(true is var x10, x10); - }; - } + if (f) + if (Dummy(f, (f ? 1 : 2) is var x1, x1)) + ; - void Test11() - { - Dummy(x11); - Dummy(true is var x11, x11); + if (f) + { + if (Dummy(f, (f ? 3 : 4) is var x1, x1)) + ; + } } - void Test12() + static bool Dummy(bool x, object y, object z) { - Dummy(true is var x12, x12); - Dummy(x12); + System.Console.WriteLine(z); + return x; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (14,31): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 31), - // (16,27): error CS0128: A local variable named 'x1' is already defined in this scope - // Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 27), - // (21,15): error CS0841: Cannot use local variable 'x2' before it is declared - // Dummy(x2, true is var x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 15), - // (26,27): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x3, x3); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 27), - // (33,27): error CS0128: A local variable named 'x4' is already defined in this scope - // Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 27), - // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope - // var x5 = 11; - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), - // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), - // (59,48): error CS0128: A local variable named 'x8' is already defined in this scope - // Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 48), - // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared - // Dummy(x11); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15) - ); + CompileAndVerify(compilation, expectedOutput: +@"1 +3"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(2, x8Ref.Length); - for (int i = 0; i < x8Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").Single(); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); } [Fact] - public void ScopeOfPatternVariables_ExpressionStatement_02() + public void Lambda_01() { - var text = @" -public class Cls + var source = +@" +public class X { public static void Main() { - Test1(2 is var x1); - System.Console.WriteLine(x1); + System.Console.WriteLine(Test1()); } - static object Test1(bool x) + static bool Test1() { - return null; + System.Func l = () => 1 is int x1 && Dummy(x1); + return l(); } -}"; - var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); + static bool Dummy(int x) + { + System.Console.WriteLine(x); + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: @"1 +True"); } [Fact] - public void ScopeOfPatternVariables_ExpressionStatement_03() + public void Query_01() { - var text = @" -public class Cls + var source = +@" +using System.Linq; + +public class X { public static void Main() { - Test0(); + Test1(); } - static object Test0() + static void Test1() { - bool test = true; - - if (test) - Test2(1 is var x1, x1); - - if (test) - { - Test2(2 is var x1, x1); - } + var res = from x1 in new[] { 1 is var y1 && Print(y1) ? 1 : 0} + from x2 in new[] { 2 is var y2 && Print(y2) ? 1 : 0} + join x3 in new[] { 3 is var y3 && Print(y3) ? 1 : 0} + on 4 is var y4 && Print(y4) ? 1 : 0 + equals 5 is var y5 && Print(y5) ? 1 : 0 + where 6 is var y6 && Print(y6) + orderby 7 is var y7 && Print(y7), + 8 is var y8 && Print(y8) + group 9 is var y9 && Print(y9) + by 10 is var y10 && Print(y10) + into g + let x11 = 11 is var y11 && Print(y11) + select 12 is var y12 && Print(y12); - return null; + res.ToArray(); } - static object Test2(object x, object y) + static bool Print(object x) { - System.Console.Write(y); - return x; + System.Console.WriteLine(x); + return true; } -}"; - var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: "12").VerifyDiagnostics(); +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"1 +3 +5 +2 +4 +6 +7 +8 +10 +9 +11 +12 +"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + for (int i = 1; i < 13; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).Single(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); + VerifyModelForDeclarationPattern(model, yDecl, yRef); + } } [Fact] - public void ScopeOfPatternVariables_ExpressionStatement_04() + public void ExpressionBodiedFunctions_01() { var source = @" @@ -936,38 +873,25 @@ public class X { public static void Main() { + System.Console.WriteLine(Test1()); } - void Dummy(params object[] x) {} + static bool Test1() => 1 is int x1 && Dummy(x1); - void Test1() + static bool Dummy(int x) { - if (true) - Dummy(true is var x1); - - x1++; + System.Console.WriteLine(x); + return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (15,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); + CompileAndVerify(compilation, expectedOutput: @"1 +True"); } [Fact] - public void ScopeOfPatternVariables_ExpressionStatement_05() + public void ExpressionBodiedProperties_01() { var source = @" @@ -975,41 +899,30 @@ public class X { public static void Main() { + System.Console.WriteLine(Test1); + System.Console.WriteLine(new X()[0]); } - void Dummy(params object[] x) {} + static bool Test1 => 2 is int x1 && Dummy(x1); + + bool this[object x] => 1 is int x1 && Dummy(x1); - void Test1() + static bool Dummy(int x) { - SpeculateHere(); + System.Console.WriteLine(x); + return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (ExpressionStatementSyntax)SyntaxFactory.ParseStatement(@" -Dummy(11 is var x1, x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + CompileAndVerify(compilation, expectedOutput: @"2 +True +1 +True"); } - [Fact, WorkItem(9258, "https://github.com/dotnet/roslyn/issues/9258")] - public void PatternVariableOrder() + [Fact] + public void FieldInitializers_01() { var source = @" @@ -1017,109 +930,33 @@ public class X { public static void Main() { + System.Console.WriteLine(Test1); } - static void Dummy(params object[] x) {} + static bool Test1 = 1 is int x1 && Dummy(x1); - void Test1(object o1, object o2) + static bool Dummy(int x) { - Dummy(o1 is int i && i < 10, - o2 is int @i && @i > 10); + System.Console.WriteLine(x); + return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - +#if ALLOW_IN_FIELD_INITIALIZER + CompileAndVerify(compilation, expectedOutput: @"1 +True"); +#else compilation.VerifyDiagnostics( - // (13,25): error CS0128: A local variable named 'i' is already defined in this scope - // o2 is int @i && @i > 10); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "@i").WithArguments("i").WithLocation(13, 25), - // (13,31): error CS0165: Use of unassigned local variable 'i' - // o2 is int @i && @i > 10); - Diagnostic(ErrorCode.ERR_UseDefViolation, "@i").WithArguments("i").WithLocation(13, 31) + // (9,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // static bool Test1 = 1 is int x1 && Dummy(x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(9, 30) ); +#endif } - private static void VerifyModelForDeclarationPattern(SemanticModel model, DeclarationPatternSyntax decl, params IdentifierNameSyntax[] references) - { - VerifyModelForDeclarationPattern(model, decl, false, references); - } - - private static void VerifyModelForDeclarationPattern(SemanticModel model, DeclarationPatternSyntax decl, bool isShadowed, params IdentifierNameSyntax[] references) - { - var symbol = model.GetDeclaredSymbol(decl); - Assert.Equal(decl.Identifier.ValueText, symbol.Name); - Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax()); - Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); - Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl)); - - if (isShadowed) - { - Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); - } - else - { - Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); - } - - Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText)); - - Assert.True(SyntaxFacts.IsInNamespaceOrTypeContext(decl.Type)); - Assert.True(SyntaxFacts.IsInTypeOnlyContext(decl.Type)); - - var type = ((LocalSymbol)symbol).Type; - if (!decl.Type.IsVar || !type.IsErrorType()) - { - Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol); - } - - foreach (var reference in references) - { - Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol); - Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single()); - Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText)); - } - } - - private static void VerifyModelForDeclarationPatternDuplicateInSameScope(SemanticModel model, DeclarationPatternSyntax decl) - { - var symbol = model.GetDeclaredSymbol(decl); - Assert.Equal(decl.Identifier.ValueText, symbol.Name); - Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax()); - Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); - Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl)); - Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single()); - Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText)); - - var type = ((LocalSymbol)symbol).Type; - if (!decl.Type.IsVar || !type.IsErrorType()) - { - Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol); - } - } - - private static void VerifyNotAPatternLocal(SemanticModel model, IdentifierNameSyntax reference) - { - var symbol = model.GetSymbolInfo(reference).Symbol; - - if (symbol.Kind == SymbolKind.Local) - { - Assert.NotEqual(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind); - } - - Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Single()); - Assert.True(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText)); - } - - private static void VerifyNotInScope(SemanticModel model, IdentifierNameSyntax reference) - { - Assert.Null(model.GetSymbolInfo(reference).Symbol); - Assert.False(model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Any()); - Assert.False(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText)); - } - - [Fact] - public void ScopeOfPatternVariables_ReturnStatement_01() + [Fact, WorkItem(10487, "https://github.com/dotnet/roslyn/issues/10487")] + public void FieldInitializers_03() { var source = @" @@ -1127,190 +964,77 @@ public class X { public static void Main() { + System.Console.WriteLine(Test1); + new X().M(); } - - object Dummy(params object[] x) { return null; } - - object Test1() - { - return Dummy(true is var x1, x1); - { - return Dummy(true is var x1, x1); - } - return Dummy(true is var x1, x1); - } - - object Test2() - { - return Dummy(x2, true is var x2); - } - - object Test3(int x3) - { - return Dummy(true is var x3, x3); - } - - object Test4() - { - var x4 = 11; - Dummy(x4); - return Dummy(true is var x4, x4); - } - - object Test5() + void M() { - return Dummy(true is var x5, x5); - var x5 = 11; - Dummy(x5); + System.Console.WriteLine(Test2); } - //object Test6() - //{ - // let x6 = 11; - // Dummy(x6); - // return Dummy(true is var x6, x6); - //} - - //object Test7() - //{ - // return Dummy(true is var x7, x7); - // let x7 = 11; - // Dummy(x7); - //} + static bool Test1 = 1 is int x1 && Dummy(() => x1); + bool Test2 = 2 is int x1 && Dummy(() => x1); - object Test8() + static bool Dummy(System.Func x) { - return Dummy(true is var x8, x8, false is var x8, x8); + System.Console.WriteLine(x()); + return true; } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); +#if ALLOW_IN_FIELD_INITIALIZER + CompileAndVerify(compilation, expectedOutput: @"1 +True +2 +True"); +#else + compilation.VerifyDiagnostics( + // (14,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // static bool Test1 = 1 is int x1 && Dummy(() => x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(14, 30), + // (15,23): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test2 = 2 is int x1 && Dummy(() => x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(15, 23) + ); +#endif + } - object Test9(bool y9) - { - if (y9) - return Dummy(true is var x9, x9); - return null; - } - System.Func Test10(bool y10) + [Fact] + public void PropertyInitializers_01() + { + var source = +@" +public class X +{ + public static void Main() { - return () => - { - if (y10) - return Dummy(true is var x10, x10); - return null;}; + System.Console.WriteLine(Test1); } - object Test11() - { - Dummy(x11); - return Dummy(true is var x11, x11); - } + static bool Test1 {get;} = 1 is int x1 && Dummy(x1); - object Test12() + static bool Dummy(int x) { - return Dummy(true is var x12, x12); - Dummy(x12); + System.Console.WriteLine(x); + return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - +#if ALLOW_IN_FIELD_INITIALIZER + CompileAndVerify(compilation, expectedOutput: @"1 +True"); +#else compilation.VerifyDiagnostics( - // (14,38): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // return Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 38), - // (16,34): error CS0128: A local variable named 'x1' is already defined in this scope - // return Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 34), - // (14,13): warning CS0162: Unreachable code detected - // return Dummy(true is var x1, x1); - Diagnostic(ErrorCode.WRN_UnreachableCode, "return").WithLocation(14, 13), - // (21,22): error CS0841: Cannot use local variable 'x2' before it is declared - // return Dummy(x2, true is var x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 22), - // (26,34): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // return Dummy(true is var x3, x3); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 34), - // (33,34): error CS0128: A local variable named 'x4' is already defined in this scope - // return Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 34), - // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope - // var x5 = 11; - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), - // (39,9): warning CS0162: Unreachable code detected - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(39, 9), - // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), - // (59,55): error CS0128: A local variable named 'x8' is already defined in this scope - // return Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 55), - // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared - // Dummy(x11); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), - // (86,9): warning CS0162: Unreachable code detected - // Dummy(x12); - Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(86, 9) + // (9,37): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // static bool Test1 {get;} = 1 is int x1 && Dummy(x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(9, 37) ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").Single(); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); +#endif } - [Fact] - public void ScopeOfPatternVariables_ReturnStatement_02() + public void ConstructorInitializers_01() { var source = @" @@ -1318,39 +1042,57 @@ public class X { public static void Main() { + var x = new D(); + } +} + +class D : C +{ + public D(object o) : base(2 is int x1 && Dummy(x1)) + { + System.Console.WriteLine(o); } - int Dummy(params object[] x) {return 0;} + public D() : this(1 is int x1 && Dummy(x1)) + { + } - int Test1(bool val) + static bool Dummy(int x) { - if (val) - return Dummy(true is var x1); + System.Console.WriteLine(x); + return true; + } +} - x1++; - return 0; +class C +{ + public C(object b) + { + System.Console.WriteLine(b); } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - +#if ALLOW_IN_CONSTRUCTOR_INITIALIZER + CompileAndVerify(compilation, expectedOutput: +@"1 +2 +True +True"); +#else compilation.VerifyDiagnostics( - // (15,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) + // (12,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // public D(object o) : base(2 is int x1 && Dummy(x1)) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(12, 36), + // (17,28): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // public D() : this(1 is int x1 && Dummy(x1)) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(17, 28) ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); +#endif } [Fact] - public void ScopeOfPatternVariables_ReturnStatement_03() + public void Switch_01() { var source = @" @@ -1358,41 +1100,48 @@ public class X { public static void Main() { + Test1(0); + Test1(1); } - void Dummy(params object[] x) {} + static bool Dummy1(bool val, params object[] x) {return val;} + static T Dummy2(T val, params object[] x) {return val;} - void Test1() + static void Test1(int val) { - SpeculateHere(); + switch (Dummy2(val, ""Test1 {0}"" is var x1)) + { + case 0 when Dummy1(true, ""case 0"" is var y1): + System.Console.WriteLine(x1, y1); + break; + case int z1: + System.Console.WriteLine(x1, z1); + break; + } + + System.Console.WriteLine(x1); } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"Test1 case 0 +Test1 {0} +Test1 1 +Test1 {0}"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var statement = (ReturnStatementSyntax)SyntaxFactory.ParseStatement(@" -return Dummy(11 is var x1, x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ScopeOfPatternVariables_ThrowStatement_01() + public void Switch_02() { var source = @" @@ -1400,187 +1149,44 @@ public class X { public static void Main() { - } + bool f = true; - System.Exception Dummy(params object[] x) { return null;} + if (f) + switch (Dummy(f, (f ? 1 : 2) is var x1, x1)) + {} - void Test1() - { - throw Dummy(true is var x1, x1); + if (f) { - throw Dummy(true is var x1, x1); + switch (Dummy(f, (f ? 3 : 4) is var x1, x1)) + {} } - throw Dummy(true is var x1, x1); - } - - void Test2() - { - throw Dummy(x2, true is var x2); - } - - void Test3(int x3) - { - throw Dummy(true is var x3, x3); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - throw Dummy(true is var x4, x4); - } - - void Test5() - { - throw Dummy(true is var x5, x5); - var x5 = 11; - Dummy(x5); - } - - //void Test6() - //{ - // let x6 = 11; - // Dummy(x6); - // throw Dummy(true is var x6, x6); - //} - - //void Test7() - //{ - // throw Dummy(true is var x7, x7); - // let x7 = 11; - // Dummy(x7); - //} - - void Test8() - { - throw Dummy(true is var x8, x8, false is var x8, x8); - } - - void Test9(bool y9) - { - if (y9) - throw Dummy(true is var x9, x9); - } - - System.Action Test10(bool y10) - { - return () => - { - if (y10) - throw Dummy(true is var x10, x10); - }; - } - - void Test11() - { - Dummy(x11); - throw Dummy(true is var x11, x11); } - void Test12() + static bool Dummy(bool x, object y, object z) { - throw Dummy(true is var x12, x12); - Dummy(x12); + System.Console.WriteLine(z); + return x; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (14,37): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // throw Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 37), - // (16,33): error CS0128: A local variable named 'x1' is already defined in this scope - // throw Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 33), - // (21,21): error CS0841: Cannot use local variable 'x2' before it is declared - // throw Dummy(x2, true is var x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 21), - // (26,33): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // throw Dummy(true is var x3, x3); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 33), - // (33,33): error CS0128: A local variable named 'x4' is already defined in this scope - // throw Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 33), - // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope - // var x5 = 11; - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), - // (39,9): warning CS0162: Unreachable code detected - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(39, 9), - // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), - // (59,54): error CS0128: A local variable named 'x8' is already defined in this scope - // throw Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 54), - // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared - // Dummy(x11); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), - // (86,9): warning CS0162: Unreachable code detected - // Dummy(x12); - Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(86, 9) - ); + CompileAndVerify(compilation, expectedOutput: +@"1 +3"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").Single(); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); } [Fact] - public void ScopeOfPatternVariables_ThrowStatement_02() + public void Using_01() { var source = @" @@ -1588,38 +1194,58 @@ public class X { public static void Main() { - } - - System.Exception Dummy(params object[] x) { return null;} - - void Test1(bool val) - { - if (val) - throw Dummy(true is var x1); + using (System.IDisposable d1 = Dummy(new C(""a""), new C(""b"") is var x1), + d2 = Dummy(new C(""c""), new C(""d"") is var x2)) + { + System.Console.WriteLine(d1); + System.Console.WriteLine(x1); + System.Console.WriteLine(d2); + System.Console.WriteLine(x2); + } - x1++; + using (Dummy(new C(""e""), new C(""f"") is var x1)) + { + System.Console.WriteLine(x1); + } } + + static System.IDisposable Dummy(System.IDisposable x, params object[] y) {return x;} } -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (15,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) - ); +class C : System.IDisposable +{ + private readonly string _val; - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); + public C(string val) + { + _val = val; + } + + public void Dispose() + { + System.Console.WriteLine(""Disposing {0}"", _val); + } - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); + public override string ToString() + { + return _val; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"a +b +c +d +Disposing c +Disposing a +f +Disposing e"); } [Fact] - public void ScopeOfPatternVariables_TrowStatement_03() + public void LocalDeclarationStmt_01() { var source = @" @@ -1627,41 +1253,54 @@ public class X { public static void Main() { + object d1 = Dummy(new C(""a""), new C(""b"") is var x1, x1), + d2 = Dummy(new C(""c""), new C(""d"") is var x2, x2); + System.Console.WriteLine(d1); + System.Console.WriteLine(d2); + System.Console.WriteLine(x1); + } + + static object Dummy(object x, object y, object z) + { + System.Console.WriteLine(z); + return x; } +} + +class C +{ + private readonly string _val; - void Dummy(params object[] x) {} + public C(string val) + { + _val = val; + } - void Test1() + public override string ToString() { - SpeculateHere(); + return _val; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - + CompileAndVerify(compilation, expectedOutput: +@"b +d +a +c +b"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var statement = (ThrowStatementSyntax)SyntaxFactory.ParseStatement(@" -throw Dummy(11 is var x1, x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ScopeOfPatternVariables_If_01() + public void LocalDeclarationStmt_02() { var source = @" @@ -1669,221 +1308,108 @@ public class X { public static void Main() { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - if (true is var x1) - { - Dummy(x1); - } - else + if (true) { - System.Console.WriteLine(x1); + object d1 = Dummy(new C(""a""), new C(""b"") is var x1, x1); } } - void Test2() - { - if (true is var x2) - Dummy(x2); - else - System.Console.WriteLine(x2); - } - - void Test3() + static object Dummy(object x, object y, object z) { - if (true is var x3) - Dummy(x3); - else - { - var x3 = 12; - System.Console.WriteLine(x3); - } + System.Console.WriteLine(z); + return x; } +} - void Test4() - { - var x4 = 11; - Dummy(x4); - - if (true is var x4) - Dummy(x4); - } +class C +{ + private readonly string _val; - void Test5(int x5) + public C(string val) { - if (true is var x5) - Dummy(x5); + _val = val; } - void Test6() + public override string ToString() { - if (x6 && true is var x6) - Dummy(x6); + return _val; } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"b"); + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); - void Test7() - { - if (true is var x7 && x7) - { - var x7 = 12; - Dummy(x7); + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } - } - void Test8() + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void DeconstructionDeclarationStmt_01() + { + var source = +@" +public class X +{ + public static void Main() { - if (true is var x8) - Dummy(x8); - - System.Console.WriteLine(x8); + (object d1, object d2) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), + Dummy(new C(""c""), (new C(""d"") is var x2), x2)); + System.Console.WriteLine(d1); + System.Console.WriteLine(d2); + System.Console.WriteLine(x1); } - void Test9() + static object Dummy(object x, object y, object z) { - if (true is var x9) - { - Dummy(x9); - if (true is var x9) // 2 - Dummy(x9); - } + System.Console.WriteLine(z); + return x; } +} + +class C +{ + private readonly string _val; - void Test10() + public C(string val) { - if (y10 is var x10) - { - var y10 = 12; - Dummy(y10); - } + _val = val; } - - - - - - - - - - void Test12() + public override string ToString() { - if (y12 is var x12) - var y12 = 12; + return _val; } - - //void Test13() - //{ - // if (y13 is var x13) - // let y13 = 12; - //} } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (110,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(110, 13), - // (36,17): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x3 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(36, 17), - // (46,25): error CS0128: A local variable named 'x4' is already defined in this scope - // if (true is var x4) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(46, 25), - // (52,25): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // if (true is var x5) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(52, 25), - // (58,13): error CS0841: Cannot use local variable 'x6' before it is declared - // if (x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(58, 13), - // (66,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(66, 17), - // (83,19): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(83, 19), - // (84,29): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // if (true is var x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(84, 29), - // (91,13): error CS0103: The name 'y10' does not exist in the current context - // if (y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(91, 13), - // (109,13): error CS0103: The name 'y12' does not exist in the current context - // if (y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(109, 13), - // (110,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(110, 17) - ); + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + CompileAndVerify(compilation, expectedOutput: +@"b +d +a +c +b"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref[0]); - VerifyNotAPatternLocal(model, x3Ref[1]); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").Single(); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(2, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ScopeOfPatternVariables_If_02() + [CompilerTrait(CompilerFeature.Tuples)] + public void DeconstructionDeclarationStmt_02() { var source = @" @@ -1891,81 +1417,52 @@ public class X { public static void Main() { + if (true) + { + (object d1, object d2) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), x1); + } } - bool Dummy(params object[] x) {return true;} - - void Test1() + static object Dummy(object x, object y, object z) { - if (true) - if (true is var x1) - { - } - - x1++; + System.Console.WriteLine(z); + return x; } } -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (17,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - [Fact] - public void ScopeOfPatternVariables_If_03() - { - var source = -@" -public class X +class C { - public static void Main() + private readonly string _val; + + public C(string val) { + _val = val; } - void Dummy(params object[] x) {} - - void Test1() + public override string ToString() { - SpeculateHere(); + return _val; } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + CompileAndVerify(compilation, expectedOutput: +@"b"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var statement = (IfStatementSyntax)SyntaxFactory.ParseStatement(@" -if (Dummy(11 is var x1, x1)); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void If_01() + [CompilerTrait(CompilerFeature.Tuples)] + public void DeconstructionDeclarationStmt_03() { var source = @" @@ -1973,21 +1470,167 @@ public class X { public static void Main() { - Test(1); - Test(2); - } - - public static void Test(int val) - { - if (Dummy(val == 1, val is var x1, x1)) + var (d1, (d2, d3)) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), + (Dummy(new C(""c""), (new C(""d"") is var x2), x2), + Dummy(new C(""e""), (new C(""f"") is var x3), x3))); + System.Console.WriteLine(d1); + System.Console.WriteLine(d2); + System.Console.WriteLine(d3); + System.Console.WriteLine(x1); + System.Console.WriteLine(x2); + System.Console.WriteLine(x3); + } + + static object Dummy(object x, object y, object z) + { + System.Console.WriteLine(z); + return x; + } +} + +class C +{ + private readonly string _val; + + public C(string val) + { + _val = val; + } + + public override string ToString() + { + return _val; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + CompileAndVerify(compilation, expectedOutput: +@"b +d +f +a +c +e +b +d +f"); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); + + var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); + var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); + Assert.Equal(1, x2Decl.Length); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); + + var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").ToArray(); + var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); + Assert.Equal(1, x3Decl.Length); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl[0], x3Ref); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void DeconstructionDeclarationStmt_04() { - System.Console.WriteLine(""true""); - System.Console.WriteLine(x1); + var source = +@" +public class X +{ + public static void Main() + { + (var d1, (var d2, var d3)) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), + (Dummy(new C(""c""), (new C(""d"") is var x2), x2), + Dummy(new C(""e""), (new C(""f"") is var x3), x3))); + System.Console.WriteLine(d1); + System.Console.WriteLine(d2); + System.Console.WriteLine(d3); + System.Console.WriteLine(x1); + System.Console.WriteLine(x2); + System.Console.WriteLine(x3); + } + + static object Dummy(object x, object y, object z) + { + System.Console.WriteLine(z); + return x; + } +} + +class C +{ + private readonly string _val; + + public C(string val) + { + _val = val; + } + + public override string ToString() + { + return _val; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + CompileAndVerify(compilation, expectedOutput: +@"b +d +f +a +c +e +b +d +f"); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); + + var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); + var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); + Assert.Equal(1, x2Decl.Length); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); + + var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").ToArray(); + var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); + Assert.Equal(1, x3Decl.Length); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl[0], x3Ref); } - else + + [Fact] + public void While_01() + { + var source = +@" +public class X +{ + public static void Main() + { + bool f = true; + + while (Dummy(f, (f ? 1 : 2) is var x1, x1)) { - System.Console.WriteLine(""false""); System.Console.WriteLine(x1); + f = false; } System.Console.WriteLine(x1); @@ -2003,12 +1646,8 @@ static bool Dummy(bool x, object y, object z) var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); CompileAndVerify(compilation, expectedOutput: @"1 -true -1 1 2 -false -2 2"); var tree = compilation.SyntaxTrees.Single(); @@ -2017,12 +1656,12 @@ static bool Dummy(bool x, object y, object z) var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); var x1Ref = GetReferences(tree, "x1").ToArray(); Assert.Equal(1, x1Decl.Length); - Assert.Equal(4, x1Ref.Length); + Assert.Equal(3, x1Ref.Length); VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void If_02() + public void While_02() { var source = @" @@ -2033,13 +1672,13 @@ public static void Main() bool f = true; if (f) - if (Dummy(f, (f ? 1 : 2) is var x1, x1)) - ; + while (Dummy(f, (f ? 1 : 2) is var x1, x1)) + break; if (f) { - if (Dummy(f, (f ? 3 : 4) is var x1, x1)) - ; + while (Dummy(f, (f ? 3 : 4) is var x1, x1)) + break; } } @@ -2067,7 +1706,7 @@ static bool Dummy(bool x, object y, object z) } [Fact] - public void ScopeOfPatternVariables_Lambda_01() + public void Do_01() { var source = @" @@ -2075,230 +1714,40 @@ public class X { public static void Main() { - } - - bool Dummy(params object[] x) {return true;} - - System.Action Test1() - { - return (o) => let x1 = o; - } - - System.Action Test2() - { - return (o) => let var x2 = o; - } - - void Test3() - { - Dummy((System.Func) (o => o is int x3 && x3 > 0)); - } - - void Test4() - { - Dummy((System.Func) (o => x4 && o is int x4)); - } - - void Test5() - { - Dummy((System.Func) ((o1, o2) => o1 is int x5 && - o2 is int x5 && - x5 > 0)); - } - - void Test6() - { - Dummy((System.Func) (o => o is int x6 && x6 > 0), (System.Func) (o => o is int x6 && x6 > 0)); - } - - void Test7() - { - Dummy(x7, 1); - Dummy(x7, - (System.Func) (o => o is int x7 && x7 > 0), - x7); - Dummy(x7, 2); - } - - void Test8() - { - Dummy(true is var x8 && x8, (System.Func) (o => o is int y8 && x8)); - } - - void Test9() - { - Dummy(true is var x9, - (System.Func) (o => o is int x9 && - x9 > 0), x9); - } + bool f; - void Test10() - { - Dummy((System.Func) (o => o is int x10 && - x10 > 0), - true is var x10, x10); - } + do + { + f = false; + } + while (Dummy(f, (f ? 1 : 2) is var x1, x1)); - void Test11() - { - var x11 = 11; - Dummy(x11); - Dummy((System.Func) (o => o is int x11 && - x11 > 0), x11); + System.Console.WriteLine(x1); } - void Test12() + static bool Dummy(bool x, object y, object z) { - Dummy((System.Func) (o => o is int x12 && - x12 > 0), - x12); - var x12 = 11; - Dummy(x12); + System.Console.WriteLine(z); + return x; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (12,27): error CS1002: ; expected - // return (o) => let x1 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(12, 27), - // (17,27): error CS1002: ; expected - // return (o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(17, 27), - // (12,23): error CS0103: The name 'let' does not exist in the current context - // return (o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(12, 23), - // (12,23): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // return (o) => let x1 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(12, 23), - // (12,27): error CS0103: The name 'x1' does not exist in the current context - // return (o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(12, 27), - // (12,32): error CS0103: The name 'o' does not exist in the current context - // return (o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(12, 32), - // (12,27): warning CS0162: Unreachable code detected - // return (o) => let x1 = o; - Diagnostic(ErrorCode.WRN_UnreachableCode, "x1").WithLocation(12, 27), - // (17,23): error CS0103: The name 'let' does not exist in the current context - // return (o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(17, 23), - // (17,23): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // return (o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(17, 23), - // (17,36): error CS0103: The name 'o' does not exist in the current context - // return (o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(17, 36), - // (17,27): warning CS0162: Unreachable code detected - // return (o) => let var x2 = o; - Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(17, 27), - // (27,49): error CS0841: Cannot use local variable 'x4' before it is declared - // Dummy((System.Func) (o => x4 && o is int x4)); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(27, 49), - // (33,74): error CS0128: A local variable named 'x5' is already defined in this scope - // o2 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(33, 74), - // (34,64): error CS0165: Use of unassigned local variable 'x5' - // x5 > 0)); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(34, 64), - // (44,15): error CS0103: The name 'x7' does not exist in the current context - // Dummy(x7, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(44, 15), - // (45,15): error CS0103: The name 'x7' does not exist in the current context - // Dummy(x7, - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(45, 15), - // (47,15): error CS0103: The name 'x7' does not exist in the current context - // x7); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(47, 15), - // (48,15): error CS0103: The name 'x7' does not exist in the current context - // Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(48, 15), - // (59,58): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // (System.Func) (o => o is int x9 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(59, 58), - // (65,58): error CS0136: A local or parameter named 'x10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy((System.Func) (o => o is int x10 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x10").WithArguments("x10").WithLocation(65, 58), - // (74,58): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy((System.Func) (o => o is int x11 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(74, 58), - // (80,58): error CS0136: A local or parameter named 'x12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy((System.Func) (o => o is int x12 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x12").WithArguments("x12").WithLocation(80, 58), - // (82,15): error CS0841: Cannot use local variable 'x12' before it is declared - // x12); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(82, 15) - ); + CompileAndVerify(compilation, expectedOutput: @"2 +2"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(5, x7Ref.Length); - VerifyNotInScope(model, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[2]); - VerifyNotInScope(model, x7Ref[3]); - VerifyNotInScope(model, x7Ref[4]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(2, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[0]); - - var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); - var x10Ref = GetReferences(tree, "x10").ToArray(); - Assert.Equal(2, x10Decl.Length); - Assert.Equal(2, x10Ref.Length); - VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref[0]); - VerifyModelForDeclarationPattern(model, x10Decl[1], x10Ref[1]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(3, x11Ref.Length); - VerifyNotAPatternLocal(model, x11Ref[0]); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[1]); - VerifyNotAPatternLocal(model, x11Ref[2]); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(3, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[0]); - VerifyNotAPatternLocal(model, x12Ref[1]); - VerifyNotAPatternLocal(model, x12Ref[2]); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void Lambda_01() + public void Do_02() { var source = @" @@ -2306,1241 +1755,445 @@ public class X { public static void Main() { - System.Console.WriteLine(Test1()); - } + bool f = true; - static bool Test1() - { - System.Func l = () => 1 is int x1 && Dummy(x1); - return l(); + if (f) + do + ; + while (Dummy(f, (f ? 1 : 2) is var x1, x1) && false); + + if (f) + { + do + ; + while (Dummy(f, (f ? 3 : 4) is var x1, x1) && false); + } } - static bool Dummy(int x) + static bool Dummy(bool x, object y, object z) { - System.Console.WriteLine(x); - return true; + System.Console.WriteLine(z); + return x; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"1 -True"); + CompileAndVerify(compilation, expectedOutput: +@"1 +3"); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); } [Fact] - public void ScopeOfPatternVariables_Query_01() + public void For_01() { var source = @" -using System.Linq; - public class X { public static void Main() { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - var res = from x in new[] { 1 is var y1 ? y1 : 0, y1} - select x + y1; - - Dummy(y1); - } - - void Test2() - { - var res = from x1 in new[] { 1 is var y2 ? y2 : 0} - from x2 in new[] { x1 is var z2 ? z2 : 0, z2, y2} - select x1 + x2 + y2 + - z2; - - Dummy(z2); - } - - void Test3() - { - var res = from x1 in new[] { 1 is var y3 ? y3 : 0} - let x2 = x1 is var z3 && z3 > 0 && y3 < 0 - select new { x1, x2, y3, - z3}; - - Dummy(z3); - } - - void Test4() - { - var res = from x1 in new[] { 1 is var y4 ? y4 : 0} - join x2 in new[] { 2 is var z4 ? z4 : 0, z4, y4} - on x1 + y4 + z4 + 3 is var u4 ? u4 : 0 + - v4 - equals x2 + y4 + z4 + 4 is var v4 ? v4 : 0 + - u4 - select new { x1, x2, y4, z4, - u4, v4 }; - - Dummy(z4); - Dummy(u4); - Dummy(v4); - } - - void Test5() - { - var res = from x1 in new[] { 1 is var y5 ? y5 : 0} - join x2 in new[] { 2 is var z5 ? z5 : 0, z5, y5} - on x1 + y5 + z5 + 3 is var u5 ? u5 : 0 + - v5 - equals x2 + y5 + z5 + 4 is var v5 ? v5 : 0 + - u5 - into g - select new { x1, y5, z5, g, - u5, v5 }; + bool f = true; - Dummy(z5); - Dummy(u5); - Dummy(v5); + for (Dummy(f, (f ? 10 : 20) is var x0, x0); + Dummy(f, (f ? 1 : 2) is var x1, x1); + Dummy(f, (f ? 100 : 200) is var x2, x2)) + { + System.Console.WriteLine(x0); + System.Console.WriteLine(x1); + f = false; + } } - void Test6() + static bool Dummy(bool x, object y, object z) { - var res = from x in new[] { 1 is var y6 ? y6 : 0} - where x > y6 && 1 is var z6 && z6 == 1 - select x + y6 + - z6; - - Dummy(z6); + System.Console.WriteLine(z); + return x; } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"10 +1 +10 +1 +200 +2"); + } - void Test7() + [Fact] + public void Foreach_01() + { + var source = +@" +public class X +{ + public static void Main() { - var res = from x in new[] { 1 is var y7 ? y7 : 0} - orderby x > y7 && 1 is var z7 && z7 == - u7, - x > y7 && 1 is var u7 && u7 == - z7 - select x + y7 + - z7 + u7; + bool f = true; - Dummy(z7); - Dummy(u7); + foreach (var i in Dummy(3 is var x1, x1)) + { + System.Console.WriteLine(x1); + } } - void Test8() + static System.Collections.IEnumerable Dummy(object y, object z) { - var res = from x in new[] { 1 is var y8 ? y8 : 0} - select x > y8 && 1 is var z8 && z8 == 1; - - Dummy(z8); + System.Console.WriteLine(z); + return ""a""; } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"3 +3"); + } - void Test9() + [Fact] + public void Lock_01() + { + var source = +@" +public class X +{ + public static void Main() { - var res = from x in new[] { 1 is var y9 ? y9 : 0} - group x > y9 && 1 is var z9 && z9 == - u9 - by - x > y9 && 1 is var u9 && u9 == - z9; - - Dummy(z9); - Dummy(u9); - } + lock (Dummy(""lock"" is var x1, x1)) + { + System.Console.WriteLine(x1); + } - void Test10() - { - var res = from x1 in new[] { 1 is var y10 ? y10 : 0} - from y10 in new[] { 1 } - select x1 + y10; + System.Console.WriteLine(x1); } - void Test11() + static object Dummy(object y, object z) { - var res = from x1 in new[] { 1 is var y11 ? y11 : 0} - let y11 = x1 + 1 - select x1 + y11; + System.Console.WriteLine(z); + return new object(); } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (25,26): error CS0103: The name 'z2' does not exist in the current context - // z2; - Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(25, 26), - // (27,15): error CS0103: The name 'z2' does not exist in the current context - // Dummy(z2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(27, 15), - // (35,32): error CS0103: The name 'z3' does not exist in the current context - // z3}; - Diagnostic(ErrorCode.ERR_NameNotInContext, "z3").WithArguments("z3").WithLocation(35, 32), - // (37,15): error CS0103: The name 'z3' does not exist in the current context - // Dummy(z3); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z3").WithArguments("z3").WithLocation(37, 15), - // (45,35): error CS0103: The name 'v4' does not exist in the current context - // v4 - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(45, 35), - // (47,35): error CS1938: The name 'u4' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. - // u4 - Diagnostic(ErrorCode.ERR_QueryInnerKey, "u4").WithArguments("u4").WithLocation(47, 35), - // (49,32): error CS0103: The name 'u4' does not exist in the current context - // u4, v4 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(49, 32), - // (49,36): error CS0103: The name 'v4' does not exist in the current context - // u4, v4 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(49, 36), - // (52,15): error CS0103: The name 'u4' does not exist in the current context - // Dummy(u4); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(52, 15), - // (53,15): error CS0103: The name 'v4' does not exist in the current context - // Dummy(v4); - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(53, 15), - // (61,35): error CS0103: The name 'v5' does not exist in the current context - // v5 - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(61, 35), - // (63,35): error CS1938: The name 'u5' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. - // u5 - Diagnostic(ErrorCode.ERR_QueryInnerKey, "u5").WithArguments("u5").WithLocation(63, 35), - // (66,32): error CS0103: The name 'u5' does not exist in the current context - // u5, v5 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(66, 32), - // (66,36): error CS0103: The name 'v5' does not exist in the current context - // u5, v5 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(66, 36), - // (69,15): error CS0103: The name 'u5' does not exist in the current context - // Dummy(u5); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(69, 15), - // (70,15): error CS0103: The name 'v5' does not exist in the current context - // Dummy(v5); - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(70, 15), - // (78,26): error CS0103: The name 'z6' does not exist in the current context - // z6; - Diagnostic(ErrorCode.ERR_NameNotInContext, "z6").WithArguments("z6").WithLocation(78, 26), - // (80,15): error CS0103: The name 'z6' does not exist in the current context - // Dummy(z6); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z6").WithArguments("z6").WithLocation(80, 15), - // (87,27): error CS0103: The name 'u7' does not exist in the current context - // u7, - Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(87, 27), - // (89,27): error CS0103: The name 'z7' does not exist in the current context - // z7 - Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(89, 27), - // (91,31): error CS0103: The name 'u7' does not exist in the current context - // z7 + u7; - Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(91, 31), - // (91,26): error CS0103: The name 'z7' does not exist in the current context - // z7 + u7; - Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(91, 26), - // (93,15): error CS0103: The name 'z7' does not exist in the current context - // Dummy(z7); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(93, 15), - // (94,15): error CS0103: The name 'u7' does not exist in the current context - // Dummy(u7); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(94, 15), - // (88,52): error CS0165: Use of unassigned local variable 'u7' - // x > y7 && 1 is var u7 && u7 == - Diagnostic(ErrorCode.ERR_UseDefViolation, "u7").WithArguments("u7").WithLocation(88, 52), - // (102,15): error CS0103: The name 'z8' does not exist in the current context - // Dummy(z8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z8").WithArguments("z8").WithLocation(102, 15), - // (112,25): error CS0103: The name 'z9' does not exist in the current context - // z9; - Diagnostic(ErrorCode.ERR_NameNotInContext, "z9").WithArguments("z9").WithLocation(112, 25), - // (109,25): error CS0103: The name 'u9' does not exist in the current context - // u9 - Diagnostic(ErrorCode.ERR_NameNotInContext, "u9").WithArguments("u9").WithLocation(109, 25), - // (114,15): error CS0103: The name 'z9' does not exist in the current context - // Dummy(z9); - Diagnostic(ErrorCode.ERR_NameNotInContext, "z9").WithArguments("z9").WithLocation(114, 15), - // (115,15): error CS0103: The name 'u9' does not exist in the current context - // Dummy(u9); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u9").WithArguments("u9").WithLocation(115, 15), - // (108,50): error CS0165: Use of unassigned local variable 'z9' - // group x > y9 && 1 is var z9 && z9 == - Diagnostic(ErrorCode.ERR_UseDefViolation, "z9").WithArguments("z9").WithLocation(108, 50), - // (121,24): error CS1931: The range variable 'y10' conflicts with a previous declaration of 'y10' - // from y10 in new[] { 1 } - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y10").WithArguments("y10").WithLocation(121, 24), - // (128,23): error CS1931: The range variable 'y11' conflicts with a previous declaration of 'y11' - // let y11 = x1 + 1 - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y11").WithArguments("y11").WithLocation(128, 23) - ); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"lock +lock +lock"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var y1Decl = GetPatternDeclarations(tree, "y1").Single(); - var y1Ref = GetReferences(tree, "y1").ToArray(); - Assert.Equal(4, y1Ref.Length); - VerifyModelForDeclarationPattern(model, y1Decl, y1Ref); - - var y2Decl = GetPatternDeclarations(tree, "y2").Single(); - var y2Ref = GetReferences(tree, "y2").ToArray(); - Assert.Equal(3, y2Ref.Length); - VerifyModelForDeclarationPattern(model, y2Decl, y2Ref); - - var z2Decl = GetPatternDeclarations(tree, "z2").Single(); - var z2Ref = GetReferences(tree, "z2").ToArray(); - Assert.Equal(4, z2Ref.Length); - VerifyModelForDeclarationPattern(model, z2Decl, z2Ref[0], z2Ref[1]); - VerifyNotInScope(model, z2Ref[2]); - VerifyNotInScope(model, z2Ref[3]); - - var y3Decl = GetPatternDeclarations(tree, "y3").Single(); - var y3Ref = GetReferences(tree, "y3").ToArray(); - Assert.Equal(3, y3Ref.Length); - VerifyModelForDeclarationPattern(model, y3Decl, y3Ref); - - var z3Decl = GetPatternDeclarations(tree, "z3").Single(); - var z3Ref = GetReferences(tree, "z3").ToArray(); - Assert.Equal(3, z3Ref.Length); - VerifyModelForDeclarationPattern(model, z3Decl, z3Ref[0]); - VerifyNotInScope(model, z3Ref[1]); - VerifyNotInScope(model, z3Ref[2]); - - var y4Decl = GetPatternDeclarations(tree, "y4").Single(); - var y4Ref = GetReferences(tree, "y4").ToArray(); - Assert.Equal(5, y4Ref.Length); - VerifyModelForDeclarationPattern(model, y4Decl, y4Ref); - - var z4Decl = GetPatternDeclarations(tree, "z4").Single(); - var z4Ref = GetReferences(tree, "z4").ToArray(); - Assert.Equal(6, z4Ref.Length); - VerifyModelForDeclarationPattern(model, z4Decl, z4Ref); - - var u4Decl = GetPatternDeclarations(tree, "u4").Single(); - var u4Ref = GetReferences(tree, "u4").ToArray(); - Assert.Equal(4, u4Ref.Length); - VerifyModelForDeclarationPattern(model, u4Decl, u4Ref[0]); - VerifyNotInScope(model, u4Ref[1]); - VerifyNotInScope(model, u4Ref[2]); - VerifyNotInScope(model, u4Ref[3]); - - var v4Decl = GetPatternDeclarations(tree, "v4").Single(); - var v4Ref = GetReferences(tree, "v4").ToArray(); - Assert.Equal(4, v4Ref.Length); - VerifyNotInScope(model, v4Ref[0]); - VerifyModelForDeclarationPattern(model, v4Decl, v4Ref[1]); - VerifyNotInScope(model, v4Ref[2]); - VerifyNotInScope(model, v4Ref[3]); - - var y5Decl = GetPatternDeclarations(tree, "y5").Single(); - var y5Ref = GetReferences(tree, "y5").ToArray(); - Assert.Equal(5, y5Ref.Length); - VerifyModelForDeclarationPattern(model, y5Decl, y5Ref); - - var z5Decl = GetPatternDeclarations(tree, "z5").Single(); - var z5Ref = GetReferences(tree, "z5").ToArray(); - Assert.Equal(6, z5Ref.Length); - VerifyModelForDeclarationPattern(model, z5Decl, z5Ref); - - var u5Decl = GetPatternDeclarations(tree, "u5").Single(); - var u5Ref = GetReferences(tree, "u5").ToArray(); - Assert.Equal(4, u5Ref.Length); - VerifyModelForDeclarationPattern(model, u5Decl, u5Ref[0]); - VerifyNotInScope(model, u5Ref[1]); - VerifyNotInScope(model, u5Ref[2]); - VerifyNotInScope(model, u5Ref[3]); - - var v5Decl = GetPatternDeclarations(tree, "v5").Single(); - var v5Ref = GetReferences(tree, "v5").ToArray(); - Assert.Equal(4, v5Ref.Length); - VerifyNotInScope(model, v5Ref[0]); - VerifyModelForDeclarationPattern(model, v5Decl, v5Ref[1]); - VerifyNotInScope(model, v5Ref[2]); - VerifyNotInScope(model, v5Ref[3]); - - var y6Decl = GetPatternDeclarations(tree, "y6").Single(); - var y6Ref = GetReferences(tree, "y6").ToArray(); - Assert.Equal(3, y6Ref.Length); - VerifyModelForDeclarationPattern(model, y6Decl, y6Ref); - - var z6Decl = GetPatternDeclarations(tree, "z6").Single(); - var z6Ref = GetReferences(tree, "z6").ToArray(); - Assert.Equal(3, z6Ref.Length); - VerifyModelForDeclarationPattern(model, z6Decl, z6Ref[0]); - VerifyNotInScope(model, z6Ref[1]); - VerifyNotInScope(model, z6Ref[2]); - - var y7Decl = GetPatternDeclarations(tree, "y7").Single(); - var y7Ref = GetReferences(tree, "y7").ToArray(); - Assert.Equal(4, y7Ref.Length); - VerifyModelForDeclarationPattern(model, y7Decl, y7Ref); - - var z7Decl = GetPatternDeclarations(tree, "z7").Single(); - var z7Ref = GetReferences(tree, "z7").ToArray(); - Assert.Equal(4, z7Ref.Length); - VerifyModelForDeclarationPattern(model, z7Decl, z7Ref[0]); - VerifyNotInScope(model, z7Ref[1]); - VerifyNotInScope(model, z7Ref[2]); - VerifyNotInScope(model, z7Ref[3]); - - var u7Decl = GetPatternDeclarations(tree, "u7").Single(); - var u7Ref = GetReferences(tree, "u7").ToArray(); - Assert.Equal(4, u7Ref.Length); - VerifyNotInScope(model, u7Ref[0]); - VerifyModelForDeclarationPattern(model, u7Decl, u7Ref[1]); - VerifyNotInScope(model, u7Ref[2]); - VerifyNotInScope(model, u7Ref[3]); - - var y8Decl = GetPatternDeclarations(tree, "y8").Single(); - var y8Ref = GetReferences(tree, "y8").ToArray(); - Assert.Equal(2, y8Ref.Length); - VerifyModelForDeclarationPattern(model, y8Decl, y8Ref); - - var z8Decl = GetPatternDeclarations(tree, "z8").Single(); - var z8Ref = GetReferences(tree, "z8").ToArray(); - Assert.Equal(2, z8Ref.Length); - VerifyModelForDeclarationPattern(model, z8Decl, z8Ref[0]); - VerifyNotInScope(model, z8Ref[1]); - - var y9Decl = GetPatternDeclarations(tree, "y9").Single(); - var y9Ref = GetReferences(tree, "y9").ToArray(); - Assert.Equal(3, y9Ref.Length); - VerifyModelForDeclarationPattern(model, y9Decl, y9Ref); - - var z9Decl = GetPatternDeclarations(tree, "z9").Single(); - var z9Ref = GetReferences(tree, "z9").ToArray(); - Assert.Equal(3, z9Ref.Length); - VerifyModelForDeclarationPattern(model, z9Decl, z9Ref[0]); - VerifyNotInScope(model, z9Ref[1]); - VerifyNotInScope(model, z9Ref[2]); - - var u9Decl = GetPatternDeclarations(tree, "u9").Single(); - var u9Ref = GetReferences(tree, "u9").ToArray(); - Assert.Equal(3, u9Ref.Length); - VerifyNotInScope(model, u9Ref[0]); - VerifyModelForDeclarationPattern(model, u9Decl, u9Ref[1]); - VerifyNotInScope(model, u9Ref[2]); - - var y10Decl = GetPatternDeclarations(tree, "y10").Single(); - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyModelForDeclarationPattern(model, y10Decl, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y11Decl = GetPatternDeclarations(tree, "y11").Single(); - var y11Ref = GetReferences(tree, "y11").ToArray(); - Assert.Equal(2, y11Ref.Length); - VerifyModelForDeclarationPattern(model, y11Decl, y11Ref[0]); - VerifyNotAPatternLocal(model, y11Ref[1]); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ScopeOfPatternVariables_Query_03() + public void Lock_02() { var source = @" -using System.Linq; - public class X { public static void Main() { - } - - bool Dummy(params object[] x) {return true;} + bool f = true; - void Test4() - { - var res = from x1 in new[] { 1 is var y4 ? y4 : 0} - select x1 into x1 - join x2 in new[] { 2 is var z4 ? z4 : 0, z4, y4} - on x1 + y4 + z4 + 3 is var u4 ? u4 : 0 + - v4 - equals x2 + y4 + z4 + 4 is var v4 ? v4 : 0 + - u4 - select new { x1, x2, y4, z4, - u4, v4 }; + if (f) + lock (Dummy(f, (f ? 1 : 2) is var x1, x1)) + {} - Dummy(z4); - Dummy(u4); - Dummy(v4); + if (f) + { + lock (Dummy(f, (f ? 3 : 4) is var x1, x1)) + {} + } } - void Test5() + static object Dummy(bool x, object y, object z) { - var res = from x1 in new[] { 1 is var y5 ? y5 : 0} - select x1 into x1 - join x2 in new[] { 2 is var z5 ? z5 : 0, z5, y5} - on x1 + y5 + z5 + 3 is var u5 ? u5 : 0 + - v5 - equals x2 + y5 + z5 + 4 is var v5 ? v5 : 0 + - u5 - into g - select new { x1, y5, z5, g, - u5, v5 }; - - Dummy(z5); - Dummy(u5); - Dummy(v5); + System.Console.WriteLine(z); + return x; } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (18,35): error CS0103: The name 'v4' does not exist in the current context - // v4 - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(18, 35), - // (20,35): error CS1938: The name 'u4' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. - // u4 - Diagnostic(ErrorCode.ERR_QueryInnerKey, "u4").WithArguments("u4").WithLocation(20, 35), - // (22,32): error CS0103: The name 'u4' does not exist in the current context - // u4, v4 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(22, 32), - // (22,36): error CS0103: The name 'v4' does not exist in the current context - // u4, v4 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(22, 36), - // (25,15): error CS0103: The name 'u4' does not exist in the current context - // Dummy(u4); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(25, 15), - // (26,15): error CS0103: The name 'v4' does not exist in the current context - // Dummy(v4); - Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(26, 15), - // (35,35): error CS0103: The name 'v5' does not exist in the current context - // v5 - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(35, 35), - // (37,35): error CS1938: The name 'u5' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. - // u5 - Diagnostic(ErrorCode.ERR_QueryInnerKey, "u5").WithArguments("u5").WithLocation(37, 35), - // (40,32): error CS0103: The name 'u5' does not exist in the current context - // u5, v5 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(40, 32), - // (40,36): error CS0103: The name 'v5' does not exist in the current context - // u5, v5 }; - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(40, 36), - // (43,15): error CS0103: The name 'u5' does not exist in the current context - // Dummy(u5); - Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(43, 15), - // (44,15): error CS0103: The name 'v5' does not exist in the current context - // Dummy(v5); - Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(44, 15) - ); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"1 +3"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var y4Decl = GetPatternDeclarations(tree, "y4").Single(); - var y4Ref = GetReferences(tree, "y4").ToArray(); - Assert.Equal(5, y4Ref.Length); - VerifyModelForDeclarationPattern(model, y4Decl, y4Ref); - - var z4Decl = GetPatternDeclarations(tree, "z4").Single(); - var z4Ref = GetReferences(tree, "z4").ToArray(); - Assert.Equal(6, z4Ref.Length); - VerifyModelForDeclarationPattern(model, z4Decl, z4Ref); - - var u4Decl = GetPatternDeclarations(tree, "u4").Single(); - var u4Ref = GetReferences(tree, "u4").ToArray(); - Assert.Equal(4, u4Ref.Length); - VerifyModelForDeclarationPattern(model, u4Decl, u4Ref[0]); - VerifyNotInScope(model, u4Ref[1]); - VerifyNotInScope(model, u4Ref[2]); - VerifyNotInScope(model, u4Ref[3]); - - var v4Decl = GetPatternDeclarations(tree, "v4").Single(); - var v4Ref = GetReferences(tree, "v4").ToArray(); - Assert.Equal(4, v4Ref.Length); - VerifyNotInScope(model, v4Ref[0]); - VerifyModelForDeclarationPattern(model, v4Decl, v4Ref[1]); - VerifyNotInScope(model, v4Ref[2]); - VerifyNotInScope(model, v4Ref[3]); - - var y5Decl = GetPatternDeclarations(tree, "y5").Single(); - var y5Ref = GetReferences(tree, "y5").ToArray(); - Assert.Equal(5, y5Ref.Length); - VerifyModelForDeclarationPattern(model, y5Decl, y5Ref); - - var z5Decl = GetPatternDeclarations(tree, "z5").Single(); - var z5Ref = GetReferences(tree, "z5").ToArray(); - Assert.Equal(6, z5Ref.Length); - VerifyModelForDeclarationPattern(model, z5Decl, z5Ref); - - var u5Decl = GetPatternDeclarations(tree, "u5").Single(); - var u5Ref = GetReferences(tree, "u5").ToArray(); - Assert.Equal(4, u5Ref.Length); - VerifyModelForDeclarationPattern(model, u5Decl, u5Ref[0]); - VerifyNotInScope(model, u5Ref[1]); - VerifyNotInScope(model, u5Ref[2]); - VerifyNotInScope(model, u5Ref[3]); - - var v5Decl = GetPatternDeclarations(tree, "v5").Single(); - var v5Ref = GetReferences(tree, "v5").ToArray(); - Assert.Equal(4, v5Ref.Length); - VerifyNotInScope(model, v5Ref[0]); - VerifyModelForDeclarationPattern(model, v5Decl, v5Ref[1]); - VerifyNotInScope(model, v5Ref[2]); - VerifyNotInScope(model, v5Ref[3]); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); } [Fact] - [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] - public void ScopeOfPatternVariables_Query_05() + public void Fixed_01() { var source = @" -using System.Linq; - -public class X +public unsafe class X { public static void Main() { + fixed (int* p = Dummy(""fixed"" is var x1, x1)) + { + System.Console.WriteLine(x1); + } } - bool Dummy(params object[] x) {return true;} - - void Test1() + static int[] Dummy(object y, object z) { - int y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0, y6 = 0, y7 = 0, y8 = 0, y9 = 0, y10 = 0, y11 = 0, y12 = 0; - - var res = from x1 in new[] { 1 is var y1 ? y1 : 0} - from x2 in new[] { 2 is var y2 ? y2 : 0} - join x3 in new[] { 3 is var y3 ? y3 : 0} - on 4 is var y4 ? y4 : 0 - equals 5 is var y5 ? y5 : 0 - where 6 is var y6 && y6 == 1 - orderby 7 is var y7 && y7 > 0, - 8 is var y8 && y8 > 0 - group 9 is var y9 && y9 > 0 - by 10 is var y10 && y10 > 0 - into g - let x11 = 11 is var y11 && y11 > 0 - select 12 is var y12 && y12 > 0 - into s - select y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10 + y11 + y12; - - Dummy(y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12); + System.Console.WriteLine(z); + return new int[1]; } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (16,47): error CS0128: A local variable named 'y1' is already defined in this scope - // var res = from x1 in new[] { 1 is var y1 ? y1 : 0} - Diagnostic(ErrorCode.ERR_LocalDuplicate, "y1").WithArguments("y1").WithLocation(16, 47), - // (17,47): error CS0136: A local or parameter named 'y2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // from x2 in new[] { 2 is var y2 ? y2 : 0} - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y2").WithArguments("y2").WithLocation(17, 47), - // (18,47): error CS0128: A local variable named 'y3' is already defined in this scope - // join x3 in new[] { 3 is var y3 ? y3 : 0} - Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(18, 47), - // (19,36): error CS0136: A local or parameter named 'y4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // on 4 is var y4 ? y4 : 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y4").WithArguments("y4").WithLocation(19, 36), - // (20,43): error CS0136: A local or parameter named 'y5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // equals 5 is var y5 ? y5 : 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y5").WithArguments("y5").WithLocation(20, 43), - // (21,34): error CS0136: A local or parameter named 'y6' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // where 6 is var y6 && y6 == 1 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y6").WithArguments("y6").WithLocation(21, 34), - // (22,36): error CS0136: A local or parameter named 'y7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // orderby 7 is var y7 && y7 > 0, - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y7").WithArguments("y7").WithLocation(22, 36), - // (23,36): error CS0136: A local or parameter named 'y8' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // 8 is var y8 && y8 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y8").WithArguments("y8").WithLocation(23, 36), - // (25,32): error CS0136: A local or parameter named 'y10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // by 10 is var y10 && y10 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y10").WithArguments("y10").WithLocation(25, 32), - // (24,34): error CS0136: A local or parameter named 'y9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // group 9 is var y9 && y9 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y9").WithArguments("y9").WithLocation(24, 34), - // (27,39): error CS0136: A local or parameter named 'y11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // let x11 = 11 is var y11 && y11 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y11").WithArguments("y11").WithLocation(27, 39), - // (28,36): error CS0136: A local or parameter named 'y12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // select 12 is var y12 && y12 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y12").WithArguments("y12").WithLocation(28, 36) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - for (int i = 1; i < 13; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).Single(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); - Assert.Equal(3, yRef.Length); - - switch (i) - { - case 1: - case 3: - VerifyModelForDeclarationPatternDuplicateInSameScope(model, yDecl); - VerifyNotAPatternLocal(model, yRef[0]); - break; - default: - VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); - break; - } - - VerifyNotAPatternLocal(model, yRef[2]); - - switch (i) - { - case 1: - case 3: - case 12: - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyNotAPatternLocal(model, yRef[1]); - break; - default: - VerifyNotAPatternLocal(model, yRef[1]); - break; - } - } + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); + CompileAndVerify(compilation, expectedOutput: +@"fixed +fixed"); } [Fact] - [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] - public void ScopeOfPatternVariables_Query_06() + public void Yield_01() { var source = @" -using System.Linq; - public class X { public static void Main() { + foreach (var o in Test()) + {} } - bool Dummy(params object[] x) {return true;} - - void Test1() - { - Dummy(1 is int y1, - 2 is int y2, - 3 is int y3, - 4 is int y4, - 5 is int y5, - 6 is int y6, - 7 is int y7, - 8 is int y8, - 9 is int y9, - 10 is int y10, - 11 is int y11, - 12 is int y12, - from x1 in new[] { 1 is var y1 ? y1 : 0} - from x2 in new[] { 2 is var y2 ? y2 : 0} - join x3 in new[] { 3 is var y3 ? y3 : 0} - on 4 is var y4 ? y4 : 0 - equals 5 is var y5 ? y5 : 0 - where 6 is var y6 && y6 == 1 - orderby 7 is var y7 && y7 > 0, - 8 is var y8 && y8 > 0 - group 9 is var y9 && y9 > 0 - by 10 is var y10 && y10 > 0 - into g - let x11 = 11 is var y11 && y11 > 0 - select 12 is var y12 && y12 > 0 - into s - select y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10 + y11 + y12); + static System.Collections.IEnumerable Test() + { + yield return Dummy(""yield1"" is var x1, x1); + yield return Dummy(""yield2"" is var x2, x2); + System.Console.WriteLine(x1); + } + + static object Dummy(object y, object z) + { + System.Console.WriteLine(z); + return new object(); } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (26,47): error CS0128: A local variable named 'y1' is already defined in this scope - // from x1 in new[] { 1 is var y1 ? y1 : 0} - Diagnostic(ErrorCode.ERR_LocalDuplicate, "y1").WithArguments("y1").WithLocation(26, 47), - // (27,47): error CS0136: A local or parameter named 'y2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // from x2 in new[] { 2 is var y2 ? y2 : 0} - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y2").WithArguments("y2").WithLocation(27, 47), - // (28,47): error CS0128: A local variable named 'y3' is already defined in this scope - // join x3 in new[] { 3 is var y3 ? y3 : 0} - Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(28, 47), - // (29,36): error CS0136: A local or parameter named 'y4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // on 4 is var y4 ? y4 : 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y4").WithArguments("y4").WithLocation(29, 36), - // (30,43): error CS0136: A local or parameter named 'y5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // equals 5 is var y5 ? y5 : 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y5").WithArguments("y5").WithLocation(30, 43), - // (31,34): error CS0136: A local or parameter named 'y6' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // where 6 is var y6 && y6 == 1 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y6").WithArguments("y6").WithLocation(31, 34), - // (32,36): error CS0136: A local or parameter named 'y7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // orderby 7 is var y7 && y7 > 0, - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y7").WithArguments("y7").WithLocation(32, 36), - // (33,36): error CS0136: A local or parameter named 'y8' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // 8 is var y8 && y8 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y8").WithArguments("y8").WithLocation(33, 36), - // (35,32): error CS0136: A local or parameter named 'y10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // by 10 is var y10 && y10 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y10").WithArguments("y10").WithLocation(35, 32), - // (34,34): error CS0136: A local or parameter named 'y9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // group 9 is var y9 && y9 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y9").WithArguments("y9").WithLocation(34, 34), - // (37,39): error CS0136: A local or parameter named 'y11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // let x11 = 11 is var y11 && y11 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y11").WithArguments("y11").WithLocation(37, 39), - // (38,36): error CS0136: A local or parameter named 'y12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // select 12 is var y12 && y12 > 0 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y12").WithArguments("y12").WithLocation(38, 36) - ); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"yield1 +yield2 +yield1"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - for (int i = 1; i < 13; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).ToArray(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); - Assert.Equal(2, yDecl.Length); - Assert.Equal(2, yRef.Length); - - switch (i) - { - case 1: - case 3: - VerifyModelForDeclarationPattern(model, yDecl[0], yRef); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, yDecl[1]); - break; - case 12: - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); - VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); - break; - - default: - VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); - VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); - break; - } - } + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] - public void ScopeOfPatternVariables_Query_07() + public void Yield_02() { var source = @" -using System.Linq; - public class X { public static void Main() { + foreach (var o in Test()) + {} } - bool Dummy(params object[] x) {return true;} + static System.Collections.IEnumerable Test() + { + bool f = true; + + if (f) + yield return Dummy(""yield1"" is var x1, x1); + + if (f) + { + yield return Dummy(""yield2"" is var x1, x1); + } + } - void Test1() + static object Dummy(object y, object z) { - Dummy(7 is int y3, - from x1 in new[] { 0 } - select x1 - into x1 - join x3 in new[] { 3 is var y3 ? y3 : 0} - on x1 equals x3 - select y3); + System.Console.WriteLine(z); + return new object(); } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (18,47): error CS0128: A local variable named 'y3' is already defined in this scope - // join x3 in new[] { 3 is var y3 ? y3 : 0} - Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(18, 47) - ); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: +@"yield1 +yield2"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - const string id = "y3"; - var yDecl = GetPatternDeclarations(tree, id).ToArray(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); - Assert.Equal(2, yDecl.Length); - Assert.Equal(2, yRef.Length); - VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); } [Fact] - public void ScopeOfPatternVariables_Query_08() + public void Return_01() { var source = @" -using System.Linq; - public class X { public static void Main() { + Test(); } - bool Dummy(params object[] x) {return true;} - - void Test1() - { - var res = from x1 in new[] { Dummy(1 is var y1, - 2 is var y2, - 3 is var y3, - 4 is var y4 - ) ? 1 : 0} - from y1 in new[] { 1 } - join y2 in new[] { 0 } - on y1 equals y2 - let y3 = 0 - group y3 - by x1 - into y4 - select y4; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (19,24): error CS1931: The range variable 'y1' conflicts with a previous declaration of 'y1' - // from y1 in new[] { 1 } - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y1").WithArguments("y1").WithLocation(19, 24), - // (20,24): error CS1931: The range variable 'y2' conflicts with a previous declaration of 'y2' - // join y2 in new[] { 0 } - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y2").WithArguments("y2").WithLocation(20, 24), - // (22,23): error CS1931: The range variable 'y3' conflicts with a previous declaration of 'y3' - // let y3 = 0 - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y3").WithArguments("y3").WithLocation(22, 23), - // (25,24): error CS1931: The range variable 'y4' conflicts with a previous declaration of 'y4' - // into y4 - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y4").WithArguments("y4").WithLocation(25, 24) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - for (int i = 1; i < 5; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).Single(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); - VerifyModelForDeclarationPattern(model, yDecl); - VerifyNotAPatternLocal(model, yRef); - } - } - - [Fact] - [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] - public void ScopeOfPatternVariables_Query_09() - { - var source = -@" -using System.Linq; - -public class X -{ - public static void Main() + static object Test() { + return Dummy(""return"" is var x1, x1); } - bool Dummy(params object[] x) {return true;} - - void Test1() + static object Dummy(object y, object z) { - var res = from y1 in new[] { 0 } - join y2 in new[] { 0 } - on y1 equals y2 - let y3 = 0 - group y3 - by 1 - into y4 - select y4 == null ? 1 : 0 - into x2 - join y5 in new[] { Dummy(1 is var y1, - 2 is var y2, - 3 is var y3, - 4 is var y4, - 5 is var y5 - ) ? 1 : 0 } - on x2 equals y5 - select x2; + System.Console.WriteLine(z); + return new object(); } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (14,24): error CS1931: The range variable 'y1' conflicts with a previous declaration of 'y1' - // var res = from y1 in new[] { 0 } - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y1").WithArguments("y1").WithLocation(14, 24), - // (15,24): error CS1931: The range variable 'y2' conflicts with a previous declaration of 'y2' - // join y2 in new[] { 0 } - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y2").WithArguments("y2").WithLocation(15, 24), - // (17,23): error CS1931: The range variable 'y3' conflicts with a previous declaration of 'y3' - // let y3 = 0 - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y3").WithArguments("y3").WithLocation(17, 23), - // (20,24): error CS1931: The range variable 'y4' conflicts with a previous declaration of 'y4' - // into y4 - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y4").WithArguments("y4").WithLocation(20, 24), - // (23,24): error CS1931: The range variable 'y5' conflicts with a previous declaration of 'y5' - // join y5 in new[] { Dummy(1 is var y1, - Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y5").WithArguments("y5").WithLocation(23, 24) - ); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput:@"return"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - for (int i = 1; i < 6; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).Single(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); - - switch (i) - { - case 4: - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyModelForDeclarationPattern(model, yDecl); - VerifyNotAPatternLocal(model, yRef); - break; - case 5: - VerifyModelForDeclarationPattern(model, yDecl); - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyNotAPatternLocal(model, yRef); - break; - default: - VerifyModelForDeclarationPattern(model, yDecl); - VerifyNotAPatternLocal(model, yRef); - break; - } - } + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] - [WorkItem(12052, "https://github.com/dotnet/roslyn/issues/12052")] - public void ScopeOfPatternVariables_Query_10() + public void Return_02() { - var source = -@" -using System.Linq; - -public class X + var text = @" +public class Cls { public static void Main() { + Test0(true); + Test0(false); } - bool Dummy(params object[] x) {return true;} - - void Test1() - { - var res = from y1 in new[] { 0 } - from x2 in new[] { 1 is var y1 ? y1 : 1 } - select y1; - } - - void Test2() - { - var res = from y2 in new[] { 0 } - join x3 in new[] { 1 } - on 2 is var y2 ? y2 : 0 - equals x3 - select y2; - } - - void Test3() - { - var res = from x3 in new[] { 0 } - join y3 in new[] { 1 } - on x3 - equals 3 is var y3 ? y3 : 0 - select y3; - } - - void Test4() - { - var res = from y4 in new[] { 0 } - where 4 is var y4 && y4 == 1 - select y4; - } - - void Test5() - { - var res = from y5 in new[] { 0 } - orderby 5 is var y5 && y5 > 1, - 1 - select y5; - } - - void Test6() - { - var res = from y6 in new[] { 0 } - orderby 1, - 6 is var y6 && y6 > 1 - select y6; - } - - void Test7() + static object Test0(bool val) { - var res = from y7 in new[] { 0 } - group 7 is var y7 && y7 == 3 - by y7; - } + if (val) + return Test2(1 is var x1, x1); - void Test8() - { - var res = from y8 in new[] { 0 } - group y8 - by 8 is var y8 && y8 == 3; - } + if (!val) + { + return Test2(2 is var x1, x1); + } - void Test9() - { - var res = from y9 in new[] { 0 } - let x4 = 9 is var y9 && y9 > 0 - select y9; + return null; } - void Test10() + static object Test2(object x, object y) { - var res = from y10 in new[] { 0 } - select 10 is var y10 && y10 > 0; + System.Console.Write(y); + return x; } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); +}"; + var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - // error CS0412 is misleading and reported due to preexisting bug https://github.com/dotnet/roslyn/issues/12052 - compilation.VerifyDiagnostics( - // (15,47): error CS0412: 'y1': a parameter or local variable cannot have the same name as a method type parameter - // from x2 in new[] { 1 is var y1 ? y1 : 1 } - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y1").WithArguments("y1").WithLocation(15, 47), - // (23,36): error CS0412: 'y2': a parameter or local variable cannot have the same name as a method type parameter - // on 2 is var y2 ? y2 : 0 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y2").WithArguments("y2").WithLocation(23, 36), - // (33,40): error CS0412: 'y3': a parameter or local variable cannot have the same name as a method type parameter - // equals 3 is var y3 ? y3 : 0 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y3").WithArguments("y3").WithLocation(33, 40), - // (40,34): error CS0412: 'y4': a parameter or local variable cannot have the same name as a method type parameter - // where 4 is var y4 && y4 == 1 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y4").WithArguments("y4").WithLocation(40, 34), - // (47,36): error CS0412: 'y5': a parameter or local variable cannot have the same name as a method type parameter - // orderby 5 is var y5 && y5 > 1, - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y5").WithArguments("y5").WithLocation(47, 36), - // (56,36): error CS0412: 'y6': a parameter or local variable cannot have the same name as a method type parameter - // 6 is var y6 && y6 > 1 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y6").WithArguments("y6").WithLocation(56, 36), - // (63,34): error CS0412: 'y7': a parameter or local variable cannot have the same name as a method type parameter - // group 7 is var y7 && y7 == 3 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y7").WithArguments("y7").WithLocation(63, 34), - // (71,31): error CS0412: 'y8': a parameter or local variable cannot have the same name as a method type parameter - // by 8 is var y8 && y8 == 3; - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y8").WithArguments("y8").WithLocation(71, 31), - // (77,37): error CS0412: 'y9': a parameter or local variable cannot have the same name as a method type parameter - // let x4 = 9 is var y9 && y9 > 0 - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y9").WithArguments("y9").WithLocation(77, 37), - // (84,36): error CS0412: 'y10': a parameter or local variable cannot have the same name as a method type parameter - // select 10 is var y10 && y10 > 0; - Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y10").WithArguments("y10").WithLocation(84, 36) - ); + CompileAndVerify(compilation, expectedOutput: "12").VerifyDiagnostics(); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - for (int i = 1; i < 11; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).Single(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); - Assert.Equal(i == 10 ? 1 : 2, yRef.Length); - - switch (i) - { - case 4: - case 6: - VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyNotAPatternLocal(model, yRef[1]); - break; - case 8: - VerifyModelForDeclarationPattern(model, yDecl, yRef[1]); - // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. - //VerifyNotAPatternLocal(model, yRef[0]); - break; - case 10: - VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); - break; - default: - VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); - VerifyNotAPatternLocal(model, yRef[1]); - break; - } - } + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); } [Fact] - public void Query_01() + public void Throw_01() { var source = @" -using System.Linq; - public class X { public static void Main() { - Test1(); + Test(); } - static void Test1() + static void Test() { - var res = from x1 in new[] { 1 is var y1 && Print(y1) ? 1 : 0} - from x2 in new[] { 2 is var y2 && Print(y2) ? 1 : 0} - join x3 in new[] { 3 is var y3 && Print(y3) ? 1 : 0} - on 4 is var y4 && Print(y4) ? 1 : 0 - equals 5 is var y5 && Print(y5) ? 1 : 0 - where 6 is var y6 && Print(y6) - orderby 7 is var y7 && Print(y7), - 8 is var y8 && Print(y8) - group 9 is var y9 && Print(y9) - by 10 is var y10 && Print(y10) - into g - let x11 = 11 is var y11 && Print(y11) - select 12 is var y12 && Print(y12); - - res.ToArray(); + try + { + throw Dummy(""throw"" is var x1, x1); + } + catch + { + } } - static bool Print(object x) + static System.Exception Dummy(object y, object z) { - System.Console.WriteLine(x); - return true; + System.Console.WriteLine(z); + return new System.ArgumentException(); } } "; - var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -3 -5 -2 -4 -6 -7 -8 -10 -9 -11 -12 -"); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: @"throw"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - for (int i = 1; i < 13; i++) - { - var id = "y" + i; - var yDecl = GetPatternDeclarations(tree, id).Single(); - var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); - VerifyModelForDeclarationPattern(model, yDecl, yRef); - } + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ScopeOfPatternVariables_ExpressionBodiedLocalFunctions_01() + public void Throw_02() { var source = @" @@ -3548,189 +2201,52 @@ public class X { public static void Main() { + Test(true); + Test(false); } - bool Dummy(params object[] x) {return true;} - - void Test1() - { - void f(object o) => let x1 = o; - f(null); - } - - void Test2() - { - void f(object o) => let var x2 = o; - f(null); - } - - void Test3() - { - bool f (object o) => o is int x3 && x3 > 0; - f(null); - } - - void Test4() - { - bool f (object o) => x4 && o is int x4; - f(null); - } - - void Test5() - { - bool f (object o1, object o2) => o1 is int x5 && - o2 is int x5 && - x5 > 0; - f(null, null); - } - - void Test6() - { - bool f1 (object o) => o is int x6 && x6 > 0; bool f2 (object o) => o is int x6 && x6 > 0; - f1(null); - f2(null); - } - - void Test7() - { - Dummy(x7, 1); - - bool f (object o) => o is int x7 && x7 > 0; - - Dummy(x7, 2); - f(null); - } - - void Test11() + static void Test(bool val) { - var x11 = 11; - Dummy(x11); - bool f (object o) => o is int x11 && - x11 > 0; - f(null); - } + try + { + if (val) + throw Dummy(""throw 1"" is var x1, x1); - void Test12() - { - bool f (object o) => o is int x12 && - x12 > 0; - var x12 = 11; - Dummy(x12); - f(null); + if (!val) + { + throw Dummy(""throw 2"" is var x1, x1); + } + } + catch + { + } } - System.Action Test13() + static System.Exception Dummy(object y, object z) { - return () => - { - bool f (object o) => o is int x13 && x13 > 0; - f(null); - }; + System.Console.WriteLine(z); + return new System.ArgumentException(); } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (12,33): error CS1002: ; expected - // void f(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(12, 33), - // (18,33): error CS1002: ; expected - // void f(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(18, 33), - // (12,29): error CS0103: The name 'let' does not exist in the current context - // void f(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(12, 29), - // (12,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // void f(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(12, 29), - // (12,33): error CS0103: The name 'x1' does not exist in the current context - // void f(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(12, 33), - // (12,38): error CS0103: The name 'o' does not exist in the current context - // void f(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(12, 38), - // (18,29): error CS0103: The name 'let' does not exist in the current context - // void f(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(18, 29), - // (18,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // void f(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(18, 29), - // (18,42): error CS0103: The name 'o' does not exist in the current context - // void f(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(18, 42), - // (30,30): error CS0841: Cannot use local variable 'x4' before it is declared - // bool f (object o) => x4 && o is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(30, 30), - // (37,52): error CS0128: A local variable named 'x5' is already defined in this scope - // o2 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(37, 52), - // (38,42): error CS0165: Use of unassigned local variable 'x5' - // x5 > 0; - Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(38, 42), - // (51,15): error CS0103: The name 'x7' does not exist in the current context - // Dummy(x7, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(51, 15), - // (55,15): error CS0103: The name 'x7' does not exist in the current context - // Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(55, 15), - // (63,39): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // bool f (object o) => o is int x11 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(63, 39), - // (70,39): error CS0136: A local or parameter named 'x12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // bool f (object o) => o is int x12 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x12").WithArguments("x12").WithLocation(70, 39) - ); + CompileAndVerify(compilation, expectedOutput: +@"throw 1 +throw 2"); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyNotInScope(model, x7Ref[0]); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyNotAPatternLocal(model, x11Ref[0]); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[1]); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[0]); - VerifyNotAPatternLocal(model, x12Ref[1]); - - var x13Decl = GetPatternDeclarations(tree, "x13").Single(); - var x13Ref = GetReferences(tree, "x13").Single(); - VerifyModelForDeclarationPattern(model, x13Decl, x13Ref); + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); } [Fact] - public void ExpressionBodiedLocalFunctions_01() + public void Catch_01() { var source = @" @@ -3738,29 +2254,31 @@ public class X { public static void Main() { - System.Console.WriteLine(Test1()); - } - - static bool Test1() - { - bool f() => 1 is int x1 && Dummy(x1); - return f(); + try + { + throw new System.InvalidOperationException(); + } + catch (System.Exception e) when (Dummy(e is var x1, x1)) + { + System.Console.WriteLine(x1.GetType()); + } } - static bool Dummy(int x) + static bool Dummy(object y, object z) { - System.Console.WriteLine(x); + System.Console.WriteLine(z.GetType()); return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"1 -True"); + CompileAndVerify(compilation, expectedOutput: +@"System.InvalidOperationException +System.InvalidOperationException"); } [Fact] - public void ScopeOfPatternVariables_ExpressionBodiedFunctions_01() + public void Catch_02() { var source = @" @@ -3768,129 +2286,38 @@ public class X { public static void Main() { - } - - - void Test1(object o) => let x1 = o; - - void Test2(object o) => let var x2 = o; - - bool Test3(object o) => o is int x3 && x3 > 0; - - bool Test4(object o) => x4 && o is int x4; - - bool Test5(object o1, object o2) => o1 is int x5 && - o2 is int x5 && - x5 > 0; - - bool Test61 (object o) => o is int x6 && x6 > 0; bool Test62 (object o) => o is int x6 && x6 > 0; - - bool Test71(object o) => o is int x7 && x7 > 0; - void Test72() => Dummy(x7, 2); - void Test73() { Dummy(x7, 3); } + try + { + throw new System.InvalidOperationException(); + } + catch (System.Exception e) when (Dummy(e is var x1, x1)) + { + System.Action d = () => + { + System.Console.WriteLine(x1.GetType()); + }; - bool Test11(object x11) => 1 is int x11 && - x11 > 0; + System.Console.WriteLine(x1.GetType()); + d(); + } + } - bool Dummy(params object[] x) {return true;} + static bool Dummy(object y, object z) + { + System.Console.WriteLine(z.GetType()); + return true; + } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (9,33): error CS1002: ; expected - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(9, 33), - // (9,36): error CS1519: Invalid token '=' in class, struct, or interface member declaration - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 36), - // (9,36): error CS1519: Invalid token '=' in class, struct, or interface member declaration - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 36), - // (9,39): error CS1519: Invalid token ';' in class, struct, or interface member declaration - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(9, 39), - // (9,39): error CS1519: Invalid token ';' in class, struct, or interface member declaration - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(9, 39), - // (11,33): error CS1002: ; expected - // void Test2(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(11, 33), - // (11,33): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code - // void Test2(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(11, 33), - // (11,42): error CS0103: The name 'o' does not exist in the current context - // void Test2(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(11, 42), - // (9,29): error CS0103: The name 'let' does not exist in the current context - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(9, 29), - // (9,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // void Test1(object o) => let x1 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(9, 29), - // (11,29): error CS0103: The name 'let' does not exist in the current context - // void Test2(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(11, 29), - // (11,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement - // void Test2(object o) => let var x2 = o; - Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(11, 29), - // (15,29): error CS0841: Cannot use local variable 'x4' before it is declared - // bool Test4(object o) => x4 && o is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(15, 29), - // (18,52): error CS0128: A local variable named 'x5' is already defined in this scope - // o2 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 52), - // (19,42): error CS0165: Use of unassigned local variable 'x5' - // x5 > 0; - Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(19, 42), - // (24,28): error CS0103: The name 'x7' does not exist in the current context - // void Test72() => Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(24, 28), - // (25,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(25, 27), - // (27,41): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // bool Test11(object x11) => 1 is int x11 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(27, 41) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").Single(); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + CompileAndVerify(compilation, expectedOutput: +@"System.InvalidOperationException +System.InvalidOperationException +System.InvalidOperationException"); } [Fact] - public void ExpressionBodiedFunctions_01() + public void Catch_03() { var source = @" @@ -3898,25 +2325,41 @@ public class X { public static void Main() { - System.Console.WriteLine(Test1()); - } + try + { + throw new System.InvalidOperationException(); + } + catch (System.Exception e) when (Dummy(e is var x1, x1)) + { + System.Action d = () => + { + e = new System.NullReferenceException(); + System.Console.WriteLine(x1.GetType()); + }; - static bool Test1() => 1 is int x1 && Dummy(x1); + System.Console.WriteLine(x1.GetType()); + d(); + System.Console.WriteLine(e.GetType()); + } + } - static bool Dummy(int x) + static bool Dummy(object y, object z) { - System.Console.WriteLine(x); + System.Console.WriteLine(z.GetType()); return true; } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"1 -True"); + CompileAndVerify(compilation, expectedOutput: +@"System.InvalidOperationException +System.InvalidOperationException +System.InvalidOperationException +System.NullReferenceException"); } [Fact] - public void ScopeOfPatternVariables_ExpressionBodiedProperties_01() + public void Catch_04() { var source = @" @@ -3924,10533 +2367,468 @@ public class X { public static void Main() { - } - - - bool Test1 => let x1 = 11; - - bool this[int o] => let var x2 = o; - - bool Test3 => 3 is int x3 && x3 > 0; - - bool Test4 => x4 && 4 is int x4; - - bool Test5 => 51 is int x5 && - 52 is int x5 && - x5 > 0; - - bool Test61 => 6 is int x6 && x6 > 0; bool Test62 => 6 is int x6 && x6 > 0; - - bool Test71 => 7 is int x7 && x7 > 0; - bool Test72 => Dummy(x7, 2); - void Test73() { Dummy(x7, 3); } + try + { + throw new System.InvalidOperationException(); + } + catch (System.Exception e) when (Dummy(e is var x1, x1)) + { + System.Action d = () => + { + e = new System.NullReferenceException(); + }; - bool this[object x11] => 1 is int x11 && - x11 > 0; + System.Console.WriteLine(x1.GetType()); + d(); + System.Console.WriteLine(e.GetType()); + } + } - bool Dummy(params object[] x) {return true;} + static bool Dummy(object y, object z) + { + System.Console.WriteLine(z.GetType()); + return true; + } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (9,23): error CS1002: ; expected - // bool Test1 => let x1 = 11; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(9, 23), - // (9,26): error CS1519: Invalid token '=' in class, struct, or interface member declaration - // bool Test1 => let x1 = 11; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 26), - // (9,26): error CS1519: Invalid token '=' in class, struct, or interface member declaration - // bool Test1 => let x1 = 11; - Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 26), - // (11,29): error CS1002: ; expected - // bool this[int o] => let var x2 = o; - Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(11, 29), - // (11,29): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code - // bool this[int o] => let var x2 = o; - Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(11, 29), - // (11,38): error CS0103: The name 'o' does not exist in the current context - // bool this[int o] => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(11, 38), - // (9,19): error CS0103: The name 'let' does not exist in the current context - // bool Test1 => let x1 = 11; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(9, 19), - // (11,25): error CS0103: The name 'let' does not exist in the current context - // bool this[int o] => let var x2 = o; - Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(11, 25), - // (15,19): error CS0841: Cannot use local variable 'x4' before it is declared - // bool Test4 => x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(15, 19), - // (18,29): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 29), - // (24,26): error CS0103: The name 'x7' does not exist in the current context - // bool Test72 => Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(24, 26), - // (25,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(25, 27), - // (27,39): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // bool this[object x11] => 1 is int x11 && - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(27, 39) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + CompileAndVerify(compilation, expectedOutput: +@"System.InvalidOperationException +System.InvalidOperationException +System.NullReferenceException"); + } - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + [Fact] + public void Labeled_01() + { + var text = @" +public class Cls +{ + public static void Main() + { +a: Test1(2 is var x1); + System.Console.WriteLine(x1); + } - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + static object Test1(bool x) + { + return null; + } +}"; + var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics( + // (6,1): warning CS0164: This label has not been referenced + // a: Test1(2 is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(6, 1) + ); - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").Single(); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } [Fact] - public void ExpressionBodiedProperties_01() + public void Labeled_02() { - var source = -@" -public class X + var text = @" +public class Cls { public static void Main() { - System.Console.WriteLine(Test1); - System.Console.WriteLine(new X()[0]); + Test0(); } - static bool Test1 => 2 is int x1 && Dummy(x1); + static object Test0() + { + bool test = true; - bool this[object x] => 1 is int x1 && Dummy(x1); + if (test) + { +a: Test2(2 is var x1, x1); + } - static bool Dummy(int x) + return null; + } + + static object Test2(object x, object y) { - System.Console.WriteLine(x); - return true; + System.Console.Write(y); + return x; } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"2 -True -1 -True"); +}"; + var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics( + // (15,1): warning CS0164: This label has not been referenced + // a: Test2(2 is var x1, x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(15, 1) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); } - [Fact] - public void FieldInitializers_01() + [Fact, WorkItem(10465, "https://github.com/dotnet/roslyn/issues/10465")] + public void Constants_Fail() { var source = @" +using System; public class X { public static void Main() { - System.Console.WriteLine(Test1); - } - - static bool Test1 = 1 is int x1 && Dummy(x1); + Console.WriteLine(1L is string); // warning: type mismatch + Console.WriteLine(1 is int[]); // warning: expression is never of the provided type - static bool Dummy(int x) - { - System.Console.WriteLine(x); - return true; + Console.WriteLine(1L is string s); // error: type mismatch + Console.WriteLine(1 is int[] a); // error: expression is never of the provided type } } "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); -#if ALLOW_IN_FIELD_INITIALIZER - CompileAndVerify(compilation, expectedOutput: @"1 -True"); -#else compilation.VerifyDiagnostics( - // (9,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // static bool Test1 = 1 is int x1 && Dummy(x1); - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(9, 30) + // (7,27): warning CS0184: The given expression is never of the provided ('string') type + // Console.WriteLine(1L is string); // warning: type mismatch + Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1L is string").WithArguments("string").WithLocation(7, 27), + // (8,27): warning CS0184: The given expression is never of the provided ('int[]') type + // Console.WriteLine(1 is int[]); // warning: expression is never of the provided type + Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is int[]").WithArguments("int[]").WithLocation(8, 27), + // (10,33): error CS8121: An expression of type long cannot be handled by a pattern of type string. + // Console.WriteLine(1L is string s); // error: type mismatch + Diagnostic(ErrorCode.ERR_PatternWrongType, "string").WithArguments("long", "string").WithLocation(10, 33), + // (11,32): error CS8121: An expression of type int cannot be handled by a pattern of type int[]. + // Console.WriteLine(1 is int[] a); // error: expression is never of the provided type + Diagnostic(ErrorCode.ERR_PatternWrongType, "int[]").WithArguments("int", "int[]").WithLocation(11, 32) ); -#endif } - [Fact, WorkItem(10487, "https://github.com/dotnet/roslyn/issues/10487")] - public void FieldInitializers_03() + [Fact, WorkItem(10465, "https://github.com/dotnet/roslyn/issues/10465")] + public void Types_Pass() { var source = @" +using System; public class X { public static void Main() { - System.Console.WriteLine(Test1); - new X().M(); - } - void M() - { - System.Console.WriteLine(Test2); - } - - static bool Test1 = 1 is int x1 && Dummy(() => x1); - bool Test2 = 2 is int x1 && Dummy(() => x1); - - static bool Dummy(System.Func x) - { - System.Console.WriteLine(x()); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); -#if ALLOW_IN_FIELD_INITIALIZER - CompileAndVerify(compilation, expectedOutput: @"1 -True -2 -True"); -#else - compilation.VerifyDiagnostics( - // (14,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // static bool Test1 = 1 is int x1 && Dummy(() => x1); - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(14, 30), - // (15,23): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test2 = 2 is int x1 && Dummy(() => x1); - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(15, 23) - ); -#endif - } - - [Fact] - public void ScopeOfPatternVariables_FieldInitializers_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Test3 = 3 is int x3 && x3 > 0; - - bool Test4 = x4 && 4 is int x4; - - bool Test5 = 51 is int x5 && - 52 is int x5 && - x5 > 0; - - bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - - bool Test71 = 7 is int x7 && x7 > 0; - bool Test72 = Dummy(x7, 2); - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,23): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test3 = 3 is int x3 && x3 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 23), - // (10,18): error CS0841: Cannot use local variable 'x4' before it is declared - // bool Test4 = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 18), - // (10,29): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test4 = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 29), - // (12,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test5 = 51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 24), - // (13,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 24), - // (13,28): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 28), - // (16,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 24), - // (16,56): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 56), - // (18,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test71 = 7 is int x7 && x7 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 24), - // (19,25): error CS0103: The name 'x7' does not exist in the current context - // bool Test72 = Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 25), - // (20,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_FieldInitializers_02() - { - var source = -@" -public enum X -{ - Test3 = 3 is int x3 ? x3 : 0, - - Test4 = x4 && 4 is int x4 ? 1 : 0, - - Test5 = 51 is int x5 && - 52 is int x5 && - x5 > 0 ? 1 : 0, - - Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, - - Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, - Test72 = x7, -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugDll); - compilation.VerifyDiagnostics( - // (6,13): error CS0841: Cannot use local variable 'x4' before it is declared - // Test4 = x4 && 4 is int x4 ? 1 : 0, - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(6, 13), - // (6,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test4 = x4 && 4 is int x4 ? 1 : 0, - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(6, 24), - // (8,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test5 = 51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(8, 19), - // (9,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(9, 19), - // (9,23): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(9, 23), - // (12,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(12, 19), - // (12,14): error CS0133: The expression being assigned to 'X.Test61' must be constant - // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0 ? 1 : 0").WithArguments("X.Test61").WithLocation(12, 14), - // (12,59): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(12, 59), - // (12,54): error CS0133: The expression being assigned to 'X.Test62' must be constant - // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0 ? 1 : 0").WithArguments("X.Test62").WithLocation(12, 54), - // (14,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(14, 19), - // (14,14): error CS0133: The expression being assigned to 'X.Test71' must be constant - // Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, - Diagnostic(ErrorCode.ERR_NotConstantExpression, "7 is int x7 && x7 > 0 ? 1 : 0").WithArguments("X.Test71").WithLocation(14, 14), - // (15,14): error CS0103: The name 'x7' does not exist in the current context - // Test72 = x7, - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 14), - // (4,18): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // Test3 = 3 is int x3 ? x3 : 0, - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(4, 18), - // (4,13): error CS0133: The expression being assigned to 'X.Test3' must be constant - // Test3 = 3 is int x3 ? x3 : 0, - Diagnostic(ErrorCode.ERR_NotConstantExpression, "3 is int x3 ? x3 : 0").WithArguments("X.Test3").WithLocation(4, 13) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_FieldInitializers_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - const bool Test3 = 3 is int x3 && x3 > 0; - - const bool Test4 = x4 && 4 is int x4; - - const bool Test5 = 51 is int x5 && - 52 is int x5 && - x5 > 0; - - const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - - const bool Test71 = 7 is int x7 && x7 > 0; - const bool Test72 = x7 > 2; - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,29): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test3 = 3 is int x3 && x3 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 29), - // (8,24): error CS0133: The expression being assigned to 'X.Test3' must be constant - // const bool Test3 = 3 is int x3 && x3 > 0; - Diagnostic(ErrorCode.ERR_NotConstantExpression, "3 is int x3 && x3 > 0").WithArguments("X.Test3").WithLocation(8, 24), - // (10,24): error CS0841: Cannot use local variable 'x4' before it is declared - // const bool Test4 = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 24), - // (10,35): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test4 = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 35), - // (12,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test5 = 51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 30), - // (13,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 30), - // (13,34): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 34), - // (16,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 30), - // (16,25): error CS0133: The expression being assigned to 'X.Test61' must be constant - // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0").WithArguments("X.Test61").WithLocation(16, 25), - // (16,62): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 62), - // (16,57): error CS0133: The expression being assigned to 'X.Test62' must be constant - // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0").WithArguments("X.Test62").WithLocation(16, 57), - // (18,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // const bool Test71 = 7 is int x7 && x7 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 30), - // (18,25): error CS0133: The expression being assigned to 'X.Test71' must be constant - // const bool Test71 = 7 is int x7 && x7 > 0; - Diagnostic(ErrorCode.ERR_NotConstantExpression, "7 is int x7 && x7 > 0").WithArguments("X.Test71").WithLocation(18, 25), - // (19,25): error CS0103: The name 'x7' does not exist in the current context - // const bool Test72 = x7 > 2; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 25), - // (20,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void PropertyInitializers_01() - { - var source = -@" -public class X -{ - public static void Main() - { - System.Console.WriteLine(Test1); - } - - static bool Test1 {get;} = 1 is int x1 && Dummy(x1); - - static bool Dummy(int x) - { - System.Console.WriteLine(x); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); -#if ALLOW_IN_FIELD_INITIALIZER - CompileAndVerify(compilation, expectedOutput: @"1 -True"); -#else - compilation.VerifyDiagnostics( - // (9,37): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // static bool Test1 {get;} = 1 is int x1 && Dummy(x1); - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(9, 37) - ); -#endif - } - - [Fact] - public void ScopeOfPatternVariables_PropertyInitializers_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Test3 {get;} = 3 is int x3 && x3 > 0; - - bool Test4 {get;} = x4 && 4 is int x4; - - bool Test5 {get;} = 51 is int x5 && - 52 is int x5 && - x5 > 0; - - bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; - - bool Test71 {get;} = 7 is int x7 && x7 > 0; - bool Test72 {get;} = Dummy(x7, 2); - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test3 {get;} = 3 is int x3 && x3 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 30), - // (10,25): error CS0841: Cannot use local variable 'x4' before it is declared - // bool Test4 {get;} = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 25), - // (10,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test4 {get;} = x4 && 4 is int x4; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 36), - // (12,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test5 {get;} = 51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 31), - // (13,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 24), - // (13,28): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 28), - // (16,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 31), - // (16,75): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 75), - // (18,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // bool Test71 {get;} = 7 is int x7 && x7 > 0; - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 31), - // (19,32): error CS0103: The name 'x7' does not exist in the current context - // bool Test72 {get;} = Dummy(x7, 2); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 32), - // (20,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_ParameterDefault_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Test3(bool p = 3 is int x3 && x3 > 0) - {} - - void Test4(bool p = x4 && 4 is int x4) - {} - - void Test5(bool p = 51 is int x5 && - 52 is int x5 && - x5 > 0) - {} - - void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) - {} - - void Test71(bool p = 7 is int x7 && x7 > 0) - { - } - - void Test72(bool p = x7 > 2) - {} - - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,25): error CS1736: Default parameter value for 'p' must be a compile-time constant - // void Test3(bool p = 3 is int x3 && x3 > 0) - Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "3 is int x3 && x3 > 0").WithArguments("p").WithLocation(8, 25), - // (11,25): error CS0841: Cannot use local variable 'x4' before it is declared - // void Test4(bool p = x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(11, 25), - // (11,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool' - // void Test4(bool p = x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(11, 21), - // (15,35): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(15, 35), - // (14,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool' - // void Test5(bool p = 51 is int x5 && - Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(14, 21), - // (19,27): error CS1736: Default parameter value for 'p1' must be a compile-time constant - // void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "6 is int x6 && x6 > 0").WithArguments("p1").WithLocation(19, 27), - // (19,60): error CS1736: Default parameter value for 'p2' must be a compile-time constant - // void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "6 is int x6 && x6 > 0").WithArguments("p2").WithLocation(19, 60), - // (22,26): error CS1736: Default parameter value for 'p' must be a compile-time constant - // void Test71(bool p = 7 is int x7 && x7 > 0) - Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "7 is int x7 && x7 > 0").WithArguments("p").WithLocation(22, 26), - // (26,26): error CS0103: The name 'x7' does not exist in the current context - // void Test72(bool p = x7 > 2) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(26, 26), - // (29,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(29, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); - VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_Attribute_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - [Test(p = 3 is int x3 && x3 > 0)] - [Test(p = x4 && 4 is int x4)] - [Test(p = 51 is int x5 && - 52 is int x5 && - x5 > 0)] - [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] - [Test(p = 7 is int x7 && x7 > 0)] - [Test(p = x7 > 2)] - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} - -class Test : System.Attribute -{ - public bool p {get; set;} - public bool p1 {get; set;} - public bool p2 {get; set;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,15): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - // [Test(p = 3 is int x3 && x3 > 0)] - Diagnostic(ErrorCode.ERR_BadAttributeArgument, "3 is int x3 && x3 > 0").WithLocation(8, 15), - // (9,15): error CS0841: Cannot use local variable 'x4' before it is declared - // [Test(p = x4 && 4 is int x4)] - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(9, 15), - // (11,25): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(11, 25), - // (13,53): error CS0128: A local variable named 'x6' is already defined in this scope - // [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(13, 53), - // (13,16): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - // [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] - Diagnostic(ErrorCode.ERR_BadAttributeArgument, "6 is int x6 && x6 > 0").WithLocation(13, 16), - // (14,15): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - // [Test(p = 7 is int x7 && x7 > 0)] - Diagnostic(ErrorCode.ERR_BadAttributeArgument, "7 is int x7 && x7 > 0").WithLocation(14, 15), - // (15,15): error CS0103: The name 'x7' does not exist in the current context - // [Test(p = x7 > 2)] - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 15), - // (16,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(16, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_Attribute_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - [Test(3 is int x3 && x3 > 0)] - [Test(x4 && 4 is int x4)] - [Test(51 is int x5 && - 52 is int x5 && - x5 > 0)] - [Test(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0)] - [Test(7 is int x7 && x7 > 0)] - [Test(x7 > 2)] - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} - -class Test : System.Attribute -{ - public Test(bool p) {} - public Test(bool p1, bool p2) {} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,11): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - // [Test(3 is int x3 && x3 > 0)] - Diagnostic(ErrorCode.ERR_BadAttributeArgument, "3 is int x3 && x3 > 0").WithLocation(8, 11), - // (9,11): error CS0841: Cannot use local variable 'x4' before it is declared - // [Test(x4 && 4 is int x4)] - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(9, 11), - // (11,21): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(11, 21), - // (13,43): error CS0128: A local variable named 'x6' is already defined in this scope - // [Test(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0)] - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(13, 43), - // (14,11): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - // [Test(7 is int x7 && x7 > 0)] - Diagnostic(ErrorCode.ERR_BadAttributeArgument, "7 is int x7 && x7 > 0").WithLocation(14, 11), - // (15,11): error CS0103: The name 'x7' does not exist in the current context - // [Test(x7 > 2)] - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 11), - // (16,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(16, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_ConstructorInitializers_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - X(byte x) - : this(3 is int x3 && x3 > 0) - {} - - X(sbyte x) - : this(x4 && 4 is int x4) - {} - - X(short x) - : this(51 is int x5 && - 52 is int x5 && - x5 > 0) - {} - - X(ushort x) - : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - {} - - X(int x) - : this(7 is int x7 && x7 > 0) - {} - X(uint x) - : this(x7, 2) - {} - void Test73() { Dummy(x7, 3); } - - X(params object[] x) {} - bool Dummy(params object[] x) {return true;} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (9,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(3 is int x3 && x3 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 21), - // (13,16): error CS0841: Cannot use local variable 'x4' before it is declared - // : this(x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(13, 16), - // (13,27): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 27), - // (17,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(17, 22), - // (18,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(18, 22), - // (18,26): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 26), - // (23,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 21), - // (23,44): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 44), - // (23,48): error CS0128: A local variable named 'x6' is already defined in this scope - // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(23, 48), - // (27,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : this(7 is int x7 && x7 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(27, 21), - // (30,16): error CS0103: The name 'x7' does not exist in the current context - // : this(x7, 2) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(30, 16), - // (32,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(32, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_ConstructorInitializers_02() - { - var source = -@" -public class X : Y -{ - public static void Main() - { - } - - X(byte x) - : base(3 is int x3 && x3 > 0) - {} - - X(sbyte x) - : base(x4 && 4 is int x4) - {} - - X(short x) - : base(51 is int x5 && - 52 is int x5 && - x5 > 0) - {} - - X(ushort x) - : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - {} - - X(int x) - : base(7 is int x7 && x7 > 0) - {} - X(uint x) - : base(x7, 2) - {} - void Test73() { Dummy(x7, 3); } - - bool Dummy(params object[] x) {return true;} -} - -public class Y -{ - public Y(params object[] x) {} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (9,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(3 is int x3 && x3 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 21), - // (13,16): error CS0841: Cannot use local variable 'x4' before it is declared - // : base(x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(13, 16), - // (13,27): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(x4 && 4 is int x4) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 27), - // (17,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(51 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(17, 22), - // (18,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(18, 22), - // (18,26): error CS0128: A local variable named 'x5' is already defined in this scope - // 52 is int x5 && - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 26), - // (23,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 21), - // (23,44): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 44), - // (23,48): error CS0128: A local variable named 'x6' is already defined in this scope - // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(23, 48), - // (27,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // : base(7 is int x7 && x7 > 0) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(27, 21), - // (30,16): error CS0103: The name 'x7' does not exist in the current context - // : base(x7, 2) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(30, 16), - // (32,27): error CS0103: The name 'x7' does not exist in the current context - // void Test73() { Dummy(x7, 3); } - Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(32, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); - var x5Ref = GetReferences(tree, "x5").Single(); - Assert.Equal(2, x5Decl.Length); - VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); - - var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Decl.Length); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(3, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotInScope(model, x7Ref[1]); - VerifyNotInScope(model, x7Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_ConstructorInitializers_03() - { - var source = -@"using System; -public class X -{ - public static void Main() - { - new D(1); - new D(10); - new D(1.2); - } -} -class D -{ - public D(object o) : this(o is int x && x >= 5) - { - Console.WriteLine(x); - } - - public D(bool b) { Console.WriteLine(b); } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (15,27): error CS0103: The name 'x' does not exist in the current context - // Console.WriteLine(x); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(15, 27), - // (13,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // public D(object o) : this(o is int x && x >= 5) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x").WithLocation(13, 36) - ); - } - - [Fact] - public void ScopeOfPatternVariables_ConstructorInitializers_04() - { - var source = -@"using System; -public class X -{ - public static void Main() - { - new D(1); - new D(10); - new D(1.2); - } -} -class D : C -{ - public D(object o) : base(o is int x && x >= 5) - { - Console.WriteLine(x); - } -} - -class C -{ - public C(bool b) { Console.WriteLine(b); } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (15,27): error CS0103: The name 'x' does not exist in the current context - // Console.WriteLine(x); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(15, 27), - // (13,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // public D(object o) : base(o is int x && x >= 5) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x").WithLocation(13, 36) - ); - } - [Fact] - public void ConstructorInitializers_01() - { - var source = -@" -public class X -{ - public static void Main() - { - var x = new D(); - } -} - -class D : C -{ - public D(object o) : base(2 is int x1 && Dummy(x1)) - { - System.Console.WriteLine(o); - } - - public D() : this(1 is int x1 && Dummy(x1)) - { - } - - static bool Dummy(int x) - { - System.Console.WriteLine(x); - return true; - } -} - -class C -{ - public C(object b) - { - System.Console.WriteLine(b); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); -#if ALLOW_IN_CONSTRUCTOR_INITIALIZER - CompileAndVerify(compilation, expectedOutput: -@"1 -2 -True -True"); -#else - compilation.VerifyDiagnostics( - // (12,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // public D(object o) : base(2 is int x1 && Dummy(x1)) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(12, 36), - // (17,28): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. - // public D() : this(1 is int x1 && Dummy(x1)) - Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(17, 28) - ); -#endif - } - - [Fact] - public void ScopeOfPatternVariables_SwitchLabelGuard_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) { return true; } - - void Test1(int val) - { - switch (val) - { - case 0 when Dummy(true is var x1, x1): - Dummy(x1); - break; - case 1 when Dummy(true is var x1, x1): - Dummy(x1); - break; - case 2 when Dummy(true is var x1, x1): - Dummy(x1); - break; - } - } - - void Test2(int val) - { - switch (val) - { - case 0 when Dummy(x2, true is var x2): - Dummy(x2); - break; - } - } - - void Test3(int x3, int val) - { - switch (val) - { - case 0 when Dummy(true is var x3, x3): - Dummy(x3); - break; - } - } - - void Test4(int val) - { - var x4 = 11; - switch (val) - { - case 0 when Dummy(true is var x4, x4): - Dummy(x4); - break; - case 1 when Dummy(x4): Dummy(x4); break; - } - } - - void Test5(int val) - { - switch (val) - { - case 0 when Dummy(true is var x5, x5): - Dummy(x5); - break; - } - - var x5 = 11; - Dummy(x5); - } - - //void Test6(int val) - //{ - // let x6 = 11; - // switch (val) - // { - // case 0 when Dummy(x6): - // Dummy(x6); - // break; - // case 1 when Dummy(true is var x6, x6): - // Dummy(x6); - // break; - // } - //} - - //void Test7(int val) - //{ - // switch (val) - // { - // case 0 when Dummy(true is var x7, x7): - // Dummy(x7); - // break; - // } - - // let x7 = 11; - // Dummy(x7); - //} - - void Test8(int val) - { - switch (val) - { - case 0 when Dummy(true is var x8, x8, false is var x8, x8): - Dummy(x8); - break; - } - } - - void Test9(int val) - { - switch (val) - { - case 0 when Dummy(x9): - int x9 = 9; - Dummy(x9); - break; - case 2 when Dummy(x9 = 9): - Dummy(x9); - break; - case 1 when Dummy(true is var x9, x9): - Dummy(x9); - break; - } - } - - //void Test10(int val) - //{ - // switch (val) - // { - // case 1 when Dummy(true is var x10, x10): - // Dummy(x10); - // break; - // case 0 when Dummy(x10): - // let x10 = 10; - // Dummy(x10); - // break; - // case 2 when Dummy(x10 = 10, x10): - // Dummy(x10); - // break; - // } - //} - - void Test11(int val) - { - switch (x11 ? val : 0) - { - case 0 when Dummy(x11): - Dummy(x11, 0); - break; - case 1 when Dummy(true is var x11, x11): - Dummy(x11, 1); - break; - } - } - - void Test12(int val) - { - switch (x12 ? val : 0) - { - case 0 when Dummy(true is var x12, x12): - Dummy(x12, 0); - break; - case 1 when Dummy(x12): - Dummy(x12, 1); - break; - } - } - - void Test13() - { - switch (1 is var x13 ? x13 : 0) - { - case 0 when Dummy(x13): - Dummy(x13); - break; - case 1 when Dummy(true is var x13, x13): - Dummy(x13); - break; - } - } - - void Test14(int val) - { - switch (val) - { - case 1 when Dummy(true is var x14, x14): - Dummy(x14); - Dummy(true is var x14, x14); - Dummy(x14); - break; - } - } - - void Test15(int val) - { - switch (val) - { - case 0 when Dummy(true is var x15, x15): - case 1 when Dummy(true is var x15, x15): - Dummy(x15); - break; - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (30,31): error CS0841: Cannot use local variable 'x2' before it is declared - // case 0 when Dummy(x2, true is var x2): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(30, 31), - // (40,43): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(true is var x3, x3): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(40, 43), - // (51,43): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(true is var x4, x4): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(51, 43), - // (62,43): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(true is var x5, x5): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(62, 43), - // (102,64): error CS0128: A local variable named 'x8' is already defined in this scope - // case 0 when Dummy(true is var x8, x8, false is var x8, x8): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(102, 64), - // (112,31): error CS0841: Cannot use local variable 'x9' before it is declared - // case 0 when Dummy(x9): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(112, 31), - // (119,43): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(true is var x9, x9): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(119, 43), - // (144,17): error CS0103: The name 'x11' does not exist in the current context - // switch (x11 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(144, 17), - // (146,31): error CS0103: The name 'x11' does not exist in the current context - // case 0 when Dummy(x11): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(146, 31), - // (147,23): error CS0103: The name 'x11' does not exist in the current context - // Dummy(x11, 0); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(147, 23), - // (157,17): error CS0103: The name 'x12' does not exist in the current context - // switch (x12 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(157, 17), - // (162,31): error CS0103: The name 'x12' does not exist in the current context - // case 1 when Dummy(x12): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(162, 31), - // (163,23): error CS0103: The name 'x12' does not exist in the current context - // Dummy(x12, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(163, 23), - // (175,43): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(true is var x13, x13): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(175, 43), - // (185,43): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(true is var x14, x14): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(185, 43), - // (198,43): error CS0128: A local variable named 'x15' is already defined in this scope - // case 1 when Dummy(true is var x15, x15): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(198, 43), - // (198,48): error CS0165: Use of unassigned local variable 'x15' - // case 1 when Dummy(true is var x15, x15): - Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(198, 48) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(6, x1Ref.Length); - for (int i = 0; i < x1Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); - } - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(4, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyNotAPatternLocal(model, x4Ref[3]); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(3, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); - VerifyNotAPatternLocal(model, x5Ref[2]); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(3, x8Ref.Length); - for (int i = 0; i < x8Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(6, x9Ref.Length); - VerifyNotAPatternLocal(model, x9Ref[0]); - VerifyNotAPatternLocal(model, x9Ref[1]); - VerifyNotAPatternLocal(model, x9Ref[2]); - VerifyNotAPatternLocal(model, x9Ref[3]); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(5, x11Ref.Length); - VerifyNotInScope(model, x11Ref[0]); - VerifyNotInScope(model, x11Ref[1]); - VerifyNotInScope(model, x11Ref[2]); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(5, x12Ref.Length); - VerifyNotInScope(model, x12Ref[0]); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); - VerifyNotInScope(model, x12Ref[3]); - VerifyNotInScope(model, x12Ref[4]); - - var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); - var x13Ref = GetReferences(tree, "x13").ToArray(); - Assert.Equal(2, x13Decl.Length); - Assert.Equal(5, x13Ref.Length); - VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); - VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(4, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPattern(model, x14Decl[1], true); - - var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(2, x15Decl.Length); - Assert.Equal(3, x15Ref.Length); - for (int i = 0; i < x15Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_SwitchLabelPattern_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) { return true; } - - void Test1(object val) - { - switch (val) - { - case byte x1 when Dummy(x1): - Dummy(x1); - break; - case int x1 when Dummy(x1): - Dummy(x1); - break; - case long x1 when Dummy(x1): - Dummy(x1); - break; - } - } - - void Test2(object val) - { - switch (val) - { - case 0 when Dummy(x2): - case int x2: - Dummy(x2); - break; - } - } - - void Test3(int x3, object val) - { - switch (val) - { - case int x3 when Dummy(x3): - Dummy(x3); - break; - } - } - - void Test4(object val) - { - var x4 = 11; - switch (val) - { - case int x4 when Dummy(x4): - Dummy(x4); - break; - case 1 when Dummy(x4): - Dummy(x4); - break; - } - } - - void Test5(object val) - { - switch (val) - { - case int x5 when Dummy(x5): - Dummy(x5); - break; - } - - var x5 = 11; - Dummy(x5); - } - - //void Test6(object val) - //{ - // let x6 = 11; - // switch (val) - // { - // case 0 when Dummy(x6): - // Dummy(x6); - // break; - // case int x6 when Dummy(x6): - // Dummy(x6); - // break; - // } - //} - - //void Test7(object val) - //{ - // switch (val) - // { - // case int x7 when Dummy(x7): - // Dummy(x7); - // break; - // } - - // let x7 = 11; - // Dummy(x7); - //} - - void Test8(object val) - { - switch (val) - { - case int x8 - when Dummy(x8, false is var x8, x8): - Dummy(x8); - break; - } - } - - void Test9(object val) - { - switch (val) - { - case 0 when Dummy(x9): - int x9 = 9; - Dummy(x9); - break; - case 2 when Dummy(x9 = 9): - Dummy(x9); - break; - case int x9 when Dummy(x9): - Dummy(x9); - break; - } - } - - //void Test10(object val) - //{ - // switch (val) - // { - // case int x10 when Dummy(x10): - // Dummy(x10); - // break; - // case 0 when Dummy(x10): - // let x10 = 10; - // Dummy(x10); - // break; - // case 2 when Dummy(x10 = 10, x10): - // Dummy(x10); - // break; - // } - //} - - void Test11(object val) - { - switch (x11 ? val : 0) - { - case 0 when Dummy(x11): - Dummy(x11, 0); - break; - case int x11 when Dummy(x11): - Dummy(x11, 1); - break; - } - } - - void Test12(object val) - { - switch (x12 ? val : 0) - { - case int x12 when Dummy(x12): - Dummy(x12, 0); - break; - case 1 when Dummy(x12): - Dummy(x12, 1); - break; - } - } - - void Test13() - { - switch (1 is var x13 ? x13 : 0) - { - case 0 when Dummy(x13): - Dummy(x13); - break; - case int x13 when Dummy(x13): - Dummy(x13); - break; - } - } - - void Test14(object val) - { - switch (val) - { - case int x14 when Dummy(x14): - Dummy(x14); - Dummy(true is var x14, x14); - Dummy(x14); - break; - } - } - - void Test15(object val) - { - switch (val) - { - case int x15 when Dummy(x15): - case long x15 when Dummy(x15): - Dummy(x15); - break; - } - } - - void Test16(object val) - { - switch (val) - { - case int x16 when Dummy(x16): - case 1 when Dummy(true is var x16, x16): - Dummy(x16); - break; - } - } - - void Test17(object val) - { - switch (val) - { - case 0 when Dummy(true is var x17, x17): - case int x17 when Dummy(x17): - Dummy(x17); - break; - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (30,31): error CS0841: Cannot use local variable 'x2' before it is declared - // case 0 when Dummy(x2): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(30, 31), - // (32,23): error CS0165: Use of unassigned local variable 'x2' - // Dummy(x2); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(32, 23), - // (41,22): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x3 when Dummy(x3): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(41, 22), - // (52,22): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x4 when Dummy(x4): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(52, 22), - // (65,22): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x5 when Dummy(x5): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(65, 22), - // (106,49): error CS0128: A local variable named 'x8' is already defined in this scope - // when Dummy(x8, false is var x8, x8): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(106, 49), - // (116,31): error CS0841: Cannot use local variable 'x9' before it is declared - // case 0 when Dummy(x9): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(116, 31), - // (123,22): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x9 when Dummy(x9): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(123, 22), - // (148,17): error CS0103: The name 'x11' does not exist in the current context - // switch (x11 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(148, 17), - // (150,31): error CS0103: The name 'x11' does not exist in the current context - // case 0 when Dummy(x11): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(150, 31), - // (151,23): error CS0103: The name 'x11' does not exist in the current context - // Dummy(x11, 0); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(151, 23), - // (161,17): error CS0103: The name 'x12' does not exist in the current context - // switch (x12 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(161, 17), - // (166,31): error CS0103: The name 'x12' does not exist in the current context - // case 1 when Dummy(x12): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(166, 31), - // (167,23): error CS0103: The name 'x12' does not exist in the current context - // Dummy(x12, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(167, 23), - // (179,22): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x13 when Dummy(x13): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(179, 22), - // (189,22): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case int x14 when Dummy(x14): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(189, 22), - // (202,23): error CS0128: A local variable named 'x15' is already defined in this scope - // case long x15 when Dummy(x15): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(202, 23), - // (202,38): error CS0165: Use of unassigned local variable 'x15' - // case long x15 when Dummy(x15): - Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(202, 38), - // (213,43): error CS0128: A local variable named 'x16' is already defined in this scope - // case 1 when Dummy(true is var x16, x16): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x16").WithArguments("x16").WithLocation(213, 43), - // (213,48): error CS0165: Use of unassigned local variable 'x16' - // case 1 when Dummy(true is var x16, x16): - Diagnostic(ErrorCode.ERR_UseDefViolation, "x16").WithArguments("x16").WithLocation(213, 48), - // (224,22): error CS0128: A local variable named 'x17' is already defined in this scope - // case int x17 when Dummy(x17): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x17").WithArguments("x17").WithLocation(224, 22), - // (224,37): error CS0165: Use of unassigned local variable 'x17' - // case int x17 when Dummy(x17): - Diagnostic(ErrorCode.ERR_UseDefViolation, "x17").WithArguments("x17").WithLocation(224, 37) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(6, x1Ref.Length); - for (int i = 0; i < x1Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); - } - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(4, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyNotAPatternLocal(model, x4Ref[3]); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(3, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); - VerifyNotAPatternLocal(model, x5Ref[2]); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(3, x8Ref.Length); - for (int i = 0; i < x8Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(6, x9Ref.Length); - VerifyNotAPatternLocal(model, x9Ref[0]); - VerifyNotAPatternLocal(model, x9Ref[1]); - VerifyNotAPatternLocal(model, x9Ref[2]); - VerifyNotAPatternLocal(model, x9Ref[3]); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(5, x11Ref.Length); - VerifyNotInScope(model, x11Ref[0]); - VerifyNotInScope(model, x11Ref[1]); - VerifyNotInScope(model, x11Ref[2]); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(5, x12Ref.Length); - VerifyNotInScope(model, x12Ref[0]); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); - VerifyNotInScope(model, x12Ref[3]); - VerifyNotInScope(model, x12Ref[4]); - - var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); - var x13Ref = GetReferences(tree, "x13").ToArray(); - Assert.Equal(2, x13Decl.Length); - Assert.Equal(5, x13Ref.Length); - VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); - VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(4, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPattern(model, x14Decl[1], true); - - var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(2, x15Decl.Length); - Assert.Equal(3, x15Ref.Length); - for (int i = 0; i < x15Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); - - var x16Decl = GetPatternDeclarations(tree, "x16").ToArray(); - var x16Ref = GetReferences(tree, "x16").ToArray(); - Assert.Equal(2, x16Decl.Length); - Assert.Equal(3, x16Ref.Length); - for (int i = 0; i < x16Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x16Decl[0], x16Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x16Decl[1]); - - var x17Decl = GetPatternDeclarations(tree, "x17").ToArray(); - var x17Ref = GetReferences(tree, "x17").ToArray(); - Assert.Equal(2, x17Decl.Length); - Assert.Equal(3, x17Ref.Length); - for (int i = 0; i < x17Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x17Decl[0], x17Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x17Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Switch_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - switch (1 is var x1 ? x1 : 0) - { - case 0: - Dummy(x1, 0); - break; - } - - Dummy(x1, 1); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - switch (4 is var x4 ? x4 : 0) - { - case 4: - Dummy(x4); - break; - } - } - - void Test5(int x5) - { - switch (5 is var x5 ? x5 : 0) - { - case 5: - Dummy(x5); - break; - } - } - - void Test6() - { - switch (x6 + 6 is var x6 ? x6 : 0) - { - case 6: - Dummy(x6); - break; - } - } - - void Test7() - { - switch (7 is var x7 ? x7 : 0) - { - case 7: - var x7 = 12; - Dummy(x7); - break; - } - } - - void Test9() - { - switch (9 is var x9 ? x9 : 0) - { - case 9: - Dummy(x9, 0); - switch (9 is var x9 ? x9 : 0) - { - case 9: - Dummy(x9, 1); - break; - } - break; - } - - } - - void Test10() - { - switch (y10 + 10 is var x10 ? x10 : 0) - { - case 0 when y10: - break; - case y10: - var y10 = 12; - Dummy(y10); - break; - } - } - - //void Test11() - //{ - // switch (y11 + 11 is var x11 ? x11 : 0) - // { - // case 0 when y11 > 0: - // break; - // case y11: - // let y11 = 12; - // Dummy(y11); - // break; - // } - //} - - void Test14() - { - switch (Dummy(1 is var x14, - 2 is var x14, - x14) ? 1 : 0) - { - case 0: - Dummy(x14); - break; - } - } - - void Test15(int val) - { - switch (val) - { - case 0 when y15 > 0: - break; - case y15: - var y15 = 15; - Dummy(y15); - break; - } - } - - //void Test16(int val) - //{ - // switch (val) - // { - // case 0 when y16 > 0: - // break; - // case y16: - // let y16 = 16; - // Dummy(y16); - // break; - // } - //} -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (27,26): error CS0128: A local variable named 'x4' is already defined in this scope - // switch (4 is var x4 ? x4 : 0) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(27, 26), - // (37,26): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // switch (5 is var x5 ? x5 : 0) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(37, 26), - // (47,17): error CS0841: Cannot use local variable 'x6' before it is declared - // switch (x6 + 6 is var x6 ? x6 : 0) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(47, 17), - // (60,21): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(60, 21), - // (71,23): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9, 0); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(71, 23), - // (72,34): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // switch (9 is var x9 ? x9 : 0) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(72, 34), - // (85,17): error CS0103: The name 'y10' does not exist in the current context - // switch (y10 + 10 is var x10 ? x10 : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 17), - // (87,25): error CS0841: Cannot use local variable 'y10' before it is declared - // case 0 when y10: - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y10").WithArguments("y10").WithLocation(87, 25), - // (89,18): error CS0841: Cannot use local variable 'y10' before it is declared - // case y10: - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y10").WithArguments("y10").WithLocation(89, 18), - // (89,18): error CS0150: A constant value is expected - // case y10: - Diagnostic(ErrorCode.ERR_ConstantExpected, "y10").WithLocation(89, 18), - // (112,28): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(112, 28), - // (125,25): error CS0841: Cannot use local variable 'y15' before it is declared - // case 0 when y15 > 0: - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y15").WithArguments("y15").WithLocation(125, 25), - // (127,18): error CS0841: Cannot use local variable 'y15' before it is declared - // case y15: - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y15").WithArguments("y15").WithLocation(127, 18) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(3, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(4, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - VerifyNotAPatternLocal(model, y10Ref[2]); - VerifyNotAPatternLocal(model, y10Ref[3]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - - var y15Ref = GetReferences(tree, "y15").ToArray(); - Assert.Equal(3, y15Ref.Length); - VerifyNotAPatternLocal(model, y15Ref[0]); - VerifyNotAPatternLocal(model, y15Ref[1]); - VerifyNotAPatternLocal(model, y15Ref[2]); - } - - [Fact] - public void ScopeOfPatternVariables_Switch_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - if (true) - switch (1 is var x1 ? 1 : 0) - { - case 0: - break; - } - - Dummy(x1, 1); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (19,15): error CS0103: The name 'x1' does not exist in the current context - // Dummy(x1, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(19, 15) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_Switch_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (SwitchStatementSyntax)SyntaxFactory.ParseStatement(@" -switch (Dummy(11 is var x1, x1)) {} -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void Switch_01() - { - var source = -@" -public class X -{ - public static void Main() - { - Test1(0); - Test1(1); - } - - static bool Dummy1(bool val, params object[] x) {return val;} - static T Dummy2(T val, params object[] x) {return val;} - - static void Test1(int val) - { - switch (Dummy2(val, ""Test1 {0}"" is var x1)) - { - case 0 when Dummy1(true, ""case 0"" is var y1): - System.Console.WriteLine(x1, y1); - break; - case int z1: - System.Console.WriteLine(x1, z1); - break; - } - - System.Console.WriteLine(x1); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"Test1 case 0 -Test1 {0} -Test1 1 -Test1 {0}"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Switch_02() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - if (f) - switch (Dummy(f, (f ? 1 : 2) is var x1, x1)) - {} - - if (f) - { - switch (Dummy(f, (f ? 3 : 4) is var x1, x1)) - {} - } - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -3"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Using_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.IDisposable Dummy(params object[] x) {return null;} - - void Test1() - { - using (Dummy(true is var x1, x1)) - { - Dummy(x1); - } - } - - void Test2() - { - using (Dummy(true is var x2, x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - using (Dummy(true is var x4, x4)) - Dummy(x4); - } - - void Test6() - { - using (Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - using (Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - using (Dummy(true is var x8, x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - using (Dummy(true is var x9, x9)) - { - Dummy(x9); - using (Dummy(true is var x9, x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - using (Dummy(y10 is var x10, x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // using (Dummy(y11 is var x11, x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - using (Dummy(y12 is var x12, x12)) - var y12 = 12; - } - - //void Test13() - //{ - // using (Dummy(y13 is var x13, x13)) - // let y13 = 12; - //} - - void Test14() - { - using (Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,34): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (Dummy(true is var x4, x4)) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 34), - // (35,22): error CS0841: Cannot use local variable 'x6' before it is declared - // using (Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 22), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (53,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), - // (61,38): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (Dummy(true is var x9, x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 38), - // (68,22): error CS0103: The name 'y10' does not exist in the current context - // using (Dummy(y10 is var x10, x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 22), - // (86,22): error CS0103: The name 'y12' does not exist in the current context - // using (Dummy(y12 is var x12, x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 22), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,31): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 31) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Using_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.IDisposable Dummy(params object[] x) {return null;} - - void Test1() - { - using (var d = Dummy(true is var x1, x1)) - { - Dummy(x1); - } - } - - void Test2() - { - using (var d = Dummy(true is var x2, x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - using (var d = Dummy(true is var x4, x4)) - Dummy(x4); - } - - void Test6() - { - using (var d = Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - using (var d = Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - using (var d = Dummy(true is var x8, x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - using (var d = Dummy(true is var x9, x9)) - { - Dummy(x9); - using (var e = Dummy(true is var x9, x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - using (var d = Dummy(y10 is var x10, x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // using (var d = Dummy(y11 is var x11, x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - using (var d = Dummy(y12 is var x12, x12)) - var y12 = 12; - } - - //void Test13() - //{ - // using (var d = Dummy(y13 is var x13, x13)) - // let y13 = 12; - //} - - void Test14() - { - using (var d = Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,42): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (var d = Dummy(true is var x4, x4)) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 42), - // (35,30): error CS0841: Cannot use local variable 'x6' before it is declared - // using (var d = Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 30), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (53,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), - // (61,46): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (var e = Dummy(true is var x9, x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 46), - // (68,30): error CS0103: The name 'y10' does not exist in the current context - // using (var d = Dummy(y10 is var x10, x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 30), - // (86,30): error CS0103: The name 'y12' does not exist in the current context - // using (var d = Dummy(y12 is var x12, x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 30), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,39): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 39) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Using_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.IDisposable Dummy(params object[] x) {return null;} - - void Test1() - { - using (System.IDisposable d = Dummy(true is var x1, x1)) - { - Dummy(x1); - } - } - - void Test2() - { - using (System.IDisposable d = Dummy(true is var x2, x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - using (System.IDisposable d = Dummy(true is var x4, x4)) - Dummy(x4); - } - - void Test6() - { - using (System.IDisposable d = Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - using (System.IDisposable d = Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - using (System.IDisposable d = Dummy(true is var x8, x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - using (System.IDisposable d = Dummy(true is var x9, x9)) - { - Dummy(x9); - using (System.IDisposable c = Dummy(true is var x9, x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - using (System.IDisposable d = Dummy(y10 is var x10, x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // using (System.IDisposable d = Dummy(y11 is var x11, x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - using (System.IDisposable d = Dummy(y12 is var x12, x12)) - var y12 = 12; - } - - //void Test13() - //{ - // using (System.IDisposable d = Dummy(y13 is var x13, x13)) - // let y13 = 12; - //} - - void Test14() - { - using (System.IDisposable d = Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,57): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (System.IDisposable d = Dummy(true is var x4, x4)) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 57), - // (35,45): error CS0841: Cannot use local variable 'x6' before it is declared - // using (System.IDisposable d = Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 45), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (53,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), - // (61,61): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // using (System.IDisposable c = Dummy(true is var x9, x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 61), - // (68,45): error CS0103: The name 'y10' does not exist in the current context - // using (System.IDisposable d = Dummy(y10 is var x10, x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 45), - // (86,45): error CS0103: The name 'y12' does not exist in the current context - // using (System.IDisposable d = Dummy(y12 is var x12, x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 45), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,54): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 54) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var x10Decl = GetPatternDeclarations(tree, "x10").Single(); - var x10Ref = GetReferences(tree, "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Using_04() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.IDisposable Dummy(params object[] x) {return null;} - - void Test1() - { - using (var x1 = Dummy(true is var x1, x1)) - { - Dummy(x1); - } - } - - void Test2() - { - using (System.IDisposable x2 = Dummy(true is var x2, x2)) - { - Dummy(x2); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (12,43): error CS0128: A local variable named 'x1' is already defined in this scope - // using (var x1 = Dummy(true is var x1, x1)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(12, 43), - // (12,47): error CS0841: Cannot use local variable 'x1' before it is declared - // using (var x1 = Dummy(true is var x1, x1)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(12, 47), - // (12,47): error CS0165: Use of unassigned local variable 'x1' - // using (var x1 = Dummy(true is var x1, x1)) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(12, 47), - // (20,58): error CS0128: A local variable named 'x2' is already defined in this scope - // using (System.IDisposable x2 = Dummy(true is var x2, x2)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 58), - // (20,62): error CS0165: Use of unassigned local variable 'x2' - // using (System.IDisposable x2 = Dummy(true is var x2, x2)) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 62) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - VerifyNotAPatternLocal(model, x1Ref[0]); - VerifyNotAPatternLocal(model, x1Ref[1]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); - VerifyNotAPatternLocal(model, x2Ref[0]); - VerifyNotAPatternLocal(model, x2Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Using_05() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.IDisposable Dummy(params object[] x) {return null;} - - void Test1() - { - using (System.IDisposable d = Dummy(true is var x1, x1), - x1 = Dummy(x1)) - { - Dummy(x1); - } - } - - void Test2() - { - using (System.IDisposable d1 = Dummy(true is var x2, x2), - d2 = Dummy(true is var x2, x2)) - { - Dummy(x2); - } - } - - void Test3() - { - using (System.IDisposable d1 = Dummy(true is var x3, x3), - d2 = Dummy(x3)) - { - Dummy(x3); - } - } - - void Test4() - { - using (System.IDisposable d1 = Dummy(x4), - d2 = Dummy(true is var x4, x4)) - { - Dummy(x4); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (13,35): error CS0128: A local variable named 'x1' is already defined in this scope - // x1 = Dummy(x1)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 35), - // (22,58): error CS0128: A local variable named 'x2' is already defined in this scope - // d2 = Dummy(true is var x2, x2)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(22, 58), - // (39,46): error CS0841: Cannot use local variable 'x4' before it is declared - // using (System.IDisposable d1 = Dummy(x4), - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(39, 46) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").ToArray(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Decl.Length); - Assert.Equal(3, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(3, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - } - - [Fact] - public void Using_01() - { - var source = -@" -public class X -{ - public static void Main() - { - using (System.IDisposable d1 = Dummy(new C(""a""), new C(""b"") is var x1), - d2 = Dummy(new C(""c""), new C(""d"") is var x2)) - { - System.Console.WriteLine(d1); - System.Console.WriteLine(x1); - System.Console.WriteLine(d2); - System.Console.WriteLine(x2); - } - - using (Dummy(new C(""e""), new C(""f"") is var x1)) - { - System.Console.WriteLine(x1); - } - } - - static System.IDisposable Dummy(System.IDisposable x, params object[] y) {return x;} -} - -class C : System.IDisposable -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public void Dispose() - { - System.Console.WriteLine(""Disposing {0}"", _val); - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"a -b -c -d -Disposing c -Disposing a -f -Disposing e"); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - var d = Dummy(true is var x1, x1); - } - void Test4() - { - var x4 = 11; - Dummy(x4); - - var d = Dummy(true is var x4, x4); - } - - void Test6() - { - var d = Dummy(x6 && true is var x6); - } - - void Test8() - { - var d = Dummy(true is var x8, x8); - System.Console.WriteLine(x8); - } - - void Test14() - { - var d = Dummy(1 is var x14, - 2 is var x14, - x14); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (19,35): error CS0128: A local variable named 'x4' is already defined in this scope - // var d = Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 35), - // (24,23): error CS0841: Cannot use local variable 'x6' before it is declared - // var d = Dummy(x6 && true is var x6); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 23), - // (36,32): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 32) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").Single(); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").Single(); - Assert.Equal(2, x14Decl.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - object d = Dummy(true is var x1, x1); - } - void Test4() - { - var x4 = 11; - Dummy(x4); - - object d = Dummy(true is var x4, x4); - } - - void Test6() - { - object d = Dummy(x6 && true is var x6); - } - - void Test8() - { - object d = Dummy(true is var x8, x8); - System.Console.WriteLine(x8); - } - - void Test14() - { - object d = Dummy(1 is var x14, - 2 is var x14, - x14); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (19,38): error CS0128: A local variable named 'x4' is already defined in this scope - // object d = Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 38), - // (24,26): error CS0841: Cannot use local variable 'x6' before it is declared - // object d = Dummy(x6 && true is var x6); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 26), - // (36,35): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 35) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").Single(); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").Single(); - Assert.Equal(2, x14Decl.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - var x1 = - Dummy(true is var x1, x1); - Dummy(x1); - } - - void Test2() - { - object x2 = - Dummy(true is var x2, x2); - Dummy(x2); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (13,36): error CS0128: A local variable named 'x1' is already defined in this scope - // Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 36), - // (13,40): error CS0841: Cannot use local variable 'x1' before it is declared - // Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 40), - // (13,40): error CS0165: Use of unassigned local variable 'x1' - // Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 40), - // (20,39): error CS0128: A local variable named 'x2' is already defined in this scope - // Dummy(true is var x2, x2); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 39), - // (20,43): error CS0165: Use of unassigned local variable 'x2' - // Dummy(true is var x2, x2); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 43) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyNotAPatternLocal(model, x1Ref[0]); - VerifyNotAPatternLocal(model, x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyNotAPatternLocal(model, x2Ref[0]); - VerifyNotAPatternLocal(model, x2Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_04() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - object d = Dummy(true is var x1, x1), - x1 = Dummy(x1); - Dummy(x1); - } - - void Test2() - { - object d1 = Dummy(true is var x2, x2), - d2 = Dummy(true is var x2, x2); - } - - void Test3() - { - object d1 = Dummy(true is var x3, x3), - d2 = Dummy(x3); - } - - void Test4() - { - object d1 = Dummy(x4), - d2 = Dummy(true is var x4, x4); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (13,16): error CS0128: A local variable named 'x1' is already defined in this scope - // x1 = Dummy(x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 16), - // (20,39): error CS0128: A local variable named 'x2' is already defined in this scope - // d2 = Dummy(true is var x2, x2); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 39), - // (31,27): error CS0841: Cannot use local variable 'x4' before it is declared - // object d1 = Dummy(x4), - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(31, 27) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").ToArray(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Decl.Length); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_05() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - long Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (LocalDeclarationStatementSyntax)SyntaxFactory.ParseStatement(@" -var y1 = Dummy(11 is var x1, x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - - Assert.Equal("System.Int64 y1", model.LookupSymbols(x1Ref[0].SpanStart, name: "y1").Single().ToTestDisplayString()); - } - - [Fact] - public void ScopeOfPatternVariables_LocalDeclarationStmt_06() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Test1() - { - if (true) - var d = true is var x1; - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (11,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var d = true is var x1; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var d = true is var x1;").WithLocation(11, 13), - // (13,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(13, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - - var d = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "d").Single(); - Assert.Equal("System.Boolean d", model.GetDeclaredSymbol(d).ToTestDisplayString()); - } - - [Fact] - public void LocalDeclarationStmt_01() - { - var source = -@" -public class X -{ - public static void Main() - { - object d1 = Dummy(new C(""a""), new C(""b"") is var x1, x1), - d2 = Dummy(new C(""c""), new C(""d"") is var x2, x2); - System.Console.WriteLine(d1); - System.Console.WriteLine(d2); - System.Console.WriteLine(x1); - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"b -d -a -c -b"); - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void LocalDeclarationStmt_02() - { - var source = -@" -public class X -{ - public static void Main() - { - if (true) - { - object d1 = Dummy(new C(""a""), new C(""b"") is var x1, x1); - } - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"b"); - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - var (d, dd) = ((true is var x1), x1); - } - void Test4() - { - var x4 = 11; - Dummy(x4); - - var (d, dd) = ((true is var x4), x4); - } - - void Test6() - { - var (d, dd) = (x6 && (true is var x6), 1); - } - - void Test8() - { - var (d, dd) = ((true is var x8), x8); - System.Console.WriteLine(x8); - } - - void Test14() - { - var (d, dd, ddd) = ((1 is var x14), - (2 is var x14), - x14); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (19,37): error CS0128: A local variable named 'x4' is already defined in this scope - // var (d, dd) = ((true is var x4), x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 37), - // (24,24): error CS0841: Cannot use local variable 'x6' before it is declared - // var (d, dd) = (x6 && (true is var x6), 1); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 24), - // (36,33): error CS0128: A local variable named 'x14' is already defined in this scope - // (2 is var x14), - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 33) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); - var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x6").Single(); - var x6Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x6").Single(); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").Single(); - var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x14Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x14").ToArray(); - var x14Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x14").Single(); - Assert.Equal(2, x14Decl.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - (object d, object dd) = ((true is var x1), x1); - } - void Test4() - { - var x4 = 11; - Dummy(x4); - - (object d, object dd) = ((true is var x4), x4); - } - - void Test6() - { - (object d, object dd) = (x6 && (true is var x6), 1); - } - - void Test8() - { - (object d, object dd) = ((true is var x8), x8); - System.Console.WriteLine(x8); - } - - void Test14() - { - (object d, object dd, object ddd) = ((1 is var x14), - (2 is var x14), - x14); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (19,47): error CS0128: A local variable named 'x4' is already defined in this scope - // (object d, object dd) = ((true is var x4), x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 47), - // (24,34): error CS0841: Cannot use local variable 'x6' before it is declared - // (object d, object dd) = (x6 && (true is var x6), 1); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 34), - // (36,33): error CS0128: A local variable named 'x14' is already defined in this scope - // (2 is var x14), - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 33) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); - var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x6").Single(); - var x6Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x6").Single(); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").Single(); - var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); - Assert.Equal(2, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x14Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x14").ToArray(); - var x14Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x14").Single(); - Assert.Equal(2, x14Decl.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - var (x1, dd) = - ((true is var x1), x1); - Dummy(x1); - } - - void Test2() - { - (object x2, object dd) = - ((true is var x2), x2); - Dummy(x2); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (13,37): error CS0128: A local variable named 'x1' is already defined in this scope - // ((true is var x1), x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 37), - // (13,42): error CS0841: Cannot use local variable 'x1' before it is declared - // ((true is var x1), x1); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 42), - // (13,42): error CS0165: Use of unassigned local variable 'x1' - // ((true is var x1), x1); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 42), - // (20,40): error CS0128: A local variable named 'x2' is already defined in this scope - // ((true is var x2), x2); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 40), - // (20,45): error CS0165: Use of unassigned local variable 'x2' - // ((true is var x2), x2); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 45) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyNotAPatternLocal(model, x1Ref[0]); - VerifyNotAPatternLocal(model, x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - - var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").Single(); - var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyNotAPatternLocal(model, x2Ref[0]); - VerifyNotAPatternLocal(model, x2Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_04() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - (object d, object x1) = (Dummy((true is var x1), x1), - Dummy(x1)); - Dummy(x1); - } - - void Test2() - { - (object d1, object d2) = (Dummy((true is var x2), x2), - Dummy((true is var x2), x2)); - } - - void Test3() - { - (object d1, object d2) = (Dummy((true is var x3), x3), - Dummy(x3)); - } - - void Test4() - { - (object d1, object d2) = (Dummy(x4), - Dummy((true is var x4), x4)); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (12,53): error CS0128: A local variable named 'x1' is already defined in this scope - // (object d, object x1) = (Dummy((true is var x1), x1), - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(12, 53), - // (12,58): error CS0165: Use of unassigned local variable 'x1' - // (object d, object x1) = (Dummy((true is var x1), x1), - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(12, 58), - // (20,40): error CS0128: A local variable named 'x2' is already defined in this scope - // Dummy((true is var x2), x2)); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 40), - // (31,41): error CS0841: Cannot use local variable 'x4' before it is declared - // (object d1, object d2) = (Dummy(x4), - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(31, 41) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(3, x1Ref.Length); - VerifyNotAPatternLocal(model, x1Ref[0]); - VerifyNotAPatternLocal(model, x1Ref[1]); - VerifyNotAPatternLocal(model, x1Ref[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - - var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); - var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); - Assert.Equal(2, x2Decl.Length); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); - - var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").Single(); - var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); - var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_05() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (DeconstructionDeclarationStatementSyntax)SyntaxFactory.ParseStatement(@" -var (y1, dd) = ((123 is var x1), x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel(tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - - Assert.Equal("System.Boolean y1", model.LookupSymbols(x1Ref[0].SpanStart, name: "y1").Single().ToTestDisplayString()); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_06() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Test1() - { - if (true) - var (d, dd) = ((true is var x1), x1); - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (11,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var (d, dd) = ((true is var x1), x1); - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var (d, dd) = ((true is var x1), x1);").WithLocation(11, 13), - // (13,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(13, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref[0]); - VerifyNotInScope(model, x1Ref[1]); - - var d = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "d").Single(); - Assert.Equal("System.Boolean d", model.GetDeclaredSymbol(d).ToTestDisplayString()); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void DeconstructionDeclarationStmt_01() - { - var source = -@" -public class X -{ - public static void Main() - { - (object d1, object d2) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), - Dummy(new C(""c""), (new C(""d"") is var x2), x2)); - System.Console.WriteLine(d1); - System.Console.WriteLine(d2); - System.Console.WriteLine(x1); - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - CompileAndVerify(compilation, expectedOutput: -@"b -d -a -c -b"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void DeconstructionDeclarationStmt_02() - { - var source = -@" -public class X -{ - public static void Main() - { - if (true) - { - (object d1, object d2) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), x1); - } - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - CompileAndVerify(compilation, expectedOutput: -@"b"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void DeconstructionDeclarationStmt_03() - { - var source = -@" -public class X -{ - public static void Main() - { - var (d1, (d2, d3)) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), - (Dummy(new C(""c""), (new C(""d"") is var x2), x2), - Dummy(new C(""e""), (new C(""f"") is var x3), x3))); - System.Console.WriteLine(d1); - System.Console.WriteLine(d2); - System.Console.WriteLine(d3); - System.Console.WriteLine(x1); - System.Console.WriteLine(x2); - System.Console.WriteLine(x3); - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - CompileAndVerify(compilation, expectedOutput: -@"b -d -f -a -c -e -b -d -f"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - - var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); - var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); - Assert.Equal(1, x2Decl.Length); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); - - var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").ToArray(); - var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); - Assert.Equal(1, x3Decl.Length); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl[0], x3Ref); - } - - [Fact] - [CompilerTrait(CompilerFeature.Tuples)] - public void DeconstructionDeclarationStmt_04() - { - var source = -@" -public class X -{ - public static void Main() - { - (var d1, (var d2, var d3)) = (Dummy(new C(""a""), (new C(""b"") is var x1), x1), - (Dummy(new C(""c""), (new C(""d"") is var x2), x2), - Dummy(new C(""e""), (new C(""f"") is var x3), x3))); - System.Console.WriteLine(d1); - System.Console.WriteLine(d2); - System.Console.WriteLine(d3); - System.Console.WriteLine(x1); - System.Console.WriteLine(x2); - System.Console.WriteLine(x3); - } - - static object Dummy(object x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} - -class C -{ - private readonly string _val; - - public C(string val) - { - _val = val; - } - - public override string ToString() - { - return _val; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, - options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - CompileAndVerify(compilation, expectedOutput: -@"b -d -f -a -c -e -b -d -f"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - - var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); - var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); - Assert.Equal(1, x2Decl.Length); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); - - var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").ToArray(); - var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); - Assert.Equal(1, x3Decl.Length); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl[0], x3Ref); - } - - [Fact] - public void ScopeOfPatternVariables_While_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - while (true is var x1 && x1) - { - Dummy(x1); - } - } - - void Test2() - { - while (true is var x2 && x2) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - while (true is var x4 && x4 > 0) - Dummy(x4); - } - - void Test6() - { - while (x6 && true is var x6) - Dummy(x6); - } - - void Test7() - { - while (true is var x7 && x7) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - while (true is var x8 && x8) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - while (true is var x9 && x9) - { - Dummy(x9); - while (true is var x9 && x9) // 2 - Dummy(x9); - } - } - - void Test10() - { - while (y10 is var x10) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // while (y11 is var x11) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - while (y12 is var x12) - var y12 = 12; - } - - //void Test13() - //{ - // while (y13 is var x13) - // let y13 = 12; - //} - - void Test14() - { - while (Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,28): error CS0128: A local variable named 'x4' is already defined in this scope - // while (true is var x4 && x4 > 0) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(29, 28), - // (35,16): error CS0841: Cannot use local variable 'x6' before it is declared - // while (x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 16), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (60,19): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(60, 19), - // (61,32): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // while (true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 32), - // (68,16): error CS0103: The name 'y10' does not exist in the current context - // while (y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 16), - // (86,16): error CS0103: The name 'y12' does not exist in the current context - // while (y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 16), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,31): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 31) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_While_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - if (true) - while (true is var x1) - { - } - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (17,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_While_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (WhileStatementSyntax)SyntaxFactory.ParseStatement(@" -while (Dummy(11 is var x1, x1)) ; -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void While_01() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - while (Dummy(f, (f ? 1 : 2) is var x1, x1)) - { - System.Console.WriteLine(x1); - f = false; - } - - System.Console.WriteLine(x1); - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -1 -2 -2"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void While_02() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - if (f) - while (Dummy(f, (f ? 1 : 2) is var x1, x1)) - break; - - if (f) - { - while (Dummy(f, (f ? 3 : 4) is var x1, x1)) - break; - } - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -3"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Do_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - do - { - Dummy(x1); - } - while (true is var x1 && x1); - } - - void Test2() - { - do - Dummy(x2); - while (true is var x2 && x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - do - Dummy(x4); - while (true is var x4 && x4 > 0); - } - - void Test6() - { - do - Dummy(x6); - while (x6 && true is var x6); - } - - void Test7() - { - do - { - var x7 = 12; - Dummy(x7); - } - while (true is var x7 && x7); - } - - void Test8() - { - do - Dummy(x8); - while (true is var x8 && x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - do - { - Dummy(x9); - do - Dummy(x9); - while (true is var x9 && x9); // 2 - } - while (true is var x9 && x9); - } - - void Test10() - { - do - { - var y10 = 12; - Dummy(y10); - } - while (y10 is var x10); - } - - //void Test11() - //{ - // do - // { - // let y11 = 12; - // Dummy(y11); - // } - // while (y11 is var x11); - //} - - void Test12() - { - do - var y12 = 12; - while (y12 is var x12); - } - - //void Test13() - //{ - // do - // let y13 = 12; - // while (y13 is var x13); - //} - - void Test14() - { - do - { - Dummy(x14); - } - while (Dummy(1 is var x14, - 2 is var x14, - x14)); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (97,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(97, 13), - // (14,19): error CS0841: Cannot use local variable 'x1' before it is declared - // Dummy(x1); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(14, 19), - // (22,19): error CS0841: Cannot use local variable 'x2' before it is declared - // Dummy(x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(22, 19), - // (33,28): error CS0128: A local variable named 'x4' is already defined in this scope - // while (true is var x4 && x4 > 0); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 28), - // (40,16): error CS0841: Cannot use local variable 'x6' before it is declared - // while (x6 && true is var x6); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(40, 16), - // (39,19): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(39, 19), - // (47,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(47, 17), - // (56,19): error CS0841: Cannot use local variable 'x8' before it is declared - // Dummy(x8); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x8").WithArguments("x8").WithLocation(56, 19), - // (66,19): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(66, 19), - // (69,32): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // while (true is var x9 && x9); // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(69, 32), - // (68,23): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(68, 23), - // (81,16): error CS0103: The name 'y10' does not exist in the current context - // while (y10 is var x10); - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(81, 16), - // (98,16): error CS0103: The name 'y12' does not exist in the current context - // while (y12 is var x12); - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(98, 16), - // (97,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(97, 17), - // (115,31): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(115, 31), - // (112,19): error CS0841: Cannot use local variable 'x14' before it is declared - // Dummy(x14); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(112, 19) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[1]); - VerifyNotAPatternLocal(model, x7Ref[0]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1], x9Ref[2]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[1]); - VerifyNotAPatternLocal(model, y10Ref[0]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Do_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - if (true) - do - { - } - while (true is var x1); - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (18,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(18, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_Do_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (DoStatementSyntax)SyntaxFactory.ParseStatement(@" -do {} while (Dummy(11 is var x1, x1)); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void Do_01() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f; - - do - { - f = false; - } - while (Dummy(f, (f ? 1 : 2) is var x1, x1)); - - System.Console.WriteLine(x1); - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"2 -2"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Do_02() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - if (f) - do - ; - while (Dummy(f, (f ? 1 : 2) is var x1, x1) && false); - - if (f) - { - do - ; - while (Dummy(f, (f ? 3 : 4) is var x1, x1) && false); - } - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -3"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for ( - Dummy(true is var x1 && x1) - ;;) - { - Dummy(x1); - } - } - - void Test2() - { - for ( - Dummy(true is var x2 && x2) - ;;) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - for ( - Dummy(true is var x4 && x4) - ;;) - Dummy(x4); - } - - void Test6() - { - for ( - Dummy(x6 && true is var x6) - ;;) - Dummy(x6); - } - - void Test7() - { - for ( - Dummy(true is var x7 && x7) - ;;) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - for ( - Dummy(true is var x8 && x8) - ;;) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - for ( - Dummy(true is var x9 && x9) - ;;) - { - Dummy(x9); - for ( - Dummy(true is var x9 && x9) // 2 - ;;) - Dummy(x9); - } - } - - void Test10() - { - for ( - Dummy(y10 is var x10) - ;;) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // for ( - // Dummy(y11 is var x11) - // ;;) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - for ( - Dummy(y12 is var x12) - ;;) - var y12 = 12; - } - - //void Test13() - //{ - // for ( - // Dummy(y13 is var x13) - // ;;) - // let y13 = 12; - //} - - void Test14() - { - for ( - Dummy(1 is var x14, - 2 is var x14, - x14) - ;;) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), - // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), - // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), - // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), - // (65,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), - // (65,9): warning CS0162: Unreachable code detected - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), - // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), - // (85,20): error CS0103: The name 'y10' does not exist in the current context - // Dummy(y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), - // (107,20): error CS0103: The name 'y12' does not exist in the current context - // Dummy(y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), - // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), - // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (; - Dummy(true is var x1 && x1) - ;) - { - Dummy(x1); - } - } - - void Test2() - { - for (; - Dummy(true is var x2 && x2) - ;) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - for (; - Dummy(true is var x4 && x4) - ;) - Dummy(x4); - } - - void Test6() - { - for (; - Dummy(x6 && true is var x6) - ;) - Dummy(x6); - } - - void Test7() - { - for (; - Dummy(true is var x7 && x7) - ;) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - for (; - Dummy(true is var x8 && x8) - ;) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - for (; - Dummy(true is var x9 && x9) - ;) - { - Dummy(x9); - for (; - Dummy(true is var x9 && x9) // 2 - ;) - Dummy(x9); - } - } - - void Test10() - { - for (; - Dummy(y10 is var x10) - ;) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // for (; - // Dummy(y11 is var x11) - // ;) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - for (; - Dummy(y12 is var x12) - ;) - var y12 = 12; - } - - //void Test13() - //{ - // for (; - // Dummy(y13 is var x13) - // ;) - // let y13 = 12; - //} - - void Test14() - { - for (; - Dummy(1 is var x14, - 2 is var x14, - x14) - ;) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), - // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), - // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), - // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), - // (65,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), - // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), - // (85,20): error CS0103: The name 'y10' does not exist in the current context - // Dummy(y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), - // (107,20): error CS0103: The name 'y12' does not exist in the current context - // Dummy(y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), - // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), - // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (;; - Dummy(true is var x1 && x1) - ) - { - Dummy(x1); - } - } - - void Test2() - { - for (;; - Dummy(true is var x2 && x2) - ) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - for (;; - Dummy(true is var x4 && x4) - ) - Dummy(x4); - } - - void Test6() - { - for (;; - Dummy(x6 && true is var x6) - ) - Dummy(x6); - } - - void Test7() - { - for (;; - Dummy(true is var x7 && x7) - ) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - for (;; - Dummy(true is var x8 && x8) - ) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - for (;; - Dummy(true is var x9 && x9) - ) - { - Dummy(x9); - for (;; - Dummy(true is var x9 && x9) // 2 - ) - Dummy(x9); - } - } - - void Test10() - { - for (;; - Dummy(y10 is var x10) - ) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // for (;; - // Dummy(y11 is var x11) - // ) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - for (;; - Dummy(y12 is var x12) - ) - var y12 = 12; - } - - //void Test13() - //{ - // for (;; - // Dummy(y13 is var x13) - // ) - // let y13 = 12; - //} - - void Test14() - { - for (;; - Dummy(1 is var x14, - 2 is var x14, - x14) - ) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), - // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), - // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), - // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), - // (65,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), - // (65,9): warning CS0162: Unreachable code detected - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), - // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), - // (85,20): error CS0103: The name 'y10' does not exist in the current context - // Dummy(y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), - // (107,20): error CS0103: The name 'y12' does not exist in the current context - // Dummy(y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), - // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), - // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29), - // (16,19): error CS0165: Use of unassigned local variable 'x1' - // Dummy(x1); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(16, 19), - // (25,19): error CS0165: Use of unassigned local variable 'x2' - // Dummy(x2); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(25, 19), - // (36,19): error CS0165: Use of unassigned local variable 'x4' - // Dummy(x4); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(36, 19), - // (44,19): error CS0165: Use of unassigned local variable 'x6' - // Dummy(x6); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x6").WithArguments("x6").WithLocation(44, 19), - // (63,19): error CS0165: Use of unassigned local variable 'x8' - // Dummy(x8); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x8").WithArguments("x8").WithLocation(63, 19), - // (71,14): warning CS0162: Unreachable code detected - // Dummy(true is var x9 && x9) - Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(71, 14), - // (74,19): error CS0165: Use of unassigned local variable 'x9' - // Dummy(x9); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x9").WithArguments("x9").WithLocation(74, 19), - // (78,23): error CS0165: Use of unassigned local variable 'x9' - // Dummy(x9); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x9").WithArguments("x9").WithLocation(78, 23), - // (128,19): error CS0165: Use of unassigned local variable 'x14' - // Dummy(x14); - Diagnostic(ErrorCode.ERR_UseDefViolation, "x14").WithArguments("x14").WithLocation(128, 19) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_04() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (var b = - Dummy(true is var x1 && x1) - ;;) - { - Dummy(x1); - } - } - - void Test2() - { - for (var b = - Dummy(true is var x2 && x2) - ;;) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - for (var b = - Dummy(true is var x4 && x4) - ;;) - Dummy(x4); - } - - void Test6() - { - for (var b = - Dummy(x6 && true is var x6) - ;;) - Dummy(x6); - } - - void Test7() - { - for (var b = - Dummy(true is var x7 && x7) - ;;) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - for (var b = - Dummy(true is var x8 && x8) - ;;) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - for (var b1 = - Dummy(true is var x9 && x9) - ;;) - { - Dummy(x9); - for (var b2 = - Dummy(true is var x9 && x9) // 2 - ;;) - Dummy(x9); - } - } - - void Test10() - { - for (var b = - Dummy(y10 is var x10) - ;;) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // for (var b = - // Dummy(y11 is var x11) - // ;;) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - for (var b = - Dummy(y12 is var x12) - ;;) - var y12 = 12; - } - - //void Test13() - //{ - // for (var b = - // Dummy(y13 is var x13) - // ;;) - // let y13 = 12; - //} - - void Test14() - { - for (var b = - Dummy(1 is var x14, - 2 is var x14, - x14) - ;;) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), - // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), - // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), - // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), - // (65,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), - // (65,9): warning CS0162: Unreachable code detected - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), - // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), - // (85,20): error CS0103: The name 'y10' does not exist in the current context - // Dummy(y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), - // (107,20): error CS0103: The name 'y12' does not exist in the current context - // Dummy(y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), - // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), - // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_05() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (bool b = - Dummy(true is var x1 && x1) - ;;) - { - Dummy(x1); - } - } - - void Test2() - { - for (bool b = - Dummy(true is var x2 && x2) - ;;) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - for (bool b = - Dummy(true is var x4 && x4) - ;;) - Dummy(x4); - } - - void Test6() - { - for (bool b = - Dummy(x6 && true is var x6) - ;;) - Dummy(x6); - } - - void Test7() - { - for (bool b = - Dummy(true is var x7 && x7) - ;;) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - for (bool b = - Dummy(true is var x8 && x8) - ;;) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - for (bool b1 = - Dummy(true is var x9 && x9) - ;;) - { - Dummy(x9); - for (bool b2 = - Dummy(true is var x9 && x9) // 2 - ;;) - Dummy(x9); - } - } - - void Test10() - { - for (bool b = - Dummy(y10 is var x10) - ;;) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // for (bool b = - // Dummy(y11 is var x11) - // ;;) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - for (bool b = - Dummy(y12 is var x12) - ;;) - var y12 = 12; - } - - //void Test13() - //{ - // for (bool b = - // Dummy(y13 is var x13) - // ;;) - // let y13 = 12; - //} - - void Test14() - { - for (bool b = - Dummy(1 is var x14, - 2 is var x14, - x14) - ;;) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), - // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), - // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared - // Dummy(x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), - // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), - // (65,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), - // (65,9): warning CS0162: Unreachable code detected - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), - // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // Dummy(true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), - // (85,20): error CS0103: The name 'y10' does not exist in the current context - // Dummy(y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), - // (107,20): error CS0103: The name 'y12' does not exist in the current context - // Dummy(y12 is var x12) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), - // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), - // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_For_06() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (var x1 = - Dummy(true is var x1 && x1) - ;;) - {} - } - - void Test2() - { - for (var x2 = true; - Dummy(true is var x2 && x2) - ;) - {} - } - - void Test3() - { - for (var x3 = true;; - Dummy(true is var x3 && x3) - ) - {} - } - - void Test4() - { - for (bool x4 = - Dummy(true is var x4 && x4) - ;;) - {} - } - - void Test5() - { - for (bool x5 = true; - Dummy(true is var x5 && x5) - ;) - {} - } - - void Test6() - { - for (bool x6 = true;; - Dummy(true is var x6 && x6) - ) - {} - } - - void Test7() - { - for (bool x7 = true, b = - Dummy(true is var x7 && x7) - ;;) - {} - } - - void Test8() - { - for (bool b1 = Dummy(true is var x8 && x8), - b2 = Dummy(true is var x8 && x8); - Dummy(true is var x8 && x8); - Dummy(true is var x8 && x8)) - {} - } - - void Test9() - { - for (bool b = x9, - b2 = Dummy(true is var x9 && x9); - Dummy(true is var x9 && x9); - Dummy(true is var x9 && x9)) - {} - } - - void Test10() - { - for (var b = x10; - Dummy(true is var x10 && x10) && - Dummy(true is var x10 && x10); - Dummy(true is var x10 && x10)) - {} - } - - void Test11() - { - for (bool b = x11; - Dummy(true is var x11 && x11) && - Dummy(true is var x11 && x11); - Dummy(true is var x11 && x11)) - {} - } - - void Test12() - { - for (Dummy(x12); - Dummy(x12) && - Dummy(true is var x12 && x12); - Dummy(true is var x12 && x12)) - {} - } - - void Test13() - { - for (var b = x13; - Dummy(x13); - Dummy(true is var x13 && x13), - Dummy(true is var x13 && x13)) - {} - } - - void Test14() - { - for (bool b = x14; - Dummy(x14); - Dummy(true is var x14 && x14), - Dummy(true is var x14 && x14)) - {} - } - - void Test15() - { - for (Dummy(x15); - Dummy(x15); - Dummy(x15), - Dummy(true is var x15 && x15)) - {} - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (13,32): error CS0128: A local variable named 'x1' is already defined in this scope - // Dummy(true is var x1 && x1) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 32), - // (13,38): error CS0841: Cannot use local variable 'x1' before it is declared - // Dummy(true is var x1 && x1) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 38), - // (13,38): error CS0165: Use of unassigned local variable 'x1' - // Dummy(true is var x1 && x1) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 38), - // (21,32): error CS0128: A local variable named 'x2' is already defined in this scope - // Dummy(true is var x2 && x2) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(21, 32), - // (29,32): error CS0128: A local variable named 'x3' is already defined in this scope - // Dummy(true is var x3 && x3) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(29, 32), - // (37,32): error CS0128: A local variable named 'x4' is already defined in this scope - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(37, 32), - // (37,38): error CS0165: Use of unassigned local variable 'x4' - // Dummy(true is var x4 && x4) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(37, 38), - // (45,32): error CS0128: A local variable named 'x5' is already defined in this scope - // Dummy(true is var x5 && x5) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(45, 32), - // (53,32): error CS0128: A local variable named 'x6' is already defined in this scope - // Dummy(true is var x6 && x6) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(53, 32), - // (61,32): error CS0128: A local variable named 'x7' is already defined in this scope - // Dummy(true is var x7 && x7) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x7").WithArguments("x7").WithLocation(61, 32), - // (69,37): error CS0128: A local variable named 'x8' is already defined in this scope - // b2 = Dummy(true is var x8 && x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(69, 37), - // (70,32): error CS0128: A local variable named 'x8' is already defined in this scope - // Dummy(true is var x8 && x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(70, 32), - // (71,32): error CS0128: A local variable named 'x8' is already defined in this scope - // Dummy(true is var x8 && x8)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(71, 32), - // (77,23): error CS0841: Cannot use local variable 'x9' before it is declared - // for (bool b = x9, - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(77, 23), - // (79,32): error CS0128: A local variable named 'x9' is already defined in this scope - // Dummy(true is var x9 && x9); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(79, 32), - // (80,32): error CS0128: A local variable named 'x9' is already defined in this scope - // Dummy(true is var x9 && x9)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(80, 32), - // (86,22): error CS0841: Cannot use local variable 'x10' before it is declared - // for (var b = x10; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x10").WithArguments("x10").WithLocation(86, 22), - // (88,32): error CS0128: A local variable named 'x10' is already defined in this scope - // Dummy(true is var x10 && x10); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(88, 32), - // (89,32): error CS0128: A local variable named 'x10' is already defined in this scope - // Dummy(true is var x10 && x10)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(89, 32), - // (95,23): error CS0841: Cannot use local variable 'x11' before it is declared - // for (bool b = x11; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(95, 23), - // (97,32): error CS0128: A local variable named 'x11' is already defined in this scope - // Dummy(true is var x11 && x11); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(97, 32), - // (98,32): error CS0128: A local variable named 'x11' is already defined in this scope - // Dummy(true is var x11 && x11)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(98, 32), - // (104,20): error CS0841: Cannot use local variable 'x12' before it is declared - // for (Dummy(x12); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(104, 20), - // (105,20): error CS0841: Cannot use local variable 'x12' before it is declared - // Dummy(x12) && - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(105, 20), - // (107,32): error CS0128: A local variable named 'x12' is already defined in this scope - // Dummy(true is var x12 && x12)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x12").WithArguments("x12").WithLocation(107, 32), - // (113,22): error CS0841: Cannot use local variable 'x13' before it is declared - // for (var b = x13; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(113, 22), - // (114,20): error CS0841: Cannot use local variable 'x13' before it is declared - // Dummy(x13); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(114, 20), - // (116,32): error CS0128: A local variable named 'x13' is already defined in this scope - // Dummy(true is var x13 && x13)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x13").WithArguments("x13").WithLocation(116, 32), - // (122,23): error CS0841: Cannot use local variable 'x14' before it is declared - // for (bool b = x14; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(122, 23), - // (123,20): error CS0841: Cannot use local variable 'x14' before it is declared - // Dummy(x14); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(123, 20), - // (125,32): error CS0128: A local variable named 'x14' is already defined in this scope - // Dummy(true is var x14 && x14)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(125, 32), - // (131,20): error CS0841: Cannot use local variable 'x15' before it is declared - // for (Dummy(x15); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(131, 20), - // (132,20): error CS0841: Cannot use local variable 'x15' before it is declared - // Dummy(x15); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(132, 20), - // (133,20): error CS0841: Cannot use local variable 'x15' before it is declared - // Dummy(x15), - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(133, 20) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - VerifyNotAPatternLocal(model, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); - VerifyNotAPatternLocal(model, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); - VerifyNotAPatternLocal(model, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - VerifyNotAPatternLocal(model, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl); - VerifyNotAPatternLocal(model, x5Ref); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl); - VerifyNotAPatternLocal(model, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x7Decl); - VerifyNotAPatternLocal(model, x7Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(4, x8Decl.Length); - Assert.Equal(4, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[3]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(3, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[2]); - - var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); - var x10Ref = GetReferences(tree, "x10").ToArray(); - Assert.Equal(3, x10Decl.Length); - Assert.Equal(4, x10Ref.Length); - VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[2]); - - var x11Decl = GetPatternDeclarations(tree, "x11").ToArray(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(3, x11Decl.Length); - Assert.Equal(4, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl[0], x11Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[2]); - - var x12Decl = GetPatternDeclarations(tree, "x12").ToArray(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Decl.Length); - Assert.Equal(4, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl[0], x12Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x12Decl[1]); - - var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); - var x13Ref = GetReferences(tree, "x13").ToArray(); - Assert.Equal(2, x13Decl.Length); - Assert.Equal(4, x13Ref.Length); - VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x13Decl[1]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(4, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - - var x15Decl = GetPatternDeclarations(tree, "x15").Single(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(4, x15Ref.Length); - VerifyModelForDeclarationPattern(model, x15Decl, x15Ref); - } - - [Fact] - public void For_01() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - for (Dummy(f, (f ? 10 : 20) is var x0, x0); - Dummy(f, (f ? 1 : 2) is var x1, x1); - Dummy(f, (f ? 100 : 200) is var x2, x2)) - { - System.Console.WriteLine(x0); - System.Console.WriteLine(x1); - f = false; - } - } - - static bool Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"10 -1 -10 -1 -200 -2"); - } - - [Fact] - public void ScopeOfPatternVariables_Foreach_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - System.Collections.IEnumerable Dummy(params object[] x) {return null;} - - void Test1() - { - foreach (var i in Dummy(true is var x1 && x1)) - { - Dummy(x1); - } - } - - void Test2() - { - foreach (var i in Dummy(true is var x2 && x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - foreach (var i in Dummy(true is var x4 && x4)) - Dummy(x4); - } - - void Test6() - { - foreach (var i in Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - foreach (var i in Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - foreach (var i in Dummy(true is var x8 && x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - foreach (var i1 in Dummy(true is var x9 && x9)) - { - Dummy(x9); - foreach (var i2 in Dummy(true is var x9 && x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - foreach (var i in Dummy(y10 is var x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // foreach (var i in Dummy(y11 is var x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - foreach (var i in Dummy(y12 is var x12)) - var y12 = 12; - } - - //void Test13() - //{ - // foreach (var i in Dummy(y13 is var x13)) - // let y13 = 12; - //} - - void Test14() - { - foreach (var i in Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } - - void Test15() - { - foreach (var x15 in - Dummy(1 is var x15, x15)) - { - Dummy(x15); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,45): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // foreach (var i in Dummy(true is var x4 && x4)) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 45), - // (35,33): error CS0841: Cannot use local variable 'x6' before it is declared - // foreach (var i in Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 33), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (53,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), - // (61,50): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // foreach (var i2 in Dummy(true is var x9 && x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 50), - // (68,33): error CS0103: The name 'y10' does not exist in the current context - // foreach (var i in Dummy(y10 is var x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 33), - // (86,33): error CS0103: The name 'y12' does not exist in the current context - // foreach (var i in Dummy(y12 is var x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 33), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,42): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 42), - // (108,22): error CS0136: A local or parameter named 'x15' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // foreach (var x15 in - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x15").WithArguments("x15").WithLocation(108, 22) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - - var x15Decl = GetPatternDeclarations(tree, "x15").Single(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(2, x15Ref.Length); - VerifyModelForDeclarationPattern(model, x15Decl, x15Ref[0]); - VerifyNotAPatternLocal(model, x15Ref[1]); - } - - [Fact] - public void Foreach_01() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - foreach (var i in Dummy(3 is var x1, x1)) - { - System.Console.WriteLine(x1); - } - } - - static System.Collections.IEnumerable Dummy(object y, object z) - { - System.Console.WriteLine(z); - return ""a""; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"3 -3"); - } - - [Fact] - public void ScopeOfPatternVariables_Lock_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - lock (Dummy(true is var x1 && x1)) - { - Dummy(x1); - } - } - - void Test2() - { - lock (Dummy(true is var x2 && x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - lock (Dummy(true is var x4 && x4)) - Dummy(x4); - } - - void Test6() - { - lock (Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - lock (Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - lock (Dummy(true is var x8 && x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - lock (Dummy(true is var x9 && x9)) - { - Dummy(x9); - lock (Dummy(true is var x9 && x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - lock (Dummy(y10 is var x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // lock (Dummy(y11 is var x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - lock (Dummy(y12 is var x12)) - var y12 = 12; - } - - //void Test13() - //{ - // lock (Dummy(y13 is var x13)) - // let y13 = 12; - //} - - void Test14() - { - lock (Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,33): error CS0128: A local variable named 'x4' is already defined in this scope - // lock (Dummy(true is var x4 && x4)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(29, 33), - // (35,21): error CS0841: Cannot use local variable 'x6' before it is declared - // lock (Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 21), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (60,19): error CS0841: Cannot use local variable 'x9' before it is declared - // Dummy(x9); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(60, 19), - // (61,37): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // lock (Dummy(true is var x9 && x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 37), - // (68,21): error CS0103: The name 'y10' does not exist in the current context - // lock (Dummy(y10 is var x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 21), - // (86,21): error CS0103: The name 'y12' does not exist in the current context - // lock (Dummy(y12 is var x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 21), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,30): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 30) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Lock_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) {return null;} - - void Test1() - { - if (true) - lock (Dummy(true is var x1)) - { - } - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (17,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_Lock_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (LockStatementSyntax)SyntaxFactory.ParseStatement(@" -lock (Dummy(11 is var x1, x1)); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void Lock_01() - { - var source = -@" -public class X -{ - public static void Main() - { - lock (Dummy(""lock"" is var x1, x1)) - { - System.Console.WriteLine(x1); - } - - System.Console.WriteLine(x1); - } - - static object Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new object(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"lock -lock -lock"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Lock_02() - { - var source = -@" -public class X -{ - public static void Main() - { - bool f = true; - - if (f) - lock (Dummy(f, (f ? 1 : 2) is var x1, x1)) - {} - - if (f) - { - lock (Dummy(f, (f ? 3 : 4) is var x1, x1)) - {} - } - } - - static object Dummy(bool x, object y, object z) - { - System.Console.WriteLine(z); - return x; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"1 -3"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Fixed_01() - { - var source = -@" -public unsafe class X -{ - public static void Main() - { - } - - int[] Dummy(params object[] x) {return null;} - - void Test1() - { - fixed (int* p = Dummy(true is var x1 && x1)) - { - Dummy(x1); - } - } - - void Test2() - { - fixed (int* p = Dummy(true is var x2 && x2)) - Dummy(x2); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - fixed (int* p = Dummy(true is var x4 && x4)) - Dummy(x4); - } - - void Test6() - { - fixed (int* p = Dummy(x6 && true is var x6)) - Dummy(x6); - } - - void Test7() - { - fixed (int* p = Dummy(true is var x7 && x7)) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - fixed (int* p = Dummy(true is var x8 && x8)) - Dummy(x8); - - System.Console.WriteLine(x8); - } - - void Test9() - { - fixed (int* p1 = Dummy(true is var x9 && x9)) - { - Dummy(x9); - fixed (int* p2 = Dummy(true is var x9 && x9)) // 2 - Dummy(x9); - } - } - - void Test10() - { - fixed (int* p = Dummy(y10 is var x10)) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // fixed (int* p = Dummy(y11 is var x11)) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test12() - { - fixed (int* p = Dummy(y12 is var x12)) - var y12 = 12; - } - - //void Test13() - //{ - // fixed (int* p = Dummy(y13 is var x13)) - // let y13 = 12; - //} - - void Test14() - { - fixed (int* p = Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); - compilation.VerifyDiagnostics( - // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement - // var y12 = 12; - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), - // (29,43): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // fixed (int* p = Dummy(true is var x4 && x4)) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 43), - // (35,31): error CS0841: Cannot use local variable 'x6' before it is declared - // fixed (int* p = Dummy(x6 && true is var x6)) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 31), - // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), - // (53,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), - // (61,48): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // fixed (int* p2 = Dummy(true is var x9 && x9)) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 48), - // (68,31): error CS0103: The name 'y10' does not exist in the current context - // fixed (int* p = Dummy(y10 is var x10)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 31), - // (86,31): error CS0103: The name 'y12' does not exist in the current context - // fixed (int* p = Dummy(y12 is var x12)) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 31), - // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used - // var y12 = 12; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), - // (99,40): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 40) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var y12Ref = GetReferences(tree, "y12").Single(); - VerifyNotInScope(model, y12Ref); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Fixed_02() - { - var source = -@" -public unsafe class X -{ - public static void Main() - { - } - - int[] Dummy(params object[] x) {return null;} - int[] Dummy(int* x) {return null;} - - void Test1() - { - fixed (int* x1 = - Dummy(true is var x1 && x1)) - { - Dummy(x1); - } - } - - void Test2() - { - fixed (int* p = Dummy(true is var x2 && x2), - x2 = Dummy()) - { - Dummy(x2); - } - } - - void Test3() - { - fixed (int* x3 = Dummy(), - p = Dummy(true is var x3 && x3)) - { - Dummy(x3); - } - } - - void Test4() - { - fixed (int* p1 = Dummy(true is var x4 && x4), - p2 = Dummy(true is var x4 && x4)) - { - Dummy(x4); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); - compilation.VerifyDiagnostics( - // (14,44): error CS0128: A local variable named 'x1' is already defined in this scope - // Dummy(true is var x1 && x1)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(14, 44), - // (14,50): error CS0165: Use of unassigned local variable 'x1' - // Dummy(true is var x1 && x1)) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(14, 50), - // (23,21): error CS0128: A local variable named 'x2' is already defined in this scope - // x2 = Dummy()) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(23, 21), - // (32,43): error CS0128: A local variable named 'x3' is already defined in this scope - // p = Dummy(true is var x3 && x3)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(32, 43), - // (41,44): error CS0128: A local variable named 'x4' is already defined in this scope - // p2 = Dummy(true is var x4 && x4)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(41, 44) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - VerifyNotAPatternLocal(model, x1Ref[0]); - VerifyNotAPatternLocal(model, x1Ref[1]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); - VerifyNotAPatternLocal(model, x3Ref[0]); - VerifyNotAPatternLocal(model, x3Ref[1]); - - var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Decl.Length); - Assert.Equal(3, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl[0], x4Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); - } - - [Fact] - public void Fixed_01() - { - var source = -@" -public unsafe class X -{ - public static void Main() - { - fixed (int* p = Dummy(""fixed"" is var x1, x1)) - { - System.Console.WriteLine(x1); - } - } - - static int[] Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new int[1]; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); - CompileAndVerify(compilation, expectedOutput: -@"fixed -fixed"); - } - - [Fact] - public void ScopeOfPatternVariables_Yield_01() - { - var source = -@" -using System.Collections; - -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) { return null;} - - IEnumerable Test1() - { - yield return Dummy(true is var x1, x1); - { - yield return Dummy(true is var x1, x1); - } - yield return Dummy(true is var x1, x1); - } - - IEnumerable Test2() - { - yield return Dummy(x2, true is var x2); - } - - IEnumerable Test3(int x3) - { - yield return Dummy(true is var x3, x3); - } - - IEnumerable Test4() - { - var x4 = 11; - Dummy(x4); - yield return Dummy(true is var x4, x4); - } - - IEnumerable Test5() - { - yield return Dummy(true is var x5, x5); - var x5 = 11; - Dummy(x5); - } - - //IEnumerable Test6() - //{ - // let x6 = 11; - // Dummy(x6); - // yield return Dummy(true is var x6, x6); - //} - - //IEnumerable Test7() - //{ - // yield return Dummy(true is var x7, x7); - // let x7 = 11; - // Dummy(x7); - //} - - IEnumerable Test8() - { - yield return Dummy(true is var x8, x8, false is var x8, x8); - } - - IEnumerable Test9(bool y9) - { - if (y9) - yield return Dummy(true is var x9, x9); - } - - IEnumerable Test11() - { - Dummy(x11); - yield return Dummy(true is var x11, x11); - } - - IEnumerable Test12() - { - yield return Dummy(true is var x12, x12); - Dummy(x12); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (16,44): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // yield return Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(16, 44), - // (18,40): error CS0128: A local variable named 'x1' is already defined in this scope - // yield return Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(18, 40), - // (23,28): error CS0841: Cannot use local variable 'x2' before it is declared - // yield return Dummy(x2, true is var x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(23, 28), - // (28,40): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // yield return Dummy(true is var x3, x3); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(28, 40), - // (35,40): error CS0128: A local variable named 'x4' is already defined in this scope - // yield return Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(35, 40), - // (41,13): error CS0128: A local variable named 'x5' is already defined in this scope - // var x5 = 11; - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(41, 13), - // (41,13): warning CS0219: The variable 'x5' is assigned but its value is never used - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(41, 13), - // (61,61): error CS0128: A local variable named 'x8' is already defined in this scope - // yield return Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(61, 61), - // (72,15): error CS0841: Cannot use local variable 'x11' before it is declared - // Dummy(x11); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(72, 15) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(2, x8Ref.Length); - for (int i = 0; i < x8Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").Single(); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); - } - - [Fact] - public void ScopeOfPatternVariables_Yield_02() - { - var source = -@" -using System.Collections; - -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) { return null;} - - IEnumerable Test1() - { - if (true) - yield return Dummy(true is var x1); - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (17,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_Yield_03() - { - var source = -@" -using System.Collections; - -public class X -{ - public static void Main() - { - } - - object Dummy(params object[] x) { return null;} - - IEnumerable Test1() - { - SpeculateHere(); - yield 0; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (YieldStatementSyntax)SyntaxFactory.ParseStatement(@" -yield return (Dummy(11 is var x1, x1)); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - GetReferences(tree, "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void Yield_01() - { - var source = -@" -public class X -{ - public static void Main() - { - foreach (var o in Test()) - {} - } - - static System.Collections.IEnumerable Test() - { - yield return Dummy(""yield1"" is var x1, x1); - yield return Dummy(""yield2"" is var x2, x2); - System.Console.WriteLine(x1); - } - - static object Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new object(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"yield1 -yield2 -yield1"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Yield_02() - { - var source = -@" -public class X -{ - public static void Main() - { - foreach (var o in Test()) - {} - } - - static System.Collections.IEnumerable Test() - { - bool f = true; - - if (f) - yield return Dummy(""yield1"" is var x1, x1); - - if (f) - { - yield return Dummy(""yield2"" is var x1, x1); - } - } - - static object Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new object(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"yield1 -yield2"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void Return_01() - { - var source = -@" -public class X -{ - public static void Main() - { - Test(); - } - - static object Test() - { - return Dummy(""return"" is var x1, x1); - } - - static object Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new object(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput:@"return"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Return_02() - { - var text = @" -public class Cls -{ - public static void Main() - { - Test0(true); - Test0(false); - } - - static object Test0(bool val) - { - if (val) - return Test2(1 is var x1, x1); - - if (!val) - { - return Test2(2 is var x1, x1); - } - - return null; - } - - static object Test2(object x, object y) - { - System.Console.Write(y); - return x; - } -}"; - var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: "12").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void Throw_01() - { - var source = -@" -public class X -{ - public static void Main() - { - Test(); - } - - static void Test() - { - try - { - throw Dummy(""throw"" is var x1, x1); - } - catch - { - } - } - - static System.Exception Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new System.ArgumentException(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: @"throw"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Throw_02() - { - var source = -@" -public class X -{ - public static void Main() - { - Test(true); - Test(false); - } - - static void Test(bool val) - { - try - { - if (val) - throw Dummy(""throw 1"" is var x1, x1); - - if (!val) - { - throw Dummy(""throw 2"" is var x1, x1); - } - } - catch - { - } - } - - static System.Exception Dummy(object y, object z) - { - System.Console.WriteLine(z); - return new System.ArgumentException(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"throw 1 -throw 2"); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(2, x1Decl.Length); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - } - - [Fact] - public void ScopeOfPatternVariables_Catch_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - try {} - catch when (true is var x1 && x1) - { - Dummy(x1); - } - } - - void Test4() - { - var x4 = 11; - Dummy(x4); - - try {} - catch when (true is var x4 && x4) - { - Dummy(x4); - } - } - - void Test6() - { - try {} - catch when (x6 && true is var x6) - { - Dummy(x6); - } - } - - void Test7() - { - try {} - catch when (true is var x7 && x7) - { - var x7 = 12; - Dummy(x7); - } - } - - void Test8() - { - try {} - catch when (true is var x8 && x8) - { - Dummy(x8); - } - - System.Console.WriteLine(x8); - } - - void Test9() - { - try {} - catch when (true is var x9 && x9) - { - Dummy(x9); - try {} - catch when (true is var x9 && x9) // 2 - { - Dummy(x9); - } - } - } - - void Test10() - { - try {} - catch when (y10 is var x10) - { - var y10 = 12; - Dummy(y10); - } - } - - //void Test11() - //{ - // try {} - // catch when (y11 is var x11) - // { - // let y11 = 12; - // Dummy(y11); - // } - //} - - void Test14() - { - try {} - catch when (Dummy(1 is var x14, - 2 is var x14, - x14)) - { - Dummy(x14); - } - } - - void Test15() - { - try {} - catch (System.Exception x15) - when (Dummy(1 is var x15, x15)) - { - Dummy(x15); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (25,33): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // catch when (true is var x4 && x4) - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(25, 33), - // (34,21): error CS0841: Cannot use local variable 'x6' before it is declared - // catch when (x6 && true is var x6) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(34, 21), - // (45,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // var x7 = 12; - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(45, 17), - // (58,34): error CS0103: The name 'x8' does not exist in the current context - // System.Console.WriteLine(x8); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(58, 34), - // (68,37): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // catch when (true is var x9 && x9) // 2 - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(68, 37), - // (78,21): error CS0103: The name 'y10' does not exist in the current context - // catch when (y10 is var x10) - Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(78, 21), - // (99,36): error CS0128: A local variable named 'x14' is already defined in this scope - // 2 is var x14, - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 36), - // (110,36): error CS0128: A local variable named 'x15' is already defined in this scope - // when (Dummy(1 is var x15, x15)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(110, 36) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(2, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(3, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").ToArray(); - Assert.Equal(2, x6Ref.Length); - VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").ToArray(); - Assert.Equal(2, x7Ref.Length); - VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); - VerifyNotAPatternLocal(model, x7Ref[1]); - - var x8Decl = GetPatternDeclarations(tree, "x8").Single(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(3, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); - VerifyNotInScope(model, x8Ref[2]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(2, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); - VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); - - var y10Ref = GetReferences(tree, "y10").ToArray(); - Assert.Equal(2, y10Ref.Length); - VerifyNotInScope(model, y10Ref[0]); - VerifyNotAPatternLocal(model, y10Ref[1]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(2, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - - var x15Decl = GetPatternDeclarations(tree, "x15").Single(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(2, x15Ref.Length); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl); - VerifyNotAPatternLocal(model, x15Ref[0]); - VerifyNotAPatternLocal(model, x15Ref[1]); - } - - [Fact] - public void Catch_01() - { - var source = -@" -public class X -{ - public static void Main() - { - try - { - throw new System.InvalidOperationException(); - } - catch (System.Exception e) when (Dummy(e is var x1, x1)) - { - System.Console.WriteLine(x1.GetType()); - } - } - - static bool Dummy(object y, object z) - { - System.Console.WriteLine(z.GetType()); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"System.InvalidOperationException -System.InvalidOperationException"); - } - - [Fact] - public void Catch_02() - { - var source = -@" -public class X -{ - public static void Main() - { - try - { - throw new System.InvalidOperationException(); - } - catch (System.Exception e) when (Dummy(e is var x1, x1)) - { - System.Action d = () => - { - System.Console.WriteLine(x1.GetType()); - }; - - System.Console.WriteLine(x1.GetType()); - d(); - } - } - - static bool Dummy(object y, object z) - { - System.Console.WriteLine(z.GetType()); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"System.InvalidOperationException -System.InvalidOperationException -System.InvalidOperationException"); - } - - [Fact] - public void Catch_03() - { - var source = -@" -public class X -{ - public static void Main() - { - try - { - throw new System.InvalidOperationException(); - } - catch (System.Exception e) when (Dummy(e is var x1, x1)) - { - System.Action d = () => - { - e = new System.NullReferenceException(); - System.Console.WriteLine(x1.GetType()); - }; - - System.Console.WriteLine(x1.GetType()); - d(); - System.Console.WriteLine(e.GetType()); - } - } - - static bool Dummy(object y, object z) - { - System.Console.WriteLine(z.GetType()); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"System.InvalidOperationException -System.InvalidOperationException -System.InvalidOperationException -System.NullReferenceException"); - } - - [Fact] - public void Catch_04() - { - var source = -@" -public class X -{ - public static void Main() - { - try - { - throw new System.InvalidOperationException(); - } - catch (System.Exception e) when (Dummy(e is var x1, x1)) - { - System.Action d = () => - { - e = new System.NullReferenceException(); - }; - - System.Console.WriteLine(x1.GetType()); - d(); - System.Console.WriteLine(e.GetType()); - } - } - - static bool Dummy(object y, object z) - { - System.Console.WriteLine(z.GetType()); - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - CompileAndVerify(compilation, expectedOutput: -@"System.InvalidOperationException -System.InvalidOperationException -System.NullReferenceException"); - } - - [Fact] - public void ScopeOfPatternVariables_LabeledStatement_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { -a: Dummy(true is var x1, x1); - { -b: Dummy(true is var x1, x1); - } -c: Dummy(true is var x1, x1); - } - - void Test2() - { - Dummy(x2, true is var x2); - } - - void Test3(int x3) - { -a: Dummy(true is var x3, x3); - } - - void Test4() - { - var x4 = 11; - Dummy(x4); -a: Dummy(true is var x4, x4); - } - - void Test5() - { -a: Dummy(true is var x5, x5); - var x5 = 11; - Dummy(x5); - } - - //void Test6() - //{ - // let x6 = 11; - // Dummy(x6); - //a: Dummy(true is var x6, x6); - //} - - //void Test7() - //{ - //a: Dummy(true is var x7, x7); - // let x7 = 11; - // Dummy(x7); - //} - - void Test8() - { -a: Dummy(true is var x8, x8, false is var x8, x8); - } - - void Test9(bool y9) - { - if (y9) -a: Dummy(true is var x9, x9); - } - - System.Action Test10(bool y10) - { - return () => - { - if (y10) -a: Dummy(true is var x10, x10); - }; - } - - void Test11() - { - Dummy(x11); -a: Dummy(true is var x11, x11); - } - - void Test12() - { -a: Dummy(true is var x12, x12); - Dummy(x12); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (65,1): error CS1023: Embedded statement cannot be a declaration or labeled statement - // a: Dummy(true is var x9, x9); - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x9, x9);").WithLocation(65, 1), - // (73,1): error CS1023: Embedded statement cannot be a declaration or labeled statement - // a: Dummy(true is var x10, x10); - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x10, x10);").WithLocation(73, 1), - // (14,31): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // b: Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 31), - // (16,27): error CS0128: A local variable named 'x1' is already defined in this scope - // c: Dummy(true is var x1, x1); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 27), - // (12,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x1, x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(12, 1), - // (14,1): warning CS0164: This label has not been referenced - // b: Dummy(true is var x1, x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(14, 1), - // (16,1): warning CS0164: This label has not been referenced - // c: Dummy(true is var x1, x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(16, 1), - // (21,15): error CS0841: Cannot use local variable 'x2' before it is declared - // Dummy(x2, true is var x2); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 15), - // (26,27): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // a: Dummy(true is var x3, x3); - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 27), - // (26,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x3, x3); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(26, 1), - // (33,27): error CS0128: A local variable named 'x4' is already defined in this scope - // a: Dummy(true is var x4, x4); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 27), - // (33,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x4, x4); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(33, 1), - // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope - // var x5 = 11; - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), - // (38,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x5, x5); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(38, 1), - // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used - // var x5 = 11; - Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), - // (59,48): error CS0128: A local variable named 'x8' is already defined in this scope - // a: Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 48), - // (59,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x8, x8, false is var x8, x8); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(59, 1), - // (65,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x9, x9); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(65, 1), - // (73,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x10, x10); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(73, 1), - // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared - // Dummy(x11); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), - // (80,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x11, x11); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(80, 1), - // (85,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x12, x12); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(85, 1) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(3, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); - VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); - - var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").Single(); - var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").Single(); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").Single(); - var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").Single(); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); - var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); - Assert.Equal(2, x4Ref.Length); - VerifyNotAPatternLocal(model, x4Ref[0]); - VerifyNotAPatternLocal(model, x4Ref[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - - var x5Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x5").Single(); - var x5Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x5").ToArray(); - Assert.Equal(2, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); - - var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").ToArray(); - var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(2, x8Ref.Length); - for (int i = 0; i < x8Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x9").Single(); - var x9Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x9").Single(); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); - - var x10Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x10").Single(); - var x10Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x10").Single(); - VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); - - var x11Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x11").Single(); - var x11Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x11").ToArray(); - Assert.Equal(2, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); - - var x12Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x12").Single(); - var x12Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x12").ToArray(); - Assert.Equal(2, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); - } - - [Fact] - public void ScopeOfPatternVariables_LabeledStatement_02() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - if (true) -a: Dummy(true is var x1); - - x1++; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - compilation.VerifyDiagnostics( - // (13,1): error CS1023: Embedded statement cannot be a declaration or labeled statement - // a: Dummy(true is var x1); - Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x1);").WithLocation(13, 1), - // (15,9): error CS0103: The name 'x1' does not exist in the current context - // x1++; - Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9), - // (13,1): warning CS0164: This label has not been referenced - // a: Dummy(true is var x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(13, 1) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); - VerifyModelForDeclarationPattern(model, x1Decl); - VerifyNotInScope(model, x1Ref); - } - - [Fact] - public void ScopeOfPatternVariables_LabeledStatement_03() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - void Dummy(params object[] x) {} - - void Test1() - { - SpeculateHere(); - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var statement = (LabeledStatementSyntax)SyntaxFactory.ParseStatement(@" -a: b: c:Dummy(11 is var x1, x1); -"); - - bool success = model.TryGetSpeculativeSemanticModel( - tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, - statement, out model); - Assert.True(success); - Assert.NotNull(model); - tree = statement.SyntaxTree; - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); - Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); - } - - [Fact] - public void Labeled_01() - { - var text = @" -public class Cls -{ - public static void Main() - { -a: Test1(2 is var x1); - System.Console.WriteLine(x1); - } - - static object Test1(bool x) - { - return null; - } -}"; - var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics( - // (6,1): warning CS0164: This label has not been referenced - // a: Test1(2 is var x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(6, 1) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact] - public void Labeled_02() - { - var text = @" -public class Cls -{ - public static void Main() - { - Test0(); - } - - static object Test0() - { - bool test = true; - - if (test) - { -a: Test2(2 is var x1, x1); - } - - return null; - } - - static object Test2(object x, object y) - { - System.Console.Write(y); - return x; - } -}"; - var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics( - // (15,1): warning CS0164: This label has not been referenced - // a: Test2(2 is var x1, x1); - Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(15, 1) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); - var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); - Assert.Equal(1, x1Decl.Length); - Assert.Equal(1, x1Ref.Length); - VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); - } - - [Fact, WorkItem(10465, "https://github.com/dotnet/roslyn/issues/10465")] - public void Constants_Fail() - { - var source = -@" -using System; -public class X -{ - public static void Main() - { - Console.WriteLine(1L is string); // warning: type mismatch - Console.WriteLine(1 is int[]); // warning: expression is never of the provided type - - Console.WriteLine(1L is string s); // error: type mismatch - Console.WriteLine(1 is int[] a); // error: expression is never of the provided type - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (7,27): warning CS0184: The given expression is never of the provided ('string') type - // Console.WriteLine(1L is string); // warning: type mismatch - Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1L is string").WithArguments("string").WithLocation(7, 27), - // (8,27): warning CS0184: The given expression is never of the provided ('int[]') type - // Console.WriteLine(1 is int[]); // warning: expression is never of the provided type - Diagnostic(ErrorCode.WRN_IsAlwaysFalse, "1 is int[]").WithArguments("int[]").WithLocation(8, 27), - // (10,33): error CS8121: An expression of type long cannot be handled by a pattern of type string. - // Console.WriteLine(1L is string s); // error: type mismatch - Diagnostic(ErrorCode.ERR_PatternWrongType, "string").WithArguments("long", "string").WithLocation(10, 33), - // (11,32): error CS8121: An expression of type int cannot be handled by a pattern of type int[]. - // Console.WriteLine(1 is int[] a); // error: expression is never of the provided type - Diagnostic(ErrorCode.ERR_PatternWrongType, "int[]").WithArguments("int", "int[]").WithLocation(11, 32) - ); - } - - [Fact, WorkItem(10465, "https://github.com/dotnet/roslyn/issues/10465")] - public void Types_Pass() - { - var source = -@" -using System; -public class X -{ - public static void Main() - { - Console.WriteLine(1 is 1); // true - Console.WriteLine(1L is int.MaxValue); // OK, but false - Console.WriteLine(1 is int.MaxValue); // false - Console.WriteLine(int.MaxValue is int.MaxValue); // true - Console.WriteLine(""foo"" is System.String); // true - Console.WriteLine(Int32.MaxValue is Int32.MaxValue); // true - Console.WriteLine(new int[] {1, 2} is int[] a); // true - object o = null; - switch (o) - { - case int[] b: - break; - case int.MaxValue: // constant, not a type - break; - case int i: - break; - case null: - Console.WriteLine(""null""); - break; - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics(); - CompileAndVerify(compilation, expectedOutput: -@"True -False -False -True -True -True -True -null"); - } - - [Fact, WorkItem(10459, "https://github.com/dotnet/roslyn/issues/10459")] - public void Typeswitch_01() - { - var source = -@" -using System; -public class X -{ - public static void Main(string[] args) - { - switch (args.GetType()) - { - case typeof(string): - Console.WriteLine(""string""); - break; - case typeof(string[]): - Console.WriteLine(""string[]""); - break; - case null: - Console.WriteLine(""null""); - break; - default: - Console.WriteLine(""default""); - break; - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (9,18): error CS0150: A constant value is expected - // case typeof(string): - Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string)").WithLocation(9, 18), - // (12,18): error CS0150: A constant value is expected - // case typeof(string[]): - Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string[])").WithLocation(12, 18) - ); - // If we support switching on System.Type as proposed, the expectation would be - // something like CompileAndVerify(compilation, expectedOutput: @"string[]"); - } - - [Fact, WorkItem(10529, "https://github.com/dotnet/roslyn/issues/10529")] - public void MissingTypeAndProperty() - { - var source = -@" -class Program -{ - public static void Main(string[] args) - { - { - if (obj.Property is var o) { } // `obj` doesn't exist. - } - { - var obj = new object(); - if (obj. is var o) { } - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (11,22): error CS1001: Identifier expected - // if (obj. is var o) { } - Diagnostic(ErrorCode.ERR_IdentifierExpected, "is").WithLocation(11, 22), - // (7,17): error CS0103: The name 'obj' does not exist in the current context - // if (obj.Property is var o) { } // `obj` doesn't exist. - Diagnostic(ErrorCode.ERR_NameNotInContext, "obj").WithArguments("obj").WithLocation(7, 17) - ); - var tree = compilation.SyntaxTrees[0]; - var model = compilation.GetSemanticModel(tree); - foreach (var isExpression in tree.GetRoot().DescendantNodes().OfType()) - { - var symbolInfo = model.GetSymbolInfo(isExpression.Expression); - Assert.Null(symbolInfo.Symbol); - Assert.True(symbolInfo.CandidateSymbols.IsDefaultOrEmpty); - Assert.Equal(CandidateReason.None, symbolInfo.CandidateReason); - } - } - - [Fact] - public void MixedDecisionTree() - { - var source = -@" -using System; -public class X -{ - public static void Main() - { - M(null); - M(1); - M((byte)1); - M((short)1); - M(2); - M((byte)2); - M((short)2); - M(""hmm""); - M(""bar""); - M(""baz""); - M(6); - } - - public static void M(object o) - { - switch (o) - { - case ""hmm"": - Console.WriteLine(""hmm""); break; - case null: - Console.WriteLine(""null""); break; - case 1: - Console.WriteLine(""int 1""); break; - case ((byte)1): - Console.WriteLine(""byte 1""); break; - case ((short)1): - Console.WriteLine(""short 1""); break; - case ""bar"": - Console.WriteLine(""bar""); break; - case object t when t != o: - Console.WriteLine(""impossible""); break; - case 2: - Console.WriteLine(""int 2""); break; - case ((byte)2): - Console.WriteLine(""byte 2""); break; - case ((short)2): - Console.WriteLine(""short 2""); break; - case ""baz"": - Console.WriteLine(""baz""); break; - default: - Console.WriteLine(""other "" + o); break; - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics(); - CompileAndVerify(compilation, expectedOutput: -@"null -int 1 -byte 1 -short 1 -int 2 -byte 2 -short 2 -hmm -bar -baz -other 6"); - } - - [Fact] - public void SemanticAnalysisWithPatternInCsharp6() - { - var source = -@"class Program -{ - public static void Main(string[] args) - { - switch (args.Length) - { - case 1 when true: - break; - } - } -}"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular6); - compilation.VerifyDiagnostics( - // (7,13): error CS8059: Feature 'pattern matching' is not available in C# 6. Please use language version 7 or greater. - // case 1 when true: - Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "case 1 when true:").WithArguments("pattern matching", "7").WithLocation(7, 13) - ); - } - - [Fact, WorkItem(11379, "https://github.com/dotnet/roslyn/issues/11379")] - public void DeclarationPatternWithStaticClass() - { - var source = -@"class Program -{ - public static void Main(string[] args) - { - object o = args; - switch (o) - { - case StaticType t: - break; - } - } -} -public static class StaticType -{ -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - // (8,18): error CS0723: Cannot declare a variable of static type 'StaticType' - // case StaticType t: - Diagnostic(ErrorCode.ERR_VarDeclIsStaticClass, "StaticType").WithArguments("StaticType").WithLocation(8, 18) - ); - } - - [Fact] - public void PatternVariablesAreMutable02() - { - var source = -@"class Program -{ - public static void Main(string[] args) - { - object o = "" whatever ""; - if (o is string s) - { - s = s.Trim(); - System.Console.WriteLine(s); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - ); - var comp = CompileAndVerify(compilation, expectedOutput: "whatever"); - } - - [Fact, WorkItem(12996, "https://github.com/dotnet/roslyn/issues/12996")] - public void TypeOfAVarPatternVariable() - { - var source = -@" -class Program -{ - public static void Main(string[] args) - { - } - - public static void Test(int val) - { - if (val is var o1) - { - System.Console.WriteLine(o1); - } - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); - compilation.VerifyDiagnostics( - ); - var tree = compilation.SyntaxTrees[0]; - - var model1 = compilation.GetSemanticModel(tree); - - var declaration = tree.GetRoot().DescendantNodes().OfType().Single(); - var o1 = GetReferences(tree, "o1").Single(); - - var typeInfo1 = model1.GetTypeInfo(declaration); - Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind); - Assert.Equal("System.Boolean", typeInfo1.Type.ToTestDisplayString()); - - typeInfo1 = model1.GetTypeInfo(o1); - Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind); - Assert.Equal("System.Int32", typeInfo1.Type.ToTestDisplayString()); - - var model2 = compilation.GetSemanticModel(tree); - - var typeInfo2 = model2.GetTypeInfo(o1); - Assert.Equal(SymbolKind.NamedType, typeInfo2.Type.Kind); - Assert.Equal("System.Int32", typeInfo2.Type.ToTestDisplayString()); - } - - [Fact] - public void Scope_For_06() - { - var source = -@" -public class X -{ - static bool Data = true; - public static void Main() - { - } - - bool Dummy(params object[] x) {return true;} - - void Test1() - { - for (var x1 = - Dummy(Dummy(true, Data is var x1) && x1) - ;;) - {} - } - - void Test2() - { - for (var x2 = true; - Dummy(Dummy(true, Data is var x2) && x2) - ;) - {} - } - - void Test3() - { - for (var x3 = true;; - Dummy(Dummy(true, Data is var x3) && x3) - ) - {} - } - - void Test4() - { - for (bool x4 = - Dummy(Dummy(true, Data is var x4) && x4) - ;;) - {} - } - - void Test5() - { - for (bool x5 = true; - Dummy(Dummy(true, Data is var x5) && x5) - ;) - {} - } - - void Test6() - { - for (bool x6 = true;; - Dummy(Dummy(true, Data is var x6) && x6) - ) - {} - } - - void Test7() - { - for (bool x7 = true, b = - Dummy(Dummy(true, Data is var x7) && x7) - ;;) - {} - } - - void Test8() - { - for (bool b1 = Dummy(Dummy(true, Data is var x8) && x8), - b2 = Dummy(Dummy(true, Data is var x8) && x8); - Dummy(Dummy(true, Data is var x8) && x8); - Dummy(Dummy(true, Data is var x8) && x8)) - {} - } - - void Test9() - { - for (bool b = x9, - b2 = Dummy(Dummy(true, Data is var x9) && x9); - Dummy(Dummy(true, Data is var x9) && x9); - Dummy(Dummy(true, Data is var x9) && x9)) - {} - } - - void Test10() - { - for (var b = x10; - Dummy(Dummy(true, Data is var x10) && x10) && - Dummy(Dummy(true, Data is var x10) && x10); - Dummy(Dummy(true, Data is var x10) && x10)) - {} - } - - void Test11() - { - for (bool b = x11; - Dummy(Dummy(true, Data is var x11) && x11) && - Dummy(Dummy(true, Data is var x11) && x11); - Dummy(Dummy(true, Data is var x11) && x11)) - {} - } - - void Test12() - { - for (Dummy(x12); - Dummy(x12) && - Dummy(Dummy(true, Data is var x12) && x12); - Dummy(Dummy(true, Data is var x12) && x12)) - {} - } - - void Test13() - { - for (var b = x13; - Dummy(x13); - Dummy(Dummy(true, Data is var x13) && x13), - Dummy(Dummy(true, Data is var x13) && x13)) - {} - } - - void Test14() - { - for (bool b = x14; - Dummy(x14); - Dummy(Dummy(true, Data is var x14) && x14), - Dummy(Dummy(true, Data is var x14) && x14)) - {} - } - - void Test15() - { - for (Dummy(x15); - Dummy(x15); - Dummy(x15), - Dummy(Dummy(true, Data is var x15) && x15)) - {} - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - compilation.VerifyDiagnostics( - // (14,44): error CS0128: A local variable named 'x1' is already defined in this scope - // Dummy(Dummy(true, Data is var x1) && x1) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(14, 44), - // (14,51): error CS0841: Cannot use local variable 'x1' before it is declared - // Dummy(Dummy(true, Data is var x1) && x1) - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(14, 51), - // (14,51): error CS0165: Use of unassigned local variable 'x1' - // Dummy(Dummy(true, Data is var x1) && x1) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(14, 51), - // (22,44): error CS0128: A local variable named 'x2' is already defined in this scope - // Dummy(Dummy(true, Data is var x2) && x2) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(22, 44), - // (30,44): error CS0128: A local variable named 'x3' is already defined in this scope - // Dummy(Dummy(true, Data is var x3) && x3) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(30, 44), - // (38,44): error CS0128: A local variable named 'x4' is already defined in this scope - // Dummy(Dummy(true, Data is var x4) && x4) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(38, 44), - // (38,51): error CS0165: Use of unassigned local variable 'x4' - // Dummy(Dummy(true, Data is var x4) && x4) - Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(38, 51), - // (46,44): error CS0128: A local variable named 'x5' is already defined in this scope - // Dummy(Dummy(true, Data is var x5) && x5) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(46, 44), - // (54,44): error CS0128: A local variable named 'x6' is already defined in this scope - // Dummy(Dummy(true, Data is var x6) && x6) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(54, 44), - // (62,44): error CS0128: A local variable named 'x7' is already defined in this scope - // Dummy(Dummy(true, Data is var x7) && x7) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x7").WithArguments("x7").WithLocation(62, 44), - // (70,49): error CS0128: A local variable named 'x8' is already defined in this scope - // b2 = Dummy(Dummy(true, Data is var x8) && x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(70, 49), - // (71,44): error CS0128: A local variable named 'x8' is already defined in this scope - // Dummy(Dummy(true, Data is var x8) && x8); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(71, 44), - // (72,44): error CS0128: A local variable named 'x8' is already defined in this scope - // Dummy(Dummy(true, Data is var x8) && x8)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(72, 44), - // (78,23): error CS0841: Cannot use local variable 'x9' before it is declared - // for (bool b = x9, - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(78, 23), - // (80,44): error CS0128: A local variable named 'x9' is already defined in this scope - // Dummy(Dummy(true, Data is var x9) && x9); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(80, 44), - // (81,44): error CS0128: A local variable named 'x9' is already defined in this scope - // Dummy(Dummy(true, Data is var x9) && x9)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(81, 44), - // (87,22): error CS0841: Cannot use local variable 'x10' before it is declared - // for (var b = x10; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x10").WithArguments("x10").WithLocation(87, 22), - // (89,44): error CS0128: A local variable named 'x10' is already defined in this scope - // Dummy(Dummy(true, Data is var x10) && x10); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(89, 44), - // (90,44): error CS0128: A local variable named 'x10' is already defined in this scope - // Dummy(Dummy(true, Data is var x10) && x10)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(90, 44), - // (96,23): error CS0841: Cannot use local variable 'x11' before it is declared - // for (bool b = x11; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(96, 23), - // (98,44): error CS0128: A local variable named 'x11' is already defined in this scope - // Dummy(Dummy(true, Data is var x11) && x11); - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(98, 44), - // (99,44): error CS0128: A local variable named 'x11' is already defined in this scope - // Dummy(Dummy(true, Data is var x11) && x11)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(99, 44), - // (105,20): error CS0841: Cannot use local variable 'x12' before it is declared - // for (Dummy(x12); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(105, 20), - // (106,20): error CS0841: Cannot use local variable 'x12' before it is declared - // Dummy(x12) && - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(106, 20), - // (108,44): error CS0128: A local variable named 'x12' is already defined in this scope - // Dummy(Dummy(true, Data is var x12) && x12)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x12").WithArguments("x12").WithLocation(108, 44), - // (114,22): error CS0841: Cannot use local variable 'x13' before it is declared - // for (var b = x13; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(114, 22), - // (115,20): error CS0841: Cannot use local variable 'x13' before it is declared - // Dummy(x13); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(115, 20), - // (117,44): error CS0128: A local variable named 'x13' is already defined in this scope - // Dummy(Dummy(true, Data is var x13) && x13)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x13").WithArguments("x13").WithLocation(117, 44), - // (123,23): error CS0841: Cannot use local variable 'x14' before it is declared - // for (bool b = x14; - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(123, 23), - // (124,20): error CS0841: Cannot use local variable 'x14' before it is declared - // Dummy(x14); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(124, 20), - // (126,44): error CS0128: A local variable named 'x14' is already defined in this scope - // Dummy(Dummy(true, Data is var x14) && x14)) - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(126, 44), - // (132,20): error CS0841: Cannot use local variable 'x15' before it is declared - // for (Dummy(x15); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(132, 20), - // (133,20): error CS0841: Cannot use local variable 'x15' before it is declared - // Dummy(x15); - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(133, 20), - // (134,20): error CS0841: Cannot use local variable 'x15' before it is declared - // Dummy(x15), - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(134, 20) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").Single(); - var x1Ref = GetReferences(tree, "x1").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); - VerifyNotAPatternLocal(model, x1Ref); - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); - VerifyNotAPatternLocal(model, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); - VerifyNotAPatternLocal(model, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); - VerifyNotAPatternLocal(model, x4Ref); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl); - VerifyNotAPatternLocal(model, x5Ref); - - var x6Decl = GetPatternDeclarations(tree, "x6").Single(); - var x6Ref = GetReferences(tree, "x6").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl); - VerifyNotAPatternLocal(model, x6Ref); - - var x7Decl = GetPatternDeclarations(tree, "x7").Single(); - var x7Ref = GetReferences(tree, "x7").Single(); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x7Decl); - VerifyNotAPatternLocal(model, x7Ref); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(4, x8Decl.Length); - Assert.Equal(4, x8Ref.Length); - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[2]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[3]); - - var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(3, x9Decl.Length); - Assert.Equal(4, x9Ref.Length); - VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[2]); - - var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); - var x10Ref = GetReferences(tree, "x10").ToArray(); - Assert.Equal(3, x10Decl.Length); - Assert.Equal(4, x10Ref.Length); - VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[2]); - - var x11Decl = GetPatternDeclarations(tree, "x11").ToArray(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(3, x11Decl.Length); - Assert.Equal(4, x11Ref.Length); - VerifyModelForDeclarationPattern(model, x11Decl[0], x11Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[1]); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[2]); - - var x12Decl = GetPatternDeclarations(tree, "x12").ToArray(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(2, x12Decl.Length); - Assert.Equal(4, x12Ref.Length); - VerifyModelForDeclarationPattern(model, x12Decl[0], x12Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x12Decl[1]); - - var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); - var x13Ref = GetReferences(tree, "x13").ToArray(); - Assert.Equal(2, x13Decl.Length); - Assert.Equal(4, x13Ref.Length); - VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x13Decl[1]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(4, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); - - var x15Decl = GetPatternDeclarations(tree, "x15").Single(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(4, x15Ref.Length); - VerifyModelForDeclarationPattern(model, x15Decl, x15Ref); - } - - [Fact] - public void Scope_SwitchLabelGuard_01() - { - var source = -@" -public class X -{ - public static void Main() - { - } - - bool Dummy(params object[] x) { return true; } - - public static int Data = 2; - - void Test1(int val) - { - switch (val) - { - case 0 when Dummy(Dummy(Data is var x1), x1): - Dummy(x1); - break; - case 1 when Dummy(Dummy(Data is var x1), x1): - Dummy(x1); - break; - case 2 when Dummy(Dummy(Data is var x1), x1): - Dummy(x1); - break; - } - } - - void Test2(int val) - { - switch (val) - { - case 0 when Dummy(x2, Dummy(Data is var x2)): - Dummy(x2); - break; - } - } - - void Test3(int x3, int val) - { - switch (val) - { - case 0 when Dummy(Dummy(Data is var x3), x3): - Dummy(x3); - break; - } - } - - void Test4(int val) - { - var x4 = 11; - switch (val) - { - case 0 when Dummy(Dummy(Data is var x4), x4): - Dummy(x4); - break; - case 1 when Dummy(x4): Dummy(x4); break; - } - } - - void Test5(int val) - { - switch (val) - { - case 0 when Dummy(Dummy(Data is var x5), x5): - Dummy(x5); - break; - } - - var x5 = 11; - Dummy(x5); - } - - //void Test6(int val) - //{ - // let x6 = 11; - // switch (val) - // { - // case 0 when Dummy(x6): - // Dummy(x6); - // break; - // case 1 when Dummy(Dummy(Data is var x6), x6): - // Dummy(x6); - // break; - // } - //} - - //void Test7(int val) - //{ - // switch (val) - // { - // case 0 when Dummy(Dummy(Data is var x7), x7): - // Dummy(x7); - // break; - // } - - // let x7 = 11; - // Dummy(x7); - //} - - void Test8(int val) - { - switch (val) - { - case 0 when Dummy(Dummy(Data is var x8), x8, Dummy(Data is var x8), x8): - Dummy(x8); - break; - } - } - - void Test9(int val) - { - switch (val) - { - case 0 when Dummy(x9): - int x9 = 9; - Dummy(x9); - break; - case 2 when Dummy(x9 = 9): - Dummy(x9); - break; - case 1 when Dummy(Dummy(Data is var x9), x9): - Dummy(x9); - break; - } - } - - //void Test10(int val) - //{ - // switch (val) - // { - // case 1 when Dummy(Dummy(Data is var x10), x10): - // Dummy(x10); - // break; - // case 0 when Dummy(x10): - // let x10 = 10; - // Dummy(x10); - // break; - // case 2 when Dummy(x10 = 10, x10): - // Dummy(x10); - // break; - // } - //} - - void Test11(int val) - { - switch (x11 ? val : 0) - { - case 0 when Dummy(x11): - Dummy(x11, 0); - break; - case 1 when Dummy(Dummy(Data is var x11), x11): - Dummy(x11, 1); - break; - } - } - - void Test12(int val) - { - switch (x12 ? val : 0) - { - case 0 when Dummy(Dummy(Data is var x12), x12): - Dummy(x12, 0); - break; - case 1 when Dummy(x12): - Dummy(x12, 1); - break; - } - } - - void Test13() - { - switch (Dummy(1, Data is var x13) ? x13 : 0) + Console.WriteLine(1 is 1); // true + Console.WriteLine(1L is int.MaxValue); // OK, but false + Console.WriteLine(1 is int.MaxValue); // false + Console.WriteLine(int.MaxValue is int.MaxValue); // true + Console.WriteLine(""foo"" is System.String); // true + Console.WriteLine(Int32.MaxValue is Int32.MaxValue); // true + Console.WriteLine(new int[] {1, 2} is int[] a); // true + object o = null; + switch (o) { - case 0 when Dummy(x13): - Dummy(x13); + case int[] b: break; - case 1 when Dummy(Dummy(Data is var x13), x13): - Dummy(x13); + case int.MaxValue: // constant, not a type break; - } - } - - void Test14(int val) - { - switch (val) - { - case 1 when Dummy(Dummy(Data is var x14), x14): - Dummy(x14); - Dummy(Dummy(Data is var x14), x14); - Dummy(x14); + case int i: break; - } - } - - void Test15(int val) - { - switch (val) - { - case 0 when Dummy(Dummy(Data is var x15), x15): - case 1 when Dummy(Dummy(Data is var x15), x15): - Dummy(x15); + case null: + Console.WriteLine(""null""); break; } } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - compilation.VerifyDiagnostics( - // (32,31): error CS0841: Cannot use local variable 'x2' before it is declared - // case 0 when Dummy(x2, Dummy(Data is var x2)): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(32, 31), - // (42,49): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(Dummy(Data is var x3), x3): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(42, 49), - // (53,49): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(Dummy(Data is var x4), x4): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(53, 49), - // (64,49): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 0 when Dummy(Dummy(Data is var x5), x5): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(64, 49), - // (104,76): error CS0128: A local variable named 'x8' is already defined in this scope - // case 0 when Dummy(Dummy(Data is var x8), x8, Dummy(Data is var x8), x8): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(104, 76), - // (114,31): error CS0841: Cannot use local variable 'x9' before it is declared - // case 0 when Dummy(x9): - Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(114, 31), - // (121,49): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(Dummy(Data is var x9), x9): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(121, 49), - // (146,17): error CS0103: The name 'x11' does not exist in the current context - // switch (x11 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(146, 17), - // (148,31): error CS0103: The name 'x11' does not exist in the current context - // case 0 when Dummy(x11): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(148, 31), - // (149,23): error CS0103: The name 'x11' does not exist in the current context - // Dummy(x11, 0); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(149, 23), - // (159,17): error CS0103: The name 'x12' does not exist in the current context - // switch (x12 ? val : 0) - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(159, 17), - // (164,31): error CS0103: The name 'x12' does not exist in the current context - // case 1 when Dummy(x12): - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(164, 31), - // (165,23): error CS0103: The name 'x12' does not exist in the current context - // Dummy(x12, 1); - Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(165, 23), - // (177,49): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(Dummy(Data is var x13), x13): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(177, 49), - // (187,49): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter - // case 1 when Dummy(Dummy(Data is var x14), x14): - Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(187, 49), - // (200,49): error CS0128: A local variable named 'x15' is already defined in this scope - // case 1 when Dummy(Dummy(Data is var x15), x15): - Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(200, 49), - // (200,55): error CS0165: Use of unassigned local variable 'x15' - // case 1 when Dummy(Dummy(Data is var x15), x15): - Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(200, 55) - ); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); - var x1Ref = GetReferences(tree, "x1").ToArray(); - Assert.Equal(3, x1Decl.Length); - Assert.Equal(6, x1Ref.Length); - for (int i = 0; i < x1Decl.Length; i++) - { - VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); - } - - var x2Decl = GetPatternDeclarations(tree, "x2").Single(); - var x2Ref = GetReferences(tree, "x2").ToArray(); - Assert.Equal(2, x2Ref.Length); - VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); - - var x3Decl = GetPatternDeclarations(tree, "x3").Single(); - var x3Ref = GetReferences(tree, "x3").ToArray(); - Assert.Equal(2, x3Ref.Length); - VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); - - var x4Decl = GetPatternDeclarations(tree, "x4").Single(); - var x4Ref = GetReferences(tree, "x4").ToArray(); - Assert.Equal(4, x4Ref.Length); - VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); - VerifyNotAPatternLocal(model, x4Ref[2]); - VerifyNotAPatternLocal(model, x4Ref[3]); - - var x5Decl = GetPatternDeclarations(tree, "x5").Single(); - var x5Ref = GetReferences(tree, "x5").ToArray(); - Assert.Equal(3, x5Ref.Length); - VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); - VerifyNotAPatternLocal(model, x5Ref[2]); - - var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); - var x8Ref = GetReferences(tree, "x8").ToArray(); - Assert.Equal(2, x8Decl.Length); - Assert.Equal(3, x8Ref.Length); - for (int i = 0; i < x8Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); - - var x9Decl = GetPatternDeclarations(tree, "x9").Single(); - var x9Ref = GetReferences(tree, "x9").ToArray(); - Assert.Equal(6, x9Ref.Length); - VerifyNotAPatternLocal(model, x9Ref[0]); - VerifyNotAPatternLocal(model, x9Ref[1]); - VerifyNotAPatternLocal(model, x9Ref[2]); - VerifyNotAPatternLocal(model, x9Ref[3]); - VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); - - var x11Decl = GetPatternDeclarations(tree, "x11").Single(); - var x11Ref = GetReferences(tree, "x11").ToArray(); - Assert.Equal(5, x11Ref.Length); - VerifyNotInScope(model, x11Ref[0]); - VerifyNotInScope(model, x11Ref[1]); - VerifyNotInScope(model, x11Ref[2]); - VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); - - var x12Decl = GetPatternDeclarations(tree, "x12").Single(); - var x12Ref = GetReferences(tree, "x12").ToArray(); - Assert.Equal(5, x12Ref.Length); - VerifyNotInScope(model, x12Ref[0]); - VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); - VerifyNotInScope(model, x12Ref[3]); - VerifyNotInScope(model, x12Ref[4]); - - var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); - var x13Ref = GetReferences(tree, "x13").ToArray(); - Assert.Equal(2, x13Decl.Length); - Assert.Equal(5, x13Ref.Length); - VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); - VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); - - var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); - var x14Ref = GetReferences(tree, "x14").ToArray(); - Assert.Equal(2, x14Decl.Length); - Assert.Equal(4, x14Ref.Length); - VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); - VerifyModelForDeclarationPattern(model, x14Decl[1], true); - - var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); - var x15Ref = GetReferences(tree, "x15").ToArray(); - Assert.Equal(2, x15Decl.Length); - Assert.Equal(3, x15Ref.Length); - for (int i = 0; i < x15Ref.Length; i++) - { - VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); - } - VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics(); + CompileAndVerify(compilation, expectedOutput: +@"True +False +False +True +True +True +True +null"); } - [Fact] - public void Scope_SwitchLabelGuard_02() + [Fact, WorkItem(10459, "https://github.com/dotnet/roslyn/issues/10459")] + public void Typeswitch_01() { var source = @" +using System; public class X { - public static int Data = 2; - - public static void Main() - { - Test(1); - } - - static void Test(int val) + public static void Main(string[] args) { - switch (val) + switch (args.GetType()) { - case 1 when Dummy(123, Data is var x1): - Dummy(x1 is var y1); - System.Console.WriteLine(y1); + case typeof(string): + Console.WriteLine(""string""); break; - } - } - - static bool Dummy(params object[] trash) - { - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); - } - - [Fact] - public void Scope_SwitchLabelGuard_03() - { - var source = -@" -public class X -{ - public static int Data = 2; - - public static void Main() - { - Test(1); - } - - static void Test(int val) - { - switch (val) - { - case 1 when Dummy(123, Data is var x1): - while (Dummy(x1 is var y1)) break; - System.Console.WriteLine(y1); + case typeof(string[]): + Console.WriteLine(""string[]""); + break; + case null: + Console.WriteLine(""null""); + break; + default: + Console.WriteLine(""default""); break; } } - - static bool Dummy(params object[] data) - { - return true; - } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (9,18): error CS0150: A constant value is expected + // case typeof(string): + Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string)").WithLocation(9, 18), + // (12,18): error CS0150: A constant value is expected + // case typeof(string[]): + Diagnostic(ErrorCode.ERR_ConstantExpected, "typeof(string[])").WithLocation(12, 18) + ); + // If we support switching on System.Type as proposed, the expectation would be + // something like CompileAndVerify(compilation, expectedOutput: @"string[]"); } - [Fact] - public void Scope_SwitchLabelGuard_04() + [Fact, WorkItem(10529, "https://github.com/dotnet/roslyn/issues/10529")] + public void MissingTypeAndProperty() { var source = @" -public class X +class Program { - public static int Data = 2; - - public static void Main() - { - Test(1); - } - - static void Test(int val) + public static void Main(string[] args) { - switch (val) { - case 1 when Dummy(123, Data is var x1): - do - val = 0; - while (Dummy(x1 is var y1) && false); - System.Console.WriteLine(y1); - break; + if (obj.Property is var o) { } // `obj` doesn't exist. + } + { + var obj = new object(); + if (obj. is var o) { } } - } - - static bool Dummy(params object[] data) - { - return true; } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (11,22): error CS1001: Identifier expected + // if (obj. is var o) { } + Diagnostic(ErrorCode.ERR_IdentifierExpected, "is").WithLocation(11, 22), + // (7,17): error CS0103: The name 'obj' does not exist in the current context + // if (obj.Property is var o) { } // `obj` doesn't exist. + Diagnostic(ErrorCode.ERR_NameNotInContext, "obj").WithArguments("obj").WithLocation(7, 17) + ); + var tree = compilation.SyntaxTrees[0]; var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + foreach (var isExpression in tree.GetRoot().DescendantNodes().OfType()) + { + var symbolInfo = model.GetSymbolInfo(isExpression.Expression); + Assert.Null(symbolInfo.Symbol); + Assert.True(symbolInfo.CandidateSymbols.IsDefaultOrEmpty); + Assert.Equal(CandidateReason.None, symbolInfo.CandidateReason); + } } [Fact] - public void Scope_SwitchLabelGuard_05() + public void MixedDecisionTree() { var source = @" +using System; public class X { - public static int Data = 2; - public static void Main() { - Test(1); + M(null); + M(1); + M((byte)1); + M((short)1); + M(2); + M((byte)2); + M((short)2); + M(""hmm""); + M(""bar""); + M(""baz""); + M(6); } - static void Test(int val) + public static void M(object o) { - switch (val) + switch (o) { - case 1 when Dummy(123, Data is var x1): - lock ((object)Dummy(x1 is var y1)) {} - System.Console.WriteLine(y1); - break; + case ""hmm"": + Console.WriteLine(""hmm""); break; + case null: + Console.WriteLine(""null""); break; + case 1: + Console.WriteLine(""int 1""); break; + case ((byte)1): + Console.WriteLine(""byte 1""); break; + case ((short)1): + Console.WriteLine(""short 1""); break; + case ""bar"": + Console.WriteLine(""bar""); break; + case object t when t != o: + Console.WriteLine(""impossible""); break; + case 2: + Console.WriteLine(""int 2""); break; + case ((byte)2): + Console.WriteLine(""byte 2""); break; + case ((short)2): + Console.WriteLine(""short 2""); break; + case ""baz"": + Console.WriteLine(""baz""); break; + default: + Console.WriteLine(""other "" + o); break; } } - - static bool Dummy(params object[] data) - { - return true; - } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics(); + CompileAndVerify(compilation, expectedOutput: +@"null +int 1 +byte 1 +short 1 +int 2 +byte 2 +short 2 +hmm +bar +baz +other 6"); } [Fact] - public void Scope_SwitchLabelGuard_06() + public void SemanticAnalysisWithPatternInCsharp6() { var source = -@" -public class X +@"class Program { - public static int Data = 2; - - public static void Main() - { - Test(1); - } - - static void Test(int val) + public static void Main(string[] args) { - switch (val) + switch (args.Length) { - case 1 when Dummy(123, Data is var x1): - if (Dummy(x1 is var y1)) {} - System.Console.WriteLine(y1); + case 1 when true: break; } } - - static bool Dummy(params object[] data) - { - return true; - } -} -"; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); +}"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular6); + compilation.VerifyDiagnostics( + // (7,13): error CS8059: Feature 'pattern matching' is not available in C# 6. Please use language version 7 or greater. + // case 1 when true: + Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "case 1 when true:").WithArguments("pattern matching", "7").WithLocation(7, 13) + ); } - [Fact] - public void Scope_SwitchLabelGuard_07() + [Fact, WorkItem(11379, "https://github.com/dotnet/roslyn/issues/11379")] + public void DeclarationPatternWithStaticClass() { var source = -@" -public class X +@"class Program { - public static int Data = 2; - - public static void Main() - { - Test(1); - } - - static void Test(int val) + public static void Main(string[] args) { - switch (val) + object o = args; + switch (o) { - case 1 when Dummy(123, Data is var x1): - switch (Dummy(x1 is var y1)) - { - default: break; - } - System.Console.WriteLine(y1); + case StaticType t: break; } } - - static bool Dummy(params object[] data) - { - return true; - } +} +public static class StaticType +{ } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,18): error CS0723: Cannot declare a variable of static type 'StaticType' + // case StaticType t: + Diagnostic(ErrorCode.ERR_VarDeclIsStaticClass, "StaticType").WithArguments("StaticType").WithLocation(8, 18) + ); } [Fact] - public void Scope_SwitchLabelGuard_08() + public void PatternVariablesAreMutable02() { var source = -@" -public class X +@"class Program { - public static int Data = 2; - - public static void Main() - { - foreach (var x in Test(1)) {} - } - - static System.Collections.IEnumerable Test(int val) + public static void Main(string[] args) { - switch (val) + object o = "" whatever ""; + if (o is string s) { - case 1 when Dummy(123, Data is var x1): - yield return Dummy(x1 is var y1); - System.Console.WriteLine(y1); - break; + s = s.Trim(); + System.Console.WriteLine(s); } } - - static bool Dummy(params object[] data) - { - return true; - } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); - - CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); - - var tree = compilation.SyntaxTrees.Single(); - var model = compilation.GetSemanticModel(tree); - - var yRef = GetReferences(tree, "y1").Single(); - - Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + ); + var comp = CompileAndVerify(compilation, expectedOutput: "whatever"); } - [Fact] - public void Scope_SwitchLabelGuard_09() + [Fact, WorkItem(12996, "https://github.com/dotnet/roslyn/issues/12996")] + public void TypeOfAVarPatternVariable() { var source = @" -public class X +class Program { - public static int Data = 2; - - public static void Main() + public static void Main(string[] args) { - Test(1); } - static void Test(int val) + public static void Test(int val) { - switch (val) + if (val is var o1) { - case 1 when Dummy(123, Data is var x1): - var z1 = x1 > 0 & Dummy(x1 is var y1); - System.Console.WriteLine(y1); - System.Console.WriteLine(z1); - break; + System.Console.WriteLine(o1); } } - - static bool Dummy(params object[] data) - { - return true; - } } "; - var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + ); + var tree = compilation.SyntaxTrees[0]; - CompileAndVerify(compilation, expectedOutput: -@"2 -True").VerifyDiagnostics(); + var model1 = compilation.GetSemanticModel(tree); - var tree = compilation.SyntaxTrees.Single(); + var declaration = tree.GetRoot().DescendantNodes().OfType().Single(); + var o1 = GetReferences(tree, "o1").Single(); + + var typeInfo1 = model1.GetTypeInfo(declaration); + Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind); + Assert.Equal("System.Boolean", typeInfo1.Type.ToTestDisplayString()); + + typeInfo1 = model1.GetTypeInfo(o1); + Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind); + Assert.Equal("System.Int32", typeInfo1.Type.ToTestDisplayString()); - var yRef = GetReferences(tree, "y1").Single(); - Assert.Equal("System.Int32", compilation.GetSemanticModel(tree).GetTypeInfo(yRef).Type.ToTestDisplayString()); + var model2 = compilation.GetSemanticModel(tree); - var zRef = GetReferences(tree, "z1").Single(); - Assert.Equal("System.Boolean", compilation.GetSemanticModel(tree).GetTypeInfo(zRef).Type.ToTestDisplayString()); + var typeInfo2 = model2.GetTypeInfo(o1); + Assert.Equal(SymbolKind.NamedType, typeInfo2.Type.Kind); + Assert.Equal("System.Int32", typeInfo2.Type.ToTestDisplayString()); } [Fact] @@ -14493,35 +2871,6 @@ unsafe struct S ); } - private static void VerifyModelNotSupported(SemanticModel model, DeclarationPatternSyntax decl, params IdentifierNameSyntax[] references) - { - Assert.Null(model.GetDeclaredSymbol(decl)); - Assert.Null(model.GetDeclaredSymbol((SyntaxNode)decl)); - - var identifierText = decl.Identifier.ValueText; - Assert.False(model.LookupSymbols(decl.SpanStart, name: identifierText).Any()); - Assert.False(model.LookupNames(decl.SpanStart).Contains(identifierText)); - - Assert.Null(model.GetSymbolInfo(decl.Type).Symbol); - - foreach (var reference in references) - { - Assert.Null(model.GetSymbolInfo(reference).Symbol); - Assert.False(model.LookupSymbols(reference.SpanStart, name: identifierText).Any()); - Assert.False(model.LookupNames(reference.SpanStart).Contains(identifierText)); - } - } - - private IEnumerable GetPatternDeclarations(SyntaxTree tree, string v) - { - return tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == v); - } - - private static IEnumerable GetReferences(SyntaxTree tree, string name) - { - return tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == name); - } - [Fact, WorkItem(13316, "https://github.com/dotnet/roslyn/issues/13316")] public void TypeAsExpressionInIsPattern() { diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Global.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Global.cs new file mode 100644 index 0000000000000..2defd9cc6e9ae --- /dev/null +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Global.cs @@ -0,0 +1,6166 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis.CSharp.Symbols; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Microsoft.CodeAnalysis.Test.Utilities; +using Roslyn.Test.Utilities; +using Xunit; +using Roslyn.Utilities; + +namespace Microsoft.CodeAnalysis.CSharp.UnitTests +{ + public class PatternMatchingTests_Global : PatternMatchingTestBase + { + [Fact] + public void GlobalCode_ExpressionStatement_01() + { + string source = +@" +H.Dummy(1 is int x1); +H.Dummy(x1); + +object x2; +H.Dummy(2 is int x2); + +H.Dummy(3 is int x3); +object x3; + +H.Dummy((41 is int x4), + (42 is int x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,20): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 20), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,20): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 20), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ExpressionStatement_02() + { + string source = +@" +H.Dummy(1 is var x1); +H.Dummy(x1); + +object x2; +H.Dummy(2 is var x2); + +H.Dummy(3 is var x3); +object x3; + +H.Dummy((41 is var x4), + (42 is var x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,20): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 20), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,20): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 20), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ExpressionStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +H.Dummy(1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_IfStatement_01() + { + string source = +@" +if ((1 is int x1)) {} +H.Dummy(x1); + +object x2; +if ((2 is int x2)) {} + +if ((3 is int x3)) {} +object x3; + +if (H.Dummy((41 is int x4), + (42 is int x4))) {} + +if ((51 is int x5)) +{ + H.Dummy(""52"" is string x5); + H.Dummy(x5); +} +H.Dummy(x5); + +int x6 = 6; +if (H.Dummy()) +{ + string x6 = ""6""; + H.Dummy(x6); +} + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,15): error CS0102: The type 'Script' already contains a definition for 'x2' + // if ((2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 15), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,24): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 24), + // (30,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(30, 17), + // (30,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(30, 21), + // (30,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(30, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,15): error CS0102: The type '' already contains a definition for 'x2' + // if ((2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 15), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,24): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 24), + // (16,28): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is string x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(16, 28), + // (24,12): error CS0102: The type '' already contains a definition for 'x6' + // string x6 = "6"; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("", "x6").WithLocation(24, 12), + // (30,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(30, 13), + // (30,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(30, 17), + // (30,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(30, 21), + // (30,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(30, 25), + // (30,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(30, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_IfStatement_02() + { + string source = +@" +if ((1 is var x1)) {} +H.Dummy(x1); + +object x2; +if ((2 is var x2)) {} + +if ((3 is var x3)) {} +object x3; + +if (H.Dummy((41 is var x4), + (42 is var x4))) {} + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +if ((51 is var x5)) +{ + H.Dummy(""52"" is var x5); + H.Dummy(x5); +} +H.Dummy(x5); + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,15): error CS0102: The type 'Script' already contains a definition for 'x2' + // if ((2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 15), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,24): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 24), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[0], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[1]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,15): error CS0102: The type '' already contains a definition for 'x2' + // if ((2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 15), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,24): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 24), + // (21,25): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(21, 25), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25), + // (16,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(16, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_IfStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +if ((1 is var x1)) +{ + H.Dummy(""11"" is var x1); + System.Console.WriteLine(x1); +} +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static bool Dummy(params object[] x) {return false;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_IfStatement_04() + { + string source = +@" +System.Console.WriteLine(x1); +if ((1 is var x1)) + H.Dummy((""11"" is var x1), x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static void Dummy(object x, object y) + { + System.Console.WriteLine(y); + } +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_YieldReturnStatement_01() + { + string source = +@" +yield return (1 is int x1); +H.Dummy(x1); + +object x2; +yield return (2 is int x2); + +yield return (3 is int x3); +object x3; + +yield return H.Dummy((41 is int x4), + (42 is int x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // yield return (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 33), + // (2,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (1 is int x1); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(2, 1), + // (6,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (2 is int x2); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(6, 1), + // (8,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (3 is int x3); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(8, 1), + // (11,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return H.Dummy((41 is int x4), + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(11, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // yield return (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 33), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_YieldReturnStatement_02() + { + string source = +@" +yield return (1 is var x1); +H.Dummy(x1); + +object x2; +yield return (2 is var x2); + +yield return (3 is var x3); +object x3; + +yield return H.Dummy((41 is var x4), + (42 is var x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // yield return (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 33), + // (2,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (1 is var x1); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(2, 1), + // (6,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (2 is var x2); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(6, 1), + // (8,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return (3 is var x3); + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(8, 1), + // (11,1): error CS7020: Cannot use 'yield' in top-level script code + // yield return H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_YieldNotAllowedInScript, "yield").WithLocation(11, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", ((FieldSymbol)compilation.GetSemanticModel(tree).GetDeclaredSymbol(x1Decl)).Type.ToTestDisplayString()); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // yield return (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 33), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ReturnStatement_01() + { + string source = +@" +return (1 is int x1); +H.Dummy(x1); + +object x2; +return (2 is int x2); + +return (3 is int x3); +object x3; + +return H.Dummy((41 is int x4), + (42 is int x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // return (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 27), + // (3,1): warning CS0162: Unreachable code detected + // H.Dummy(x1); + Diagnostic(ErrorCode.WRN_UnreachableCode, "H").WithLocation(3, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // return (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 27), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ReturnStatement_02() + { + string source = +@" +return (1 is var x1); +H.Dummy(x1); + +object x2; +return (2 is var x2); + +return (3 is var x3); +object x3; + +return H.Dummy((41 is var x4), + (42 is var x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // return (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 27), + // (3,1): warning CS0162: Unreachable code detected + // H.Dummy(x1); + Diagnostic(ErrorCode.WRN_UnreachableCode, "H").WithLocation(3, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // return (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 27), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ReturnStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +Test(); +return H.Dummy((1 is var x1), x1); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static bool Dummy(object x, object y) + { + System.Console.WriteLine(y); + return true; + } +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +0 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_ThrowStatement_01() + { + string source = +@" +throw H.Dummy(1 is int x1); +H.Dummy(x1); + +object x2; +throw H.Dummy(2 is int x2); + +throw H.Dummy(3 is int x3); +object x3; + +throw H.Dummy((41 is int x4), + (42 is int x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static System.Exception Dummy(params object[] x) {return null;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // throw H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 26), + // (3,1): warning CS0162: Unreachable code detected + // H.Dummy(x1); + Diagnostic(ErrorCode.WRN_UnreachableCode, "H").WithLocation(3, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // throw H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 26), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_ThrowStatement_02() + { + string source = +@" +throw H.Dummy(1 is var x1); +H.Dummy(x1); + +object x2; +throw H.Dummy(2 is var x2); + +throw H.Dummy(3 is var x3); +object x3; + +throw H.Dummy((41 is var x4), + (42 is var x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static System.Exception Dummy(params object[] x) {return null;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // throw H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 26), + // (3,1): warning CS0162: Unreachable code detected + // H.Dummy(x1); + Diagnostic(ErrorCode.WRN_UnreachableCode, "H").WithLocation(3, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", ((FieldSymbol)compilation.GetSemanticModel(tree).GetDeclaredSymbol(x1Decl)).Type.ToTestDisplayString()); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // throw H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 26), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_SwitchStatement_01() + { + string source = +@" +switch ((1 is int x1)) {default: break;} +H.Dummy(x1); + +object x2; +switch ((2 is int x2)) {default: break;} + +switch ((3 is int x3)) {default: break;} +object x3; + +switch (H.Dummy((41 is int x4), + (42 is int x4))) {default: break;} + +switch ((51 is int x5)) +{ +default: + H.Dummy(""52"" is string x5); + H.Dummy(x5); + break; +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,19): error CS0102: The type 'Script' already contains a definition for 'x2' + // switch ((2 is int x2)) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 19), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,28): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4))) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 28), + // (25,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(25, 17), + // (25,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(25, 21), + // (25,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(25, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,19): error CS0102: The type '' already contains a definition for 'x2' + // switch ((2 is int x2)) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 19), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,28): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4))) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 28), + // (17,28): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is string x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(17, 28), + // (25,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(25, 13), + // (25,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(25, 17), + // (25,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(25, 21), + // (25,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(25, 25), + // (25,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(25, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_SwitchStatement_02() + { + string source = +@" +switch ((1 is var x1)) {default: break;} +H.Dummy(x1); + +object x2; +switch ((2 is var x2)) {default: break;} + +switch ((3 is var x3)) {default: break;} +object x3; + +switch (H.Dummy((41 is var x4), + (42 is var x4))) {default: break;} + +switch ((51 is var x5)) +{ +default: + H.Dummy(""52"" is var x5); + H.Dummy(x5); + break; +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,19): error CS0102: The type 'Script' already contains a definition for 'x2' + // switch ((2 is var x2)) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 19), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,28): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4))) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 28), + // (25,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(25, 17), + // (25,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(25, 21), + // (25,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(25, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,19): error CS0102: The type '' already contains a definition for 'x2' + // switch ((2 is var x2)) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 19), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,28): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4))) {default: break;} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 28), + // (17,25): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(17, 25), + // (6,15): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // switch ((2 is var x2)) {default: break;} + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(6, 15), + // (8,15): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // switch ((3 is var x3)) {default: break;} + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(8, 15), + // (11,24): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // switch (H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(11, 24), + // (12,24): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // (42 is var x4))) {default: break;} + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(12, 24), + // (14,16): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // switch ((51 is var x5)) + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(14, 16), + // (17,21): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // H.Dummy("52" is var x5); + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(17, 21), + // (2,15): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // switch ((1 is var x1)) {default: break;} + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(2, 15), + // (25,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(25, 13), + // (25,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(25, 17), + // (25,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(25, 21), + // (25,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(25, 25), + // (25,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(25, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_SwitchStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +switch ((1 is var x1)) +{ +default: + H.Dummy(""11"" is var x1); + System.Console.WriteLine(x1); + break; +} +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_WhileStatement_01() + { + string source = +@" +while ((1 is int x1)) {} +H.Dummy(x1); + +object x2; +while ((2 is int x2)) {} + +while ((3 is int x3)) {} +object x3; + +while (H.Dummy((41 is int x4), + (42 is int x4))) {} + +while ((51 is int x5)) +{ + H.Dummy(""52"" is string x5); + H.Dummy(x5); +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // while ((2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 27), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // while ((2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 27), + // (16,28): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is string x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(16, 28), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_WhileStatement_02() + { + string source = +@" +while ((1 is var x1)) {} +H.Dummy(x1); + +object x2; +while ((2 is var x2)) {} + +while ((3 is var x3)) {} +object x3; + +while (H.Dummy((41 is var x4), + (42 is var x4))) {} + +while ((51 is var x5)) +{ + H.Dummy(""52"" is var x5); + H.Dummy(x5); +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,18): error CS0102: The type 'Script' already contains a definition for 'x2' + // while ((2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 27), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,18): error CS0102: The type '' already contains a definition for 'x2' + // while ((2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 18), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,27): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 27), + // (16,25): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(16, 25), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_WhileStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +while ((1 is var x1)) +{ + H.Dummy(""11"" is var x1); + System.Console.WriteLine(x1); + break; +} +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static bool Dummy(params object[] x) {return false;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_WhileStatement_04() + { + string source = +@" +bool x0 = true; +System.Console.WriteLine(x1); +while (x0 && (1 is var x1)) + H.Dummy((""11"" is var x1) && (x0 = false), x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static void Dummy(object x, object y) + { + System.Console.WriteLine(y); + } +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_DoStatement_01() + { + string source = +@" +do {} while ((1 is int x1)); +H.Dummy(x1); + +object x2; +do {} while ((2 is int x2)); + +do {} while ((3 is int x3)); +object x3; + +do {} while (H.Dummy((41 is int x4), + (42 is int x4))); + +do +{ + H.Dummy(""52"" is string x5); + H.Dummy(x5); +} +while ((51 is int x5)); +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // do {} while ((2 is int x2)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4))); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 33), + // (24,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(24, 17), + // (24,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(24, 21), + // (24,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(24, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref[0]); + VerifyModelForDeclarationField(model, x5Decl[1], x5Ref[1], x5Ref[2]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // do {} while ((2 is int x2)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4))); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 33), + // (19,19): error CS0102: The type '' already contains a definition for 'x5' + // while ((51 is int x5)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(19, 19), + // (24,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(24, 13), + // (24,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(24, 17), + // (24,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(24, 21), + // (24,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(24, 25), + // (24,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(24, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_DoStatement_02() + { + string source = +@" +do {} while ((1 is var x1)); +H.Dummy(x1); + +object x2; +do {} while ((2 is var x2)); + +do {} while ((3 is var x3)); +object x3; + +do {} while (H.Dummy((41 is var x4), + (42 is var x4))); + +do +{ + H.Dummy(""52"" is var x5); + H.Dummy(x5); +} +while ((51 is var x5)); +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // do {} while ((2 is var x2)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4))); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 33), + // (24,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(24, 17), + // (24,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(24, 21), + // (24,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(24, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref[0]); + VerifyModelForDeclarationField(model, x5Decl[1], x5Ref[1], x5Ref[2]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // do {} while ((2 is var x2)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,33): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4))); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 33), + // (19,19): error CS0102: The type '' already contains a definition for 'x5' + // while ((51 is var x5)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(19, 19), + // (24,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(24, 13), + // (24,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(24, 17), + // (24,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(24, 21), + // (24,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(24, 25), + // (24,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(24, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_DoStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +do +{ + H.Dummy(""11"" is var x1); + System.Console.WriteLine(x1); +} +while ((1 is var x1) && false); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static bool Dummy(params object[] x) {return false;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[1]); + VerifyModelForDeclarationField(model, x1Decl[1], x1Ref[0], x1Ref[2]); + } + + [Fact] + public void GlobalCode_DoStatement_04() + { + string source = +@" +System.Console.WriteLine(x1); +do + H.Dummy((""11"" is var x1), x1); +while ((1 is var x1) && false); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static void Dummy(object x, object y) + { + System.Console.WriteLine(y); + } +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[1]); + VerifyModelForDeclarationField(model, x1Decl[1], x1Ref[0], x1Ref[2]); + } + + [Fact] + public void GlobalCode_LockStatement_01() + { + string source = +@" +lock (H.Dummy(1 is int x1)) {} +H.Dummy(x1); + +object x2; +lock (H.Dummy(2 is int x2)) {} + +lock (H.Dummy(3 is int x3)) {} +object x3; + +lock (H.Dummy((41 is int x4), + (42 is int x4))) {} + +lock (H.Dummy(51 is int x5)) +{ + H.Dummy(""52"" is string x5); + H.Dummy(x5); +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static object Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // lock (H.Dummy(2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 26), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // lock (H.Dummy(2 is int x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 26), + // (16,28): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is string x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(16, 28), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_LockStatement_02() + { + string source = +@" +lock (H.Dummy(1 is var x1)) {} +H.Dummy(x1); + +object x2; +lock (H.Dummy(2 is var x2)) {} + +lock (H.Dummy(3 is var x3)) {} +object x3; + +lock (H.Dummy((41 is var x4), + (42 is var x4))) {} + +lock (H.Dummy(51 is var x5)) +{ + H.Dummy(""52"" is var x5); + H.Dummy(x5); +} +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static object Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,24): error CS0102: The type 'Script' already contains a definition for 'x2' + // lock (H.Dummy(2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 26), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Decl.Length); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl[0], x5Ref[1], x5Ref[2]); + VerifyModelForDeclarationPattern(model, x5Decl[1], x5Ref[0]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,24): error CS0102: The type '' already contains a definition for 'x2' + // lock (H.Dummy(2 is var x2)) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 24), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,26): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4))) {} + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 26), + // (16,25): error CS0102: The type '' already contains a definition for 'x5' + // H.Dummy("52" is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("", "x5").WithLocation(16, 25), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_LockStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +lock (H.Dummy(1 is var x1)) +{ + H.Dummy(""11"" is var x1); + System.Console.WriteLine(x1); +} +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static object Dummy(params object[] x) {return new object();} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void GlobalCode_LockStatement_04() + { + string source = +@" +System.Console.WriteLine(x1); +lock (H.Dummy(1 is var x1)) + H.Dummy((""11"" is var x1), x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static void Dummy(object x, object y) + { + System.Console.WriteLine(y); + } + public static object Dummy(object x) + { + return x; + } +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +11 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact(Skip = "https://github.com/dotnet/roslyn/issues/13716")] + [CompilerTrait(CompilerFeature.Tuples)] + public void GlobalCode_DeconstructionDeclarationStatement_01() + { + string source = +@" +(bool a, int b) = ((1 is int x1), 1); +H.Dummy(x1); + +object x2; +(bool c, int d) = ((2 is int x2), 2); + +(bool e, int f) = ((3 is int x3), 3); +object x3; + +(bool g, bool h) = ((41 is int x4), + (42 is int x4)); + +(bool x5, bool x6) = ((5 is int x5), + (6 is int x6)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (2,17): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // (bool a, int b) = ((1 is int x1), 1); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(2, 17), + // (2,17): error CS1525: Invalid expression term '=' + // (bool a, int b) = ((1 is int x1), 1); + Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(2, 17), + // (6,17): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(6, 17), + // (6,17): error CS1525: Invalid expression term '=' + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(6, 17), + // (8,17): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // (bool e, int f) = ((3 is int x3), 3); + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(8, 17), + // (8,17): error CS1525: Invalid expression term '=' + // (bool e, int f) = ((3 is int x3), 3); + Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(8, 17), + // (11,18): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // (bool g, bool h) = ((41 is int x4), + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(11, 18), + // (11,18): error CS1525: Invalid expression term '=' + // (bool g, bool h) = ((41 is int x4), + Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(11, 18), + // (14,20): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // (bool x5, bool x6) = ((5 is int x5), + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(14, 20), + // (14,20): error CS1525: Invalid expression term '=' + // (bool x5, bool x6) = ((5 is int x5), + Diagnostic(ErrorCode.ERR_InvalidExprTerm, "=").WithArguments("=").WithLocation(14, 20), + // (6,30): error CS0102: The type 'Script' already contains a definition for 'x2' + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 32), + // (19,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(19, 17), + // (19,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(19, 21), + // (19,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(19, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl, x5Ref[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationField(model, x6Decl, x6Ref[1]); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,30): error CS0102: The type '' already contains a definition for 'x2' + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 32), + // (19,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(19, 13), + // (19,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(19, 17), + // (19,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(19, 21), + // (19,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(19, 25), + // (19,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(19, 29), + // (19,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(19, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_LabeledStatement_01() + { + string source = +@" +a: H.Dummy(1 is int x1); +H.Dummy(x1); + +object x2; +b: H.Dummy(2 is int x2); + +c: H.Dummy(3 is int x3); +object x3; + +d: H.Dummy((41 is int x4), + (42 is int x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,21): error CS0102: The type 'Script' already contains a definition for 'x2' + // b: H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 21), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,23): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 23), + // (2,1): warning CS0164: This label has not been referenced + // a: H.Dummy(1 is int x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(2, 1), + // (6,1): warning CS0164: This label has not been referenced + // b: H.Dummy(2 is int x2); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(6, 1), + // (8,1): warning CS0164: This label has not been referenced + // c: H.Dummy(3 is int x3); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(8, 1), + // (11,1): warning CS0164: This label has not been referenced + // d: H.Dummy((41 is int x4), + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "d").WithLocation(11, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,21): error CS0102: The type '' already contains a definition for 'x2' + // b: H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 21), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,23): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 23), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_LabeledStatement_02() + { + string source = +@" +a: H.Dummy(1 is var x1); +H.Dummy(x1); + +object x2; +b: H.Dummy(2 is var x2); + +c: H.Dummy(3 is var x3); +object x3; + +d: H.Dummy((41 is var x4), + (42 is var x4)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,21): error CS0102: The type 'Script' already contains a definition for 'x2' + // b: H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 21), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,23): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 23), + // (2,1): warning CS0164: This label has not been referenced + // a: H.Dummy(1 is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(2, 1), + // (6,1): warning CS0164: This label has not been referenced + // b: H.Dummy(2 is var x2); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(6, 1), + // (8,1): warning CS0164: This label has not been referenced + // c: H.Dummy(3 is var x3); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(8, 1), + // (11,1): warning CS0164: This label has not been referenced + // d: H.Dummy((41 is var x4), + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "d").WithLocation(11, 1), + // (16,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(16, 17), + // (16,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(16, 21), + // (16,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,21): error CS0102: The type '' already contains a definition for 'x2' + // b: H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 21), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,23): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 23), + // (16,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(16, 13), + // (16,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(16, 17), + // (16,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(16, 21), + // (16,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(16, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + public void GlobalCode_LabeledStatement_03() + { + string source = +@" +System.Console.WriteLine(x1); +a:b:c:H.Dummy(1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics( + // (3,1): warning CS0164: This label has not been referenced + // a:b:c:(1 is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(3, 1), + // (3,3): warning CS0164: This label has not been referenced + // a:b:c:(1 is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(3, 3), + // (3,5): warning CS0164: This label has not been referenced + // a:b:c:(1 is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(3, 5) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_LabeledStatement_04() + { + string source = +@" +a: +bool b = (1 is int x1); +H.Dummy(x1); +object x2; +c: +bool d = (2 is int x2); +e: +bool f = (3 is int x3); +object x3; +g: +bool h = H.Dummy((41 is int x4), + (42 is int x4)); +i: +bool x5 = (5 is int x5); +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,20): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d = (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 20), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,29): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 29), + // (2,1): warning CS0164: This label has not been referenced + // a: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(2, 1), + // (6,1): warning CS0164: This label has not been referenced + // c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(6, 1), + // (8,1): warning CS0164: This label has not been referenced + // e: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "e").WithLocation(8, 1), + // (11,1): warning CS0164: This label has not been referenced + // g: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "g").WithLocation(11, 1), + // (14,1): warning CS0164: This label has not been referenced + // i: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "i").WithLocation(14, 1), + // (20,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(20, 17), + // (20,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(20, 21), + // (20,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(20, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl, x5Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b = (1 is int x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(3, 16), + // (7,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d = (2 is int x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x2").WithLocation(7, 16), + // (9,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f = (3 is int x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 16), + // (12,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h = H.Dummy((41 is int x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(12, 25), + // (13,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 25), + // (13,29): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 29), + // (15,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool x5 = (5 is int x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(15, 17), + // (20,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(20, 13), + // (20,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(20, 17), + // (20,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(20, 21), + // (20,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(20, 25), + // (20,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + } + } + + [Fact] + public void GlobalCode_LabeledStatement_05() + { + string source = +@" +a: +bool b = (1 is var x1); +H.Dummy(x1); +object x2; +c: +bool d = (2 is var x2); +e: +bool f = (3 is var x3); +object x3; +g: +bool h = H.Dummy((41 is var x4), + (42 is var x4)); +i: +bool x5 = (5 is var x5); +H.Dummy(x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,20): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d = (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 20), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,29): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 29), + // (2,1): warning CS0164: This label has not been referenced + // a: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(2, 1), + // (6,1): warning CS0164: This label has not been referenced + // c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(6, 1), + // (8,1): warning CS0164: This label has not been referenced + // e: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "e").WithLocation(8, 1), + // (11,1): warning CS0164: This label has not been referenced + // g: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "g").WithLocation(11, 1), + // (14,1): warning CS0164: This label has not been referenced + // i: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "i").WithLocation(14, 1), + // (20,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(20, 17), + // (20,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(20, 21), + // (20,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(20, 25) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationField(model, x5Decl, x5Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b = (1 is var x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x1").WithLocation(3, 16), + // (7,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d = (2 is var x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x2").WithLocation(7, 16), + // (9,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f = (3 is var x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x3").WithLocation(9, 16), + // (12,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h = H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(12, 25), + // (13,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(13, 25), + // (13,29): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 29), + // (15,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool x5 = (5 is var x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x5").WithLocation(15, 17), + // (20,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(20, 13), + // (20,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(20, 17), + // (20,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(20, 21), + // (20,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(20, 25), + // (20,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + } + } + + [Fact] + public void GlobalCode_LabeledStatement_06() + { + string source = +@" +System.Console.WriteLine(x1); +a:b:c: +var d = (1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics( + // (3,1): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(3, 1), + // (3,3): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(3, 3), + // (3,5): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(3, 5) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void GlobalCode_LabeledStatement_07() + { + string source = +@"l1: +(bool a, int b) = ((1 is int x1), 1); +H.Dummy(x1); +object x2; +l2: +(bool c, int d) = ((2 is int x2), 2); +l3: +(bool e, int f) = ((3 is int x3), 3); +object x3; +l4: +(bool g, bool h) = ((41 is int x4), + (42 is int x4)); +l5: +(bool x5, bool x6) = ((5 is int x5), + (6 is int x6)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,30): error CS0102: The type 'Script' already contains a definition for 'x2' + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 32), + // (14,33): error CS0102: The type 'Script' already contains a definition for 'x5' + // (bool x5, bool x6) = ((5 is int x5), + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(14, 33), + // (15,33): error CS0102: The type 'Script' already contains a definition for 'x6' + // (6 is int x6)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(15, 33), + // (1,1): warning CS0164: This label has not been referenced + // l1: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l1").WithLocation(1, 1), + // (5,1): warning CS0164: This label has not been referenced + // l2: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l2").WithLocation(5, 1), + // (7,1): warning CS0164: This label has not been referenced + // l3: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l3").WithLocation(7, 1), + // (10,1): warning CS0164: This label has not been referenced + // l4: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l4").WithLocation(10, 1), + // (13,1): warning CS0164: This label has not been referenced + // l5: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l5").WithLocation(13, 1), + // (19,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(19, 17), + // (19,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(19, 21), + // (19,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(19, 25), + // (19,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(19, 29), + // (19,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(19, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(1, x5Ref.Length); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(1, x6Ref.Length); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,30): error CS0102: The type '' already contains a definition for 'x2' + // (bool c, int d) = ((2 is int x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type '' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 32), + // (19,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(19, 13), + // (19,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(19, 17), + // (19,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(19, 21), + // (19,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(19, 25), + // (19,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(19, 29), + // (19,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(19, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void GlobalCode_LabeledStatement_08() + { + string source = +@"l1: +(bool a, int b) = ((1 is var x1), 1); +H.Dummy(x1); +object x2; +l2: +(bool c, int d) = ((2 is var x2), 2); +l3: +(bool e, int f) = ((3 is var x3), 3); +object x3; +l4: +(bool g, bool h) = ((41 is var x4), + (42 is var x4)); +l5: +(bool x5, bool x6) = ((5 is var x5), + (6 is var x6)); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (6,30): error CS0102: The type 'Script' already contains a definition for 'x2' + // (bool c, int d) = ((2 is var x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(12, 32), + // (14,33): error CS0102: The type 'Script' already contains a definition for 'x5' + // (bool x5, bool x6) = ((5 is var x5), + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(14, 33), + // (15,33): error CS0102: The type 'Script' already contains a definition for 'x6' + // (6 is var x6)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(15, 33), + // (1,1): warning CS0164: This label has not been referenced + // l1: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l1").WithLocation(1, 1), + // (5,1): warning CS0164: This label has not been referenced + // l2: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l2").WithLocation(5, 1), + // (7,1): warning CS0164: This label has not been referenced + // l3: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l3").WithLocation(7, 1), + // (10,1): warning CS0164: This label has not been referenced + // l4: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l4").WithLocation(10, 1), + // (13,1): warning CS0164: This label has not been referenced + // l5: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "l5").WithLocation(13, 1), + // (19,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(19, 17), + // (19,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(19, 21), + // (19,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(19, 25), + // (19,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(19, 29), + // (19,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(19, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(1, x5Ref.Length); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(1, x6Ref.Length); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TypeVarNotFound, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (6,30): error CS0102: The type '' already contains a definition for 'x2' + // (bool c, int d) = ((2 is var x2), 2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("", "x2").WithLocation(6, 30), + // (9,8): error CS0102: The type '' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("", "x3").WithLocation(9, 8), + // (12,32): error CS0102: The type '' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("", "x4").WithLocation(12, 32), + // (19,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(19, 13), + // (19,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(19, 17), + // (19,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(19, 21), + // (19,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(19, 25), + // (19,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(19, 29), + // (19,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(19, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + Assert.Empty(GetPatternDeclarations(tree)); + } + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void GlobalCode_LabeledStatement_09() + { + string source = +@" +System.Console.WriteLine(x1); +a:b:c: +var (d, e) = ((1 is var x1), 1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics( + // (3,1): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(3, 1), + // (3,3): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(3, 3), + // (3,5): warning CS0164: This label has not been referenced + // a:b:c: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(3, 5) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_FieldDeclaration_01() + { + string source = +@" + +bool b = (1 is int x1); +H.Dummy(x1); + +object x2; +bool d = (2 is int x2); + +bool f = (3 is int x3); +object x3; + +bool h = H.Dummy((41 is int x4), + (42 is int x4)); + +bool x5 = + (5 is int x5); + +bool i = (5 is int x6), + x6; + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,20): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d = (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 20), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,29): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 29), + // (16,21): error CS0102: The type 'Script' already contains a definition for 'x5' + // (5 is int x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 21), + // (19,10): error CS0102: The type 'Script' already contains a definition for 'x6' + // x6; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(19, 10), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25), + // (23,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(23, 29), + // (23,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b = (1 is int x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(3, 16), + // (7,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d = (2 is int x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x2").WithLocation(7, 16), + // (9,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f = (3 is int x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 16), + // (12,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h = H.Dummy((41 is int x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(12, 25), + // (13,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 25), + // (13,29): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 29), + // (16,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (5 is int x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(16, 17), + // (18,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool i = (5 is int x6), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(18, 16), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29), + // (23,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl); + VerifyModelNotSupported(model, x6Ref); + } + } + + [Fact] + public void GlobalCode_FieldDeclaration_02() + { + string source = +@" + +bool b = (1 is var x1); +H.Dummy(x1); + +object x2; +bool d = (2 is var x2); + +bool f = (3 is var x3); +object x3; + +bool h = H.Dummy((41 is var x4), + (42 is var x4)); + +bool x5 = + (5 is var x5); + +bool i = (5 is var x6), + x6; + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,20): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d = (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 20), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,29): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 29), + // (16,21): error CS0102: The type 'Script' already contains a definition for 'x5' + // (5 is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 21), + // (19,10): error CS0102: The type 'Script' already contains a definition for 'x6' + // x6; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(19, 10), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25), + // (23,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(23, 29), + // (23,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b = (1 is var x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x1").WithLocation(3, 16), + // (7,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d = (2 is var x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x2").WithLocation(7, 16), + // (9,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f = (3 is var x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x3").WithLocation(9, 16), + // (12,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h = H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(12, 25), + // (13,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(13, 25), + // (13,29): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 29), + // (16,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (5 is var x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x5").WithLocation(16, 17), + // (18,16): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool i = (5 is var x6), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x6").WithLocation(18, 16), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29), + // (23,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl); + VerifyModelNotSupported(model, x6Ref); + } + } + + [Fact] + public void GlobalCode_FieldDeclaration_03() + { + string source = +@" +System.Console.WriteLine(x1); +var d = (1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_FieldDeclaration_04() + { + string source = +@" +static var a = InitA(); +System.Console.WriteLine(x1); +static var b = (1 is var x1); +Test(); +static var c = InitB(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +static object InitA() +{ + System.Console.WriteLine(""InitA {0}"", x1); + return null; +} + +static object InitB() +{ + System.Console.WriteLine(""InitB {0}"", x1); + return null; +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"InitA 0 +InitB 1 +1 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(4, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_FieldDeclaration_05() + { + string source = +@" + +bool b = (1 is var x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_FieldDeclaration_06() + { + string source = +@" + +bool b = (1 is int x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_FieldDeclaration_07() + { + string source = +@" +Test(); +bool a = (1 is var x1), b = Test(), c = (2 is var x2); +Test(); + +bool Test() +{ + System.Console.WriteLine(""{0} {1}"", x1, x2); + return false; +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 0 +1 0 +1 2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationField(model, x2Decl, x2Ref); + } + + [Fact] + public void GlobalCode_PropertyDeclaration_01() + { + string source = +@" + +bool b { get; } = (1 is int x1); +H.Dummy(x1); + +object x2; +bool d { get; } = (2 is int x2); + +bool f { get; } = (3 is int x3); +object x3; + +bool h { get; } = H.Dummy((41 is int x4), + (42 is int x4)); + +bool x5 { get; } = + (5 is int x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,29): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d { get; } = (2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 29), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,38): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 38), + // (16,21): error CS0102: The type 'Script' already contains a definition for 'x5' + // (5 is int x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 21), + // (20,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(20, 17), + // (20,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(20, 21), + // (20,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(20, 25), + // (20,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b { get; } = (1 is int x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(3, 25), + // (7,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d { get; } = (2 is int x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x2").WithLocation(7, 25), + // (9,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f { get; } = (3 is int x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 25), + // (12,34): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h { get; } = H.Dummy((41 is int x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(12, 34), + // (13,34): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 34), + // (13,38): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 38), + // (16,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (5 is int x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(16, 17), + // (20,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(20, 13), + // (20,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(20, 17), + // (20,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(20, 21), + // (20,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(20, 25), + // (20,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + } + } + + [Fact] + public void GlobalCode_PropertyDeclaration_02() + { + string source = +@" + +bool b { get; } = (1 is var x1); +H.Dummy(x1); + +object x2; +bool d { get; } = (2 is var x2); + +bool f { get; } = (3 is var x3); +object x3; + +bool h { get; } = H.Dummy((41 is var x4), + (42 is var x4)); + +bool x5 { get; } = + (5 is var x5); + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,29): error CS0102: The type 'Script' already contains a definition for 'x2' + // bool d { get; } = (2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 29), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,38): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 38), + // (16,21): error CS0102: The type 'Script' already contains a definition for 'x5' + // (5 is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 21), + // (20,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(20, 17), + // (20,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(20, 21), + // (20,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(20, 25), + // (20,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool b { get; } = (1 is var x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x1").WithLocation(3, 25), + // (7,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool d { get; } = (2 is var x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x2").WithLocation(7, 25), + // (9,25): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool f { get; } = (3 is var x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x3").WithLocation(9, 25), + // (12,34): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool h { get; } = H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(12, 34), + // (13,34): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(13, 34), + // (13,38): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 38), + // (16,17): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (5 is var x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x5").WithLocation(16, 17), + // (20,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(20, 13), + // (20,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(20, 17), + // (20,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(20, 21), + // (20,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(20, 25), + // (20,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(20, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + } + } + + [Fact] + public void GlobalCode_PropertyDeclaration_03() + { + string source = +@" +System.Console.WriteLine(x1); +bool d { get; set; } = (1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_PropertyDeclaration_04() + { + string source = +@" +static var a = InitA(); +System.Console.WriteLine(x1); +static bool b { get; } = (1 is var x1); +Test(); +static var c = InitB(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +static object InitA() +{ + System.Console.WriteLine(""InitA {0}"", x1); + return null; +} + +static object InitB() +{ + System.Console.WriteLine(""InitB {0}"", x1); + return null; +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"InitA 0 +InitB 1 +1 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(4, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_PropertyDeclaration_05() + { + string source = +@" + +bool b { get; } = (1 is var x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_PropertyDeclaration_06() + { + string source = +@" + +bool b { get; } = (1 is int x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_EventDeclaration_01() + { + string source = +@" + +event System.Action b = H.Dummy(1 is int x1); +H.Dummy(x1); + +object x2; +event System.Action d = H.Dummy(2 is int x2); + +event System.Action f = H.Dummy(3 is int x3); +object x3; + +event System.Action h = H.Dummy((41 is int x4), + (42 is int x4)); + +event System.Action x5 = + H.Dummy(5 is int x5); + +event System.Action i = H.Dummy(5 is int x6), + x6; + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,42): error CS0102: The type 'Script' already contains a definition for 'x2' + // event System.Action d = H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 42), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,36): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 36), + // (16,28): error CS0102: The type 'Script' already contains a definition for 'x5' + // H.Dummy(5 is int x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 28), + // (19,10): error CS0102: The type 'Script' already contains a definition for 'x6' + // x6; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(19, 10), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25), + // (23,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(23, 29), + // (23,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action b = H.Dummy(1 is int x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x1").WithLocation(3, 38), + // (7,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action d = H.Dummy(2 is int x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x2").WithLocation(7, 38), + // (9,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action f = H.Dummy(3 is int x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 38), + // (12,40): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action h = H.Dummy((41 is int x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(12, 40), + // (13,32): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 32), + // (13,36): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is int x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 36), + // (16,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // H.Dummy(5 is int x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(16, 24), + // (18,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action i = H.Dummy(5 is int x6), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(18, 38), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29), + // (23,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl); + VerifyModelNotSupported(model, x6Ref); + } + } + + [Fact] + public void GlobalCode_EventDeclaration_02() + { + string source = +@" + +event System.Action b = H.Dummy(1 is var x1); +H.Dummy(x1); + +object x2; +event System.Action d = H.Dummy(2 is var x2); + +event System.Action f = H.Dummy(3 is var x3); +object x3; + +event System.Action h = H.Dummy((41 is var x4), + (42 is var x4)); + +event System.Action x5 = + H.Dummy(5 is var x5); + +event System.Action i = H.Dummy(5 is var x6), + x6; + +void Test() +{ + H.Dummy(x1, x2, x3, x4, x5, x6); +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (7,42): error CS0102: The type 'Script' already contains a definition for 'x2' + // event System.Action d = H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x2").WithArguments("Script", "x2").WithLocation(7, 42), + // (10,8): error CS0102: The type 'Script' already contains a definition for 'x3' + // object x3; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x3").WithArguments("Script", "x3").WithLocation(10, 8), + // (13,36): error CS0102: The type 'Script' already contains a definition for 'x4' + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x4").WithArguments("Script", "x4").WithLocation(13, 36), + // (16,28): error CS0102: The type 'Script' already contains a definition for 'x5' + // H.Dummy(5 is var x5); + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x5").WithArguments("Script", "x5").WithLocation(16, 28), + // (19,10): error CS0102: The type 'Script' already contains a definition for 'x6' + // x6; + Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "x6").WithArguments("Script", "x6").WithLocation(19, 10), + // (23,17): error CS0229: Ambiguity between 'x2' and 'x2' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x2").WithArguments("x2", "x2").WithLocation(23, 17), + // (23,21): error CS0229: Ambiguity between 'x3' and 'x3' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x3").WithArguments("x3", "x3").WithLocation(23, 21), + // (23,25): error CS0229: Ambiguity between 'x4' and 'x4' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x4").WithArguments("x4", "x4").WithLocation(23, 25), + // (23,29): error CS0229: Ambiguity between 'x5' and 'x5' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x5").WithArguments("x5", "x5").WithLocation(23, 29), + // (23,33): error CS0229: Ambiguity between 'x6' and 'x6' + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_AmbigMember, "x6").WithArguments("x6", "x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationFieldDuplicate(model, x4Decl[1], x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationFieldDuplicate(model, x6Decl, x6Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action b = H.Dummy(1 is var x1); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x1").WithLocation(3, 38), + // (7,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action d = H.Dummy(2 is var x2); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x2").WithLocation(7, 38), + // (9,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action f = H.Dummy(3 is var x3); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x3").WithLocation(9, 38), + // (12,40): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action h = H.Dummy((41 is var x4), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(12, 40), + // (13,32): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x4").WithLocation(13, 32), + // (13,36): error CS0128: A local variable named 'x4' is already defined in this scope + // (42 is var x4)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(13, 36), + // (16,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // H.Dummy(5 is var x5); + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x5").WithLocation(16, 24), + // (18,38): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // event System.Action i = H.Dummy(5 is var x6), + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "var x6").WithLocation(18, 38), + // (23,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(23, 13), + // (23,17): error CS0103: The name 'x2' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x2").WithArguments("x2").WithLocation(23, 17), + // (23,21): error CS0103: The name 'x3' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x3").WithArguments("x3").WithLocation(23, 21), + // (23,25): error CS0103: The name 'x4' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x4").WithArguments("x4").WithLocation(23, 25), + // (23,29): error CS0103: The name 'x5' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x5").WithArguments("x5").WithLocation(23, 29), + // (23,33): error CS0103: The name 'x6' does not exist in the current context + // H.Dummy(x1, x2, x3, x4, x5, x6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x6").WithArguments("x6").WithLocation(23, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyModelNotSupported(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl); + VerifyModelNotSupported(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl); + VerifyModelNotSupported(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").Single(); + Assert.Equal(2, x4Decl.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + VerifyModelNotSupported(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl); + VerifyModelNotSupported(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl); + VerifyModelNotSupported(model, x6Ref); + } + } + + [Fact] + public void GlobalCode_EventDeclaration_03() + { + string source = +@" +System.Console.WriteLine(x1); +event System.Action d = H.Dummy(1 is var x1); +Test(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_EventDeclaration_04() + { + string source = +@" +static var a = InitA(); +System.Console.WriteLine(x1); +static event System.Action b = H.Dummy(1 is var x1); +Test(); +static var c = InitB(); + +void Test() +{ + System.Console.WriteLine(x1); +} + +static object InitA() +{ + System.Console.WriteLine(""InitA {0}"", x1); + return null; +} + +static object InitB() +{ + System.Console.WriteLine(""InitB {0}"", x1); + return null; +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"InitA 0 +InitB 1 +1 +1").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(4, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_EventDeclaration_05() + { + string source = +@" + +event System.Action b = H.Dummy(1 is var x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_EventDeclaration_06() + { + string source = +@" + +event System.Action b = H.Dummy(1 is int x1); +static var d = x1; + +static void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (4,16): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // static var d = x1; + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(4, 16), + // (8,13): error CS0120: An object reference is required for the non-static field, method, or property 'x1' + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_ObjectRequired, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + [Fact] + public void GlobalCode_EventDeclaration_07() + { + string source = +@" +Test(); +event System.Action a = H.Dummy(1 is var x1), b = Test(), c = H.Dummy(2 is var x2); +Test(); + +System.Action Test() +{ + System.Console.WriteLine(""{0} {1}"", x1, x2); + return null; +} + +class H +{ + public static System.Action Dummy(params object[] x) {return null;} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + CompileAndVerify(compilation, expectedOutput: +@"0 0 +1 0 +1 2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationField(model, x2Decl, x2Ref); + } + + [Fact] + public void GlobalCode_DeclaratorArguments_01() + { + string source = +@" + +bool a, b(""5948"" is var x1); +H.Dummy(x1); + +void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (3,10): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_BadVarDecl, @"(""5948"" is var x1)").WithLocation(3, 10), + // (3,10): error CS1003: Syntax error, '[' expected + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 10), + // (3,28): error CS1003: Syntax error, ']' expected + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 28) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + // the following would fail due to https://github.com/dotnet/roslyn/issues/13569 + // VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,10): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_BadVarDecl, @"(""5948"" is var x1)").WithLocation(3, 10), + // (3,10): error CS1003: Syntax error, '[' expected + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 10), + // (3,28): error CS1003: Syntax error, ']' expected + // bool a, b("5948" is var x1); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 28), + // (8,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + VerifyModelNotSupported(model, x1Decl, x1Ref); + } + } + + [Fact] + public void GlobalCode_DeclaratorArguments_02() + { + string source = +@" +label: +bool a, b((1 is var x1)); +H.Dummy(x1); + +void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return true;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (3,10): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_BadVarDecl, "((1 is var x1))").WithLocation(3, 10), + // (3,10): error CS1003: Syntax error, '[' expected + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 10), + // (3,25): error CS1003: Syntax error, ']' expected + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 25), + // (2,1): warning CS0164: This label has not been referenced + // label: + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "label").WithLocation(2, 1) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,10): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_BadVarDecl, "((1 is var x1))").WithLocation(3, 10), + // (3,10): error CS1003: Syntax error, '[' expected + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 10), + // (3,25): error CS1003: Syntax error, ']' expected + // bool a, b((1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 25), + // (8,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + VerifyModelNotSupported(model, x1Decl, x1Ref); + } + } + + [Fact] + public void GlobalCode_DeclaratorArguments_03() + { + string source = +@" + +event System.Action a, b(H.Dummy(1 is var x1)); +H.Dummy(x1); + +void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static bool Dummy(params object[] x) {return false;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (3,25): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_BadVarDecl, "(H.Dummy(1 is var x1))").WithLocation(3, 25), + // (3,25): error CS1003: Syntax error, '[' expected + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 25), + // (3,47): error CS1003: Syntax error, ']' expected + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 47) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + // the following would fail due to https://github.com/dotnet/roslyn/issues/13569 + // VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,25): error CS1528: Expected ; or = (cannot specify constructor arguments in declaration) + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_BadVarDecl, "(H.Dummy(1 is var x1))").WithLocation(3, 25), + // (3,25): error CS1003: Syntax error, '[' expected + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, "(").WithArguments("[", "(").WithLocation(3, 25), + // (3,47): error CS1003: Syntax error, ']' expected + // event System.Action a, b(H.Dummy(1 is var x1)); + Diagnostic(ErrorCode.ERR_SyntaxError, ";").WithArguments("]", ";").WithLocation(3, 47), + // (8,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + VerifyModelNotSupported(model, x1Decl, x1Ref); + } + } + + [Fact] + public void GlobalCode_DeclaratorArguments_04() + { + string source = +@" + +fixed bool a[2], b[H.Dummy(1 is var x1)]; +H.Dummy(x1); + +void Test() +{ + H.Dummy(x1); +} + +class H +{ + public static int Dummy(params object[] x) {return 0;} +} +"; + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), + parseOptions: TestOptions.Script); + + compilation.VerifyDiagnostics( + // (3,18): error CS1642: Fixed size buffer fields may only be members of structs + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_FixedNotInStruct, "b").WithLocation(3, 18), + // (3,20): error CS0133: The expression being assigned to 'b' must be constant + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "H.Dummy(1 is var x1)").WithArguments("b").WithLocation(3, 20), + // (3,12): error CS1642: Fixed size buffer fields may only be members of structs + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_FixedNotInStruct, "a").WithLocation(3, 12), + // (3,12): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_UnsafeNeeded, "a[2]").WithLocation(3, 12) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + // the following would fail due to https://github.com/dotnet/roslyn/issues/13569 + // VerifyModelForDeclarationField(model, x1Decl, x1Ref); + } + + { + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseDll, parseOptions: TestOptions.Regular); + int[] exclude = new int[] { (int)ErrorCode.ERR_EOFExpected, + (int)ErrorCode.ERR_CloseParenExpected, + (int)ErrorCode.ERR_SemicolonExpected, + (int)ErrorCode.ERR_TypeExpected, + (int)ErrorCode.ERR_NamespaceUnexpected, + (int)ErrorCode.ERR_TupleTooFewElements + }; + + compilation.GetDiagnostics().Where(d => !exclude.Contains(d.Code)).Verify( + // (3,18): error CS1642: Fixed size buffer fields may only be members of structs + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_FixedNotInStruct, "b").WithLocation(3, 18), + // (3,20): error CS0133: The expression being assigned to '.b' must be constant + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "H.Dummy(1 is var x1)").WithArguments(".b").WithLocation(3, 20), + // (3,12): error CS1642: Fixed size buffer fields may only be members of structs + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_FixedNotInStruct, "a").WithLocation(3, 12), + // (3,12): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context + // fixed bool a[2], b[H.Dummy(1 is var x1)]; + Diagnostic(ErrorCode.ERR_UnsafeNeeded, "a[2]").WithLocation(3, 12), + // (8,13): error CS0103: The name 'x1' does not exist in the current context + // H.Dummy(x1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(8, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + AssertContainedInDeclaratorArguments(x1Decl); + VerifyModelNotSupported(model, x1Decl, x1Ref); + } + } + + [Fact] + public void GlobalCode_RestrictedType_01() + { + string source = +@" + +H.Dummy(null is System.ArgIterator x1); + +class H +{ + public static void Dummy(params object[] x) {} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.GetDeclarationDiagnostics().Verify( + // (3,17): error CS0610: Field or property cannot be of type 'ArgIterator' + // H.Dummy(null is System.ArgIterator x1); + Diagnostic(ErrorCode.ERR_FieldCantBeRefAny, "System.ArgIterator").WithArguments("System.ArgIterator").WithLocation(3, 17) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + VerifyModelForDeclarationField(model, x1Decl); + } + + [Fact] + public void GlobalCode_StaticType_01() + { + string source = +@" +H.Dummy(null is StaticType x1); + +class H +{ + public static void Dummy(params object[] x) {} +} + +static class StaticType{} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + compilation.GetDeclarationDiagnostics().Verify( + // (2,28): error CS0723: Cannot declare a variable of static type 'StaticType' + // H.Dummy(null is StaticType x1); + Diagnostic(ErrorCode.ERR_VarDeclIsStaticClass, "x1").WithArguments("StaticType").WithLocation(2, 28) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + VerifyModelForDeclarationField(model, x1Decl); + } + + [Fact] + public void GlobalCode_AliasInfo_01() + { + string source = +@" +H.Dummy(1 is var x1); + +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + Assert.Null(model.GetAliasInfo(x1Decl.Type)); + } + + [Fact] + public void GlobalCode_AliasInfo_02() + { + string source = +@" +using var = System.Int32; + +H.Dummy(1 is var x1); + +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + Assert.Equal("var=System.Int32", model.GetAliasInfo(x1Decl.Type).ToTestDisplayString()); + } + + [Fact] + public void GlobalCode_AliasInfo_03() + { + string source = +@" +using a = System.Int32; + +H.Dummy(1 is a x1); + +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + Assert.Equal("a=System.Int32", model.GetAliasInfo(x1Decl.Type).ToTestDisplayString()); + } + + [Fact] + public void GlobalCode_AliasInfo_04() + { + string source = +@" +H.Dummy(1 is int x1); + +class H +{ + public static void Dummy(params object[] x) {} +} +"; + + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + Assert.Null(model.GetAliasInfo(x1Decl.Type)); + } + + } +} diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Scope.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Scope.cs new file mode 100644 index 0000000000000..98f42ea387233 --- /dev/null +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_Scope.cs @@ -0,0 +1,11562 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis.CSharp.Symbols; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Microsoft.CodeAnalysis.Test.Utilities; +using Roslyn.Test.Utilities; +using Xunit; + +namespace Microsoft.CodeAnalysis.CSharp.UnitTests +{ + [CompilerTrait(CompilerFeature.Patterns)] + public class PatternMatchingTests_Scope : PatternMatchingTestBase + { + [Fact] + public void ScopeOfPatternVariables_ExpressionStatement_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + Dummy(true is var x1, x1); + { + Dummy(true is var x1, x1); + } + Dummy(true is var x1, x1); + } + + void Test2() + { + Dummy(x2, true is var x2); + } + + void Test3(int x3) + { + Dummy(true is var x3, x3); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + Dummy(true is var x4, x4); + } + + void Test5() + { + Dummy(true is var x5, x5); + var x5 = 11; + Dummy(x5); + } + + //void Test6() + //{ + // let x6 = 11; + // Dummy(x6); + // Dummy(true is var x6, x6); + //} + + //void Test7() + //{ + // Dummy(true is var x7, x7); + // let x7 = 11; + // Dummy(x7); + //} + + void Test8() + { + Dummy(true is var x8, x8, false is var x8, x8); + } + + void Test9(bool y9) + { + if (y9) + Dummy(true is var x9, x9); + } + + System.Action Test10(bool y10) + { + return () => + { + if (y10) + Dummy(true is var x10, x10); + }; + } + + void Test11() + { + Dummy(x11); + Dummy(true is var x11, x11); + } + + void Test12() + { + Dummy(true is var x12, x12); + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (14,31): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 31), + // (16,27): error CS0128: A local variable named 'x1' is already defined in this scope + // Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 27), + // (21,15): error CS0841: Cannot use local variable 'x2' before it is declared + // Dummy(x2, true is var x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 15), + // (26,27): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x3, x3); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 27), + // (33,27): error CS0128: A local variable named 'x4' is already defined in this scope + // Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 27), + // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope + // var x5 = 11; + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), + // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), + // (59,48): error CS0128: A local variable named 'x8' is already defined in this scope + // Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 48), + // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared + // Dummy(x11); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(2, x8Ref.Length); + for (int i = 0; i < x8Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").Single(); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionStatement_02() + { + var text = @" +public class Cls +{ + public static void Main() + { + Test1(2 is var x1); + System.Console.WriteLine(x1); + } + + static object Test1(bool x) + { + return null; + } +}"; + var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: "2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Decl.Length); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionStatement_03() + { + var text = @" +public class Cls +{ + public static void Main() + { + Test0(); + } + + static object Test0() + { + bool test = true; + + if (test) + Test2(1 is var x1, x1); + + if (test) + { + Test2(2 is var x1, x1); + } + + return null; + } + + static object Test2(object x, object y) + { + System.Console.Write(y); + return x; + } +}"; + var compilation = CreateCompilationWithMscorlib(text, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: "12").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Decl.Length); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionStatement_04() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + if (true) + Dummy(true is var x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (15,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionStatement_05() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (ExpressionStatementSyntax)SyntaxFactory.ParseStatement(@" +Dummy(11 is var x1, x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact, WorkItem(9258, "https://github.com/dotnet/roslyn/issues/9258")] + public void PatternVariableOrder() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + static void Dummy(params object[] x) {} + + void Test1(object o1, object o2) + { + Dummy(o1 is int i && i < 10, + o2 is int @i && @i > 10); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (13,25): error CS0128: A local variable named 'i' is already defined in this scope + // o2 is int @i && @i > 10); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "@i").WithArguments("i").WithLocation(13, 25), + // (13,31): error CS0165: Use of unassigned local variable 'i' + // o2 is int @i && @i > 10); + Diagnostic(ErrorCode.ERR_UseDefViolation, "@i").WithArguments("i").WithLocation(13, 31) + ); + } + + [Fact] + public void ScopeOfPatternVariables_ReturnStatement_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) { return null; } + + object Test1() + { + return Dummy(true is var x1, x1); + { + return Dummy(true is var x1, x1); + } + return Dummy(true is var x1, x1); + } + + object Test2() + { + return Dummy(x2, true is var x2); + } + + object Test3(int x3) + { + return Dummy(true is var x3, x3); + } + + object Test4() + { + var x4 = 11; + Dummy(x4); + return Dummy(true is var x4, x4); + } + + object Test5() + { + return Dummy(true is var x5, x5); + var x5 = 11; + Dummy(x5); + } + + //object Test6() + //{ + // let x6 = 11; + // Dummy(x6); + // return Dummy(true is var x6, x6); + //} + + //object Test7() + //{ + // return Dummy(true is var x7, x7); + // let x7 = 11; + // Dummy(x7); + //} + + object Test8() + { + return Dummy(true is var x8, x8, false is var x8, x8); + } + + object Test9(bool y9) + { + if (y9) + return Dummy(true is var x9, x9); + return null; + } + System.Func Test10(bool y10) + { + return () => + { + if (y10) + return Dummy(true is var x10, x10); + return null;}; + } + + object Test11() + { + Dummy(x11); + return Dummy(true is var x11, x11); + } + + object Test12() + { + return Dummy(true is var x12, x12); + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (14,38): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // return Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 38), + // (16,34): error CS0128: A local variable named 'x1' is already defined in this scope + // return Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 34), + // (14,13): warning CS0162: Unreachable code detected + // return Dummy(true is var x1, x1); + Diagnostic(ErrorCode.WRN_UnreachableCode, "return").WithLocation(14, 13), + // (21,22): error CS0841: Cannot use local variable 'x2' before it is declared + // return Dummy(x2, true is var x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 22), + // (26,34): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // return Dummy(true is var x3, x3); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 34), + // (33,34): error CS0128: A local variable named 'x4' is already defined in this scope + // return Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 34), + // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope + // var x5 = 11; + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), + // (39,9): warning CS0162: Unreachable code detected + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(39, 9), + // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), + // (59,55): error CS0128: A local variable named 'x8' is already defined in this scope + // return Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 55), + // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared + // Dummy(x11); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), + // (86,9): warning CS0162: Unreachable code detected + // Dummy(x12); + Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(86, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").Single(); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ReturnStatement_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + int Dummy(params object[] x) {return 0;} + + int Test1(bool val) + { + if (val) + return Dummy(true is var x1); + + x1++; + return 0; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (15,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ReturnStatement_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (ReturnStatementSyntax)SyntaxFactory.ParseStatement(@" +return Dummy(11 is var x1, x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_ThrowStatement_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.Exception Dummy(params object[] x) { return null;} + + void Test1() + { + throw Dummy(true is var x1, x1); + { + throw Dummy(true is var x1, x1); + } + throw Dummy(true is var x1, x1); + } + + void Test2() + { + throw Dummy(x2, true is var x2); + } + + void Test3(int x3) + { + throw Dummy(true is var x3, x3); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + throw Dummy(true is var x4, x4); + } + + void Test5() + { + throw Dummy(true is var x5, x5); + var x5 = 11; + Dummy(x5); + } + + //void Test6() + //{ + // let x6 = 11; + // Dummy(x6); + // throw Dummy(true is var x6, x6); + //} + + //void Test7() + //{ + // throw Dummy(true is var x7, x7); + // let x7 = 11; + // Dummy(x7); + //} + + void Test8() + { + throw Dummy(true is var x8, x8, false is var x8, x8); + } + + void Test9(bool y9) + { + if (y9) + throw Dummy(true is var x9, x9); + } + + System.Action Test10(bool y10) + { + return () => + { + if (y10) + throw Dummy(true is var x10, x10); + }; + } + + void Test11() + { + Dummy(x11); + throw Dummy(true is var x11, x11); + } + + void Test12() + { + throw Dummy(true is var x12, x12); + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (14,37): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // throw Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 37), + // (16,33): error CS0128: A local variable named 'x1' is already defined in this scope + // throw Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 33), + // (21,21): error CS0841: Cannot use local variable 'x2' before it is declared + // throw Dummy(x2, true is var x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 21), + // (26,33): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // throw Dummy(true is var x3, x3); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 33), + // (33,33): error CS0128: A local variable named 'x4' is already defined in this scope + // throw Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 33), + // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope + // var x5 = 11; + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), + // (39,9): warning CS0162: Unreachable code detected + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(39, 9), + // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), + // (59,54): error CS0128: A local variable named 'x8' is already defined in this scope + // throw Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 54), + // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared + // Dummy(x11); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), + // (86,9): warning CS0162: Unreachable code detected + // Dummy(x12); + Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(86, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").Single(); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ThrowStatement_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.Exception Dummy(params object[] x) { return null;} + + void Test1(bool val) + { + if (val) + throw Dummy(true is var x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (15,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_TrowStatement_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (ThrowStatementSyntax)SyntaxFactory.ParseStatement(@" +throw Dummy(11 is var x1, x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_If_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + if (true is var x1) + { + Dummy(x1); + } + else + { + System.Console.WriteLine(x1); + } + } + + void Test2() + { + if (true is var x2) + Dummy(x2); + else + System.Console.WriteLine(x2); + } + + void Test3() + { + if (true is var x3) + Dummy(x3); + else + { + var x3 = 12; + System.Console.WriteLine(x3); + } + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + if (true is var x4) + Dummy(x4); + } + + void Test5(int x5) + { + if (true is var x5) + Dummy(x5); + } + + void Test6() + { + if (x6 && true is var x6) + Dummy(x6); + } + + void Test7() + { + if (true is var x7 && x7) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + if (true is var x8) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + if (true is var x9) + { + Dummy(x9); + if (true is var x9) // 2 + Dummy(x9); + } + } + + void Test10() + { + if (y10 is var x10) + { + var y10 = 12; + Dummy(y10); + } + } + + + + + + + + + + + void Test12() + { + if (y12 is var x12) + var y12 = 12; + } + + //void Test13() + //{ + // if (y13 is var x13) + // let y13 = 12; + //} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (110,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(110, 13), + // (36,17): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x3 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(36, 17), + // (46,25): error CS0128: A local variable named 'x4' is already defined in this scope + // if (true is var x4) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(46, 25), + // (52,25): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // if (true is var x5) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(52, 25), + // (58,13): error CS0841: Cannot use local variable 'x6' before it is declared + // if (x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(58, 13), + // (66,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(66, 17), + // (83,19): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(83, 19), + // (84,29): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // if (true is var x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(84, 29), + // (91,13): error CS0103: The name 'y10' does not exist in the current context + // if (y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(91, 13), + // (109,13): error CS0103: The name 'y12' does not exist in the current context + // if (y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(109, 13), + // (110,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(110, 17) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref[0]); + VerifyNotAPatternLocal(model, x3Ref[1]); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(2, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_If_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + if (true) + if (true is var x1) + { + } + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (17,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_If_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (IfStatementSyntax)SyntaxFactory.ParseStatement(@" +if (Dummy(11 is var x1, x1)); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_Lambda_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + System.Action Test1() + { + return (o) => let x1 = o; + } + + System.Action Test2() + { + return (o) => let var x2 = o; + } + + void Test3() + { + Dummy((System.Func) (o => o is int x3 && x3 > 0)); + } + + void Test4() + { + Dummy((System.Func) (o => x4 && o is int x4)); + } + + void Test5() + { + Dummy((System.Func) ((o1, o2) => o1 is int x5 && + o2 is int x5 && + x5 > 0)); + } + + void Test6() + { + Dummy((System.Func) (o => o is int x6 && x6 > 0), (System.Func) (o => o is int x6 && x6 > 0)); + } + + void Test7() + { + Dummy(x7, 1); + Dummy(x7, + (System.Func) (o => o is int x7 && x7 > 0), + x7); + Dummy(x7, 2); + } + + void Test8() + { + Dummy(true is var x8 && x8, (System.Func) (o => o is int y8 && x8)); + } + + void Test9() + { + Dummy(true is var x9, + (System.Func) (o => o is int x9 && + x9 > 0), x9); + } + + void Test10() + { + Dummy((System.Func) (o => o is int x10 && + x10 > 0), + true is var x10, x10); + } + + void Test11() + { + var x11 = 11; + Dummy(x11); + Dummy((System.Func) (o => o is int x11 && + x11 > 0), x11); + } + + void Test12() + { + Dummy((System.Func) (o => o is int x12 && + x12 > 0), + x12); + var x12 = 11; + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (12,27): error CS1002: ; expected + // return (o) => let x1 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(12, 27), + // (17,27): error CS1002: ; expected + // return (o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(17, 27), + // (12,23): error CS0103: The name 'let' does not exist in the current context + // return (o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(12, 23), + // (12,23): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // return (o) => let x1 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(12, 23), + // (12,27): error CS0103: The name 'x1' does not exist in the current context + // return (o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(12, 27), + // (12,32): error CS0103: The name 'o' does not exist in the current context + // return (o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(12, 32), + // (12,27): warning CS0162: Unreachable code detected + // return (o) => let x1 = o; + Diagnostic(ErrorCode.WRN_UnreachableCode, "x1").WithLocation(12, 27), + // (17,23): error CS0103: The name 'let' does not exist in the current context + // return (o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(17, 23), + // (17,23): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // return (o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(17, 23), + // (17,36): error CS0103: The name 'o' does not exist in the current context + // return (o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(17, 36), + // (17,27): warning CS0162: Unreachable code detected + // return (o) => let var x2 = o; + Diagnostic(ErrorCode.WRN_UnreachableCode, "var").WithLocation(17, 27), + // (27,49): error CS0841: Cannot use local variable 'x4' before it is declared + // Dummy((System.Func) (o => x4 && o is int x4)); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(27, 49), + // (33,74): error CS0128: A local variable named 'x5' is already defined in this scope + // o2 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(33, 74), + // (34,64): error CS0165: Use of unassigned local variable 'x5' + // x5 > 0)); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(34, 64), + // (44,15): error CS0103: The name 'x7' does not exist in the current context + // Dummy(x7, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(44, 15), + // (45,15): error CS0103: The name 'x7' does not exist in the current context + // Dummy(x7, + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(45, 15), + // (47,15): error CS0103: The name 'x7' does not exist in the current context + // x7); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(47, 15), + // (48,15): error CS0103: The name 'x7' does not exist in the current context + // Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(48, 15), + // (59,58): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // (System.Func) (o => o is int x9 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(59, 58), + // (65,58): error CS0136: A local or parameter named 'x10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy((System.Func) (o => o is int x10 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x10").WithArguments("x10").WithLocation(65, 58), + // (74,58): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy((System.Func) (o => o is int x11 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(74, 58), + // (80,58): error CS0136: A local or parameter named 'x12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy((System.Func) (o => o is int x12 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x12").WithArguments("x12").WithLocation(80, 58), + // (82,15): error CS0841: Cannot use local variable 'x12' before it is declared + // x12); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(82, 15) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(5, x7Ref.Length); + VerifyNotInScope(model, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[2]); + VerifyNotInScope(model, x7Ref[3]); + VerifyNotInScope(model, x7Ref[4]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(2, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[0]); + + var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); + var x10Ref = GetReferences(tree, "x10").ToArray(); + Assert.Equal(2, x10Decl.Length); + Assert.Equal(2, x10Ref.Length); + VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref[0]); + VerifyModelForDeclarationPattern(model, x10Decl[1], x10Ref[1]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(3, x11Ref.Length); + VerifyNotAPatternLocal(model, x11Ref[0]); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[1]); + VerifyNotAPatternLocal(model, x11Ref[2]); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(3, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[0]); + VerifyNotAPatternLocal(model, x12Ref[1]); + VerifyNotAPatternLocal(model, x12Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_Query_01() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + var res = from x in new[] { 1 is var y1 ? y1 : 0, y1} + select x + y1; + + Dummy(y1); + } + + void Test2() + { + var res = from x1 in new[] { 1 is var y2 ? y2 : 0} + from x2 in new[] { x1 is var z2 ? z2 : 0, z2, y2} + select x1 + x2 + y2 + + z2; + + Dummy(z2); + } + + void Test3() + { + var res = from x1 in new[] { 1 is var y3 ? y3 : 0} + let x2 = x1 is var z3 && z3 > 0 && y3 < 0 + select new { x1, x2, y3, + z3}; + + Dummy(z3); + } + + void Test4() + { + var res = from x1 in new[] { 1 is var y4 ? y4 : 0} + join x2 in new[] { 2 is var z4 ? z4 : 0, z4, y4} + on x1 + y4 + z4 + 3 is var u4 ? u4 : 0 + + v4 + equals x2 + y4 + z4 + 4 is var v4 ? v4 : 0 + + u4 + select new { x1, x2, y4, z4, + u4, v4 }; + + Dummy(z4); + Dummy(u4); + Dummy(v4); + } + + void Test5() + { + var res = from x1 in new[] { 1 is var y5 ? y5 : 0} + join x2 in new[] { 2 is var z5 ? z5 : 0, z5, y5} + on x1 + y5 + z5 + 3 is var u5 ? u5 : 0 + + v5 + equals x2 + y5 + z5 + 4 is var v5 ? v5 : 0 + + u5 + into g + select new { x1, y5, z5, g, + u5, v5 }; + + Dummy(z5); + Dummy(u5); + Dummy(v5); + } + + void Test6() + { + var res = from x in new[] { 1 is var y6 ? y6 : 0} + where x > y6 && 1 is var z6 && z6 == 1 + select x + y6 + + z6; + + Dummy(z6); + } + + void Test7() + { + var res = from x in new[] { 1 is var y7 ? y7 : 0} + orderby x > y7 && 1 is var z7 && z7 == + u7, + x > y7 && 1 is var u7 && u7 == + z7 + select x + y7 + + z7 + u7; + + Dummy(z7); + Dummy(u7); + } + + void Test8() + { + var res = from x in new[] { 1 is var y8 ? y8 : 0} + select x > y8 && 1 is var z8 && z8 == 1; + + Dummy(z8); + } + + void Test9() + { + var res = from x in new[] { 1 is var y9 ? y9 : 0} + group x > y9 && 1 is var z9 && z9 == + u9 + by + x > y9 && 1 is var u9 && u9 == + z9; + + Dummy(z9); + Dummy(u9); + } + + void Test10() + { + var res = from x1 in new[] { 1 is var y10 ? y10 : 0} + from y10 in new[] { 1 } + select x1 + y10; + } + + void Test11() + { + var res = from x1 in new[] { 1 is var y11 ? y11 : 0} + let y11 = x1 + 1 + select x1 + y11; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (25,26): error CS0103: The name 'z2' does not exist in the current context + // z2; + Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(25, 26), + // (27,15): error CS0103: The name 'z2' does not exist in the current context + // Dummy(z2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(27, 15), + // (35,32): error CS0103: The name 'z3' does not exist in the current context + // z3}; + Diagnostic(ErrorCode.ERR_NameNotInContext, "z3").WithArguments("z3").WithLocation(35, 32), + // (37,15): error CS0103: The name 'z3' does not exist in the current context + // Dummy(z3); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z3").WithArguments("z3").WithLocation(37, 15), + // (45,35): error CS0103: The name 'v4' does not exist in the current context + // v4 + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(45, 35), + // (47,35): error CS1938: The name 'u4' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. + // u4 + Diagnostic(ErrorCode.ERR_QueryInnerKey, "u4").WithArguments("u4").WithLocation(47, 35), + // (49,32): error CS0103: The name 'u4' does not exist in the current context + // u4, v4 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(49, 32), + // (49,36): error CS0103: The name 'v4' does not exist in the current context + // u4, v4 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(49, 36), + // (52,15): error CS0103: The name 'u4' does not exist in the current context + // Dummy(u4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(52, 15), + // (53,15): error CS0103: The name 'v4' does not exist in the current context + // Dummy(v4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(53, 15), + // (61,35): error CS0103: The name 'v5' does not exist in the current context + // v5 + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(61, 35), + // (63,35): error CS1938: The name 'u5' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. + // u5 + Diagnostic(ErrorCode.ERR_QueryInnerKey, "u5").WithArguments("u5").WithLocation(63, 35), + // (66,32): error CS0103: The name 'u5' does not exist in the current context + // u5, v5 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(66, 32), + // (66,36): error CS0103: The name 'v5' does not exist in the current context + // u5, v5 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(66, 36), + // (69,15): error CS0103: The name 'u5' does not exist in the current context + // Dummy(u5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(69, 15), + // (70,15): error CS0103: The name 'v5' does not exist in the current context + // Dummy(v5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(70, 15), + // (78,26): error CS0103: The name 'z6' does not exist in the current context + // z6; + Diagnostic(ErrorCode.ERR_NameNotInContext, "z6").WithArguments("z6").WithLocation(78, 26), + // (80,15): error CS0103: The name 'z6' does not exist in the current context + // Dummy(z6); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z6").WithArguments("z6").WithLocation(80, 15), + // (87,27): error CS0103: The name 'u7' does not exist in the current context + // u7, + Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(87, 27), + // (89,27): error CS0103: The name 'z7' does not exist in the current context + // z7 + Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(89, 27), + // (91,31): error CS0103: The name 'u7' does not exist in the current context + // z7 + u7; + Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(91, 31), + // (91,26): error CS0103: The name 'z7' does not exist in the current context + // z7 + u7; + Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(91, 26), + // (93,15): error CS0103: The name 'z7' does not exist in the current context + // Dummy(z7); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z7").WithArguments("z7").WithLocation(93, 15), + // (94,15): error CS0103: The name 'u7' does not exist in the current context + // Dummy(u7); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u7").WithArguments("u7").WithLocation(94, 15), + // (88,52): error CS0165: Use of unassigned local variable 'u7' + // x > y7 && 1 is var u7 && u7 == + Diagnostic(ErrorCode.ERR_UseDefViolation, "u7").WithArguments("u7").WithLocation(88, 52), + // (102,15): error CS0103: The name 'z8' does not exist in the current context + // Dummy(z8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z8").WithArguments("z8").WithLocation(102, 15), + // (112,25): error CS0103: The name 'z9' does not exist in the current context + // z9; + Diagnostic(ErrorCode.ERR_NameNotInContext, "z9").WithArguments("z9").WithLocation(112, 25), + // (109,25): error CS0103: The name 'u9' does not exist in the current context + // u9 + Diagnostic(ErrorCode.ERR_NameNotInContext, "u9").WithArguments("u9").WithLocation(109, 25), + // (114,15): error CS0103: The name 'z9' does not exist in the current context + // Dummy(z9); + Diagnostic(ErrorCode.ERR_NameNotInContext, "z9").WithArguments("z9").WithLocation(114, 15), + // (115,15): error CS0103: The name 'u9' does not exist in the current context + // Dummy(u9); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u9").WithArguments("u9").WithLocation(115, 15), + // (108,50): error CS0165: Use of unassigned local variable 'z9' + // group x > y9 && 1 is var z9 && z9 == + Diagnostic(ErrorCode.ERR_UseDefViolation, "z9").WithArguments("z9").WithLocation(108, 50), + // (121,24): error CS1931: The range variable 'y10' conflicts with a previous declaration of 'y10' + // from y10 in new[] { 1 } + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y10").WithArguments("y10").WithLocation(121, 24), + // (128,23): error CS1931: The range variable 'y11' conflicts with a previous declaration of 'y11' + // let y11 = x1 + 1 + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y11").WithArguments("y11").WithLocation(128, 23) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var y1Decl = GetPatternDeclarations(tree, "y1").Single(); + var y1Ref = GetReferences(tree, "y1").ToArray(); + Assert.Equal(4, y1Ref.Length); + VerifyModelForDeclarationPattern(model, y1Decl, y1Ref); + + var y2Decl = GetPatternDeclarations(tree, "y2").Single(); + var y2Ref = GetReferences(tree, "y2").ToArray(); + Assert.Equal(3, y2Ref.Length); + VerifyModelForDeclarationPattern(model, y2Decl, y2Ref); + + var z2Decl = GetPatternDeclarations(tree, "z2").Single(); + var z2Ref = GetReferences(tree, "z2").ToArray(); + Assert.Equal(4, z2Ref.Length); + VerifyModelForDeclarationPattern(model, z2Decl, z2Ref[0], z2Ref[1]); + VerifyNotInScope(model, z2Ref[2]); + VerifyNotInScope(model, z2Ref[3]); + + var y3Decl = GetPatternDeclarations(tree, "y3").Single(); + var y3Ref = GetReferences(tree, "y3").ToArray(); + Assert.Equal(3, y3Ref.Length); + VerifyModelForDeclarationPattern(model, y3Decl, y3Ref); + + var z3Decl = GetPatternDeclarations(tree, "z3").Single(); + var z3Ref = GetReferences(tree, "z3").ToArray(); + Assert.Equal(3, z3Ref.Length); + VerifyModelForDeclarationPattern(model, z3Decl, z3Ref[0]); + VerifyNotInScope(model, z3Ref[1]); + VerifyNotInScope(model, z3Ref[2]); + + var y4Decl = GetPatternDeclarations(tree, "y4").Single(); + var y4Ref = GetReferences(tree, "y4").ToArray(); + Assert.Equal(5, y4Ref.Length); + VerifyModelForDeclarationPattern(model, y4Decl, y4Ref); + + var z4Decl = GetPatternDeclarations(tree, "z4").Single(); + var z4Ref = GetReferences(tree, "z4").ToArray(); + Assert.Equal(6, z4Ref.Length); + VerifyModelForDeclarationPattern(model, z4Decl, z4Ref); + + var u4Decl = GetPatternDeclarations(tree, "u4").Single(); + var u4Ref = GetReferences(tree, "u4").ToArray(); + Assert.Equal(4, u4Ref.Length); + VerifyModelForDeclarationPattern(model, u4Decl, u4Ref[0]); + VerifyNotInScope(model, u4Ref[1]); + VerifyNotInScope(model, u4Ref[2]); + VerifyNotInScope(model, u4Ref[3]); + + var v4Decl = GetPatternDeclarations(tree, "v4").Single(); + var v4Ref = GetReferences(tree, "v4").ToArray(); + Assert.Equal(4, v4Ref.Length); + VerifyNotInScope(model, v4Ref[0]); + VerifyModelForDeclarationPattern(model, v4Decl, v4Ref[1]); + VerifyNotInScope(model, v4Ref[2]); + VerifyNotInScope(model, v4Ref[3]); + + var y5Decl = GetPatternDeclarations(tree, "y5").Single(); + var y5Ref = GetReferences(tree, "y5").ToArray(); + Assert.Equal(5, y5Ref.Length); + VerifyModelForDeclarationPattern(model, y5Decl, y5Ref); + + var z5Decl = GetPatternDeclarations(tree, "z5").Single(); + var z5Ref = GetReferences(tree, "z5").ToArray(); + Assert.Equal(6, z5Ref.Length); + VerifyModelForDeclarationPattern(model, z5Decl, z5Ref); + + var u5Decl = GetPatternDeclarations(tree, "u5").Single(); + var u5Ref = GetReferences(tree, "u5").ToArray(); + Assert.Equal(4, u5Ref.Length); + VerifyModelForDeclarationPattern(model, u5Decl, u5Ref[0]); + VerifyNotInScope(model, u5Ref[1]); + VerifyNotInScope(model, u5Ref[2]); + VerifyNotInScope(model, u5Ref[3]); + + var v5Decl = GetPatternDeclarations(tree, "v5").Single(); + var v5Ref = GetReferences(tree, "v5").ToArray(); + Assert.Equal(4, v5Ref.Length); + VerifyNotInScope(model, v5Ref[0]); + VerifyModelForDeclarationPattern(model, v5Decl, v5Ref[1]); + VerifyNotInScope(model, v5Ref[2]); + VerifyNotInScope(model, v5Ref[3]); + + var y6Decl = GetPatternDeclarations(tree, "y6").Single(); + var y6Ref = GetReferences(tree, "y6").ToArray(); + Assert.Equal(3, y6Ref.Length); + VerifyModelForDeclarationPattern(model, y6Decl, y6Ref); + + var z6Decl = GetPatternDeclarations(tree, "z6").Single(); + var z6Ref = GetReferences(tree, "z6").ToArray(); + Assert.Equal(3, z6Ref.Length); + VerifyModelForDeclarationPattern(model, z6Decl, z6Ref[0]); + VerifyNotInScope(model, z6Ref[1]); + VerifyNotInScope(model, z6Ref[2]); + + var y7Decl = GetPatternDeclarations(tree, "y7").Single(); + var y7Ref = GetReferences(tree, "y7").ToArray(); + Assert.Equal(4, y7Ref.Length); + VerifyModelForDeclarationPattern(model, y7Decl, y7Ref); + + var z7Decl = GetPatternDeclarations(tree, "z7").Single(); + var z7Ref = GetReferences(tree, "z7").ToArray(); + Assert.Equal(4, z7Ref.Length); + VerifyModelForDeclarationPattern(model, z7Decl, z7Ref[0]); + VerifyNotInScope(model, z7Ref[1]); + VerifyNotInScope(model, z7Ref[2]); + VerifyNotInScope(model, z7Ref[3]); + + var u7Decl = GetPatternDeclarations(tree, "u7").Single(); + var u7Ref = GetReferences(tree, "u7").ToArray(); + Assert.Equal(4, u7Ref.Length); + VerifyNotInScope(model, u7Ref[0]); + VerifyModelForDeclarationPattern(model, u7Decl, u7Ref[1]); + VerifyNotInScope(model, u7Ref[2]); + VerifyNotInScope(model, u7Ref[3]); + + var y8Decl = GetPatternDeclarations(tree, "y8").Single(); + var y8Ref = GetReferences(tree, "y8").ToArray(); + Assert.Equal(2, y8Ref.Length); + VerifyModelForDeclarationPattern(model, y8Decl, y8Ref); + + var z8Decl = GetPatternDeclarations(tree, "z8").Single(); + var z8Ref = GetReferences(tree, "z8").ToArray(); + Assert.Equal(2, z8Ref.Length); + VerifyModelForDeclarationPattern(model, z8Decl, z8Ref[0]); + VerifyNotInScope(model, z8Ref[1]); + + var y9Decl = GetPatternDeclarations(tree, "y9").Single(); + var y9Ref = GetReferences(tree, "y9").ToArray(); + Assert.Equal(3, y9Ref.Length); + VerifyModelForDeclarationPattern(model, y9Decl, y9Ref); + + var z9Decl = GetPatternDeclarations(tree, "z9").Single(); + var z9Ref = GetReferences(tree, "z9").ToArray(); + Assert.Equal(3, z9Ref.Length); + VerifyModelForDeclarationPattern(model, z9Decl, z9Ref[0]); + VerifyNotInScope(model, z9Ref[1]); + VerifyNotInScope(model, z9Ref[2]); + + var u9Decl = GetPatternDeclarations(tree, "u9").Single(); + var u9Ref = GetReferences(tree, "u9").ToArray(); + Assert.Equal(3, u9Ref.Length); + VerifyNotInScope(model, u9Ref[0]); + VerifyModelForDeclarationPattern(model, u9Decl, u9Ref[1]); + VerifyNotInScope(model, u9Ref[2]); + + var y10Decl = GetPatternDeclarations(tree, "y10").Single(); + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyModelForDeclarationPattern(model, y10Decl, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y11Decl = GetPatternDeclarations(tree, "y11").Single(); + var y11Ref = GetReferences(tree, "y11").ToArray(); + Assert.Equal(2, y11Ref.Length); + VerifyModelForDeclarationPattern(model, y11Decl, y11Ref[0]); + VerifyNotAPatternLocal(model, y11Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Query_03() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test4() + { + var res = from x1 in new[] { 1 is var y4 ? y4 : 0} + select x1 into x1 + join x2 in new[] { 2 is var z4 ? z4 : 0, z4, y4} + on x1 + y4 + z4 + 3 is var u4 ? u4 : 0 + + v4 + equals x2 + y4 + z4 + 4 is var v4 ? v4 : 0 + + u4 + select new { x1, x2, y4, z4, + u4, v4 }; + + Dummy(z4); + Dummy(u4); + Dummy(v4); + } + + void Test5() + { + var res = from x1 in new[] { 1 is var y5 ? y5 : 0} + select x1 into x1 + join x2 in new[] { 2 is var z5 ? z5 : 0, z5, y5} + on x1 + y5 + z5 + 3 is var u5 ? u5 : 0 + + v5 + equals x2 + y5 + z5 + 4 is var v5 ? v5 : 0 + + u5 + into g + select new { x1, y5, z5, g, + u5, v5 }; + + Dummy(z5); + Dummy(u5); + Dummy(v5); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (18,35): error CS0103: The name 'v4' does not exist in the current context + // v4 + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(18, 35), + // (20,35): error CS1938: The name 'u4' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. + // u4 + Diagnostic(ErrorCode.ERR_QueryInnerKey, "u4").WithArguments("u4").WithLocation(20, 35), + // (22,32): error CS0103: The name 'u4' does not exist in the current context + // u4, v4 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(22, 32), + // (22,36): error CS0103: The name 'v4' does not exist in the current context + // u4, v4 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(22, 36), + // (25,15): error CS0103: The name 'u4' does not exist in the current context + // Dummy(u4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u4").WithArguments("u4").WithLocation(25, 15), + // (26,15): error CS0103: The name 'v4' does not exist in the current context + // Dummy(v4); + Diagnostic(ErrorCode.ERR_NameNotInContext, "v4").WithArguments("v4").WithLocation(26, 15), + // (35,35): error CS0103: The name 'v5' does not exist in the current context + // v5 + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(35, 35), + // (37,35): error CS1938: The name 'u5' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'. + // u5 + Diagnostic(ErrorCode.ERR_QueryInnerKey, "u5").WithArguments("u5").WithLocation(37, 35), + // (40,32): error CS0103: The name 'u5' does not exist in the current context + // u5, v5 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(40, 32), + // (40,36): error CS0103: The name 'v5' does not exist in the current context + // u5, v5 }; + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(40, 36), + // (43,15): error CS0103: The name 'u5' does not exist in the current context + // Dummy(u5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "u5").WithArguments("u5").WithLocation(43, 15), + // (44,15): error CS0103: The name 'v5' does not exist in the current context + // Dummy(v5); + Diagnostic(ErrorCode.ERR_NameNotInContext, "v5").WithArguments("v5").WithLocation(44, 15) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var y4Decl = GetPatternDeclarations(tree, "y4").Single(); + var y4Ref = GetReferences(tree, "y4").ToArray(); + Assert.Equal(5, y4Ref.Length); + VerifyModelForDeclarationPattern(model, y4Decl, y4Ref); + + var z4Decl = GetPatternDeclarations(tree, "z4").Single(); + var z4Ref = GetReferences(tree, "z4").ToArray(); + Assert.Equal(6, z4Ref.Length); + VerifyModelForDeclarationPattern(model, z4Decl, z4Ref); + + var u4Decl = GetPatternDeclarations(tree, "u4").Single(); + var u4Ref = GetReferences(tree, "u4").ToArray(); + Assert.Equal(4, u4Ref.Length); + VerifyModelForDeclarationPattern(model, u4Decl, u4Ref[0]); + VerifyNotInScope(model, u4Ref[1]); + VerifyNotInScope(model, u4Ref[2]); + VerifyNotInScope(model, u4Ref[3]); + + var v4Decl = GetPatternDeclarations(tree, "v4").Single(); + var v4Ref = GetReferences(tree, "v4").ToArray(); + Assert.Equal(4, v4Ref.Length); + VerifyNotInScope(model, v4Ref[0]); + VerifyModelForDeclarationPattern(model, v4Decl, v4Ref[1]); + VerifyNotInScope(model, v4Ref[2]); + VerifyNotInScope(model, v4Ref[3]); + + var y5Decl = GetPatternDeclarations(tree, "y5").Single(); + var y5Ref = GetReferences(tree, "y5").ToArray(); + Assert.Equal(5, y5Ref.Length); + VerifyModelForDeclarationPattern(model, y5Decl, y5Ref); + + var z5Decl = GetPatternDeclarations(tree, "z5").Single(); + var z5Ref = GetReferences(tree, "z5").ToArray(); + Assert.Equal(6, z5Ref.Length); + VerifyModelForDeclarationPattern(model, z5Decl, z5Ref); + + var u5Decl = GetPatternDeclarations(tree, "u5").Single(); + var u5Ref = GetReferences(tree, "u5").ToArray(); + Assert.Equal(4, u5Ref.Length); + VerifyModelForDeclarationPattern(model, u5Decl, u5Ref[0]); + VerifyNotInScope(model, u5Ref[1]); + VerifyNotInScope(model, u5Ref[2]); + VerifyNotInScope(model, u5Ref[3]); + + var v5Decl = GetPatternDeclarations(tree, "v5").Single(); + var v5Ref = GetReferences(tree, "v5").ToArray(); + Assert.Equal(4, v5Ref.Length); + VerifyNotInScope(model, v5Ref[0]); + VerifyModelForDeclarationPattern(model, v5Decl, v5Ref[1]); + VerifyNotInScope(model, v5Ref[2]); + VerifyNotInScope(model, v5Ref[3]); + } + + [Fact] + [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] + public void ScopeOfPatternVariables_Query_05() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + int y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0, y6 = 0, y7 = 0, y8 = 0, y9 = 0, y10 = 0, y11 = 0, y12 = 0; + + var res = from x1 in new[] { 1 is var y1 ? y1 : 0} + from x2 in new[] { 2 is var y2 ? y2 : 0} + join x3 in new[] { 3 is var y3 ? y3 : 0} + on 4 is var y4 ? y4 : 0 + equals 5 is var y5 ? y5 : 0 + where 6 is var y6 && y6 == 1 + orderby 7 is var y7 && y7 > 0, + 8 is var y8 && y8 > 0 + group 9 is var y9 && y9 > 0 + by 10 is var y10 && y10 > 0 + into g + let x11 = 11 is var y11 && y11 > 0 + select 12 is var y12 && y12 > 0 + into s + select y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10 + y11 + y12; + + Dummy(y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (16,47): error CS0128: A local variable named 'y1' is already defined in this scope + // var res = from x1 in new[] { 1 is var y1 ? y1 : 0} + Diagnostic(ErrorCode.ERR_LocalDuplicate, "y1").WithArguments("y1").WithLocation(16, 47), + // (17,47): error CS0136: A local or parameter named 'y2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // from x2 in new[] { 2 is var y2 ? y2 : 0} + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y2").WithArguments("y2").WithLocation(17, 47), + // (18,47): error CS0128: A local variable named 'y3' is already defined in this scope + // join x3 in new[] { 3 is var y3 ? y3 : 0} + Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(18, 47), + // (19,36): error CS0136: A local or parameter named 'y4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // on 4 is var y4 ? y4 : 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y4").WithArguments("y4").WithLocation(19, 36), + // (20,43): error CS0136: A local or parameter named 'y5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // equals 5 is var y5 ? y5 : 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y5").WithArguments("y5").WithLocation(20, 43), + // (21,34): error CS0136: A local or parameter named 'y6' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // where 6 is var y6 && y6 == 1 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y6").WithArguments("y6").WithLocation(21, 34), + // (22,36): error CS0136: A local or parameter named 'y7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // orderby 7 is var y7 && y7 > 0, + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y7").WithArguments("y7").WithLocation(22, 36), + // (23,36): error CS0136: A local or parameter named 'y8' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // 8 is var y8 && y8 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y8").WithArguments("y8").WithLocation(23, 36), + // (25,32): error CS0136: A local or parameter named 'y10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // by 10 is var y10 && y10 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y10").WithArguments("y10").WithLocation(25, 32), + // (24,34): error CS0136: A local or parameter named 'y9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // group 9 is var y9 && y9 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y9").WithArguments("y9").WithLocation(24, 34), + // (27,39): error CS0136: A local or parameter named 'y11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // let x11 = 11 is var y11 && y11 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y11").WithArguments("y11").WithLocation(27, 39), + // (28,36): error CS0136: A local or parameter named 'y12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // select 12 is var y12 && y12 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y12").WithArguments("y12").WithLocation(28, 36) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + for (int i = 1; i < 13; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).Single(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); + Assert.Equal(3, yRef.Length); + + switch (i) + { + case 1: + case 3: + VerifyModelForDeclarationPatternDuplicateInSameScope(model, yDecl); + VerifyNotAPatternLocal(model, yRef[0]); + break; + default: + VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); + break; + } + + VerifyNotAPatternLocal(model, yRef[2]); + + switch (i) + { + case 1: + case 3: + case 12: + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyNotAPatternLocal(model, yRef[1]); + break; + default: + VerifyNotAPatternLocal(model, yRef[1]); + break; + } + } + } + + [Fact] + [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] + public void ScopeOfPatternVariables_Query_06() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + Dummy(1 is int y1, + 2 is int y2, + 3 is int y3, + 4 is int y4, + 5 is int y5, + 6 is int y6, + 7 is int y7, + 8 is int y8, + 9 is int y9, + 10 is int y10, + 11 is int y11, + 12 is int y12, + from x1 in new[] { 1 is var y1 ? y1 : 0} + from x2 in new[] { 2 is var y2 ? y2 : 0} + join x3 in new[] { 3 is var y3 ? y3 : 0} + on 4 is var y4 ? y4 : 0 + equals 5 is var y5 ? y5 : 0 + where 6 is var y6 && y6 == 1 + orderby 7 is var y7 && y7 > 0, + 8 is var y8 && y8 > 0 + group 9 is var y9 && y9 > 0 + by 10 is var y10 && y10 > 0 + into g + let x11 = 11 is var y11 && y11 > 0 + select 12 is var y12 && y12 > 0 + into s + select y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10 + y11 + y12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (26,47): error CS0128: A local variable named 'y1' is already defined in this scope + // from x1 in new[] { 1 is var y1 ? y1 : 0} + Diagnostic(ErrorCode.ERR_LocalDuplicate, "y1").WithArguments("y1").WithLocation(26, 47), + // (27,47): error CS0136: A local or parameter named 'y2' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // from x2 in new[] { 2 is var y2 ? y2 : 0} + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y2").WithArguments("y2").WithLocation(27, 47), + // (28,47): error CS0128: A local variable named 'y3' is already defined in this scope + // join x3 in new[] { 3 is var y3 ? y3 : 0} + Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(28, 47), + // (29,36): error CS0136: A local or parameter named 'y4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // on 4 is var y4 ? y4 : 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y4").WithArguments("y4").WithLocation(29, 36), + // (30,43): error CS0136: A local or parameter named 'y5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // equals 5 is var y5 ? y5 : 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y5").WithArguments("y5").WithLocation(30, 43), + // (31,34): error CS0136: A local or parameter named 'y6' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // where 6 is var y6 && y6 == 1 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y6").WithArguments("y6").WithLocation(31, 34), + // (32,36): error CS0136: A local or parameter named 'y7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // orderby 7 is var y7 && y7 > 0, + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y7").WithArguments("y7").WithLocation(32, 36), + // (33,36): error CS0136: A local or parameter named 'y8' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // 8 is var y8 && y8 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y8").WithArguments("y8").WithLocation(33, 36), + // (35,32): error CS0136: A local or parameter named 'y10' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // by 10 is var y10 && y10 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y10").WithArguments("y10").WithLocation(35, 32), + // (34,34): error CS0136: A local or parameter named 'y9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // group 9 is var y9 && y9 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y9").WithArguments("y9").WithLocation(34, 34), + // (37,39): error CS0136: A local or parameter named 'y11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // let x11 = 11 is var y11 && y11 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y11").WithArguments("y11").WithLocation(37, 39), + // (38,36): error CS0136: A local or parameter named 'y12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // select 12 is var y12 && y12 > 0 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y12").WithArguments("y12").WithLocation(38, 36) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + for (int i = 1; i < 13; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).ToArray(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); + Assert.Equal(2, yDecl.Length); + Assert.Equal(2, yRef.Length); + + switch (i) + { + case 1: + case 3: + VerifyModelForDeclarationPattern(model, yDecl[0], yRef); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, yDecl[1]); + break; + case 12: + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); + VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); + break; + + default: + VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); + VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); + break; + } + } + } + + [Fact] + [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] + public void ScopeOfPatternVariables_Query_07() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + Dummy(7 is int y3, + from x1 in new[] { 0 } + select x1 + into x1 + join x3 in new[] { 3 is var y3 ? y3 : 0} + on x1 equals x3 + select y3); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (18,47): error CS0128: A local variable named 'y3' is already defined in this scope + // join x3 in new[] { 3 is var y3 ? y3 : 0} + Diagnostic(ErrorCode.ERR_LocalDuplicate, "y3").WithArguments("y3").WithLocation(18, 47) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + const string id = "y3"; + var yDecl = GetPatternDeclarations(tree, id).ToArray(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); + Assert.Equal(2, yDecl.Length); + Assert.Equal(2, yRef.Length); + VerifyModelForDeclarationPattern(model, yDecl[0], yRef[1]); + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyModelForDeclarationPattern(model, yDecl[1], yRef[0]); + } + + [Fact] + public void ScopeOfPatternVariables_Query_08() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + var res = from x1 in new[] { Dummy(1 is var y1, + 2 is var y2, + 3 is var y3, + 4 is var y4 + ) ? 1 : 0} + from y1 in new[] { 1 } + join y2 in new[] { 0 } + on y1 equals y2 + let y3 = 0 + group y3 + by x1 + into y4 + select y4; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (19,24): error CS1931: The range variable 'y1' conflicts with a previous declaration of 'y1' + // from y1 in new[] { 1 } + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y1").WithArguments("y1").WithLocation(19, 24), + // (20,24): error CS1931: The range variable 'y2' conflicts with a previous declaration of 'y2' + // join y2 in new[] { 0 } + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y2").WithArguments("y2").WithLocation(20, 24), + // (22,23): error CS1931: The range variable 'y3' conflicts with a previous declaration of 'y3' + // let y3 = 0 + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y3").WithArguments("y3").WithLocation(22, 23), + // (25,24): error CS1931: The range variable 'y4' conflicts with a previous declaration of 'y4' + // into y4 + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y4").WithArguments("y4").WithLocation(25, 24) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + for (int i = 1; i < 5; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).Single(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); + VerifyModelForDeclarationPattern(model, yDecl); + VerifyNotAPatternLocal(model, yRef); + } + } + + [Fact] + [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] + public void ScopeOfPatternVariables_Query_09() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + var res = from y1 in new[] { 0 } + join y2 in new[] { 0 } + on y1 equals y2 + let y3 = 0 + group y3 + by 1 + into y4 + select y4 == null ? 1 : 0 + into x2 + join y5 in new[] { Dummy(1 is var y1, + 2 is var y2, + 3 is var y3, + 4 is var y4, + 5 is var y5 + ) ? 1 : 0 } + on x2 equals y5 + select x2; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (14,24): error CS1931: The range variable 'y1' conflicts with a previous declaration of 'y1' + // var res = from y1 in new[] { 0 } + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y1").WithArguments("y1").WithLocation(14, 24), + // (15,24): error CS1931: The range variable 'y2' conflicts with a previous declaration of 'y2' + // join y2 in new[] { 0 } + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y2").WithArguments("y2").WithLocation(15, 24), + // (17,23): error CS1931: The range variable 'y3' conflicts with a previous declaration of 'y3' + // let y3 = 0 + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y3").WithArguments("y3").WithLocation(17, 23), + // (20,24): error CS1931: The range variable 'y4' conflicts with a previous declaration of 'y4' + // into y4 + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y4").WithArguments("y4").WithLocation(20, 24), + // (23,24): error CS1931: The range variable 'y5' conflicts with a previous declaration of 'y5' + // join y5 in new[] { Dummy(1 is var y1, + Diagnostic(ErrorCode.ERR_QueryRangeVariableOverrides, "y5").WithArguments("y5").WithLocation(23, 24) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + for (int i = 1; i < 6; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).Single(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).Single(); + + switch (i) + { + case 4: + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyModelForDeclarationPattern(model, yDecl); + VerifyNotAPatternLocal(model, yRef); + break; + case 5: + VerifyModelForDeclarationPattern(model, yDecl); + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyNotAPatternLocal(model, yRef); + break; + default: + VerifyModelForDeclarationPattern(model, yDecl); + VerifyNotAPatternLocal(model, yRef); + break; + } + } + } + + [Fact] + [WorkItem(10466, "https://github.com/dotnet/roslyn/issues/10466")] + [WorkItem(12052, "https://github.com/dotnet/roslyn/issues/12052")] + public void ScopeOfPatternVariables_Query_10() + { + var source = +@" +using System.Linq; + +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + var res = from y1 in new[] { 0 } + from x2 in new[] { 1 is var y1 ? y1 : 1 } + select y1; + } + + void Test2() + { + var res = from y2 in new[] { 0 } + join x3 in new[] { 1 } + on 2 is var y2 ? y2 : 0 + equals x3 + select y2; + } + + void Test3() + { + var res = from x3 in new[] { 0 } + join y3 in new[] { 1 } + on x3 + equals 3 is var y3 ? y3 : 0 + select y3; + } + + void Test4() + { + var res = from y4 in new[] { 0 } + where 4 is var y4 && y4 == 1 + select y4; + } + + void Test5() + { + var res = from y5 in new[] { 0 } + orderby 5 is var y5 && y5 > 1, + 1 + select y5; + } + + void Test6() + { + var res = from y6 in new[] { 0 } + orderby 1, + 6 is var y6 && y6 > 1 + select y6; + } + + void Test7() + { + var res = from y7 in new[] { 0 } + group 7 is var y7 && y7 == 3 + by y7; + } + + void Test8() + { + var res = from y8 in new[] { 0 } + group y8 + by 8 is var y8 && y8 == 3; + } + + void Test9() + { + var res = from y9 in new[] { 0 } + let x4 = 9 is var y9 && y9 > 0 + select y9; + } + + void Test10() + { + var res = from y10 in new[] { 0 } + select 10 is var y10 && y10 > 0; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, new[] { SystemCoreRef }, options: TestOptions.DebugExe); + + // error CS0412 is misleading and reported due to preexisting bug https://github.com/dotnet/roslyn/issues/12052 + compilation.VerifyDiagnostics( + // (15,47): error CS0412: 'y1': a parameter or local variable cannot have the same name as a method type parameter + // from x2 in new[] { 1 is var y1 ? y1 : 1 } + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y1").WithArguments("y1").WithLocation(15, 47), + // (23,36): error CS0412: 'y2': a parameter or local variable cannot have the same name as a method type parameter + // on 2 is var y2 ? y2 : 0 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y2").WithArguments("y2").WithLocation(23, 36), + // (33,40): error CS0412: 'y3': a parameter or local variable cannot have the same name as a method type parameter + // equals 3 is var y3 ? y3 : 0 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y3").WithArguments("y3").WithLocation(33, 40), + // (40,34): error CS0412: 'y4': a parameter or local variable cannot have the same name as a method type parameter + // where 4 is var y4 && y4 == 1 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y4").WithArguments("y4").WithLocation(40, 34), + // (47,36): error CS0412: 'y5': a parameter or local variable cannot have the same name as a method type parameter + // orderby 5 is var y5 && y5 > 1, + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y5").WithArguments("y5").WithLocation(47, 36), + // (56,36): error CS0412: 'y6': a parameter or local variable cannot have the same name as a method type parameter + // 6 is var y6 && y6 > 1 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y6").WithArguments("y6").WithLocation(56, 36), + // (63,34): error CS0412: 'y7': a parameter or local variable cannot have the same name as a method type parameter + // group 7 is var y7 && y7 == 3 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y7").WithArguments("y7").WithLocation(63, 34), + // (71,31): error CS0412: 'y8': a parameter or local variable cannot have the same name as a method type parameter + // by 8 is var y8 && y8 == 3; + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y8").WithArguments("y8").WithLocation(71, 31), + // (77,37): error CS0412: 'y9': a parameter or local variable cannot have the same name as a method type parameter + // let x4 = 9 is var y9 && y9 > 0 + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y9").WithArguments("y9").WithLocation(77, 37), + // (84,36): error CS0412: 'y10': a parameter or local variable cannot have the same name as a method type parameter + // select 10 is var y10 && y10 > 0; + Diagnostic(ErrorCode.ERR_LocalSameNameAsTypeParam, "y10").WithArguments("y10").WithLocation(84, 36) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + for (int i = 1; i < 11; i++) + { + var id = "y" + i; + var yDecl = GetPatternDeclarations(tree, id).Single(); + var yRef = tree.GetRoot().DescendantNodes().OfType().Where(name => name.Identifier.ValueText == id).ToArray(); + Assert.Equal(i == 10 ? 1 : 2, yRef.Length); + + switch (i) + { + case 4: + case 6: + VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyNotAPatternLocal(model, yRef[1]); + break; + case 8: + VerifyModelForDeclarationPattern(model, yDecl, yRef[1]); + // Should be uncommented once https://github.com/dotnet/roslyn/issues/10466 is fixed. + //VerifyNotAPatternLocal(model, yRef[0]); + break; + case 10: + VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); + break; + default: + VerifyModelForDeclarationPattern(model, yDecl, yRef[0]); + VerifyNotAPatternLocal(model, yRef[1]); + break; + } + } + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionBodiedLocalFunctions_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + void f(object o) => let x1 = o; + f(null); + } + + void Test2() + { + void f(object o) => let var x2 = o; + f(null); + } + + void Test3() + { + bool f (object o) => o is int x3 && x3 > 0; + f(null); + } + + void Test4() + { + bool f (object o) => x4 && o is int x4; + f(null); + } + + void Test5() + { + bool f (object o1, object o2) => o1 is int x5 && + o2 is int x5 && + x5 > 0; + f(null, null); + } + + void Test6() + { + bool f1 (object o) => o is int x6 && x6 > 0; bool f2 (object o) => o is int x6 && x6 > 0; + f1(null); + f2(null); + } + + void Test7() + { + Dummy(x7, 1); + + bool f (object o) => o is int x7 && x7 > 0; + + Dummy(x7, 2); + f(null); + } + + void Test11() + { + var x11 = 11; + Dummy(x11); + bool f (object o) => o is int x11 && + x11 > 0; + f(null); + } + + void Test12() + { + bool f (object o) => o is int x12 && + x12 > 0; + var x12 = 11; + Dummy(x12); + f(null); + } + + System.Action Test13() + { + return () => + { + bool f (object o) => o is int x13 && x13 > 0; + f(null); + }; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (12,33): error CS1002: ; expected + // void f(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(12, 33), + // (18,33): error CS1002: ; expected + // void f(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(18, 33), + // (12,29): error CS0103: The name 'let' does not exist in the current context + // void f(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(12, 29), + // (12,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // void f(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(12, 29), + // (12,33): error CS0103: The name 'x1' does not exist in the current context + // void f(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(12, 33), + // (12,38): error CS0103: The name 'o' does not exist in the current context + // void f(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(12, 38), + // (18,29): error CS0103: The name 'let' does not exist in the current context + // void f(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(18, 29), + // (18,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // void f(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(18, 29), + // (18,42): error CS0103: The name 'o' does not exist in the current context + // void f(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(18, 42), + // (30,30): error CS0841: Cannot use local variable 'x4' before it is declared + // bool f (object o) => x4 && o is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(30, 30), + // (37,52): error CS0128: A local variable named 'x5' is already defined in this scope + // o2 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(37, 52), + // (38,42): error CS0165: Use of unassigned local variable 'x5' + // x5 > 0; + Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(38, 42), + // (51,15): error CS0103: The name 'x7' does not exist in the current context + // Dummy(x7, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(51, 15), + // (55,15): error CS0103: The name 'x7' does not exist in the current context + // Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(55, 15), + // (63,39): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // bool f (object o) => o is int x11 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(63, 39), + // (70,39): error CS0136: A local or parameter named 'x12' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // bool f (object o) => o is int x12 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x12").WithArguments("x12").WithLocation(70, 39) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyNotInScope(model, x7Ref[0]); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyNotAPatternLocal(model, x11Ref[0]); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[1]); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[0]); + VerifyNotAPatternLocal(model, x12Ref[1]); + + var x13Decl = GetPatternDeclarations(tree, "x13").Single(); + var x13Ref = GetReferences(tree, "x13").Single(); + VerifyModelForDeclarationPattern(model, x13Decl, x13Ref); + } + + [Fact] + public void ExpressionBodiedLocalFunctions_01() + { + var source = +@" +public class X +{ + public static void Main() + { + System.Console.WriteLine(Test1()); + } + + static bool Test1() + { + bool f() => 1 is int x1 && Dummy(x1); + return f(); + } + + static bool Dummy(int x) + { + System.Console.WriteLine(x); + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + CompileAndVerify(compilation, expectedOutput: @"1 +True"); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionBodiedFunctions_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + + void Test1(object o) => let x1 = o; + + void Test2(object o) => let var x2 = o; + + bool Test3(object o) => o is int x3 && x3 > 0; + + bool Test4(object o) => x4 && o is int x4; + + bool Test5(object o1, object o2) => o1 is int x5 && + o2 is int x5 && + x5 > 0; + + bool Test61 (object o) => o is int x6 && x6 > 0; bool Test62 (object o) => o is int x6 && x6 > 0; + + bool Test71(object o) => o is int x7 && x7 > 0; + void Test72() => Dummy(x7, 2); + void Test73() { Dummy(x7, 3); } + + bool Test11(object x11) => 1 is int x11 && + x11 > 0; + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (9,33): error CS1002: ; expected + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(9, 33), + // (9,36): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 36), + // (9,36): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 36), + // (9,39): error CS1519: Invalid token ';' in class, struct, or interface member declaration + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(9, 39), + // (9,39): error CS1519: Invalid token ';' in class, struct, or interface member declaration + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, ";").WithArguments(";").WithLocation(9, 39), + // (11,33): error CS1002: ; expected + // void Test2(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(11, 33), + // (11,33): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // void Test2(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(11, 33), + // (11,42): error CS0103: The name 'o' does not exist in the current context + // void Test2(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(11, 42), + // (9,29): error CS0103: The name 'let' does not exist in the current context + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(9, 29), + // (9,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // void Test1(object o) => let x1 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(9, 29), + // (11,29): error CS0103: The name 'let' does not exist in the current context + // void Test2(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(11, 29), + // (11,29): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement + // void Test2(object o) => let var x2 = o; + Diagnostic(ErrorCode.ERR_IllegalStatement, "let").WithLocation(11, 29), + // (15,29): error CS0841: Cannot use local variable 'x4' before it is declared + // bool Test4(object o) => x4 && o is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(15, 29), + // (18,52): error CS0128: A local variable named 'x5' is already defined in this scope + // o2 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 52), + // (19,42): error CS0165: Use of unassigned local variable 'x5' + // x5 > 0; + Diagnostic(ErrorCode.ERR_UseDefViolation, "x5").WithArguments("x5").WithLocation(19, 42), + // (24,28): error CS0103: The name 'x7' does not exist in the current context + // void Test72() => Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(24, 28), + // (25,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(25, 27), + // (27,41): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // bool Test11(object x11) => 1 is int x11 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(27, 41) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").Single(); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + } + + [Fact] + public void ScopeOfPatternVariables_ExpressionBodiedProperties_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + + bool Test1 => let x1 = 11; + + bool this[int o] => let var x2 = o; + + bool Test3 => 3 is int x3 && x3 > 0; + + bool Test4 => x4 && 4 is int x4; + + bool Test5 => 51 is int x5 && + 52 is int x5 && + x5 > 0; + + bool Test61 => 6 is int x6 && x6 > 0; bool Test62 => 6 is int x6 && x6 > 0; + + bool Test71 => 7 is int x7 && x7 > 0; + bool Test72 => Dummy(x7, 2); + void Test73() { Dummy(x7, 3); } + + bool this[object x11] => 1 is int x11 && + x11 > 0; + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (9,23): error CS1002: ; expected + // bool Test1 => let x1 = 11; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "x1").WithLocation(9, 23), + // (9,26): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // bool Test1 => let x1 = 11; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 26), + // (9,26): error CS1519: Invalid token '=' in class, struct, or interface member declaration + // bool Test1 => let x1 = 11; + Diagnostic(ErrorCode.ERR_InvalidMemberDecl, "=").WithArguments("=").WithLocation(9, 26), + // (11,29): error CS1002: ; expected + // bool this[int o] => let var x2 = o; + Diagnostic(ErrorCode.ERR_SemicolonExpected, "var").WithLocation(11, 29), + // (11,29): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code + // bool this[int o] => let var x2 = o; + Diagnostic(ErrorCode.ERR_TypeVarNotFound, "var").WithLocation(11, 29), + // (11,38): error CS0103: The name 'o' does not exist in the current context + // bool this[int o] => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "o").WithArguments("o").WithLocation(11, 38), + // (9,19): error CS0103: The name 'let' does not exist in the current context + // bool Test1 => let x1 = 11; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(9, 19), + // (11,25): error CS0103: The name 'let' does not exist in the current context + // bool this[int o] => let var x2 = o; + Diagnostic(ErrorCode.ERR_NameNotInContext, "let").WithArguments("let").WithLocation(11, 25), + // (15,19): error CS0841: Cannot use local variable 'x4' before it is declared + // bool Test4 => x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(15, 19), + // (18,29): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 29), + // (24,26): error CS0103: The name 'x7' does not exist in the current context + // bool Test72 => Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(24, 26), + // (25,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(25, 27), + // (27,39): error CS0136: A local or parameter named 'x11' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // bool this[object x11] => 1 is int x11 && + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x11").WithArguments("x11").WithLocation(27, 39) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").Single(); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + } + + [Fact] + public void ScopeOfPatternVariables_FieldInitializers_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Test3 = 3 is int x3 && x3 > 0; + + bool Test4 = x4 && 4 is int x4; + + bool Test5 = 51 is int x5 && + 52 is int x5 && + x5 > 0; + + bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + + bool Test71 = 7 is int x7 && x7 > 0; + bool Test72 = Dummy(x7, 2); + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,23): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test3 = 3 is int x3 && x3 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 23), + // (10,18): error CS0841: Cannot use local variable 'x4' before it is declared + // bool Test4 = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 18), + // (10,29): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test4 = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 29), + // (12,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test5 = 51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 24), + // (13,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 24), + // (13,28): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 28), + // (16,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 24), + // (16,56): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 56), + // (18,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test71 = 7 is int x7 && x7 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 24), + // (19,25): error CS0103: The name 'x7' does not exist in the current context + // bool Test72 = Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 25), + // (20,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_FieldInitializers_02() + { + var source = +@" +public enum X +{ + Test3 = 3 is int x3 ? x3 : 0, + + Test4 = x4 && 4 is int x4 ? 1 : 0, + + Test5 = 51 is int x5 && + 52 is int x5 && + x5 > 0 ? 1 : 0, + + Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, + + Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, + Test72 = x7, +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugDll); + compilation.VerifyDiagnostics( + // (6,13): error CS0841: Cannot use local variable 'x4' before it is declared + // Test4 = x4 && 4 is int x4 ? 1 : 0, + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(6, 13), + // (6,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test4 = x4 && 4 is int x4 ? 1 : 0, + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(6, 24), + // (8,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test5 = 51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(8, 19), + // (9,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(9, 19), + // (9,23): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(9, 23), + // (12,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(12, 19), + // (12,14): error CS0133: The expression being assigned to 'X.Test61' must be constant + // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0 ? 1 : 0").WithArguments("X.Test61").WithLocation(12, 14), + // (12,59): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(12, 59), + // (12,54): error CS0133: The expression being assigned to 'X.Test62' must be constant + // Test61 = 6 is int x6 && x6 > 0 ? 1 : 0, Test62 = 6 is int x6 && x6 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0 ? 1 : 0").WithArguments("X.Test62").WithLocation(12, 54), + // (14,19): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(14, 19), + // (14,14): error CS0133: The expression being assigned to 'X.Test71' must be constant + // Test71 = 7 is int x7 && x7 > 0 ? 1 : 0, + Diagnostic(ErrorCode.ERR_NotConstantExpression, "7 is int x7 && x7 > 0 ? 1 : 0").WithArguments("X.Test71").WithLocation(14, 14), + // (15,14): error CS0103: The name 'x7' does not exist in the current context + // Test72 = x7, + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 14), + // (4,18): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // Test3 = 3 is int x3 ? x3 : 0, + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(4, 18), + // (4,13): error CS0133: The expression being assigned to 'X.Test3' must be constant + // Test3 = 3 is int x3 ? x3 : 0, + Diagnostic(ErrorCode.ERR_NotConstantExpression, "3 is int x3 ? x3 : 0").WithArguments("X.Test3").WithLocation(4, 13) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_FieldInitializers_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + const bool Test3 = 3 is int x3 && x3 > 0; + + const bool Test4 = x4 && 4 is int x4; + + const bool Test5 = 51 is int x5 && + 52 is int x5 && + x5 > 0; + + const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + + const bool Test71 = 7 is int x7 && x7 > 0; + const bool Test72 = x7 > 2; + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,29): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test3 = 3 is int x3 && x3 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 29), + // (8,24): error CS0133: The expression being assigned to 'X.Test3' must be constant + // const bool Test3 = 3 is int x3 && x3 > 0; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "3 is int x3 && x3 > 0").WithArguments("X.Test3").WithLocation(8, 24), + // (10,24): error CS0841: Cannot use local variable 'x4' before it is declared + // const bool Test4 = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 24), + // (10,35): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test4 = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 35), + // (12,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test5 = 51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 30), + // (13,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 30), + // (13,34): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 34), + // (16,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 30), + // (16,25): error CS0133: The expression being assigned to 'X.Test61' must be constant + // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0").WithArguments("X.Test61").WithLocation(16, 25), + // (16,62): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 62), + // (16,57): error CS0133: The expression being assigned to 'X.Test62' must be constant + // const bool Test61 = 6 is int x6 && x6 > 0, Test62 = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "6 is int x6 && x6 > 0").WithArguments("X.Test62").WithLocation(16, 57), + // (18,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // const bool Test71 = 7 is int x7 && x7 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 30), + // (18,25): error CS0133: The expression being assigned to 'X.Test71' must be constant + // const bool Test71 = 7 is int x7 && x7 > 0; + Diagnostic(ErrorCode.ERR_NotConstantExpression, "7 is int x7 && x7 > 0").WithArguments("X.Test71").WithLocation(18, 25), + // (19,25): error CS0103: The name 'x7' does not exist in the current context + // const bool Test72 = x7 > 2; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 25), + // (20,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_PropertyInitializers_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Test3 {get;} = 3 is int x3 && x3 > 0; + + bool Test4 {get;} = x4 && 4 is int x4; + + bool Test5 {get;} = 51 is int x5 && + 52 is int x5 && + x5 > 0; + + bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; + + bool Test71 {get;} = 7 is int x7 && x7 > 0; + bool Test72 {get;} = Dummy(x7, 2); + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,30): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test3 {get;} = 3 is int x3 && x3 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(8, 30), + // (10,25): error CS0841: Cannot use local variable 'x4' before it is declared + // bool Test4 {get;} = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(10, 25), + // (10,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test4 {get;} = x4 && 4 is int x4; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(10, 36), + // (12,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test5 {get;} = 51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(12, 31), + // (13,24): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(13, 24), + // (13,28): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(13, 28), + // (16,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 31), + // (16,75): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test61 {get;} = 6 is int x6 && x6 > 0; bool Test62 {get;} = 6 is int x6 && x6 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(16, 75), + // (18,31): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // bool Test71 {get;} = 7 is int x7 && x7 > 0; + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(18, 31), + // (19,32): error CS0103: The name 'x7' does not exist in the current context + // bool Test72 {get;} = Dummy(x7, 2); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(19, 32), + // (20,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(20, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_ParameterDefault_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Test3(bool p = 3 is int x3 && x3 > 0) + {} + + void Test4(bool p = x4 && 4 is int x4) + {} + + void Test5(bool p = 51 is int x5 && + 52 is int x5 && + x5 > 0) + {} + + void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) + {} + + void Test71(bool p = 7 is int x7 && x7 > 0) + { + } + + void Test72(bool p = x7 > 2) + {} + + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,25): error CS1736: Default parameter value for 'p' must be a compile-time constant + // void Test3(bool p = 3 is int x3 && x3 > 0) + Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "3 is int x3 && x3 > 0").WithArguments("p").WithLocation(8, 25), + // (11,25): error CS0841: Cannot use local variable 'x4' before it is declared + // void Test4(bool p = x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(11, 25), + // (11,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool' + // void Test4(bool p = x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(11, 21), + // (15,35): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(15, 35), + // (14,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool' + // void Test5(bool p = 51 is int x5 && + Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(14, 21), + // (19,27): error CS1736: Default parameter value for 'p1' must be a compile-time constant + // void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "6 is int x6 && x6 > 0").WithArguments("p1").WithLocation(19, 27), + // (19,60): error CS1736: Default parameter value for 'p2' must be a compile-time constant + // void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "6 is int x6 && x6 > 0").WithArguments("p2").WithLocation(19, 60), + // (22,26): error CS1736: Default parameter value for 'p' must be a compile-time constant + // void Test71(bool p = 7 is int x7 && x7 > 0) + Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "7 is int x7 && x7 > 0").WithArguments("p").WithLocation(22, 26), + // (26,26): error CS0103: The name 'x7' does not exist in the current context + // void Test72(bool p = x7 > 2) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(26, 26), + // (29,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(29, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref[0]); + VerifyModelForDeclarationPattern(model, x6Decl[1], x6Ref[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_Attribute_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + [Test(p = 3 is int x3 && x3 > 0)] + [Test(p = x4 && 4 is int x4)] + [Test(p = 51 is int x5 && + 52 is int x5 && + x5 > 0)] + [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] + [Test(p = 7 is int x7 && x7 > 0)] + [Test(p = x7 > 2)] + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} + +class Test : System.Attribute +{ + public bool p {get; set;} + public bool p1 {get; set;} + public bool p2 {get; set;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,15): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type + // [Test(p = 3 is int x3 && x3 > 0)] + Diagnostic(ErrorCode.ERR_BadAttributeArgument, "3 is int x3 && x3 > 0").WithLocation(8, 15), + // (9,15): error CS0841: Cannot use local variable 'x4' before it is declared + // [Test(p = x4 && 4 is int x4)] + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(9, 15), + // (11,25): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(11, 25), + // (13,53): error CS0128: A local variable named 'x6' is already defined in this scope + // [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(13, 53), + // (13,16): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type + // [Test(p1 = 6 is int x6 && x6 > 0, p2 = 6 is int x6 && x6 > 0)] + Diagnostic(ErrorCode.ERR_BadAttributeArgument, "6 is int x6 && x6 > 0").WithLocation(13, 16), + // (14,15): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type + // [Test(p = 7 is int x7 && x7 > 0)] + Diagnostic(ErrorCode.ERR_BadAttributeArgument, "7 is int x7 && x7 > 0").WithLocation(14, 15), + // (15,15): error CS0103: The name 'x7' does not exist in the current context + // [Test(p = x7 > 2)] + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 15), + // (16,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(16, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_Attribute_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + [Test(3 is int x3 && x3 > 0)] + [Test(x4 && 4 is int x4)] + [Test(51 is int x5 && + 52 is int x5 && + x5 > 0)] + [Test(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0)] + [Test(7 is int x7 && x7 > 0)] + [Test(x7 > 2)] + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} + +class Test : System.Attribute +{ + public Test(bool p) {} + public Test(bool p1, bool p2) {} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (8,11): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type + // [Test(3 is int x3 && x3 > 0)] + Diagnostic(ErrorCode.ERR_BadAttributeArgument, "3 is int x3 && x3 > 0").WithLocation(8, 11), + // (9,11): error CS0841: Cannot use local variable 'x4' before it is declared + // [Test(x4 && 4 is int x4)] + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(9, 11), + // (11,21): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(11, 21), + // (13,43): error CS0128: A local variable named 'x6' is already defined in this scope + // [Test(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0)] + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(13, 43), + // (14,11): error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type + // [Test(7 is int x7 && x7 > 0)] + Diagnostic(ErrorCode.ERR_BadAttributeArgument, "7 is int x7 && x7 > 0").WithLocation(14, 11), + // (15,11): error CS0103: The name 'x7' does not exist in the current context + // [Test(x7 > 2)] + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(15, 11), + // (16,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(16, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_ConstructorInitializers_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + X(byte x) + : this(3 is int x3 && x3 > 0) + {} + + X(sbyte x) + : this(x4 && 4 is int x4) + {} + + X(short x) + : this(51 is int x5 && + 52 is int x5 && + x5 > 0) + {} + + X(ushort x) + : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + {} + + X(int x) + : this(7 is int x7 && x7 > 0) + {} + X(uint x) + : this(x7, 2) + {} + void Test73() { Dummy(x7, 3); } + + X(params object[] x) {} + bool Dummy(params object[] x) {return true;} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (9,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(3 is int x3 && x3 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 21), + // (13,16): error CS0841: Cannot use local variable 'x4' before it is declared + // : this(x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(13, 16), + // (13,27): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 27), + // (17,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(17, 22), + // (18,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(18, 22), + // (18,26): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 26), + // (23,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 21), + // (23,44): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 44), + // (23,48): error CS0128: A local variable named 'x6' is already defined in this scope + // : this(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(23, 48), + // (27,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : this(7 is int x7 && x7 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(27, 21), + // (30,16): error CS0103: The name 'x7' does not exist in the current context + // : this(x7, 2) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(30, 16), + // (32,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(32, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_ConstructorInitializers_02() + { + var source = +@" +public class X : Y +{ + public static void Main() + { + } + + X(byte x) + : base(3 is int x3 && x3 > 0) + {} + + X(sbyte x) + : base(x4 && 4 is int x4) + {} + + X(short x) + : base(51 is int x5 && + 52 is int x5 && + x5 > 0) + {} + + X(ushort x) + : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + {} + + X(int x) + : base(7 is int x7 && x7 > 0) + {} + X(uint x) + : base(x7, 2) + {} + void Test73() { Dummy(x7, 3); } + + bool Dummy(params object[] x) {return true;} +} + +public class Y +{ + public Y(params object[] x) {} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (9,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(3 is int x3 && x3 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x3").WithLocation(9, 21), + // (13,16): error CS0841: Cannot use local variable 'x4' before it is declared + // : base(x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(13, 16), + // (13,27): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(x4 && 4 is int x4) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x4").WithLocation(13, 27), + // (17,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(51 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(17, 22), + // (18,22): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x5").WithLocation(18, 22), + // (18,26): error CS0128: A local variable named 'x5' is already defined in this scope + // 52 is int x5 && + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(18, 26), + // (23,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 21), + // (23,44): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x6").WithLocation(23, 44), + // (23,48): error CS0128: A local variable named 'x6' is already defined in this scope + // : base(6 is int x6 && x6 > 0, 6 is int x6 && x6 > 0) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(23, 48), + // (27,21): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // : base(7 is int x7 && x7 > 0) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x7").WithLocation(27, 21), + // (30,16): error CS0103: The name 'x7' does not exist in the current context + // : base(x7, 2) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(30, 16), + // (32,27): error CS0103: The name 'x7' does not exist in the current context + // void Test73() { Dummy(x7, 3); } + Diagnostic(ErrorCode.ERR_NameNotInContext, "x7").WithArguments("x7").WithLocation(32, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").ToArray(); + var x5Ref = GetReferences(tree, "x5").Single(); + Assert.Equal(2, x5Decl.Length); + VerifyModelForDeclarationPattern(model, x5Decl[0], x5Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl[1]); + + var x6Decl = GetPatternDeclarations(tree, "x6").ToArray(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Decl.Length); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl[0], x6Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl[1]); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(3, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotInScope(model, x7Ref[1]); + VerifyNotInScope(model, x7Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_ConstructorInitializers_03() + { + var source = +@"using System; +public class X +{ + public static void Main() + { + new D(1); + new D(10); + new D(1.2); + } +} +class D +{ + public D(object o) : this(o is int x && x >= 5) + { + Console.WriteLine(x); + } + + public D(bool b) { Console.WriteLine(b); } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (15,27): error CS0103: The name 'x' does not exist in the current context + // Console.WriteLine(x); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(15, 27), + // (13,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // public D(object o) : this(o is int x && x >= 5) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x").WithLocation(13, 36) + ); + } + + [Fact] + public void ScopeOfPatternVariables_ConstructorInitializers_04() + { + var source = +@"using System; +public class X +{ + public static void Main() + { + new D(1); + new D(10); + new D(1.2); + } +} +class D : C +{ + public D(object o) : base(o is int x && x >= 5) + { + Console.WriteLine(x); + } +} + +class C +{ + public C(bool b) { Console.WriteLine(b); } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (15,27): error CS0103: The name 'x' does not exist in the current context + // Console.WriteLine(x); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x").WithLocation(15, 27), + // (13,36): error CS8200: Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers. + // public D(object o) : base(o is int x && x >= 5) + Diagnostic(ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, "int x").WithLocation(13, 36) + ); + } + + [Fact] + public void ScopeOfPatternVariables_SwitchLabelGuard_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) { return true; } + + void Test1(int val) + { + switch (val) + { + case 0 when Dummy(true is var x1, x1): + Dummy(x1); + break; + case 1 when Dummy(true is var x1, x1): + Dummy(x1); + break; + case 2 when Dummy(true is var x1, x1): + Dummy(x1); + break; + } + } + + void Test2(int val) + { + switch (val) + { + case 0 when Dummy(x2, true is var x2): + Dummy(x2); + break; + } + } + + void Test3(int x3, int val) + { + switch (val) + { + case 0 when Dummy(true is var x3, x3): + Dummy(x3); + break; + } + } + + void Test4(int val) + { + var x4 = 11; + switch (val) + { + case 0 when Dummy(true is var x4, x4): + Dummy(x4); + break; + case 1 when Dummy(x4): Dummy(x4); break; + } + } + + void Test5(int val) + { + switch (val) + { + case 0 when Dummy(true is var x5, x5): + Dummy(x5); + break; + } + + var x5 = 11; + Dummy(x5); + } + + //void Test6(int val) + //{ + // let x6 = 11; + // switch (val) + // { + // case 0 when Dummy(x6): + // Dummy(x6); + // break; + // case 1 when Dummy(true is var x6, x6): + // Dummy(x6); + // break; + // } + //} + + //void Test7(int val) + //{ + // switch (val) + // { + // case 0 when Dummy(true is var x7, x7): + // Dummy(x7); + // break; + // } + + // let x7 = 11; + // Dummy(x7); + //} + + void Test8(int val) + { + switch (val) + { + case 0 when Dummy(true is var x8, x8, false is var x8, x8): + Dummy(x8); + break; + } + } + + void Test9(int val) + { + switch (val) + { + case 0 when Dummy(x9): + int x9 = 9; + Dummy(x9); + break; + case 2 when Dummy(x9 = 9): + Dummy(x9); + break; + case 1 when Dummy(true is var x9, x9): + Dummy(x9); + break; + } + } + + //void Test10(int val) + //{ + // switch (val) + // { + // case 1 when Dummy(true is var x10, x10): + // Dummy(x10); + // break; + // case 0 when Dummy(x10): + // let x10 = 10; + // Dummy(x10); + // break; + // case 2 when Dummy(x10 = 10, x10): + // Dummy(x10); + // break; + // } + //} + + void Test11(int val) + { + switch (x11 ? val : 0) + { + case 0 when Dummy(x11): + Dummy(x11, 0); + break; + case 1 when Dummy(true is var x11, x11): + Dummy(x11, 1); + break; + } + } + + void Test12(int val) + { + switch (x12 ? val : 0) + { + case 0 when Dummy(true is var x12, x12): + Dummy(x12, 0); + break; + case 1 when Dummy(x12): + Dummy(x12, 1); + break; + } + } + + void Test13() + { + switch (1 is var x13 ? x13 : 0) + { + case 0 when Dummy(x13): + Dummy(x13); + break; + case 1 when Dummy(true is var x13, x13): + Dummy(x13); + break; + } + } + + void Test14(int val) + { + switch (val) + { + case 1 when Dummy(true is var x14, x14): + Dummy(x14); + Dummy(true is var x14, x14); + Dummy(x14); + break; + } + } + + void Test15(int val) + { + switch (val) + { + case 0 when Dummy(true is var x15, x15): + case 1 when Dummy(true is var x15, x15): + Dummy(x15); + break; + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (30,31): error CS0841: Cannot use local variable 'x2' before it is declared + // case 0 when Dummy(x2, true is var x2): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(30, 31), + // (40,43): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(true is var x3, x3): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(40, 43), + // (51,43): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(true is var x4, x4): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(51, 43), + // (62,43): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(true is var x5, x5): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(62, 43), + // (102,64): error CS0128: A local variable named 'x8' is already defined in this scope + // case 0 when Dummy(true is var x8, x8, false is var x8, x8): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(102, 64), + // (112,31): error CS0841: Cannot use local variable 'x9' before it is declared + // case 0 when Dummy(x9): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(112, 31), + // (119,43): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(true is var x9, x9): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(119, 43), + // (144,17): error CS0103: The name 'x11' does not exist in the current context + // switch (x11 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(144, 17), + // (146,31): error CS0103: The name 'x11' does not exist in the current context + // case 0 when Dummy(x11): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(146, 31), + // (147,23): error CS0103: The name 'x11' does not exist in the current context + // Dummy(x11, 0); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(147, 23), + // (157,17): error CS0103: The name 'x12' does not exist in the current context + // switch (x12 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(157, 17), + // (162,31): error CS0103: The name 'x12' does not exist in the current context + // case 1 when Dummy(x12): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(162, 31), + // (163,23): error CS0103: The name 'x12' does not exist in the current context + // Dummy(x12, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(163, 23), + // (175,43): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(true is var x13, x13): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(175, 43), + // (185,43): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(true is var x14, x14): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(185, 43), + // (198,43): error CS0128: A local variable named 'x15' is already defined in this scope + // case 1 when Dummy(true is var x15, x15): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(198, 43), + // (198,48): error CS0165: Use of unassigned local variable 'x15' + // case 1 when Dummy(true is var x15, x15): + Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(198, 48) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(6, x1Ref.Length); + for (int i = 0; i < x1Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); + } + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(4, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyNotAPatternLocal(model, x4Ref[3]); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); + VerifyNotAPatternLocal(model, x5Ref[2]); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(3, x8Ref.Length); + for (int i = 0; i < x8Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(6, x9Ref.Length); + VerifyNotAPatternLocal(model, x9Ref[0]); + VerifyNotAPatternLocal(model, x9Ref[1]); + VerifyNotAPatternLocal(model, x9Ref[2]); + VerifyNotAPatternLocal(model, x9Ref[3]); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(5, x11Ref.Length); + VerifyNotInScope(model, x11Ref[0]); + VerifyNotInScope(model, x11Ref[1]); + VerifyNotInScope(model, x11Ref[2]); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(5, x12Ref.Length); + VerifyNotInScope(model, x12Ref[0]); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); + VerifyNotInScope(model, x12Ref[3]); + VerifyNotInScope(model, x12Ref[4]); + + var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); + var x13Ref = GetReferences(tree, "x13").ToArray(); + Assert.Equal(2, x13Decl.Length); + Assert.Equal(5, x13Ref.Length); + VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); + VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(4, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPattern(model, x14Decl[1], true); + + var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(2, x15Decl.Length); + Assert.Equal(3, x15Ref.Length); + for (int i = 0; i < x15Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_SwitchLabelPattern_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) { return true; } + + void Test1(object val) + { + switch (val) + { + case byte x1 when Dummy(x1): + Dummy(x1); + break; + case int x1 when Dummy(x1): + Dummy(x1); + break; + case long x1 when Dummy(x1): + Dummy(x1); + break; + } + } + + void Test2(object val) + { + switch (val) + { + case 0 when Dummy(x2): + case int x2: + Dummy(x2); + break; + } + } + + void Test3(int x3, object val) + { + switch (val) + { + case int x3 when Dummy(x3): + Dummy(x3); + break; + } + } + + void Test4(object val) + { + var x4 = 11; + switch (val) + { + case int x4 when Dummy(x4): + Dummy(x4); + break; + case 1 when Dummy(x4): + Dummy(x4); + break; + } + } + + void Test5(object val) + { + switch (val) + { + case int x5 when Dummy(x5): + Dummy(x5); + break; + } + + var x5 = 11; + Dummy(x5); + } + + //void Test6(object val) + //{ + // let x6 = 11; + // switch (val) + // { + // case 0 when Dummy(x6): + // Dummy(x6); + // break; + // case int x6 when Dummy(x6): + // Dummy(x6); + // break; + // } + //} + + //void Test7(object val) + //{ + // switch (val) + // { + // case int x7 when Dummy(x7): + // Dummy(x7); + // break; + // } + + // let x7 = 11; + // Dummy(x7); + //} + + void Test8(object val) + { + switch (val) + { + case int x8 + when Dummy(x8, false is var x8, x8): + Dummy(x8); + break; + } + } + + void Test9(object val) + { + switch (val) + { + case 0 when Dummy(x9): + int x9 = 9; + Dummy(x9); + break; + case 2 when Dummy(x9 = 9): + Dummy(x9); + break; + case int x9 when Dummy(x9): + Dummy(x9); + break; + } + } + + //void Test10(object val) + //{ + // switch (val) + // { + // case int x10 when Dummy(x10): + // Dummy(x10); + // break; + // case 0 when Dummy(x10): + // let x10 = 10; + // Dummy(x10); + // break; + // case 2 when Dummy(x10 = 10, x10): + // Dummy(x10); + // break; + // } + //} + + void Test11(object val) + { + switch (x11 ? val : 0) + { + case 0 when Dummy(x11): + Dummy(x11, 0); + break; + case int x11 when Dummy(x11): + Dummy(x11, 1); + break; + } + } + + void Test12(object val) + { + switch (x12 ? val : 0) + { + case int x12 when Dummy(x12): + Dummy(x12, 0); + break; + case 1 when Dummy(x12): + Dummy(x12, 1); + break; + } + } + + void Test13() + { + switch (1 is var x13 ? x13 : 0) + { + case 0 when Dummy(x13): + Dummy(x13); + break; + case int x13 when Dummy(x13): + Dummy(x13); + break; + } + } + + void Test14(object val) + { + switch (val) + { + case int x14 when Dummy(x14): + Dummy(x14); + Dummy(true is var x14, x14); + Dummy(x14); + break; + } + } + + void Test15(object val) + { + switch (val) + { + case int x15 when Dummy(x15): + case long x15 when Dummy(x15): + Dummy(x15); + break; + } + } + + void Test16(object val) + { + switch (val) + { + case int x16 when Dummy(x16): + case 1 when Dummy(true is var x16, x16): + Dummy(x16); + break; + } + } + + void Test17(object val) + { + switch (val) + { + case 0 when Dummy(true is var x17, x17): + case int x17 when Dummy(x17): + Dummy(x17); + break; + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (30,31): error CS0841: Cannot use local variable 'x2' before it is declared + // case 0 when Dummy(x2): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(30, 31), + // (32,23): error CS0165: Use of unassigned local variable 'x2' + // Dummy(x2); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(32, 23), + // (41,22): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x3 when Dummy(x3): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(41, 22), + // (52,22): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x4 when Dummy(x4): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(52, 22), + // (65,22): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x5 when Dummy(x5): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(65, 22), + // (106,49): error CS0128: A local variable named 'x8' is already defined in this scope + // when Dummy(x8, false is var x8, x8): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(106, 49), + // (116,31): error CS0841: Cannot use local variable 'x9' before it is declared + // case 0 when Dummy(x9): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(116, 31), + // (123,22): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x9 when Dummy(x9): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(123, 22), + // (148,17): error CS0103: The name 'x11' does not exist in the current context + // switch (x11 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(148, 17), + // (150,31): error CS0103: The name 'x11' does not exist in the current context + // case 0 when Dummy(x11): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(150, 31), + // (151,23): error CS0103: The name 'x11' does not exist in the current context + // Dummy(x11, 0); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(151, 23), + // (161,17): error CS0103: The name 'x12' does not exist in the current context + // switch (x12 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(161, 17), + // (166,31): error CS0103: The name 'x12' does not exist in the current context + // case 1 when Dummy(x12): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(166, 31), + // (167,23): error CS0103: The name 'x12' does not exist in the current context + // Dummy(x12, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(167, 23), + // (179,22): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x13 when Dummy(x13): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(179, 22), + // (189,22): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case int x14 when Dummy(x14): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(189, 22), + // (202,23): error CS0128: A local variable named 'x15' is already defined in this scope + // case long x15 when Dummy(x15): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(202, 23), + // (202,38): error CS0165: Use of unassigned local variable 'x15' + // case long x15 when Dummy(x15): + Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(202, 38), + // (213,43): error CS0128: A local variable named 'x16' is already defined in this scope + // case 1 when Dummy(true is var x16, x16): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x16").WithArguments("x16").WithLocation(213, 43), + // (213,48): error CS0165: Use of unassigned local variable 'x16' + // case 1 when Dummy(true is var x16, x16): + Diagnostic(ErrorCode.ERR_UseDefViolation, "x16").WithArguments("x16").WithLocation(213, 48), + // (224,22): error CS0128: A local variable named 'x17' is already defined in this scope + // case int x17 when Dummy(x17): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x17").WithArguments("x17").WithLocation(224, 22), + // (224,37): error CS0165: Use of unassigned local variable 'x17' + // case int x17 when Dummy(x17): + Diagnostic(ErrorCode.ERR_UseDefViolation, "x17").WithArguments("x17").WithLocation(224, 37) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(6, x1Ref.Length); + for (int i = 0; i < x1Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); + } + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(4, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyNotAPatternLocal(model, x4Ref[3]); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); + VerifyNotAPatternLocal(model, x5Ref[2]); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(3, x8Ref.Length); + for (int i = 0; i < x8Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(6, x9Ref.Length); + VerifyNotAPatternLocal(model, x9Ref[0]); + VerifyNotAPatternLocal(model, x9Ref[1]); + VerifyNotAPatternLocal(model, x9Ref[2]); + VerifyNotAPatternLocal(model, x9Ref[3]); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(5, x11Ref.Length); + VerifyNotInScope(model, x11Ref[0]); + VerifyNotInScope(model, x11Ref[1]); + VerifyNotInScope(model, x11Ref[2]); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(5, x12Ref.Length); + VerifyNotInScope(model, x12Ref[0]); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); + VerifyNotInScope(model, x12Ref[3]); + VerifyNotInScope(model, x12Ref[4]); + + var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); + var x13Ref = GetReferences(tree, "x13").ToArray(); + Assert.Equal(2, x13Decl.Length); + Assert.Equal(5, x13Ref.Length); + VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); + VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(4, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPattern(model, x14Decl[1], true); + + var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(2, x15Decl.Length); + Assert.Equal(3, x15Ref.Length); + for (int i = 0; i < x15Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); + + var x16Decl = GetPatternDeclarations(tree, "x16").ToArray(); + var x16Ref = GetReferences(tree, "x16").ToArray(); + Assert.Equal(2, x16Decl.Length); + Assert.Equal(3, x16Ref.Length); + for (int i = 0; i < x16Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x16Decl[0], x16Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x16Decl[1]); + + var x17Decl = GetPatternDeclarations(tree, "x17").ToArray(); + var x17Ref = GetReferences(tree, "x17").ToArray(); + Assert.Equal(2, x17Decl.Length); + Assert.Equal(3, x17Ref.Length); + for (int i = 0; i < x17Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x17Decl[0], x17Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x17Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Switch_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + switch (1 is var x1 ? x1 : 0) + { + case 0: + Dummy(x1, 0); + break; + } + + Dummy(x1, 1); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + switch (4 is var x4 ? x4 : 0) + { + case 4: + Dummy(x4); + break; + } + } + + void Test5(int x5) + { + switch (5 is var x5 ? x5 : 0) + { + case 5: + Dummy(x5); + break; + } + } + + void Test6() + { + switch (x6 + 6 is var x6 ? x6 : 0) + { + case 6: + Dummy(x6); + break; + } + } + + void Test7() + { + switch (7 is var x7 ? x7 : 0) + { + case 7: + var x7 = 12; + Dummy(x7); + break; + } + } + + void Test9() + { + switch (9 is var x9 ? x9 : 0) + { + case 9: + Dummy(x9, 0); + switch (9 is var x9 ? x9 : 0) + { + case 9: + Dummy(x9, 1); + break; + } + break; + } + + } + + void Test10() + { + switch (y10 + 10 is var x10 ? x10 : 0) + { + case 0 when y10: + break; + case y10: + var y10 = 12; + Dummy(y10); + break; + } + } + + //void Test11() + //{ + // switch (y11 + 11 is var x11 ? x11 : 0) + // { + // case 0 when y11 > 0: + // break; + // case y11: + // let y11 = 12; + // Dummy(y11); + // break; + // } + //} + + void Test14() + { + switch (Dummy(1 is var x14, + 2 is var x14, + x14) ? 1 : 0) + { + case 0: + Dummy(x14); + break; + } + } + + void Test15(int val) + { + switch (val) + { + case 0 when y15 > 0: + break; + case y15: + var y15 = 15; + Dummy(y15); + break; + } + } + + //void Test16(int val) + //{ + // switch (val) + // { + // case 0 when y16 > 0: + // break; + // case y16: + // let y16 = 16; + // Dummy(y16); + // break; + // } + //} +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (27,26): error CS0128: A local variable named 'x4' is already defined in this scope + // switch (4 is var x4 ? x4 : 0) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(27, 26), + // (37,26): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // switch (5 is var x5 ? x5 : 0) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(37, 26), + // (47,17): error CS0841: Cannot use local variable 'x6' before it is declared + // switch (x6 + 6 is var x6 ? x6 : 0) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(47, 17), + // (60,21): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(60, 21), + // (71,23): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9, 0); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(71, 23), + // (72,34): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // switch (9 is var x9 ? x9 : 0) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(72, 34), + // (85,17): error CS0103: The name 'y10' does not exist in the current context + // switch (y10 + 10 is var x10 ? x10 : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 17), + // (87,25): error CS0841: Cannot use local variable 'y10' before it is declared + // case 0 when y10: + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y10").WithArguments("y10").WithLocation(87, 25), + // (89,18): error CS0841: Cannot use local variable 'y10' before it is declared + // case y10: + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y10").WithArguments("y10").WithLocation(89, 18), + // (89,18): error CS0150: A constant value is expected + // case y10: + Diagnostic(ErrorCode.ERR_ConstantExpected, "y10").WithLocation(89, 18), + // (112,28): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(112, 28), + // (125,25): error CS0841: Cannot use local variable 'y15' before it is declared + // case 0 when y15 > 0: + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y15").WithArguments("y15").WithLocation(125, 25), + // (127,18): error CS0841: Cannot use local variable 'y15' before it is declared + // case y15: + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "y15").WithArguments("y15").WithLocation(127, 18) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(3, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(4, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + VerifyNotAPatternLocal(model, y10Ref[2]); + VerifyNotAPatternLocal(model, y10Ref[3]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + + var y15Ref = GetReferences(tree, "y15").ToArray(); + Assert.Equal(3, y15Ref.Length); + VerifyNotAPatternLocal(model, y15Ref[0]); + VerifyNotAPatternLocal(model, y15Ref[1]); + VerifyNotAPatternLocal(model, y15Ref[2]); + } + + [Fact] + public void ScopeOfPatternVariables_Switch_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + if (true) + switch (1 is var x1 ? 1 : 0) + { + case 0: + break; + } + + Dummy(x1, 1); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (19,15): error CS0103: The name 'x1' does not exist in the current context + // Dummy(x1, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(19, 15) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Switch_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (SwitchStatementSyntax)SyntaxFactory.ParseStatement(@" +switch (Dummy(11 is var x1, x1)) {} +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_Using_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.IDisposable Dummy(params object[] x) {return null;} + + void Test1() + { + using (Dummy(true is var x1, x1)) + { + Dummy(x1); + } + } + + void Test2() + { + using (Dummy(true is var x2, x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + using (Dummy(true is var x4, x4)) + Dummy(x4); + } + + void Test6() + { + using (Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + using (Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + using (Dummy(true is var x8, x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + using (Dummy(true is var x9, x9)) + { + Dummy(x9); + using (Dummy(true is var x9, x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + using (Dummy(y10 is var x10, x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // using (Dummy(y11 is var x11, x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + using (Dummy(y12 is var x12, x12)) + var y12 = 12; + } + + //void Test13() + //{ + // using (Dummy(y13 is var x13, x13)) + // let y13 = 12; + //} + + void Test14() + { + using (Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,34): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (Dummy(true is var x4, x4)) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 34), + // (35,22): error CS0841: Cannot use local variable 'x6' before it is declared + // using (Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 22), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (53,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), + // (61,38): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (Dummy(true is var x9, x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 38), + // (68,22): error CS0103: The name 'y10' does not exist in the current context + // using (Dummy(y10 is var x10, x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 22), + // (86,22): error CS0103: The name 'y12' does not exist in the current context + // using (Dummy(y12 is var x12, x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 22), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,31): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 31) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Using_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.IDisposable Dummy(params object[] x) {return null;} + + void Test1() + { + using (var d = Dummy(true is var x1, x1)) + { + Dummy(x1); + } + } + + void Test2() + { + using (var d = Dummy(true is var x2, x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + using (var d = Dummy(true is var x4, x4)) + Dummy(x4); + } + + void Test6() + { + using (var d = Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + using (var d = Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + using (var d = Dummy(true is var x8, x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + using (var d = Dummy(true is var x9, x9)) + { + Dummy(x9); + using (var e = Dummy(true is var x9, x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + using (var d = Dummy(y10 is var x10, x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // using (var d = Dummy(y11 is var x11, x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + using (var d = Dummy(y12 is var x12, x12)) + var y12 = 12; + } + + //void Test13() + //{ + // using (var d = Dummy(y13 is var x13, x13)) + // let y13 = 12; + //} + + void Test14() + { + using (var d = Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,42): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (var d = Dummy(true is var x4, x4)) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 42), + // (35,30): error CS0841: Cannot use local variable 'x6' before it is declared + // using (var d = Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 30), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (53,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), + // (61,46): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (var e = Dummy(true is var x9, x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 46), + // (68,30): error CS0103: The name 'y10' does not exist in the current context + // using (var d = Dummy(y10 is var x10, x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 30), + // (86,30): error CS0103: The name 'y12' does not exist in the current context + // using (var d = Dummy(y12 is var x12, x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 30), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,39): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 39) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Using_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.IDisposable Dummy(params object[] x) {return null;} + + void Test1() + { + using (System.IDisposable d = Dummy(true is var x1, x1)) + { + Dummy(x1); + } + } + + void Test2() + { + using (System.IDisposable d = Dummy(true is var x2, x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + using (System.IDisposable d = Dummy(true is var x4, x4)) + Dummy(x4); + } + + void Test6() + { + using (System.IDisposable d = Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + using (System.IDisposable d = Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + using (System.IDisposable d = Dummy(true is var x8, x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + using (System.IDisposable d = Dummy(true is var x9, x9)) + { + Dummy(x9); + using (System.IDisposable c = Dummy(true is var x9, x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + using (System.IDisposable d = Dummy(y10 is var x10, x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // using (System.IDisposable d = Dummy(y11 is var x11, x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + using (System.IDisposable d = Dummy(y12 is var x12, x12)) + var y12 = 12; + } + + //void Test13() + //{ + // using (System.IDisposable d = Dummy(y13 is var x13, x13)) + // let y13 = 12; + //} + + void Test14() + { + using (System.IDisposable d = Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,57): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (System.IDisposable d = Dummy(true is var x4, x4)) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 57), + // (35,45): error CS0841: Cannot use local variable 'x6' before it is declared + // using (System.IDisposable d = Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 45), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (53,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), + // (61,61): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // using (System.IDisposable c = Dummy(true is var x9, x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 61), + // (68,45): error CS0103: The name 'y10' does not exist in the current context + // using (System.IDisposable d = Dummy(y10 is var x10, x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 45), + // (86,45): error CS0103: The name 'y12' does not exist in the current context + // using (System.IDisposable d = Dummy(y12 is var x12, x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 45), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,54): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 54) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var x10Decl = GetPatternDeclarations(tree, "x10").Single(); + var x10Ref = GetReferences(tree, "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Using_04() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.IDisposable Dummy(params object[] x) {return null;} + + void Test1() + { + using (var x1 = Dummy(true is var x1, x1)) + { + Dummy(x1); + } + } + + void Test2() + { + using (System.IDisposable x2 = Dummy(true is var x2, x2)) + { + Dummy(x2); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (12,43): error CS0128: A local variable named 'x1' is already defined in this scope + // using (var x1 = Dummy(true is var x1, x1)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(12, 43), + // (12,47): error CS0841: Cannot use local variable 'x1' before it is declared + // using (var x1 = Dummy(true is var x1, x1)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(12, 47), + // (12,47): error CS0165: Use of unassigned local variable 'x1' + // using (var x1 = Dummy(true is var x1, x1)) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(12, 47), + // (20,58): error CS0128: A local variable named 'x2' is already defined in this scope + // using (System.IDisposable x2 = Dummy(true is var x2, x2)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 58), + // (20,62): error CS0165: Use of unassigned local variable 'x2' + // using (System.IDisposable x2 = Dummy(true is var x2, x2)) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 62) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + VerifyNotAPatternLocal(model, x1Ref[0]); + VerifyNotAPatternLocal(model, x1Ref[1]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); + VerifyNotAPatternLocal(model, x2Ref[0]); + VerifyNotAPatternLocal(model, x2Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Using_05() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.IDisposable Dummy(params object[] x) {return null;} + + void Test1() + { + using (System.IDisposable d = Dummy(true is var x1, x1), + x1 = Dummy(x1)) + { + Dummy(x1); + } + } + + void Test2() + { + using (System.IDisposable d1 = Dummy(true is var x2, x2), + d2 = Dummy(true is var x2, x2)) + { + Dummy(x2); + } + } + + void Test3() + { + using (System.IDisposable d1 = Dummy(true is var x3, x3), + d2 = Dummy(x3)) + { + Dummy(x3); + } + } + + void Test4() + { + using (System.IDisposable d1 = Dummy(x4), + d2 = Dummy(true is var x4, x4)) + { + Dummy(x4); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (13,35): error CS0128: A local variable named 'x1' is already defined in this scope + // x1 = Dummy(x1)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 35), + // (22,58): error CS0128: A local variable named 'x2' is already defined in this scope + // d2 = Dummy(true is var x2, x2)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(22, 58), + // (39,46): error CS0841: Cannot use local variable 'x4' before it is declared + // using (System.IDisposable d1 = Dummy(x4), + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(39, 46) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").ToArray(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Decl.Length); + Assert.Equal(3, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(3, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + var d = Dummy(true is var x1, x1); + } + void Test4() + { + var x4 = 11; + Dummy(x4); + + var d = Dummy(true is var x4, x4); + } + + void Test6() + { + var d = Dummy(x6 && true is var x6); + } + + void Test8() + { + var d = Dummy(true is var x8, x8); + System.Console.WriteLine(x8); + } + + void Test14() + { + var d = Dummy(1 is var x14, + 2 is var x14, + x14); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (19,35): error CS0128: A local variable named 'x4' is already defined in this scope + // var d = Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 35), + // (24,23): error CS0841: Cannot use local variable 'x6' before it is declared + // var d = Dummy(x6 && true is var x6); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 23), + // (36,32): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 32) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").Single(); + Assert.Equal(2, x14Decl.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + object d = Dummy(true is var x1, x1); + } + void Test4() + { + var x4 = 11; + Dummy(x4); + + object d = Dummy(true is var x4, x4); + } + + void Test6() + { + object d = Dummy(x6 && true is var x6); + } + + void Test8() + { + object d = Dummy(true is var x8, x8); + System.Console.WriteLine(x8); + } + + void Test14() + { + object d = Dummy(1 is var x14, + 2 is var x14, + x14); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (19,38): error CS0128: A local variable named 'x4' is already defined in this scope + // object d = Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 38), + // (24,26): error CS0841: Cannot use local variable 'x6' before it is declared + // object d = Dummy(x6 && true is var x6); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 26), + // (36,35): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 35) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").Single(); + Assert.Equal(2, x14Decl.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + var x1 = + Dummy(true is var x1, x1); + Dummy(x1); + } + + void Test2() + { + object x2 = + Dummy(true is var x2, x2); + Dummy(x2); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (13,36): error CS0128: A local variable named 'x1' is already defined in this scope + // Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 36), + // (13,40): error CS0841: Cannot use local variable 'x1' before it is declared + // Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 40), + // (13,40): error CS0165: Use of unassigned local variable 'x1' + // Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 40), + // (20,39): error CS0128: A local variable named 'x2' is already defined in this scope + // Dummy(true is var x2, x2); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 39), + // (20,43): error CS0165: Use of unassigned local variable 'x2' + // Dummy(true is var x2, x2); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 43) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyNotAPatternLocal(model, x1Ref[0]); + VerifyNotAPatternLocal(model, x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyNotAPatternLocal(model, x2Ref[0]); + VerifyNotAPatternLocal(model, x2Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_04() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + object d = Dummy(true is var x1, x1), + x1 = Dummy(x1); + Dummy(x1); + } + + void Test2() + { + object d1 = Dummy(true is var x2, x2), + d2 = Dummy(true is var x2, x2); + } + + void Test3() + { + object d1 = Dummy(true is var x3, x3), + d2 = Dummy(x3); + } + + void Test4() + { + object d1 = Dummy(x4), + d2 = Dummy(true is var x4, x4); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (13,16): error CS0128: A local variable named 'x1' is already defined in this scope + // x1 = Dummy(x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 16), + // (20,39): error CS0128: A local variable named 'x2' is already defined in this scope + // d2 = Dummy(true is var x2, x2); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 39), + // (31,27): error CS0841: Cannot use local variable 'x4' before it is declared + // object d1 = Dummy(x4), + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(31, 27) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").ToArray(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Decl.Length); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_05() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + long Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (LocalDeclarationStatementSyntax)SyntaxFactory.ParseStatement(@" +var y1 = Dummy(11 is var x1, x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + + Assert.Equal("System.Int64 y1", model.LookupSymbols(x1Ref[0].SpanStart, name: "y1").Single().ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_LocalDeclarationStmt_06() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Test1() + { + if (true) + var d = true is var x1; + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (11,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var d = true is var x1; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var d = true is var x1;").WithLocation(11, 13), + // (13,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(13, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + + var d = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "d").Single(); + Assert.Equal("System.Boolean d", model.GetDeclaredSymbol(d).ToTestDisplayString()); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + var (d, dd) = ((true is var x1), x1); + } + void Test4() + { + var x4 = 11; + Dummy(x4); + + var (d, dd) = ((true is var x4), x4); + } + + void Test6() + { + var (d, dd) = (x6 && (true is var x6), 1); + } + + void Test8() + { + var (d, dd) = ((true is var x8), x8); + System.Console.WriteLine(x8); + } + + void Test14() + { + var (d, dd, ddd) = ((1 is var x14), + (2 is var x14), + x14); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (19,37): error CS0128: A local variable named 'x4' is already defined in this scope + // var (d, dd) = ((true is var x4), x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 37), + // (24,24): error CS0841: Cannot use local variable 'x6' before it is declared + // var (d, dd) = (x6 && (true is var x6), 1); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 24), + // (36,33): error CS0128: A local variable named 'x14' is already defined in this scope + // (2 is var x14), + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); + var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x6").Single(); + var x6Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").Single(); + var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x14Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x14").ToArray(); + var x14Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x14").Single(); + Assert.Equal(2, x14Decl.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + (object d, object dd) = ((true is var x1), x1); + } + void Test4() + { + var x4 = 11; + Dummy(x4); + + (object d, object dd) = ((true is var x4), x4); + } + + void Test6() + { + (object d, object dd) = (x6 && (true is var x6), 1); + } + + void Test8() + { + (object d, object dd) = ((true is var x8), x8); + System.Console.WriteLine(x8); + } + + void Test14() + { + (object d, object dd, object ddd) = ((1 is var x14), + (2 is var x14), + x14); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (19,47): error CS0128: A local variable named 'x4' is already defined in this scope + // (object d, object dd) = ((true is var x4), x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(19, 47), + // (24,34): error CS0841: Cannot use local variable 'x6' before it is declared + // (object d, object dd) = (x6 && (true is var x6), 1); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(24, 34), + // (36,33): error CS0128: A local variable named 'x14' is already defined in this scope + // (2 is var x14), + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(36, 33) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); + var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x6").Single(); + var x6Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x6").Single(); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").Single(); + var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); + Assert.Equal(2, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x14Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x14").ToArray(); + var x14Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x14").Single(); + Assert.Equal(2, x14Decl.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + var (x1, dd) = + ((true is var x1), x1); + Dummy(x1); + } + + void Test2() + { + (object x2, object dd) = + ((true is var x2), x2); + Dummy(x2); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (13,37): error CS0128: A local variable named 'x1' is already defined in this scope + // ((true is var x1), x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 37), + // (13,42): error CS0841: Cannot use local variable 'x1' before it is declared + // ((true is var x1), x1); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 42), + // (13,42): error CS0165: Use of unassigned local variable 'x1' + // ((true is var x1), x1); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 42), + // (20,40): error CS0128: A local variable named 'x2' is already defined in this scope + // ((true is var x2), x2); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 40), + // (20,45): error CS0165: Use of unassigned local variable 'x2' + // ((true is var x2), x2); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(20, 45) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyNotAPatternLocal(model, x1Ref[0]); + VerifyNotAPatternLocal(model, x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + + var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").Single(); + var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyNotAPatternLocal(model, x2Ref[0]); + VerifyNotAPatternLocal(model, x2Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_04() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + (object d, object x1) = (Dummy((true is var x1), x1), + Dummy(x1)); + Dummy(x1); + } + + void Test2() + { + (object d1, object d2) = (Dummy((true is var x2), x2), + Dummy((true is var x2), x2)); + } + + void Test3() + { + (object d1, object d2) = (Dummy((true is var x3), x3), + Dummy(x3)); + } + + void Test4() + { + (object d1, object d2) = (Dummy(x4), + Dummy((true is var x4), x4)); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (12,53): error CS0128: A local variable named 'x1' is already defined in this scope + // (object d, object x1) = (Dummy((true is var x1), x1), + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(12, 53), + // (12,58): error CS0165: Use of unassigned local variable 'x1' + // (object d, object x1) = (Dummy((true is var x1), x1), + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(12, 58), + // (20,40): error CS0128: A local variable named 'x2' is already defined in this scope + // Dummy((true is var x2), x2)); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(20, 40), + // (31,41): error CS0841: Cannot use local variable 'x4' before it is declared + // (object d1, object d2) = (Dummy(x4), + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(31, 41) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(3, x1Ref.Length); + VerifyNotAPatternLocal(model, x1Ref[0]); + VerifyNotAPatternLocal(model, x1Ref[1]); + VerifyNotAPatternLocal(model, x1Ref[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + + var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").ToArray(); + var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").ToArray(); + Assert.Equal(2, x2Decl.Length); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl[0], x2Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl[1]); + + var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").Single(); + var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); + var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_05() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (DeconstructionDeclarationStatementSyntax)SyntaxFactory.ParseStatement(@" +var (y1, dd) = ((123 is var x1), x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel(tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + + Assert.Equal("System.Boolean y1", model.LookupSymbols(x1Ref[0].SpanStart, name: "y1").Single().ToTestDisplayString()); + } + + [Fact] + [CompilerTrait(CompilerFeature.Tuples)] + public void ScopeOfPatternVariables_DeconstructionDeclarationStmt_06() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Test1() + { + if (true) + var (d, dd) = ((true is var x1), x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, references: new[] { ValueTupleRef, SystemRuntimeFacadeRef }, + options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (11,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var (d, dd) = ((true is var x1), x1); + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var (d, dd) = ((true is var x1), x1);").WithLocation(11, 13), + // (13,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(13, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref[0]); + VerifyNotInScope(model, x1Ref[1]); + + var d = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "d").Single(); + Assert.Equal("System.Boolean d", model.GetDeclaredSymbol(d).ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_While_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + while (true is var x1 && x1) + { + Dummy(x1); + } + } + + void Test2() + { + while (true is var x2 && x2) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + while (true is var x4 && x4 > 0) + Dummy(x4); + } + + void Test6() + { + while (x6 && true is var x6) + Dummy(x6); + } + + void Test7() + { + while (true is var x7 && x7) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + while (true is var x8 && x8) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + while (true is var x9 && x9) + { + Dummy(x9); + while (true is var x9 && x9) // 2 + Dummy(x9); + } + } + + void Test10() + { + while (y10 is var x10) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // while (y11 is var x11) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + while (y12 is var x12) + var y12 = 12; + } + + //void Test13() + //{ + // while (y13 is var x13) + // let y13 = 12; + //} + + void Test14() + { + while (Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,28): error CS0128: A local variable named 'x4' is already defined in this scope + // while (true is var x4 && x4 > 0) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(29, 28), + // (35,16): error CS0841: Cannot use local variable 'x6' before it is declared + // while (x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 16), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (60,19): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(60, 19), + // (61,32): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // while (true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 32), + // (68,16): error CS0103: The name 'y10' does not exist in the current context + // while (y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 16), + // (86,16): error CS0103: The name 'y12' does not exist in the current context + // while (y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 16), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,31): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 31) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_While_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + if (true) + while (true is var x1) + { + } + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (17,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_While_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (WhileStatementSyntax)SyntaxFactory.ParseStatement(@" +while (Dummy(11 is var x1, x1)) ; +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_Do_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + do + { + Dummy(x1); + } + while (true is var x1 && x1); + } + + void Test2() + { + do + Dummy(x2); + while (true is var x2 && x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + do + Dummy(x4); + while (true is var x4 && x4 > 0); + } + + void Test6() + { + do + Dummy(x6); + while (x6 && true is var x6); + } + + void Test7() + { + do + { + var x7 = 12; + Dummy(x7); + } + while (true is var x7 && x7); + } + + void Test8() + { + do + Dummy(x8); + while (true is var x8 && x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + do + { + Dummy(x9); + do + Dummy(x9); + while (true is var x9 && x9); // 2 + } + while (true is var x9 && x9); + } + + void Test10() + { + do + { + var y10 = 12; + Dummy(y10); + } + while (y10 is var x10); + } + + //void Test11() + //{ + // do + // { + // let y11 = 12; + // Dummy(y11); + // } + // while (y11 is var x11); + //} + + void Test12() + { + do + var y12 = 12; + while (y12 is var x12); + } + + //void Test13() + //{ + // do + // let y13 = 12; + // while (y13 is var x13); + //} + + void Test14() + { + do + { + Dummy(x14); + } + while (Dummy(1 is var x14, + 2 is var x14, + x14)); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (97,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(97, 13), + // (14,19): error CS0841: Cannot use local variable 'x1' before it is declared + // Dummy(x1); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(14, 19), + // (22,19): error CS0841: Cannot use local variable 'x2' before it is declared + // Dummy(x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(22, 19), + // (33,28): error CS0128: A local variable named 'x4' is already defined in this scope + // while (true is var x4 && x4 > 0); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 28), + // (40,16): error CS0841: Cannot use local variable 'x6' before it is declared + // while (x6 && true is var x6); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(40, 16), + // (39,19): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(39, 19), + // (47,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(47, 17), + // (56,19): error CS0841: Cannot use local variable 'x8' before it is declared + // Dummy(x8); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x8").WithArguments("x8").WithLocation(56, 19), + // (66,19): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(66, 19), + // (69,32): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // while (true is var x9 && x9); // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(69, 32), + // (68,23): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(68, 23), + // (81,16): error CS0103: The name 'y10' does not exist in the current context + // while (y10 is var x10); + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(81, 16), + // (98,16): error CS0103: The name 'y12' does not exist in the current context + // while (y12 is var x12); + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(98, 16), + // (97,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(97, 17), + // (115,31): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(115, 31), + // (112,19): error CS0841: Cannot use local variable 'x14' before it is declared + // Dummy(x14); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(112, 19) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[1]); + VerifyNotAPatternLocal(model, x7Ref[0]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1], x9Ref[2]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[1]); + VerifyNotAPatternLocal(model, y10Ref[0]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Do_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + if (true) + do + { + } + while (true is var x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (18,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(18, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Do_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (DoStatementSyntax)SyntaxFactory.ParseStatement(@" +do {} while (Dummy(11 is var x1, x1)); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_For_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for ( + Dummy(true is var x1 && x1) + ;;) + { + Dummy(x1); + } + } + + void Test2() + { + for ( + Dummy(true is var x2 && x2) + ;;) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + for ( + Dummy(true is var x4 && x4) + ;;) + Dummy(x4); + } + + void Test6() + { + for ( + Dummy(x6 && true is var x6) + ;;) + Dummy(x6); + } + + void Test7() + { + for ( + Dummy(true is var x7 && x7) + ;;) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + for ( + Dummy(true is var x8 && x8) + ;;) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + for ( + Dummy(true is var x9 && x9) + ;;) + { + Dummy(x9); + for ( + Dummy(true is var x9 && x9) // 2 + ;;) + Dummy(x9); + } + } + + void Test10() + { + for ( + Dummy(y10 is var x10) + ;;) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // for ( + // Dummy(y11 is var x11) + // ;;) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + for ( + Dummy(y12 is var x12) + ;;) + var y12 = 12; + } + + //void Test13() + //{ + // for ( + // Dummy(y13 is var x13) + // ;;) + // let y13 = 12; + //} + + void Test14() + { + for ( + Dummy(1 is var x14, + 2 is var x14, + x14) + ;;) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), + // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), + // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), + // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), + // (65,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), + // (65,9): warning CS0162: Unreachable code detected + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), + // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), + // (85,20): error CS0103: The name 'y10' does not exist in the current context + // Dummy(y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), + // (107,20): error CS0103: The name 'y12' does not exist in the current context + // Dummy(y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), + // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), + // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_For_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (; + Dummy(true is var x1 && x1) + ;) + { + Dummy(x1); + } + } + + void Test2() + { + for (; + Dummy(true is var x2 && x2) + ;) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + for (; + Dummy(true is var x4 && x4) + ;) + Dummy(x4); + } + + void Test6() + { + for (; + Dummy(x6 && true is var x6) + ;) + Dummy(x6); + } + + void Test7() + { + for (; + Dummy(true is var x7 && x7) + ;) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + for (; + Dummy(true is var x8 && x8) + ;) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + for (; + Dummy(true is var x9 && x9) + ;) + { + Dummy(x9); + for (; + Dummy(true is var x9 && x9) // 2 + ;) + Dummy(x9); + } + } + + void Test10() + { + for (; + Dummy(y10 is var x10) + ;) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // for (; + // Dummy(y11 is var x11) + // ;) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + for (; + Dummy(y12 is var x12) + ;) + var y12 = 12; + } + + //void Test13() + //{ + // for (; + // Dummy(y13 is var x13) + // ;) + // let y13 = 12; + //} + + void Test14() + { + for (; + Dummy(1 is var x14, + 2 is var x14, + x14) + ;) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), + // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), + // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), + // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), + // (65,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), + // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), + // (85,20): error CS0103: The name 'y10' does not exist in the current context + // Dummy(y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), + // (107,20): error CS0103: The name 'y12' does not exist in the current context + // Dummy(y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), + // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), + // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_For_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (;; + Dummy(true is var x1 && x1) + ) + { + Dummy(x1); + } + } + + void Test2() + { + for (;; + Dummy(true is var x2 && x2) + ) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + for (;; + Dummy(true is var x4 && x4) + ) + Dummy(x4); + } + + void Test6() + { + for (;; + Dummy(x6 && true is var x6) + ) + Dummy(x6); + } + + void Test7() + { + for (;; + Dummy(true is var x7 && x7) + ) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + for (;; + Dummy(true is var x8 && x8) + ) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + for (;; + Dummy(true is var x9 && x9) + ) + { + Dummy(x9); + for (;; + Dummy(true is var x9 && x9) // 2 + ) + Dummy(x9); + } + } + + void Test10() + { + for (;; + Dummy(y10 is var x10) + ) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // for (;; + // Dummy(y11 is var x11) + // ) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + for (;; + Dummy(y12 is var x12) + ) + var y12 = 12; + } + + //void Test13() + //{ + // for (;; + // Dummy(y13 is var x13) + // ) + // let y13 = 12; + //} + + void Test14() + { + for (;; + Dummy(1 is var x14, + 2 is var x14, + x14) + ) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), + // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), + // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), + // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), + // (65,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), + // (65,9): warning CS0162: Unreachable code detected + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), + // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), + // (85,20): error CS0103: The name 'y10' does not exist in the current context + // Dummy(y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), + // (107,20): error CS0103: The name 'y12' does not exist in the current context + // Dummy(y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), + // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), + // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29), + // (16,19): error CS0165: Use of unassigned local variable 'x1' + // Dummy(x1); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(16, 19), + // (25,19): error CS0165: Use of unassigned local variable 'x2' + // Dummy(x2); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x2").WithArguments("x2").WithLocation(25, 19), + // (36,19): error CS0165: Use of unassigned local variable 'x4' + // Dummy(x4); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(36, 19), + // (44,19): error CS0165: Use of unassigned local variable 'x6' + // Dummy(x6); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x6").WithArguments("x6").WithLocation(44, 19), + // (63,19): error CS0165: Use of unassigned local variable 'x8' + // Dummy(x8); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x8").WithArguments("x8").WithLocation(63, 19), + // (71,14): warning CS0162: Unreachable code detected + // Dummy(true is var x9 && x9) + Diagnostic(ErrorCode.WRN_UnreachableCode, "Dummy").WithLocation(71, 14), + // (74,19): error CS0165: Use of unassigned local variable 'x9' + // Dummy(x9); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x9").WithArguments("x9").WithLocation(74, 19), + // (78,23): error CS0165: Use of unassigned local variable 'x9' + // Dummy(x9); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x9").WithArguments("x9").WithLocation(78, 23), + // (128,19): error CS0165: Use of unassigned local variable 'x14' + // Dummy(x14); + Diagnostic(ErrorCode.ERR_UseDefViolation, "x14").WithArguments("x14").WithLocation(128, 19) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_For_04() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (var b = + Dummy(true is var x1 && x1) + ;;) + { + Dummy(x1); + } + } + + void Test2() + { + for (var b = + Dummy(true is var x2 && x2) + ;;) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + for (var b = + Dummy(true is var x4 && x4) + ;;) + Dummy(x4); + } + + void Test6() + { + for (var b = + Dummy(x6 && true is var x6) + ;;) + Dummy(x6); + } + + void Test7() + { + for (var b = + Dummy(true is var x7 && x7) + ;;) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + for (var b = + Dummy(true is var x8 && x8) + ;;) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + for (var b1 = + Dummy(true is var x9 && x9) + ;;) + { + Dummy(x9); + for (var b2 = + Dummy(true is var x9 && x9) // 2 + ;;) + Dummy(x9); + } + } + + void Test10() + { + for (var b = + Dummy(y10 is var x10) + ;;) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // for (var b = + // Dummy(y11 is var x11) + // ;;) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + for (var b = + Dummy(y12 is var x12) + ;;) + var y12 = 12; + } + + //void Test13() + //{ + // for (var b = + // Dummy(y13 is var x13) + // ;;) + // let y13 = 12; + //} + + void Test14() + { + for (var b = + Dummy(1 is var x14, + 2 is var x14, + x14) + ;;) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), + // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), + // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), + // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), + // (65,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), + // (65,9): warning CS0162: Unreachable code detected + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), + // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), + // (85,20): error CS0103: The name 'y10' does not exist in the current context + // Dummy(y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), + // (107,20): error CS0103: The name 'y12' does not exist in the current context + // Dummy(y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), + // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), + // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_For_05() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (bool b = + Dummy(true is var x1 && x1) + ;;) + { + Dummy(x1); + } + } + + void Test2() + { + for (bool b = + Dummy(true is var x2 && x2) + ;;) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + for (bool b = + Dummy(true is var x4 && x4) + ;;) + Dummy(x4); + } + + void Test6() + { + for (bool b = + Dummy(x6 && true is var x6) + ;;) + Dummy(x6); + } + + void Test7() + { + for (bool b = + Dummy(true is var x7 && x7) + ;;) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + for (bool b = + Dummy(true is var x8 && x8) + ;;) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + for (bool b1 = + Dummy(true is var x9 && x9) + ;;) + { + Dummy(x9); + for (bool b2 = + Dummy(true is var x9 && x9) // 2 + ;;) + Dummy(x9); + } + } + + void Test10() + { + for (bool b = + Dummy(y10 is var x10) + ;;) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // for (bool b = + // Dummy(y11 is var x11) + // ;;) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + for (bool b = + Dummy(y12 is var x12) + ;;) + var y12 = 12; + } + + //void Test13() + //{ + // for (bool b = + // Dummy(y13 is var x13) + // ;;) + // let y13 = 12; + //} + + void Test14() + { + for (bool b = + Dummy(1 is var x14, + 2 is var x14, + x14) + ;;) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (109,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(109, 13), + // (34,32): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(34, 32), + // (42,20): error CS0841: Cannot use local variable 'x6' before it is declared + // Dummy(x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(42, 20), + // (53,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(53, 17), + // (65,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(65, 34), + // (65,9): warning CS0162: Unreachable code detected + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.WRN_UnreachableCode, "System").WithLocation(65, 9), + // (76,36): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // Dummy(true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(76, 36), + // (85,20): error CS0103: The name 'y10' does not exist in the current context + // Dummy(y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(85, 20), + // (107,20): error CS0103: The name 'y12' does not exist in the current context + // Dummy(y12 is var x12) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(107, 20), + // (109,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(109, 17), + // (124,29): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(124, 29) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_For_06() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (var x1 = + Dummy(true is var x1 && x1) + ;;) + {} + } + + void Test2() + { + for (var x2 = true; + Dummy(true is var x2 && x2) + ;) + {} + } + + void Test3() + { + for (var x3 = true;; + Dummy(true is var x3 && x3) + ) + {} + } + + void Test4() + { + for (bool x4 = + Dummy(true is var x4 && x4) + ;;) + {} + } + + void Test5() + { + for (bool x5 = true; + Dummy(true is var x5 && x5) + ;) + {} + } + + void Test6() + { + for (bool x6 = true;; + Dummy(true is var x6 && x6) + ) + {} + } + + void Test7() + { + for (bool x7 = true, b = + Dummy(true is var x7 && x7) + ;;) + {} + } + + void Test8() + { + for (bool b1 = Dummy(true is var x8 && x8), + b2 = Dummy(true is var x8 && x8); + Dummy(true is var x8 && x8); + Dummy(true is var x8 && x8)) + {} + } + + void Test9() + { + for (bool b = x9, + b2 = Dummy(true is var x9 && x9); + Dummy(true is var x9 && x9); + Dummy(true is var x9 && x9)) + {} + } + + void Test10() + { + for (var b = x10; + Dummy(true is var x10 && x10) && + Dummy(true is var x10 && x10); + Dummy(true is var x10 && x10)) + {} + } + + void Test11() + { + for (bool b = x11; + Dummy(true is var x11 && x11) && + Dummy(true is var x11 && x11); + Dummy(true is var x11 && x11)) + {} + } + + void Test12() + { + for (Dummy(x12); + Dummy(x12) && + Dummy(true is var x12 && x12); + Dummy(true is var x12 && x12)) + {} + } + + void Test13() + { + for (var b = x13; + Dummy(x13); + Dummy(true is var x13 && x13), + Dummy(true is var x13 && x13)) + {} + } + + void Test14() + { + for (bool b = x14; + Dummy(x14); + Dummy(true is var x14 && x14), + Dummy(true is var x14 && x14)) + {} + } + + void Test15() + { + for (Dummy(x15); + Dummy(x15); + Dummy(x15), + Dummy(true is var x15 && x15)) + {} + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (13,32): error CS0128: A local variable named 'x1' is already defined in this scope + // Dummy(true is var x1 && x1) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(13, 32), + // (13,38): error CS0841: Cannot use local variable 'x1' before it is declared + // Dummy(true is var x1 && x1) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(13, 38), + // (13,38): error CS0165: Use of unassigned local variable 'x1' + // Dummy(true is var x1 && x1) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(13, 38), + // (21,32): error CS0128: A local variable named 'x2' is already defined in this scope + // Dummy(true is var x2 && x2) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(21, 32), + // (29,32): error CS0128: A local variable named 'x3' is already defined in this scope + // Dummy(true is var x3 && x3) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(29, 32), + // (37,32): error CS0128: A local variable named 'x4' is already defined in this scope + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(37, 32), + // (37,38): error CS0165: Use of unassigned local variable 'x4' + // Dummy(true is var x4 && x4) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(37, 38), + // (45,32): error CS0128: A local variable named 'x5' is already defined in this scope + // Dummy(true is var x5 && x5) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(45, 32), + // (53,32): error CS0128: A local variable named 'x6' is already defined in this scope + // Dummy(true is var x6 && x6) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(53, 32), + // (61,32): error CS0128: A local variable named 'x7' is already defined in this scope + // Dummy(true is var x7 && x7) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x7").WithArguments("x7").WithLocation(61, 32), + // (69,37): error CS0128: A local variable named 'x8' is already defined in this scope + // b2 = Dummy(true is var x8 && x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(69, 37), + // (70,32): error CS0128: A local variable named 'x8' is already defined in this scope + // Dummy(true is var x8 && x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(70, 32), + // (71,32): error CS0128: A local variable named 'x8' is already defined in this scope + // Dummy(true is var x8 && x8)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(71, 32), + // (77,23): error CS0841: Cannot use local variable 'x9' before it is declared + // for (bool b = x9, + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(77, 23), + // (79,32): error CS0128: A local variable named 'x9' is already defined in this scope + // Dummy(true is var x9 && x9); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(79, 32), + // (80,32): error CS0128: A local variable named 'x9' is already defined in this scope + // Dummy(true is var x9 && x9)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(80, 32), + // (86,22): error CS0841: Cannot use local variable 'x10' before it is declared + // for (var b = x10; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x10").WithArguments("x10").WithLocation(86, 22), + // (88,32): error CS0128: A local variable named 'x10' is already defined in this scope + // Dummy(true is var x10 && x10); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(88, 32), + // (89,32): error CS0128: A local variable named 'x10' is already defined in this scope + // Dummy(true is var x10 && x10)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(89, 32), + // (95,23): error CS0841: Cannot use local variable 'x11' before it is declared + // for (bool b = x11; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(95, 23), + // (97,32): error CS0128: A local variable named 'x11' is already defined in this scope + // Dummy(true is var x11 && x11); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(97, 32), + // (98,32): error CS0128: A local variable named 'x11' is already defined in this scope + // Dummy(true is var x11 && x11)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(98, 32), + // (104,20): error CS0841: Cannot use local variable 'x12' before it is declared + // for (Dummy(x12); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(104, 20), + // (105,20): error CS0841: Cannot use local variable 'x12' before it is declared + // Dummy(x12) && + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(105, 20), + // (107,32): error CS0128: A local variable named 'x12' is already defined in this scope + // Dummy(true is var x12 && x12)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x12").WithArguments("x12").WithLocation(107, 32), + // (113,22): error CS0841: Cannot use local variable 'x13' before it is declared + // for (var b = x13; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(113, 22), + // (114,20): error CS0841: Cannot use local variable 'x13' before it is declared + // Dummy(x13); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(114, 20), + // (116,32): error CS0128: A local variable named 'x13' is already defined in this scope + // Dummy(true is var x13 && x13)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x13").WithArguments("x13").WithLocation(116, 32), + // (122,23): error CS0841: Cannot use local variable 'x14' before it is declared + // for (bool b = x14; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(122, 23), + // (123,20): error CS0841: Cannot use local variable 'x14' before it is declared + // Dummy(x14); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(123, 20), + // (125,32): error CS0128: A local variable named 'x14' is already defined in this scope + // Dummy(true is var x14 && x14)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(125, 32), + // (131,20): error CS0841: Cannot use local variable 'x15' before it is declared + // for (Dummy(x15); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(131, 20), + // (132,20): error CS0841: Cannot use local variable 'x15' before it is declared + // Dummy(x15); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(132, 20), + // (133,20): error CS0841: Cannot use local variable 'x15' before it is declared + // Dummy(x15), + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(133, 20) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + VerifyNotAPatternLocal(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); + VerifyNotAPatternLocal(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); + VerifyNotAPatternLocal(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + VerifyNotAPatternLocal(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl); + VerifyNotAPatternLocal(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl); + VerifyNotAPatternLocal(model, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x7Decl); + VerifyNotAPatternLocal(model, x7Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(4, x8Decl.Length); + Assert.Equal(4, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[3]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(3, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[2]); + + var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); + var x10Ref = GetReferences(tree, "x10").ToArray(); + Assert.Equal(3, x10Decl.Length); + Assert.Equal(4, x10Ref.Length); + VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[2]); + + var x11Decl = GetPatternDeclarations(tree, "x11").ToArray(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(3, x11Decl.Length); + Assert.Equal(4, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl[0], x11Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[2]); + + var x12Decl = GetPatternDeclarations(tree, "x12").ToArray(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Decl.Length); + Assert.Equal(4, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl[0], x12Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x12Decl[1]); + + var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); + var x13Ref = GetReferences(tree, "x13").ToArray(); + Assert.Equal(2, x13Decl.Length); + Assert.Equal(4, x13Ref.Length); + VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x13Decl[1]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(4, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + + var x15Decl = GetPatternDeclarations(tree, "x15").Single(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(4, x15Ref.Length); + VerifyModelForDeclarationPattern(model, x15Decl, x15Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Foreach_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + System.Collections.IEnumerable Dummy(params object[] x) {return null;} + + void Test1() + { + foreach (var i in Dummy(true is var x1 && x1)) + { + Dummy(x1); + } + } + + void Test2() + { + foreach (var i in Dummy(true is var x2 && x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + foreach (var i in Dummy(true is var x4 && x4)) + Dummy(x4); + } + + void Test6() + { + foreach (var i in Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + foreach (var i in Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + foreach (var i in Dummy(true is var x8 && x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + foreach (var i1 in Dummy(true is var x9 && x9)) + { + Dummy(x9); + foreach (var i2 in Dummy(true is var x9 && x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + foreach (var i in Dummy(y10 is var x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // foreach (var i in Dummy(y11 is var x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + foreach (var i in Dummy(y12 is var x12)) + var y12 = 12; + } + + //void Test13() + //{ + // foreach (var i in Dummy(y13 is var x13)) + // let y13 = 12; + //} + + void Test14() + { + foreach (var i in Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } + + void Test15() + { + foreach (var x15 in + Dummy(1 is var x15, x15)) + { + Dummy(x15); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,45): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // foreach (var i in Dummy(true is var x4 && x4)) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 45), + // (35,33): error CS0841: Cannot use local variable 'x6' before it is declared + // foreach (var i in Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 33), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (53,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), + // (61,50): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // foreach (var i2 in Dummy(true is var x9 && x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 50), + // (68,33): error CS0103: The name 'y10' does not exist in the current context + // foreach (var i in Dummy(y10 is var x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 33), + // (86,33): error CS0103: The name 'y12' does not exist in the current context + // foreach (var i in Dummy(y12 is var x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 33), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,42): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 42), + // (108,22): error CS0136: A local or parameter named 'x15' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // foreach (var x15 in + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x15").WithArguments("x15").WithLocation(108, 22) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + + var x15Decl = GetPatternDeclarations(tree, "x15").Single(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(2, x15Ref.Length); + VerifyModelForDeclarationPattern(model, x15Decl, x15Ref[0]); + VerifyNotAPatternLocal(model, x15Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Lock_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + lock (Dummy(true is var x1 && x1)) + { + Dummy(x1); + } + } + + void Test2() + { + lock (Dummy(true is var x2 && x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + lock (Dummy(true is var x4 && x4)) + Dummy(x4); + } + + void Test6() + { + lock (Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + lock (Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + lock (Dummy(true is var x8 && x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + lock (Dummy(true is var x9 && x9)) + { + Dummy(x9); + lock (Dummy(true is var x9 && x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + lock (Dummy(y10 is var x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // lock (Dummy(y11 is var x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + lock (Dummy(y12 is var x12)) + var y12 = 12; + } + + //void Test13() + //{ + // lock (Dummy(y13 is var x13)) + // let y13 = 12; + //} + + void Test14() + { + lock (Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,33): error CS0128: A local variable named 'x4' is already defined in this scope + // lock (Dummy(true is var x4 && x4)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(29, 33), + // (35,21): error CS0841: Cannot use local variable 'x6' before it is declared + // lock (Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 21), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (60,19): error CS0841: Cannot use local variable 'x9' before it is declared + // Dummy(x9); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(60, 19), + // (61,37): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // lock (Dummy(true is var x9 && x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 37), + // (68,21): error CS0103: The name 'y10' does not exist in the current context + // lock (Dummy(y10 is var x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 21), + // (86,21): error CS0103: The name 'y12' does not exist in the current context + // lock (Dummy(y12 is var x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 21), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,30): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 30) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Lock_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) {return null;} + + void Test1() + { + if (true) + lock (Dummy(true is var x1)) + { + } + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (17,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Lock_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (LockStatementSyntax)SyntaxFactory.ParseStatement(@" +lock (Dummy(11 is var x1, x1)); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_Fixed_01() + { + var source = +@" +public unsafe class X +{ + public static void Main() + { + } + + int[] Dummy(params object[] x) {return null;} + + void Test1() + { + fixed (int* p = Dummy(true is var x1 && x1)) + { + Dummy(x1); + } + } + + void Test2() + { + fixed (int* p = Dummy(true is var x2 && x2)) + Dummy(x2); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + fixed (int* p = Dummy(true is var x4 && x4)) + Dummy(x4); + } + + void Test6() + { + fixed (int* p = Dummy(x6 && true is var x6)) + Dummy(x6); + } + + void Test7() + { + fixed (int* p = Dummy(true is var x7 && x7)) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + fixed (int* p = Dummy(true is var x8 && x8)) + Dummy(x8); + + System.Console.WriteLine(x8); + } + + void Test9() + { + fixed (int* p1 = Dummy(true is var x9 && x9)) + { + Dummy(x9); + fixed (int* p2 = Dummy(true is var x9 && x9)) // 2 + Dummy(x9); + } + } + + void Test10() + { + fixed (int* p = Dummy(y10 is var x10)) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // fixed (int* p = Dummy(y11 is var x11)) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test12() + { + fixed (int* p = Dummy(y12 is var x12)) + var y12 = 12; + } + + //void Test13() + //{ + // fixed (int* p = Dummy(y13 is var x13)) + // let y13 = 12; + //} + + void Test14() + { + fixed (int* p = Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); + compilation.VerifyDiagnostics( + // (87,13): error CS1023: Embedded statement cannot be a declaration or labeled statement + // var y12 = 12; + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "var y12 = 12;").WithLocation(87, 13), + // (29,43): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // fixed (int* p = Dummy(true is var x4 && x4)) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(29, 43), + // (35,31): error CS0841: Cannot use local variable 'x6' before it is declared + // fixed (int* p = Dummy(x6 && true is var x6)) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(35, 31), + // (43,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(43, 17), + // (53,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(53, 34), + // (61,48): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // fixed (int* p2 = Dummy(true is var x9 && x9)) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(61, 48), + // (68,31): error CS0103: The name 'y10' does not exist in the current context + // fixed (int* p = Dummy(y10 is var x10)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(68, 31), + // (86,31): error CS0103: The name 'y12' does not exist in the current context + // fixed (int* p = Dummy(y12 is var x12)) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y12").WithArguments("y12").WithLocation(86, 31), + // (87,17): warning CS0219: The variable 'y12' is assigned but its value is never used + // var y12 = 12; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "y12").WithArguments("y12").WithLocation(87, 17), + // (99,40): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 40) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var y12Ref = GetReferences(tree, "y12").Single(); + VerifyNotInScope(model, y12Ref); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Fixed_02() + { + var source = +@" +public unsafe class X +{ + public static void Main() + { + } + + int[] Dummy(params object[] x) {return null;} + int[] Dummy(int* x) {return null;} + + void Test1() + { + fixed (int* x1 = + Dummy(true is var x1 && x1)) + { + Dummy(x1); + } + } + + void Test2() + { + fixed (int* p = Dummy(true is var x2 && x2), + x2 = Dummy()) + { + Dummy(x2); + } + } + + void Test3() + { + fixed (int* x3 = Dummy(), + p = Dummy(true is var x3 && x3)) + { + Dummy(x3); + } + } + + void Test4() + { + fixed (int* p1 = Dummy(true is var x4 && x4), + p2 = Dummy(true is var x4 && x4)) + { + Dummy(x4); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe.WithAllowUnsafe(true)); + compilation.VerifyDiagnostics( + // (14,44): error CS0128: A local variable named 'x1' is already defined in this scope + // Dummy(true is var x1 && x1)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(14, 44), + // (14,50): error CS0165: Use of unassigned local variable 'x1' + // Dummy(true is var x1 && x1)) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(14, 50), + // (23,21): error CS0128: A local variable named 'x2' is already defined in this scope + // x2 = Dummy()) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(23, 21), + // (32,43): error CS0128: A local variable named 'x3' is already defined in this scope + // p = Dummy(true is var x3 && x3)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(32, 43), + // (41,44): error CS0128: A local variable named 'x4' is already defined in this scope + // p2 = Dummy(true is var x4 && x4)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(41, 44) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + VerifyNotAPatternLocal(model, x1Ref[0]); + VerifyNotAPatternLocal(model, x1Ref[1]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); + VerifyNotAPatternLocal(model, x3Ref[0]); + VerifyNotAPatternLocal(model, x3Ref[1]); + + var x4Decl = GetPatternDeclarations(tree, "x4").ToArray(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Decl.Length); + Assert.Equal(3, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl[0], x4Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl[1]); + } + + [Fact] + public void ScopeOfPatternVariables_Yield_01() + { + var source = +@" +using System.Collections; + +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) { return null;} + + IEnumerable Test1() + { + yield return Dummy(true is var x1, x1); + { + yield return Dummy(true is var x1, x1); + } + yield return Dummy(true is var x1, x1); + } + + IEnumerable Test2() + { + yield return Dummy(x2, true is var x2); + } + + IEnumerable Test3(int x3) + { + yield return Dummy(true is var x3, x3); + } + + IEnumerable Test4() + { + var x4 = 11; + Dummy(x4); + yield return Dummy(true is var x4, x4); + } + + IEnumerable Test5() + { + yield return Dummy(true is var x5, x5); + var x5 = 11; + Dummy(x5); + } + + //IEnumerable Test6() + //{ + // let x6 = 11; + // Dummy(x6); + // yield return Dummy(true is var x6, x6); + //} + + //IEnumerable Test7() + //{ + // yield return Dummy(true is var x7, x7); + // let x7 = 11; + // Dummy(x7); + //} + + IEnumerable Test8() + { + yield return Dummy(true is var x8, x8, false is var x8, x8); + } + + IEnumerable Test9(bool y9) + { + if (y9) + yield return Dummy(true is var x9, x9); + } + + IEnumerable Test11() + { + Dummy(x11); + yield return Dummy(true is var x11, x11); + } + + IEnumerable Test12() + { + yield return Dummy(true is var x12, x12); + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (16,44): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // yield return Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(16, 44), + // (18,40): error CS0128: A local variable named 'x1' is already defined in this scope + // yield return Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(18, 40), + // (23,28): error CS0841: Cannot use local variable 'x2' before it is declared + // yield return Dummy(x2, true is var x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(23, 28), + // (28,40): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // yield return Dummy(true is var x3, x3); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(28, 40), + // (35,40): error CS0128: A local variable named 'x4' is already defined in this scope + // yield return Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(35, 40), + // (41,13): error CS0128: A local variable named 'x5' is already defined in this scope + // var x5 = 11; + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(41, 13), + // (41,13): warning CS0219: The variable 'x5' is assigned but its value is never used + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(41, 13), + // (61,61): error CS0128: A local variable named 'x8' is already defined in this scope + // yield return Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(61, 61), + // (72,15): error CS0841: Cannot use local variable 'x11' before it is declared + // Dummy(x11); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(72, 15) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(2, x8Ref.Length); + for (int i = 0; i < x8Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").Single(); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Yield_02() + { + var source = +@" +using System.Collections; + +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) { return null;} + + IEnumerable Test1() + { + if (true) + yield return Dummy(true is var x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (17,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(17, 9) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_Yield_03() + { + var source = +@" +using System.Collections; + +public class X +{ + public static void Main() + { + } + + object Dummy(params object[] x) { return null;} + + IEnumerable Test1() + { + SpeculateHere(); + yield 0; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (YieldStatementSyntax)SyntaxFactory.ParseStatement(@" +yield return (Dummy(11 is var x1, x1)); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + GetReferences(tree, "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void ScopeOfPatternVariables_Catch_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + try {} + catch when (true is var x1 && x1) + { + Dummy(x1); + } + } + + void Test4() + { + var x4 = 11; + Dummy(x4); + + try {} + catch when (true is var x4 && x4) + { + Dummy(x4); + } + } + + void Test6() + { + try {} + catch when (x6 && true is var x6) + { + Dummy(x6); + } + } + + void Test7() + { + try {} + catch when (true is var x7 && x7) + { + var x7 = 12; + Dummy(x7); + } + } + + void Test8() + { + try {} + catch when (true is var x8 && x8) + { + Dummy(x8); + } + + System.Console.WriteLine(x8); + } + + void Test9() + { + try {} + catch when (true is var x9 && x9) + { + Dummy(x9); + try {} + catch when (true is var x9 && x9) // 2 + { + Dummy(x9); + } + } + } + + void Test10() + { + try {} + catch when (y10 is var x10) + { + var y10 = 12; + Dummy(y10); + } + } + + //void Test11() + //{ + // try {} + // catch when (y11 is var x11) + // { + // let y11 = 12; + // Dummy(y11); + // } + //} + + void Test14() + { + try {} + catch when (Dummy(1 is var x14, + 2 is var x14, + x14)) + { + Dummy(x14); + } + } + + void Test15() + { + try {} + catch (System.Exception x15) + when (Dummy(1 is var x15, x15)) + { + Dummy(x15); + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + compilation.VerifyDiagnostics( + // (25,33): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // catch when (true is var x4 && x4) + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(25, 33), + // (34,21): error CS0841: Cannot use local variable 'x6' before it is declared + // catch when (x6 && true is var x6) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x6").WithArguments("x6").WithLocation(34, 21), + // (45,17): error CS0136: A local or parameter named 'x7' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // var x7 = 12; + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x7").WithArguments("x7").WithLocation(45, 17), + // (58,34): error CS0103: The name 'x8' does not exist in the current context + // System.Console.WriteLine(x8); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x8").WithArguments("x8").WithLocation(58, 34), + // (68,37): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // catch when (true is var x9 && x9) // 2 + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(68, 37), + // (78,21): error CS0103: The name 'y10' does not exist in the current context + // catch when (y10 is var x10) + Diagnostic(ErrorCode.ERR_NameNotInContext, "y10").WithArguments("y10").WithLocation(78, 21), + // (99,36): error CS0128: A local variable named 'x14' is already defined in this scope + // 2 is var x14, + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(99, 36), + // (110,36): error CS0128: A local variable named 'x15' is already defined in this scope + // when (Dummy(1 is var x15, x15)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(110, 36) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(2, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(3, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[1], x4Ref[2]); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").ToArray(); + Assert.Equal(2, x6Ref.Length); + VerifyModelForDeclarationPattern(model, x6Decl, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").ToArray(); + Assert.Equal(2, x7Ref.Length); + VerifyModelForDeclarationPattern(model, x7Decl, x7Ref[0]); + VerifyNotAPatternLocal(model, x7Ref[1]); + + var x8Decl = GetPatternDeclarations(tree, "x8").Single(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(3, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl, x8Ref[0], x8Ref[1]); + VerifyNotInScope(model, x8Ref[2]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(2, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref[0], x9Ref[1]); + VerifyModelForDeclarationPattern(model, x9Decl[1], x9Ref[2], x9Ref[3]); + + var y10Ref = GetReferences(tree, "y10").ToArray(); + Assert.Equal(2, y10Ref.Length); + VerifyNotInScope(model, y10Ref[0]); + VerifyNotAPatternLocal(model, y10Ref[1]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(2, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + + var x15Decl = GetPatternDeclarations(tree, "x15").Single(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(2, x15Ref.Length); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl); + VerifyNotAPatternLocal(model, x15Ref[0]); + VerifyNotAPatternLocal(model, x15Ref[1]); + } + + [Fact] + public void ScopeOfPatternVariables_LabeledStatement_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { +a: Dummy(true is var x1, x1); + { +b: Dummy(true is var x1, x1); + } +c: Dummy(true is var x1, x1); + } + + void Test2() + { + Dummy(x2, true is var x2); + } + + void Test3(int x3) + { +a: Dummy(true is var x3, x3); + } + + void Test4() + { + var x4 = 11; + Dummy(x4); +a: Dummy(true is var x4, x4); + } + + void Test5() + { +a: Dummy(true is var x5, x5); + var x5 = 11; + Dummy(x5); + } + + //void Test6() + //{ + // let x6 = 11; + // Dummy(x6); + //a: Dummy(true is var x6, x6); + //} + + //void Test7() + //{ + //a: Dummy(true is var x7, x7); + // let x7 = 11; + // Dummy(x7); + //} + + void Test8() + { +a: Dummy(true is var x8, x8, false is var x8, x8); + } + + void Test9(bool y9) + { + if (y9) +a: Dummy(true is var x9, x9); + } + + System.Action Test10(bool y10) + { + return () => + { + if (y10) +a: Dummy(true is var x10, x10); + }; + } + + void Test11() + { + Dummy(x11); +a: Dummy(true is var x11, x11); + } + + void Test12() + { +a: Dummy(true is var x12, x12); + Dummy(x12); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (65,1): error CS1023: Embedded statement cannot be a declaration or labeled statement + // a: Dummy(true is var x9, x9); + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x9, x9);").WithLocation(65, 1), + // (73,1): error CS1023: Embedded statement cannot be a declaration or labeled statement + // a: Dummy(true is var x10, x10); + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x10, x10);").WithLocation(73, 1), + // (14,31): error CS0136: A local or parameter named 'x1' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // b: Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x1").WithArguments("x1").WithLocation(14, 31), + // (16,27): error CS0128: A local variable named 'x1' is already defined in this scope + // c: Dummy(true is var x1, x1); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(16, 27), + // (12,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x1, x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(12, 1), + // (14,1): warning CS0164: This label has not been referenced + // b: Dummy(true is var x1, x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "b").WithLocation(14, 1), + // (16,1): warning CS0164: This label has not been referenced + // c: Dummy(true is var x1, x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "c").WithLocation(16, 1), + // (21,15): error CS0841: Cannot use local variable 'x2' before it is declared + // Dummy(x2, true is var x2); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(21, 15), + // (26,27): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // a: Dummy(true is var x3, x3); + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(26, 27), + // (26,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x3, x3); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(26, 1), + // (33,27): error CS0128: A local variable named 'x4' is already defined in this scope + // a: Dummy(true is var x4, x4); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(33, 27), + // (33,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x4, x4); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(33, 1), + // (39,13): error CS0128: A local variable named 'x5' is already defined in this scope + // var x5 = 11; + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(39, 13), + // (38,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x5, x5); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(38, 1), + // (39,13): warning CS0219: The variable 'x5' is assigned but its value is never used + // var x5 = 11; + Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x5").WithArguments("x5").WithLocation(39, 13), + // (59,48): error CS0128: A local variable named 'x8' is already defined in this scope + // a: Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(59, 48), + // (59,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x8, x8, false is var x8, x8); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(59, 1), + // (65,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x9, x9); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(65, 1), + // (73,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x10, x10); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(73, 1), + // (79,15): error CS0841: Cannot use local variable 'x11' before it is declared + // Dummy(x11); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(79, 15), + // (80,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x11, x11); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(80, 1), + // (85,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x12, x12); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(85, 1) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").ToArray(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(3, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl[0], x1Ref[0], x1Ref[2]); + VerifyModelForDeclarationPattern(model, x1Decl[1], x1Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl[2]); + + var x2Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x2").Single(); + var x2Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x2").Single(); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x3").Single(); + var x3Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x3").Single(); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x4").Single(); + var x4Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x4").ToArray(); + Assert.Equal(2, x4Ref.Length); + VerifyNotAPatternLocal(model, x4Ref[0]); + VerifyNotAPatternLocal(model, x4Ref[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + + var x5Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x5").Single(); + var x5Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x5").ToArray(); + Assert.Equal(2, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref); + + var x8Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x8").ToArray(); + var x8Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(2, x8Ref.Length); + for (int i = 0; i < x8Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x9").Single(); + var x9Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x9").Single(); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref); + + var x10Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x10").Single(); + var x10Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x10").Single(); + VerifyModelForDeclarationPattern(model, x10Decl, x10Ref); + + var x11Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x11").Single(); + var x11Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x11").ToArray(); + Assert.Equal(2, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref); + + var x12Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x12").Single(); + var x12Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x12").ToArray(); + Assert.Equal(2, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref); + } + + [Fact] + public void ScopeOfPatternVariables_LabeledStatement_02() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + if (true) +a: Dummy(true is var x1); + + x1++; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + compilation.VerifyDiagnostics( + // (13,1): error CS1023: Embedded statement cannot be a declaration or labeled statement + // a: Dummy(true is var x1); + Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: Dummy(true is var x1);").WithLocation(13, 1), + // (15,9): error CS0103: The name 'x1' does not exist in the current context + // x1++; + Diagnostic(ErrorCode.ERR_NameNotInContext, "x1").WithArguments("x1").WithLocation(15, 9), + // (13,1): warning CS0164: This label has not been referenced + // a: Dummy(true is var x1); + Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(13, 1) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").Single(); + VerifyModelForDeclarationPattern(model, x1Decl); + VerifyNotInScope(model, x1Ref); + } + + [Fact] + public void ScopeOfPatternVariables_LabeledStatement_03() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + void Dummy(params object[] x) {} + + void Test1() + { + SpeculateHere(); + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var statement = (LabeledStatementSyntax)SyntaxFactory.ParseStatement(@" +a: b: c:Dummy(11 is var x1, x1); +"); + + bool success = model.TryGetSpeculativeSemanticModel( + tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "SpeculateHere").Single().SpanStart, + statement, out model); + Assert.True(success); + Assert.NotNull(model); + tree = statement.SyntaxTree; + + var x1Decl = tree.GetRoot().DescendantNodes().OfType().Where(p => p.Identifier.ValueText == "x1").Single(); + var x1Ref = tree.GetRoot().DescendantNodes().OfType().Where(id => id.Identifier.ValueText == "x1").ToArray(); + Assert.Equal(1, x1Ref.Length); + VerifyModelForDeclarationPattern(model, x1Decl, x1Ref); + Assert.Equal("System.Int32", model.GetTypeInfo(x1Ref[0]).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_For_06() + { + var source = +@" +public class X +{ + static bool Data = true; + public static void Main() + { + } + + bool Dummy(params object[] x) {return true;} + + void Test1() + { + for (var x1 = + Dummy(Dummy(true, Data is var x1) && x1) + ;;) + {} + } + + void Test2() + { + for (var x2 = true; + Dummy(Dummy(true, Data is var x2) && x2) + ;) + {} + } + + void Test3() + { + for (var x3 = true;; + Dummy(Dummy(true, Data is var x3) && x3) + ) + {} + } + + void Test4() + { + for (bool x4 = + Dummy(Dummy(true, Data is var x4) && x4) + ;;) + {} + } + + void Test5() + { + for (bool x5 = true; + Dummy(Dummy(true, Data is var x5) && x5) + ;) + {} + } + + void Test6() + { + for (bool x6 = true;; + Dummy(Dummy(true, Data is var x6) && x6) + ) + {} + } + + void Test7() + { + for (bool x7 = true, b = + Dummy(Dummy(true, Data is var x7) && x7) + ;;) + {} + } + + void Test8() + { + for (bool b1 = Dummy(Dummy(true, Data is var x8) && x8), + b2 = Dummy(Dummy(true, Data is var x8) && x8); + Dummy(Dummy(true, Data is var x8) && x8); + Dummy(Dummy(true, Data is var x8) && x8)) + {} + } + + void Test9() + { + for (bool b = x9, + b2 = Dummy(Dummy(true, Data is var x9) && x9); + Dummy(Dummy(true, Data is var x9) && x9); + Dummy(Dummy(true, Data is var x9) && x9)) + {} + } + + void Test10() + { + for (var b = x10; + Dummy(Dummy(true, Data is var x10) && x10) && + Dummy(Dummy(true, Data is var x10) && x10); + Dummy(Dummy(true, Data is var x10) && x10)) + {} + } + + void Test11() + { + for (bool b = x11; + Dummy(Dummy(true, Data is var x11) && x11) && + Dummy(Dummy(true, Data is var x11) && x11); + Dummy(Dummy(true, Data is var x11) && x11)) + {} + } + + void Test12() + { + for (Dummy(x12); + Dummy(x12) && + Dummy(Dummy(true, Data is var x12) && x12); + Dummy(Dummy(true, Data is var x12) && x12)) + {} + } + + void Test13() + { + for (var b = x13; + Dummy(x13); + Dummy(Dummy(true, Data is var x13) && x13), + Dummy(Dummy(true, Data is var x13) && x13)) + {} + } + + void Test14() + { + for (bool b = x14; + Dummy(x14); + Dummy(Dummy(true, Data is var x14) && x14), + Dummy(Dummy(true, Data is var x14) && x14)) + {} + } + + void Test15() + { + for (Dummy(x15); + Dummy(x15); + Dummy(x15), + Dummy(Dummy(true, Data is var x15) && x15)) + {} + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + compilation.VerifyDiagnostics( + // (14,44): error CS0128: A local variable named 'x1' is already defined in this scope + // Dummy(Dummy(true, Data is var x1) && x1) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x1").WithArguments("x1").WithLocation(14, 44), + // (14,51): error CS0841: Cannot use local variable 'x1' before it is declared + // Dummy(Dummy(true, Data is var x1) && x1) + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x1").WithArguments("x1").WithLocation(14, 51), + // (14,51): error CS0165: Use of unassigned local variable 'x1' + // Dummy(Dummy(true, Data is var x1) && x1) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x1").WithArguments("x1").WithLocation(14, 51), + // (22,44): error CS0128: A local variable named 'x2' is already defined in this scope + // Dummy(Dummy(true, Data is var x2) && x2) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x2").WithArguments("x2").WithLocation(22, 44), + // (30,44): error CS0128: A local variable named 'x3' is already defined in this scope + // Dummy(Dummy(true, Data is var x3) && x3) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x3").WithArguments("x3").WithLocation(30, 44), + // (38,44): error CS0128: A local variable named 'x4' is already defined in this scope + // Dummy(Dummy(true, Data is var x4) && x4) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x4").WithArguments("x4").WithLocation(38, 44), + // (38,51): error CS0165: Use of unassigned local variable 'x4' + // Dummy(Dummy(true, Data is var x4) && x4) + Diagnostic(ErrorCode.ERR_UseDefViolation, "x4").WithArguments("x4").WithLocation(38, 51), + // (46,44): error CS0128: A local variable named 'x5' is already defined in this scope + // Dummy(Dummy(true, Data is var x5) && x5) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(46, 44), + // (54,44): error CS0128: A local variable named 'x6' is already defined in this scope + // Dummy(Dummy(true, Data is var x6) && x6) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x6").WithArguments("x6").WithLocation(54, 44), + // (62,44): error CS0128: A local variable named 'x7' is already defined in this scope + // Dummy(Dummy(true, Data is var x7) && x7) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x7").WithArguments("x7").WithLocation(62, 44), + // (70,49): error CS0128: A local variable named 'x8' is already defined in this scope + // b2 = Dummy(Dummy(true, Data is var x8) && x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(70, 49), + // (71,44): error CS0128: A local variable named 'x8' is already defined in this scope + // Dummy(Dummy(true, Data is var x8) && x8); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(71, 44), + // (72,44): error CS0128: A local variable named 'x8' is already defined in this scope + // Dummy(Dummy(true, Data is var x8) && x8)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(72, 44), + // (78,23): error CS0841: Cannot use local variable 'x9' before it is declared + // for (bool b = x9, + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(78, 23), + // (80,44): error CS0128: A local variable named 'x9' is already defined in this scope + // Dummy(Dummy(true, Data is var x9) && x9); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(80, 44), + // (81,44): error CS0128: A local variable named 'x9' is already defined in this scope + // Dummy(Dummy(true, Data is var x9) && x9)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x9").WithArguments("x9").WithLocation(81, 44), + // (87,22): error CS0841: Cannot use local variable 'x10' before it is declared + // for (var b = x10; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x10").WithArguments("x10").WithLocation(87, 22), + // (89,44): error CS0128: A local variable named 'x10' is already defined in this scope + // Dummy(Dummy(true, Data is var x10) && x10); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(89, 44), + // (90,44): error CS0128: A local variable named 'x10' is already defined in this scope + // Dummy(Dummy(true, Data is var x10) && x10)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x10").WithArguments("x10").WithLocation(90, 44), + // (96,23): error CS0841: Cannot use local variable 'x11' before it is declared + // for (bool b = x11; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x11").WithArguments("x11").WithLocation(96, 23), + // (98,44): error CS0128: A local variable named 'x11' is already defined in this scope + // Dummy(Dummy(true, Data is var x11) && x11); + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(98, 44), + // (99,44): error CS0128: A local variable named 'x11' is already defined in this scope + // Dummy(Dummy(true, Data is var x11) && x11)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x11").WithArguments("x11").WithLocation(99, 44), + // (105,20): error CS0841: Cannot use local variable 'x12' before it is declared + // for (Dummy(x12); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(105, 20), + // (106,20): error CS0841: Cannot use local variable 'x12' before it is declared + // Dummy(x12) && + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x12").WithArguments("x12").WithLocation(106, 20), + // (108,44): error CS0128: A local variable named 'x12' is already defined in this scope + // Dummy(Dummy(true, Data is var x12) && x12)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x12").WithArguments("x12").WithLocation(108, 44), + // (114,22): error CS0841: Cannot use local variable 'x13' before it is declared + // for (var b = x13; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(114, 22), + // (115,20): error CS0841: Cannot use local variable 'x13' before it is declared + // Dummy(x13); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x13").WithArguments("x13").WithLocation(115, 20), + // (117,44): error CS0128: A local variable named 'x13' is already defined in this scope + // Dummy(Dummy(true, Data is var x13) && x13)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x13").WithArguments("x13").WithLocation(117, 44), + // (123,23): error CS0841: Cannot use local variable 'x14' before it is declared + // for (bool b = x14; + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(123, 23), + // (124,20): error CS0841: Cannot use local variable 'x14' before it is declared + // Dummy(x14); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x14").WithArguments("x14").WithLocation(124, 20), + // (126,44): error CS0128: A local variable named 'x14' is already defined in this scope + // Dummy(Dummy(true, Data is var x14) && x14)) + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x14").WithArguments("x14").WithLocation(126, 44), + // (132,20): error CS0841: Cannot use local variable 'x15' before it is declared + // for (Dummy(x15); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(132, 20), + // (133,20): error CS0841: Cannot use local variable 'x15' before it is declared + // Dummy(x15); + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(133, 20), + // (134,20): error CS0841: Cannot use local variable 'x15' before it is declared + // Dummy(x15), + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x15").WithArguments("x15").WithLocation(134, 20) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").Single(); + var x1Ref = GetReferences(tree, "x1").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x1Decl); + VerifyNotAPatternLocal(model, x1Ref); + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x2Decl); + VerifyNotAPatternLocal(model, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x3Decl); + VerifyNotAPatternLocal(model, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x4Decl); + VerifyNotAPatternLocal(model, x4Ref); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x5Decl); + VerifyNotAPatternLocal(model, x5Ref); + + var x6Decl = GetPatternDeclarations(tree, "x6").Single(); + var x6Ref = GetReferences(tree, "x6").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x6Decl); + VerifyNotAPatternLocal(model, x6Ref); + + var x7Decl = GetPatternDeclarations(tree, "x7").Single(); + var x7Ref = GetReferences(tree, "x7").Single(); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x7Decl); + VerifyNotAPatternLocal(model, x7Ref); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(4, x8Decl.Length); + Assert.Equal(4, x8Ref.Length); + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[2]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[3]); + + var x9Decl = GetPatternDeclarations(tree, "x9").ToArray(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(3, x9Decl.Length); + Assert.Equal(4, x9Ref.Length); + VerifyModelForDeclarationPattern(model, x9Decl[0], x9Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x9Decl[2]); + + var x10Decl = GetPatternDeclarations(tree, "x10").ToArray(); + var x10Ref = GetReferences(tree, "x10").ToArray(); + Assert.Equal(3, x10Decl.Length); + Assert.Equal(4, x10Ref.Length); + VerifyModelForDeclarationPattern(model, x10Decl[0], x10Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x10Decl[2]); + + var x11Decl = GetPatternDeclarations(tree, "x11").ToArray(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(3, x11Decl.Length); + Assert.Equal(4, x11Ref.Length); + VerifyModelForDeclarationPattern(model, x11Decl[0], x11Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[1]); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x11Decl[2]); + + var x12Decl = GetPatternDeclarations(tree, "x12").ToArray(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(2, x12Decl.Length); + Assert.Equal(4, x12Ref.Length); + VerifyModelForDeclarationPattern(model, x12Decl[0], x12Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x12Decl[1]); + + var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); + var x13Ref = GetReferences(tree, "x13").ToArray(); + Assert.Equal(2, x13Decl.Length); + Assert.Equal(4, x13Ref.Length); + VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x13Decl[1]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(4, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x14Decl[1]); + + var x15Decl = GetPatternDeclarations(tree, "x15").Single(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(4, x15Ref.Length); + VerifyModelForDeclarationPattern(model, x15Decl, x15Ref); + } + + [Fact] + public void Scope_SwitchLabelGuard_01() + { + var source = +@" +public class X +{ + public static void Main() + { + } + + bool Dummy(params object[] x) { return true; } + + public static int Data = 2; + + void Test1(int val) + { + switch (val) + { + case 0 when Dummy(Dummy(Data is var x1), x1): + Dummy(x1); + break; + case 1 when Dummy(Dummy(Data is var x1), x1): + Dummy(x1); + break; + case 2 when Dummy(Dummy(Data is var x1), x1): + Dummy(x1); + break; + } + } + + void Test2(int val) + { + switch (val) + { + case 0 when Dummy(x2, Dummy(Data is var x2)): + Dummy(x2); + break; + } + } + + void Test3(int x3, int val) + { + switch (val) + { + case 0 when Dummy(Dummy(Data is var x3), x3): + Dummy(x3); + break; + } + } + + void Test4(int val) + { + var x4 = 11; + switch (val) + { + case 0 when Dummy(Dummy(Data is var x4), x4): + Dummy(x4); + break; + case 1 when Dummy(x4): Dummy(x4); break; + } + } + + void Test5(int val) + { + switch (val) + { + case 0 when Dummy(Dummy(Data is var x5), x5): + Dummy(x5); + break; + } + + var x5 = 11; + Dummy(x5); + } + + //void Test6(int val) + //{ + // let x6 = 11; + // switch (val) + // { + // case 0 when Dummy(x6): + // Dummy(x6); + // break; + // case 1 when Dummy(Dummy(Data is var x6), x6): + // Dummy(x6); + // break; + // } + //} + + //void Test7(int val) + //{ + // switch (val) + // { + // case 0 when Dummy(Dummy(Data is var x7), x7): + // Dummy(x7); + // break; + // } + + // let x7 = 11; + // Dummy(x7); + //} + + void Test8(int val) + { + switch (val) + { + case 0 when Dummy(Dummy(Data is var x8), x8, Dummy(Data is var x8), x8): + Dummy(x8); + break; + } + } + + void Test9(int val) + { + switch (val) + { + case 0 when Dummy(x9): + int x9 = 9; + Dummy(x9); + break; + case 2 when Dummy(x9 = 9): + Dummy(x9); + break; + case 1 when Dummy(Dummy(Data is var x9), x9): + Dummy(x9); + break; + } + } + + //void Test10(int val) + //{ + // switch (val) + // { + // case 1 when Dummy(Dummy(Data is var x10), x10): + // Dummy(x10); + // break; + // case 0 when Dummy(x10): + // let x10 = 10; + // Dummy(x10); + // break; + // case 2 when Dummy(x10 = 10, x10): + // Dummy(x10); + // break; + // } + //} + + void Test11(int val) + { + switch (x11 ? val : 0) + { + case 0 when Dummy(x11): + Dummy(x11, 0); + break; + case 1 when Dummy(Dummy(Data is var x11), x11): + Dummy(x11, 1); + break; + } + } + + void Test12(int val) + { + switch (x12 ? val : 0) + { + case 0 when Dummy(Dummy(Data is var x12), x12): + Dummy(x12, 0); + break; + case 1 when Dummy(x12): + Dummy(x12, 1); + break; + } + } + + void Test13() + { + switch (Dummy(1, Data is var x13) ? x13 : 0) + { + case 0 when Dummy(x13): + Dummy(x13); + break; + case 1 when Dummy(Dummy(Data is var x13), x13): + Dummy(x13); + break; + } + } + + void Test14(int val) + { + switch (val) + { + case 1 when Dummy(Dummy(Data is var x14), x14): + Dummy(x14); + Dummy(Dummy(Data is var x14), x14); + Dummy(x14); + break; + } + } + + void Test15(int val) + { + switch (val) + { + case 0 when Dummy(Dummy(Data is var x15), x15): + case 1 when Dummy(Dummy(Data is var x15), x15): + Dummy(x15); + break; + } + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + compilation.VerifyDiagnostics( + // (32,31): error CS0841: Cannot use local variable 'x2' before it is declared + // case 0 when Dummy(x2, Dummy(Data is var x2)): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x2").WithArguments("x2").WithLocation(32, 31), + // (42,49): error CS0136: A local or parameter named 'x3' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(Dummy(Data is var x3), x3): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x3").WithArguments("x3").WithLocation(42, 49), + // (53,49): error CS0136: A local or parameter named 'x4' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(Dummy(Data is var x4), x4): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x4").WithArguments("x4").WithLocation(53, 49), + // (64,49): error CS0136: A local or parameter named 'x5' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 0 when Dummy(Dummy(Data is var x5), x5): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x5").WithArguments("x5").WithLocation(64, 49), + // (104,76): error CS0128: A local variable named 'x8' is already defined in this scope + // case 0 when Dummy(Dummy(Data is var x8), x8, Dummy(Data is var x8), x8): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x8").WithArguments("x8").WithLocation(104, 76), + // (114,31): error CS0841: Cannot use local variable 'x9' before it is declared + // case 0 when Dummy(x9): + Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x9").WithArguments("x9").WithLocation(114, 31), + // (121,49): error CS0136: A local or parameter named 'x9' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(Dummy(Data is var x9), x9): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x9").WithArguments("x9").WithLocation(121, 49), + // (146,17): error CS0103: The name 'x11' does not exist in the current context + // switch (x11 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(146, 17), + // (148,31): error CS0103: The name 'x11' does not exist in the current context + // case 0 when Dummy(x11): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(148, 31), + // (149,23): error CS0103: The name 'x11' does not exist in the current context + // Dummy(x11, 0); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x11").WithArguments("x11").WithLocation(149, 23), + // (159,17): error CS0103: The name 'x12' does not exist in the current context + // switch (x12 ? val : 0) + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(159, 17), + // (164,31): error CS0103: The name 'x12' does not exist in the current context + // case 1 when Dummy(x12): + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(164, 31), + // (165,23): error CS0103: The name 'x12' does not exist in the current context + // Dummy(x12, 1); + Diagnostic(ErrorCode.ERR_NameNotInContext, "x12").WithArguments("x12").WithLocation(165, 23), + // (177,49): error CS0136: A local or parameter named 'x13' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(Dummy(Data is var x13), x13): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x13").WithArguments("x13").WithLocation(177, 49), + // (187,49): error CS0136: A local or parameter named 'x14' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter + // case 1 when Dummy(Dummy(Data is var x14), x14): + Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x14").WithArguments("x14").WithLocation(187, 49), + // (200,49): error CS0128: A local variable named 'x15' is already defined in this scope + // case 1 when Dummy(Dummy(Data is var x15), x15): + Diagnostic(ErrorCode.ERR_LocalDuplicate, "x15").WithArguments("x15").WithLocation(200, 49), + // (200,55): error CS0165: Use of unassigned local variable 'x15' + // case 1 when Dummy(Dummy(Data is var x15), x15): + Diagnostic(ErrorCode.ERR_UseDefViolation, "x15").WithArguments("x15").WithLocation(200, 55) + ); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var x1Decl = GetPatternDeclarations(tree, "x1").ToArray(); + var x1Ref = GetReferences(tree, "x1").ToArray(); + Assert.Equal(3, x1Decl.Length); + Assert.Equal(6, x1Ref.Length); + for (int i = 0; i < x1Decl.Length; i++) + { + VerifyModelForDeclarationPattern(model, x1Decl[i], x1Ref[i * 2], x1Ref[i * 2 + 1]); + } + + var x2Decl = GetPatternDeclarations(tree, "x2").Single(); + var x2Ref = GetReferences(tree, "x2").ToArray(); + Assert.Equal(2, x2Ref.Length); + VerifyModelForDeclarationPattern(model, x2Decl, x2Ref); + + var x3Decl = GetPatternDeclarations(tree, "x3").Single(); + var x3Ref = GetReferences(tree, "x3").ToArray(); + Assert.Equal(2, x3Ref.Length); + VerifyModelForDeclarationPattern(model, x3Decl, x3Ref); + + var x4Decl = GetPatternDeclarations(tree, "x4").Single(); + var x4Ref = GetReferences(tree, "x4").ToArray(); + Assert.Equal(4, x4Ref.Length); + VerifyModelForDeclarationPattern(model, x4Decl, x4Ref[0], x4Ref[1]); + VerifyNotAPatternLocal(model, x4Ref[2]); + VerifyNotAPatternLocal(model, x4Ref[3]); + + var x5Decl = GetPatternDeclarations(tree, "x5").Single(); + var x5Ref = GetReferences(tree, "x5").ToArray(); + Assert.Equal(3, x5Ref.Length); + VerifyModelForDeclarationPattern(model, x5Decl, x5Ref[0], x5Ref[1]); + VerifyNotAPatternLocal(model, x5Ref[2]); + + var x8Decl = GetPatternDeclarations(tree, "x8").ToArray(); + var x8Ref = GetReferences(tree, "x8").ToArray(); + Assert.Equal(2, x8Decl.Length); + Assert.Equal(3, x8Ref.Length); + for (int i = 0; i < x8Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x8Decl[0], x8Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x8Decl[1]); + + var x9Decl = GetPatternDeclarations(tree, "x9").Single(); + var x9Ref = GetReferences(tree, "x9").ToArray(); + Assert.Equal(6, x9Ref.Length); + VerifyNotAPatternLocal(model, x9Ref[0]); + VerifyNotAPatternLocal(model, x9Ref[1]); + VerifyNotAPatternLocal(model, x9Ref[2]); + VerifyNotAPatternLocal(model, x9Ref[3]); + VerifyModelForDeclarationPattern(model, x9Decl, x9Ref[4], x9Ref[5]); + + var x11Decl = GetPatternDeclarations(tree, "x11").Single(); + var x11Ref = GetReferences(tree, "x11").ToArray(); + Assert.Equal(5, x11Ref.Length); + VerifyNotInScope(model, x11Ref[0]); + VerifyNotInScope(model, x11Ref[1]); + VerifyNotInScope(model, x11Ref[2]); + VerifyModelForDeclarationPattern(model, x11Decl, x11Ref[3], x11Ref[4]); + + var x12Decl = GetPatternDeclarations(tree, "x12").Single(); + var x12Ref = GetReferences(tree, "x12").ToArray(); + Assert.Equal(5, x12Ref.Length); + VerifyNotInScope(model, x12Ref[0]); + VerifyModelForDeclarationPattern(model, x12Decl, x12Ref[1], x12Ref[2]); + VerifyNotInScope(model, x12Ref[3]); + VerifyNotInScope(model, x12Ref[4]); + + var x13Decl = GetPatternDeclarations(tree, "x13").ToArray(); + var x13Ref = GetReferences(tree, "x13").ToArray(); + Assert.Equal(2, x13Decl.Length); + Assert.Equal(5, x13Ref.Length); + VerifyModelForDeclarationPattern(model, x13Decl[0], x13Ref[0], x13Ref[1], x13Ref[2]); + VerifyModelForDeclarationPattern(model, x13Decl[1], x13Ref[3], x13Ref[4]); + + var x14Decl = GetPatternDeclarations(tree, "x14").ToArray(); + var x14Ref = GetReferences(tree, "x14").ToArray(); + Assert.Equal(2, x14Decl.Length); + Assert.Equal(4, x14Ref.Length); + VerifyModelForDeclarationPattern(model, x14Decl[0], x14Ref); + VerifyModelForDeclarationPattern(model, x14Decl[1], true); + + var x15Decl = GetPatternDeclarations(tree, "x15").ToArray(); + var x15Ref = GetReferences(tree, "x15").ToArray(); + Assert.Equal(2, x15Decl.Length); + Assert.Equal(3, x15Ref.Length); + for (int i = 0; i < x15Ref.Length; i++) + { + VerifyModelForDeclarationPattern(model, x15Decl[0], x15Ref[i]); + } + VerifyModelForDeclarationPatternDuplicateInSameScope(model, x15Decl[1]); + } + + [Fact] + public void Scope_SwitchLabelGuard_02() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + Dummy(x1 is var y1); + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] trash) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_03() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + while (Dummy(x1 is var y1)) break; + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_04() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + do + val = 0; + while (Dummy(x1 is var y1) && false); + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_05() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + lock ((object)Dummy(x1 is var y1)) {} + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_06() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + if (Dummy(x1 is var y1)) {} + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_07() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + switch (Dummy(x1 is var y1)) + { + default: break; + } + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_08() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + foreach (var x in Test(1)) {} + } + + static System.Collections.IEnumerable Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + yield return Dummy(x1 is var y1); + System.Console.WriteLine(y1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: @"2").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + var model = compilation.GetSemanticModel(tree); + + var yRef = GetReferences(tree, "y1").Single(); + + Assert.Equal("System.Int32", model.GetTypeInfo(yRef).Type.ToTestDisplayString()); + } + + [Fact] + public void Scope_SwitchLabelGuard_09() + { + var source = +@" +public class X +{ + public static int Data = 2; + + public static void Main() + { + Test(1); + } + + static void Test(int val) + { + switch (val) + { + case 1 when Dummy(123, Data is var x1): + var z1 = x1 > 0 & Dummy(x1 is var y1); + System.Console.WriteLine(y1); + System.Console.WriteLine(z1); + break; + } + } + + static bool Dummy(params object[] data) + { + return true; + } +} +"; + var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: TestOptions.Regular); + + CompileAndVerify(compilation, expectedOutput: +@"2 +True").VerifyDiagnostics(); + + var tree = compilation.SyntaxTrees.Single(); + + var yRef = GetReferences(tree, "y1").Single(); + Assert.Equal("System.Int32", compilation.GetSemanticModel(tree).GetTypeInfo(yRef).Type.ToTestDisplayString()); + + var zRef = GetReferences(tree, "z1").Single(); + Assert.Equal("System.Boolean", compilation.GetSemanticModel(tree).GetTypeInfo(zRef).Type.ToTestDisplayString()); + } + } +}