PageRenderTime 59ms CodeModel.GetById 3ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs

https://github.com/EkardNT/Roslyn
C# | 3182 lines | 2664 code | 457 blank | 61 comment | 7 complexity | a419aa8711e65a8f533b53b24d32f8d5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
   2
   3using System;
   4using System.Collections.Generic;
   5using System.Collections.Immutable;
   6using System.Linq;
   7using Microsoft.CodeAnalysis.CSharp.Symbols;
   8using Microsoft.CodeAnalysis.CSharp.Syntax;
   9using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
  10using Microsoft.CodeAnalysis.Test.Utilities;
  11using Microsoft.CodeAnalysis.Text;
  12using Roslyn.Test.Utilities;
  13using Xunit;
  14
  15namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  16{
  17    public class SemanticTests : CSharpTestBase
  18    {
  19        [Fact]
  20        public void LocalSymbolsAreEquivalentAcrossSemanticModelsFromTheSameCompilation()
  21        {
  22            var text = @"public class C { public void M() { int x = 10; } }";
  23            var tree = Parse(text);
  24            var comp = CreateCompilationWithMscorlib(tree); 
  25
  26            var model1 = comp.GetSemanticModel(tree);
  27            var model2 = comp.GetSemanticModel(tree);
  28            Assert.NotEqual(model1, model2);
  29
  30            var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
  31            var symbol1 = model1.GetDeclaredSymbol(vardecl);
  32            var symbol2 = model2.GetDeclaredSymbol(vardecl);
  33
  34            Assert.NotSame(symbol1, symbol2);
  35            Assert.Equal(symbol1, symbol2);
  36        }
  37
  38        [Fact]
  39        public void LocalSymbolsAreDifferentArossSemanticModelsFromDifferentCompilations()
  40        {
  41            var text = @"public class C { public void M() { int x = 10; } }";
  42            var tree = Parse(text);
  43            var comp1 = CreateCompilationWithMscorlib(tree);
  44            var comp2 = CreateCompilationWithMscorlib(tree);
  45
  46            var model1 = comp1.GetSemanticModel(tree);
  47            var model2 = comp2.GetSemanticModel(tree);
  48            Assert.NotEqual(model1, model2);
  49
  50            var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
  51            var symbol1 = model1.GetDeclaredSymbol(vardecl);
  52            var symbol2 = model2.GetDeclaredSymbol(vardecl);
  53
  54            Assert.NotSame(symbol1, symbol2);
  55            Assert.NotEqual(symbol1, symbol2);
  56        }
  57
  58        [Fact]
  59        public void RangeVariableSymbolsAreEquivalentAcrossSemanticModelsFromTheSameCompilation()
  60        {
  61            var text = @"using System.Linq; public class C { public void M() { var q = from c in string.Empty select c; } }";
  62            var tree = Parse(text);
  63            var comp = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
  64
  65            var model1 = comp.GetSemanticModel(tree);
  66            var model2 = comp.GetSemanticModel(tree);
  67            Assert.NotEqual(model1, model2);
  68
  69            var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryClauseSyntax>().First();
  70            var symbol1 = model1.GetDeclaredSymbol(vardecl);
  71            var symbol2 = model2.GetDeclaredSymbol(vardecl);
  72
  73            Assert.NotSame(symbol1, symbol2);
  74            Assert.Equal(symbol1, symbol2);
  75        }
  76
  77        [Fact]
  78        public void RangeVariableSymbolsAreDifferentArossSemanticModelsFromDifferentCompilations()
  79        {
  80            var text = @"using System.Linq; public class C { public void M() { var q = from c in string.Empty select c; } }";
  81            var tree = Parse(text);
  82            var comp1 = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
  83            var comp2 = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
  84
  85            var model1 = comp1.GetSemanticModel(tree);
  86            var model2 = comp2.GetSemanticModel(tree);
  87            Assert.NotEqual(model1, model2);
  88
  89            var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryClauseSyntax>().First();
  90            var symbol1 = model1.GetDeclaredSymbol(vardecl);
  91            var symbol2 = model2.GetDeclaredSymbol(vardecl);
  92
  93            Assert.NotSame(symbol1, symbol2);
  94            Assert.NotEqual(symbol1, symbol2);
  95        }
  96
  97        [Fact]
  98        public void LabelSymbolsAreEquivalentAcrossSemanticModelsFromSameCompilation()
  99        {
 100            var text = @"public class C { public void M() { label: goto label; } }";
 101            var tree = Parse(text);
 102            var comp = CreateCompilationWithMscorlib(tree);
 103
 104            var model1 = comp.GetSemanticModel(tree);
 105            var model2 = comp.GetSemanticModel(tree);
 106            Assert.NotEqual(model1, model2);
 107
 108            var statement = tree.GetCompilationUnitRoot().DescendantNodes().OfType<GotoStatementSyntax>().First();
 109            var symbol1 = model1.GetSymbolInfo(statement.Expression).Symbol;
 110            var symbol2 = model2.GetSymbolInfo(statement.Expression).Symbol;
 111
 112            Assert.Equal(false, ReferenceEquals(symbol1, symbol2));
 113            Assert.Equal(symbol1, symbol2);
 114        }
 115
 116        [Fact]
 117        public void LambdaParameterSymbolsAreEquivalentAcrossSemanticModelsFromSameCompilation()
 118        {
 119            var text = @"using System; public class C { public void M() { Func<int,int> f = (p) => p; } }";
 120            var tree = Parse(text);
 121            var comp = CreateCompilationWithMscorlib(tree);
 122
 123            var model1 = comp.GetSemanticModel(tree);
 124            var model2 = comp.GetSemanticModel(tree);
 125            Assert.NotEqual(model1, model2);
 126
 127            var paramdecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 128            var symbol1 = model1.GetDeclaredSymbol(paramdecl);
 129            var symbol2 = model2.GetDeclaredSymbol(paramdecl);
 130
 131            Assert.NotSame(symbol1, symbol2);
 132            Assert.Equal(symbol1.ContainingSymbol, symbol2.ContainingSymbol);
 133            Assert.Equal(symbol1, symbol2);
 134        }
 135
 136        [Fact]
 137        public void LambdaParameterSymbolsAreDifferentAcrossSemanticModelsFromDifferentCompilations()
 138        {
 139            var text = @"using System; public class C { public void M() { Func<int,int> f = (p) => p; } }";
 140            var tree1 = Parse(text);
 141            var tree2 = Parse(text);
 142            var comp1 = CreateCompilationWithMscorlib(tree1);
 143            var comp2 = CreateCompilationWithMscorlib(tree2);
 144
 145            var model1 = comp1.GetSemanticModel(tree1);
 146            var model2 = comp2.GetSemanticModel(tree2);
 147            Assert.NotEqual(model1, model2);
 148
 149            var paramdecl1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 150            var symbol1 = model1.GetDeclaredSymbol(paramdecl1);
 151            var paramdecl2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 152            var symbol2 = model2.GetDeclaredSymbol(paramdecl2);
 153
 154            Assert.NotSame(symbol1, symbol2);
 155            Assert.NotEqual(symbol1.ContainingSymbol, symbol2.ContainingSymbol);
 156            Assert.NotEqual(symbol1, symbol2);
 157        }
 158
 159        [WorkItem(539740, "DevDiv")]
 160        [Fact]
 161        public void NamespaceWithoutName()
 162        {
 163            var text = "namespace";
 164            var tree = Parse(text);
 165            var comp = CreateCompilationWithMscorlib(tree);
 166            var model = comp.GetSemanticModel(tree);
 167            var errors = comp.GetDiagnostics().ToArray();
 168            Assert.Equal(3, errors.Length);
 169
 170            var nsArray = tree.GetCompilationUnitRoot().DescendantNodes().Where(node => node.IsKind(SyntaxKind.NamespaceDeclaration)).ToArray();
 171            Assert.Equal(1, nsArray.Length);
 172
 173            var nsSyntax = nsArray[0] as NamespaceDeclarationSyntax;
 174            var symbol = model.GetDeclaredSymbol(nsSyntax);
 175            Assert.Equal(string.Empty, symbol.Name);
 176        }
 177
 178        [Fact]
 179        public void LazyBoundUsings1()
 180        {
 181            var text =
 182@"
 183// Peter Golde[7/19/2010]: I managed to construct the following interesting example today,
 184// which Dev10 does compile. Interestingly, the resolution of one “using” can depend
 185// on the resolution of another “using” later in the same namespace.
 186using K = A.Q;
 187using L = B.R;
 188 
 189class B
 190{
 191    public class R
 192    {
 193        public class Q
 194        {
 195            public class S : K { }
 196        }
 197    }
 198}
 199 
 200class A : L
 201{
 202    public K.S v = null;
 203}
 204";
 205            var comp = CreateCompilationWithMscorlib(text);
 206            var global = comp.GlobalNamespace;
 207            var a = global.GetTypeMembers("A", 0).Single();
 208            var abase = a.BaseType;
 209            Assert.Equal("B.R", abase.ToTestDisplayString());
 210
 211            var b = global.GetTypeMembers("B", 0).Single();
 212            var r = b.GetTypeMembers("R", 0).Single();
 213            var q = r.GetTypeMembers("Q", 0).Single();
 214            var v = a.GetMembers("v").Single() as FieldSymbol;
 215            var s = v.Type;
 216            Assert.Equal("B.R.Q.S", s.ToTestDisplayString());
 217            var sbase = s.BaseType;
 218            Assert.Equal("B.R.Q", sbase.ToTestDisplayString());
 219        }
 220
 221        [Fact]
 222        public void Diagnostics1()
 223        {
 224            var text =
 225@"
 226class A : A {}
 227";
 228            var tree = Parse(text);
 229            var comp = CreateCompilationWithMscorlib(tree);
 230
 231            var errs = comp.GetSemanticModel(tree).GetDeclarationDiagnostics();
 232            Assert.Equal(1, errs.Count());
 233        }
 234
 235        [Fact]
 236        public void DiagnosticsInOneTree()
 237        {
 238            var partial1 =
 239@"
 240partial class A 
 241{ 
 242    void foo() { int x = y; }
 243}
 244
 245class C : B {}
 246";
 247
 248            var partial2 =
 249@"
 250partial class A 
 251{ 
 252    int q;      //an unused field in a partial type
 253    void bar() { int x = z; }
 254}
 255
 256class B : NonExistent {}
 257";
 258
 259            var partial1Tree = Parse(partial1);
 260            var partial2Tree = Parse(partial2);
 261            var comp = CreateCompilationWithMscorlib(new SyntaxTree[] { partial1Tree, partial2Tree });
 262
 263            var errs = comp.GetSemanticModel(partial1Tree).GetDiagnostics();
 264            Assert.Equal(1, errs.Count());
 265        }
 266
 267        [Fact]
 268        public void Bindings1()
 269        {
 270            var text =
 271@"
 272class B : A {}
 273class A {}
 274";
 275            var tree = Parse(text);
 276            var comp = CreateCompilationWithMscorlib(tree);
 277
 278            var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 279            var bbase = bdecl.BaseList.Types[0] as TypeSyntax;
 280            var model = comp.GetSemanticModel(tree);
 281
 282            var info = model.GetSymbolInfo(bbase);
 283            Assert.NotNull(info.Symbol);
 284            var a = comp.GlobalNamespace.GetTypeMembers("A", 0).Single();
 285            Assert.Equal(a, info.Symbol);
 286            Assert.Equal(a, model.GetTypeInfo(bbase).Type);
 287        }
 288
 289        [Fact]
 290        public void BaseScope1()
 291        {
 292            // ensure the base clause is not bound in the scope of the class
 293            var text =
 294@"
 295public class C : B {}
 296public class A {
 297    public class B {}
 298}
 299public class B : A {}
 300";
 301            var tree = Parse(text);
 302            var comp = CreateCompilationWithMscorlib(tree);
 303
 304            var cdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 305            var cbase = cdecl.BaseList.Types[0] as TypeSyntax;
 306            var model = comp.GetSemanticModel(tree);
 307
 308            var info = model.GetSymbolInfo(cbase);
 309            Assert.NotNull(info.Symbol);
 310            var b = comp.GlobalNamespace.GetTypeMembers("B", 0).Single();
 311            Assert.Equal(b, info.Symbol);
 312            Assert.Equal(b, model.GetTypeInfo(cbase).Type);
 313        }
 314
 315        [Fact]
 316        public void BaseScope2()
 317        {
 318            // ensure type parameters are in scope in the base clause
 319            var text =
 320@"
 321public class C<T> : A<T> { }
 322public class A<T> : B { }
 323public class B {
 324    public class T { }
 325}
 326";
 327            var tree = Parse(text);
 328            var comp = CreateCompilationWithMscorlib(tree);
 329
 330            var cdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 331            var cbase = cdecl.BaseList.Types[0] as TypeSyntax;
 332            var model = comp.GetSemanticModel(tree);
 333
 334            var info = model.GetSymbolInfo(cbase);
 335            Assert.NotNull(info.Symbol);
 336            var cbasetype = info.Symbol as NamedTypeSymbol;
 337
 338            var c = comp.GlobalNamespace.GetTypeMembers("C", 1).Single();
 339            Assert.Equal(c.BaseType, cbasetype);
 340        }
 341
 342        [Fact]
 343        public void Bindings2()
 344        {
 345            var text =
 346@"
 347class B<T> : A<T> {}
 348class A<T> {}
 349";
 350            var tree = Parse(text);
 351            var comp = CreateCompilationWithMscorlib(tree);
 352
 353            var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 354            var bbase = bdecl.BaseList.Types[0] as TypeSyntax; // A<T>
 355            var model = comp.GetSemanticModel(tree);
 356
 357            var info = model.GetSymbolInfo(bbase);
 358            Assert.NotNull(info.Symbol);
 359            var at2 = info.Symbol as NamedTypeSymbol;
 360            Assert.Equal(at2, model.GetTypeInfo(bbase).Type);
 361
 362            var a = comp.GlobalNamespace.GetTypeMembers("A", 1).Single();
 363            var at = a.TypeParameters.First();
 364            var b = comp.GlobalNamespace.GetTypeMembers("B", 1).Single();
 365            var bt = b.TypeParameters.First();
 366
 367            Assert.Equal(a.OriginalDefinition, at2.OriginalDefinition);
 368            Assert.Equal(b.TypeParameters.First(), at2.TypeArguments.First());
 369        }
 370
 371        [Fact]
 372        public void Bindings3()
 373        {
 374            var text =
 375@"using System;
 376using System.Collections.Generic;
 377using System.Linq;
 378
 379class Program
 380{
 381    static Program Field;
 382}";
 383            var tree1 = Parse(text);
 384            var compilation = CreateCompilationWithMscorlib(tree1);
 385
 386            var tree2 = Parse(text);
 387            var classProgram = tree2.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 388            var staticProgramField = classProgram.Members[0] as FieldDeclarationSyntax;
 389            var program = staticProgramField.Declaration.Type;
 390            var model = compilation.GetSemanticModel(tree1);
 391
 392            Assert.Throws<ArgumentException>(() =>
 393            {
 394                // tree2 not in the compilation
 395                var lookup = model.GetSymbolInfo(program);
 396            });
 397        }
 398
 399        [Fact]
 400        public void Bindings4()
 401        {
 402            var text =
 403@"using System;
 404using System.Collections.Generic;
 405using System.Linq;
 406
 407class Program
 408{
 409    Program p;
 410    static void Main(string[] args)
 411    {
 412    }
 413}";
 414
 415            var tree1 = Parse(text);
 416            var compilation = CreateCompilationWithMscorlib(tree1);
 417
 418            var decl = tree1.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 419            var field = decl.Members[0] as FieldDeclarationSyntax;
 420            var type = field.Declaration.Type;
 421            var model = compilation.GetSemanticModel(tree1);
 422
 423            var info = model.GetSymbolInfo(type);
 424            Assert.Equal(compilation.GlobalNamespace.GetTypeMembers("Program", 0).Single(), info.Symbol);
 425        }
 426
 427        [Fact]
 428        public void Bindings5()
 429        {
 430            var text =
 431@"using System;
 432using System.Collections.Generic;
 433using System.Linq;
 434
 435class Program
 436{
 437    static void Main(string[] args)
 438    {
 439    }
 440}";
 441
 442            var tree1 = Parse(text);
 443            var tree2 = Parse(text);
 444            var compilation = CreateCompilationWithMscorlib(new SyntaxTree[] { tree1, tree2 });
 445
 446            var decl = tree1.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 447            var method = decl.Members[0] as MethodDeclarationSyntax;
 448            var type = method.ParameterList.Parameters[0].Type;
 449
 450            var model = compilation.GetSemanticModel(tree1);
 451
 452            var info = model.GetSymbolInfo(type);
 453            Assert.Equal<Symbol>(compilation.GetSpecialType(SpecialType.System_String), (info.Symbol as ArrayTypeSymbol).ElementType);
 454        }
 455
 456        [Fact]
 457        public void Speculative1()
 458        {
 459            var text =
 460@"
 461class B {
 462    object x;
 463}
 464class A {}
 465";
 466            var tree = Parse(text);
 467            var comp = CreateCompilationWithMscorlib(tree);
 468
 469            var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 470            var xdecl = bdecl.Members[0] as FieldDeclarationSyntax;
 471
 472            var model = comp.GetSemanticModel(tree);
 473
 474            TypeSyntax speculate = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier("A"));
 475            var symbolInfo = model.GetSpeculativeSymbolInfo(xdecl.SpanStart, speculate, SpeculativeBindingOption.BindAsTypeOrNamespace);
 476            var lookup = symbolInfo.Symbol as TypeSymbol;
 477
 478
 479            Assert.NotNull(lookup);
 480            var a = comp.GlobalNamespace.GetTypeMembers("A", 0).Single();
 481            Assert.Equal(a, lookup);
 482        }
 483
 484        [Fact]
 485        public void GetType1()
 486        {
 487            var text =
 488@"
 489class A {
 490    class B {}
 491}
 492";
 493            var tree = Parse(text);
 494            var comp = CreateCompilationWithMscorlib(tree);
 495
 496            var adecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
 497            var bdecl = adecl.Members[0] as TypeDeclarationSyntax;
 498
 499            var model = comp.GetSemanticModel(tree);
 500            var a1 = model.GetDeclaredSymbol(adecl);
 501            var b1 = model.GetDeclaredSymbol(bdecl);
 502
 503            var global = comp.GlobalNamespace;
 504            var a2 = global.GetTypeMembers("A", 0).Single();
 505            var b2 = a2.GetTypeMembers("B", 0).Single();
 506
 507            Assert.Equal(a2, a1);
 508            Assert.Equal(b2, b1);
 509        }
 510
 511        [Fact]
 512        public void DottedName()
 513        {
 514            var text =
 515@"
 516class Main {
 517  A.B x; // this refers to the B within A.
 518}
 519class A {
 520    public class B {}
 521}
 522class B {}
 523";
 524            var tree = Parse(text);
 525            var comp = CreateCompilationWithMscorlib(tree);
 526            var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
 527
 528            var root = tree.GetCompilationUnitRoot();
 529            var mainDecl = root.Members[0] as TypeDeclarationSyntax;
 530            var mainType = model.GetDeclaredSymbol(mainDecl);
 531
 532            var aDecl = root.Members[1] as TypeDeclarationSyntax;
 533            var aType = model.GetDeclaredSymbol(aDecl);
 534
 535            var abDecl = aDecl.Members[0] as TypeDeclarationSyntax;
 536            var abType = model.GetDeclaredSymbol(abDecl);
 537
 538            var bDecl = root.Members[2] as TypeDeclarationSyntax;
 539            var bType = model.GetDeclaredSymbol(bDecl);
 540
 541            var xDecl = mainDecl.Members[0] as FieldDeclarationSyntax;
 542            var xSym = mainType.GetMembers("x").Single() as FieldSymbol;
 543            Assert.Equal<ISymbol>(abType, xSym.Type);
 544            var info = model.GetSymbolInfo((xDecl.Declaration.Type as QualifiedNameSyntax).Right);
 545            Assert.Equal(abType, info.Symbol);
 546        }
 547
 548        [Fact]
 549        public void AliasQualifiedName()
 550        {
 551            var text =
 552@"
 553class B {}
 554namespace N {
 555  class C : global::B {}
 556  class B {}
 557}
 558";
 559            var tree = Parse(text);
 560            var comp = CreateCompilationWithMscorlib(tree);
 561            var model = comp.GetSemanticModel(tree);
 562
 563            var root = tree.GetCompilationUnitRoot();
 564            var nDecl = root.Members[0] as NamespaceDeclarationSyntax;
 565            var n2Decl = root.Members[1] as NamespaceDeclarationSyntax;
 566            var cDecl = n2Decl.Members[0] as TypeDeclarationSyntax;
 567            var cBase = (cDecl.BaseList.Types[0] as AliasQualifiedNameSyntax).Name;
 568
 569            var cBaseType = model.GetSymbolInfo(cBase).Symbol;
 570            var bOuter = comp.GlobalNamespace.GetTypeMembers("B", 0).Single();
 571            var bInner = (comp.GlobalNamespace.GetMembers("N").Single() as NamespaceSymbol).GetTypeMembers("B", 0).Single();
 572            Assert.Equal(bOuter, cBaseType);
 573        }
 574
 575        [Fact, WorkItem(528655, "DevDiv")]
 576        public void ErrorSymbolForInvalidCode()
 577        {
 578            var text = @"
 579public class A 
 580{
 581	int foo	{	void foo() {}	} // Error
 582	static int Main() {	return 1;    }
 583}
 584";
 585            var tree = Parse(text);
 586            var comp = CreateCompilationWithMscorlib(tree);
 587            var mems = comp.SourceModule.GlobalNamespace.GetMembers();
 588
 589            var typeA = mems.Where(s => s.Name == "A").Select(s => s);
 590            Assert.Equal(1, typeA.Count());
 591            var invalid = mems.Where(s => s.Name == "<invalid-global-code>").Select(s => s);
 592            Assert.Equal(1, invalid.Count());
 593        }
 594
 595        [Fact, WorkItem(543225, "DevDiv"), WorkItem(529057, "DevDiv")]
 596        public void MergePartialMethodAndParameterSymbols()
 597        {
 598            var text = @"
 599using System;
 600
 601partial class PC
 602{
 603    partial void PM(int pp);
 604}
 605
 606partial class PC
 607{
 608    partial void PM(int pp) {}
 609}
 610";
 611            var tree = Parse(text);
 612            var comp = CreateCompilationWithMscorlib(tree);
 613            var pTypeSym = comp.SourceModule.GlobalNamespace.GetTypeMembers("PC").Single();
 614            var pMethodSym = pTypeSym.GetMembers("PM").Single();
 615
 616            var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
 617            var pType01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();
 618            var pType02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Last();
 619            Assert.NotEqual(pType01, pType02);
 620            var ptSym01 = model.GetDeclaredSymbol(pType01);
 621            var ptSym02 = model.GetDeclaredSymbol(pType02);
 622            // same partial type symbol
 623            Assert.Same(ptSym01, ptSym02);
 624            Assert.Equal(2, ptSym01.Locations.Length);
 625
 626            var pMethod01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First();
 627            var pMethod02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Last();
 628            Assert.NotEqual(pMethod01, pMethod02);
 629
 630            var pmSym01 = model.GetDeclaredSymbol(pMethod01);
 631            var pmSym02 = model.GetDeclaredSymbol(pMethod02);
 632            // different partial method symbols:(
 633            Assert.NotSame(pmSym01, pmSym02);
 634            // the declaration one is what one can get from GetMembers()
 635            Assert.Same(pMethodSym, pmSym01);
 636
 637            // with decl|impl point to each nother
 638            Assert.Null(pmSym01.PartialDefinitionPart);
 639            Assert.Same(pmSym02, pmSym01.PartialImplementationPart);
 640
 641            Assert.Same(pmSym01, pmSym02.PartialDefinitionPart);
 642            Assert.Null(pmSym02.PartialImplementationPart);
 643
 644            var pParam01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 645            var pParam02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().Last();
 646            Assert.NotEqual(pParam01, pParam02);
 647
 648            var ppSym01 = model.GetDeclaredSymbol(pParam01);
 649            var ppSym02 = model.GetDeclaredSymbol(pParam02);
 650            Assert.NotSame(ppSym01, ppSym02);
 651            Assert.Equal(1, ppSym01.Locations.Length);
 652            Assert.Equal(1, ppSym02.Locations.Length);
 653        }
 654
 655        [Fact, WorkItem(544221, "DevDiv")]
 656        public void GetTypeInfoForOptionalParameterDefaultValueInDelegate()
 657        {
 658            var text = @"
 659using System;
 660
 661class Test
 662{
 663    public delegate void DFoo(byte i = 1);
 664    protected internal void MFoo(sbyte j = 2) { }
 665}
 666";
 667            var tree = Parse(text);
 668            var comp = CreateCompilationWithMscorlib(tree);
 669            var model = comp.GetSemanticModel(tree);
 670            var root = tree.GetCompilationUnitRoot();
 671            var exprs = root.DescendantNodes().OfType<LiteralExpressionSyntax>().ToArray();
 672            Assert.Equal(2, exprs.Length);
 673
 674            var type1 = model.GetTypeInfo(exprs[0]);
 675            var type2 = model.GetTypeInfo(exprs[1]);
 676
 677            Assert.NotNull(type1.Type);
 678            Assert.Equal("System.Int32", type1.Type.ToTestDisplayString());
 679            Assert.NotNull(type2.Type);
 680            Assert.Equal("System.Int32", type2.Type.ToTestDisplayString());
 681        }
 682
 683        [Fact, WorkItem(544231, "DevDiv")]
 684        public void GetDeclSymbolForParameterOfPartialMethod()
 685        {
 686            var text1 = @"
 687using System;
 688
 689partial class Partial001
 690{
 691    static partial void Foo(ulong x);
 692}
 693";
 694
 695            var text2 = @"
 696using System;
 697
 698partial class Partial001
 699{
 700    static partial void Foo(ulong x)  {    }
 701    static int Main()  {    return 1;    }
 702}
 703";
 704            var tree1 = Parse(text1);
 705            var tree2 = Parse(text2);
 706            var comp = CreateCompilationWithMscorlib(new List<SyntaxTree> {tree1, tree2});
 707
 708            var model1 = comp.GetSemanticModel(tree1);
 709            var model2 = comp.GetSemanticModel(tree2);
 710            var root1 = tree1.GetCompilationUnitRoot();
 711            var root2 = tree1.GetCompilationUnitRoot();
 712            var para1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 713            var para2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
 714            var sym1 = model1.GetDeclaredSymbol(para1);
 715            var sym2 = model2.GetDeclaredSymbol(para2);
 716
 717            Assert.NotNull(sym1);
 718            Assert.NotNull(sym2);
 719            Assert.Equal("System.UInt64 x", sym1.ToTestDisplayString());
 720            Assert.Equal("System.UInt64 x", sym2.ToTestDisplayString());
 721            Assert.NotEqual(sym1.Locations[0], sym2.Locations[0]);
 722        }
 723
 724        [Fact, WorkItem(544473, "DevDiv")]
 725        public void GetDeclSymbolForTypeParameterOfPartialMethod()
 726        {
 727            var text1 = @"
 728using System;
 729
 730partial class Partial001
 731{
 732    static partial void Foo<T>(T x);
 733}
 734";
 735
 736            var text2 = @"
 737using System;
 738
 739partial class Partial001
 740{
 741    static partial void Foo<T>(T x)  {    }
 742    static int Main()  {    return 1;    }
 743}
 744";
 745            var tree1 = Parse(text1);
 746            var tree2 = Parse(text2);
 747            var comp = CreateCompilationWithMscorlib(new List<SyntaxTree> { tree1, tree2 });
 748
 749            var model1 = comp.GetSemanticModel(tree1);
 750            var model2 = comp.GetSemanticModel(tree2);
 751            var root1 = tree1.GetCompilationUnitRoot();
 752            var root2 = tree1.GetCompilationUnitRoot();
 753            var para1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<TypeParameterSyntax>().First();
 754            var para2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<TypeParameterSyntax>().First();
 755            var sym1 = model1.GetDeclaredSymbol(para1);
 756            var sym2 = model2.GetDeclaredSymbol(para2);
 757
 758            Assert.NotNull(sym1);
 759            Assert.NotNull(sym2);
 760            Assert.Equal("T", sym1.ToTestDisplayString());
 761            Assert.Equal("T", sym2.ToTestDisplayString());
 762            Assert.NotEqual(sym1.Locations[0], sym2.Locations[0]);
 763        }
 764
 765        [Fact]
 766        public void GetDeclaredSymbolForAnonymousTypeProperty01()
 767        {
 768            var text = @"
 769using System;
 770
 771struct AnonTypeTest
 772{
 773    static long Prop
 774    {
 775        get
 776        {
 777            short @short = -1;
 778            var anonType = new { id = 123, @do = ""QC"", @short, Prop };
 779            return anonType.id + anonType.@short;
 780        }
 781    }
 782}
 783";
 784            var tree = Parse(text);
 785            var comp = CreateCompilationWithMscorlib(tree);
 786            var model = comp.GetSemanticModel(tree);
 787            var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
 788            Assert.Equal(4, anonProps.Count());
 789            var symList = from ap in anonProps
 790                          let apsym = model.GetDeclaredSymbol(ap)
 791                          orderby apsym.Name
 792                          select apsym.Name;
 793
 794            var results = string.Join(", ", symList);
 795            Assert.Equal("do, id, Prop, short", results);
 796        }
 797
 798        [Fact]
 799        public void GetDeclaredSymbolForAnonymousTypeProperty02()
 800        {
 801            var text = @"
 802using System;
 803
 804class AnonTypeTest
 805{
 806    long field = 111;
 807    void M(byte p1, ref sbyte p2, out string p3, params string[] ary)
 808    {
 809        ulong local = 12345;
 810        var anonType = new { local, this.field, p1, p2, ary };
 811        p3 = anonType.ary.Length > 0 ? anonType.ary[0] : """";
 812    }
 813}
 814";
 815            var tree = Parse(text);
 816            var comp = CreateCompilationWithMscorlib(tree);
 817            var model = comp.GetSemanticModel(tree);
 818            var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
 819            Assert.Equal(5, anonProps.Count());
 820            var symList = from ap in anonProps
 821                          let apsym = model.GetDeclaredSymbol(ap)
 822                          orderby apsym.Name
 823                          select apsym.Name;
 824
 825            var results = string.Join(", ", symList);
 826            Assert.Equal("ary, field, local, p1, p2", results);
 827        }
 828
 829        [Fact]
 830        public void GetDeclaredSymbolForAnonymousTypeProperty03()
 831        {
 832            var text = @"
 833using System;
 834
 835enum E { a, b, c }
 836class Base
 837{
 838    protected E baseField = E.b;
 839    protected virtual Base BaseProp { get { return this; } }
 840    public Func<string, char> deleField;
 841}
 842
 843class AnonTypeTest : Base
 844{
 845    protected override Base BaseProp { get { return null; } }
 846    char this[string @string]
 847    {
 848        get
 849        {
 850            var anonType = new { id = deleField, base.BaseProp, base.baseField, ret = @string };
 851            return anonType.id(anonType.ret);
 852        }
 853    }
 854}
 855";
 856            var tree = Parse(text);
 857            var comp = CreateCompilationWithMscorlib(tree);
 858            var model = comp.GetSemanticModel(tree);
 859            var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
 860            Assert.Equal(4, anonProps.Count());
 861            var symList = from ap in anonProps
 862                          let apsym = model.GetDeclaredSymbol(ap)
 863                          orderby apsym.Name
 864                          select apsym.Name;
 865
 866            var results = string.Join(", ", symList);
 867            Assert.Equal("baseField, BaseProp, id, ret", results);
 868        }
 869
 870        [Fact]
 871        public void GetDeclaredSymbolForAnonymousTypeProperty04()
 872        {
 873            var text = @"
 874using System;
 875
 876enum E { a, b, c }
 877struct S
 878{
 879    public static E sField;
 880    public interface IFoo {  }
 881    public IFoo GetFoo { get; set; }
 882    public IFoo GetFoo2() { return null; }
 883}
 884
 885class AnonTypeTest
 886{
 887    event Action<ushort> Eve
 888    {
 889        add
 890        {
 891            var anonType = new { a1 = new { S.sField, ifoo = new { new S().GetFoo } } };
 892        }
 893        remove 
 894        {
 895            var anonType = new { a1 = new { a2 = new { a2 = S.sField, a3 = new { a3 = new S().GetFoo2() } } } };
 896        }
 897    }
 898}
 899";
 900            var tree = Parse(text);
 901            var comp = CreateCompilationWithMscorlib(tree);
 902            var model = comp.GetSemanticModel(tree);
 903            var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
 904            Assert.Equal(9, anonProps.Count());
 905            var symList = from ap in anonProps
 906                          let apsym = model.GetDeclaredSymbol(ap)
 907                          orderby apsym.Name
 908                          select apsym.Name;
 909
 910            var results = string.Join(", ", symList);
 911            Assert.Equal("a1, a1, a2, a2, a3, a3, GetFoo, ifoo, sField", results);
 912        }
 913
 914        [Fact(), WorkItem(542861, "DevDiv"), WorkItem(529673, "DevDiv")]
 915        public void GetSymbolInfoForAccessorParameters()
 916        {
 917            var text = @"
 918using System;
 919
 920public class Test
 921{
 922    object[] _Items = new object[3];
 923    public object this[int index]
 924    {
 925        get
 926        {
 927            return _Items[index];
 928        }
 929        set
 930        {
 931            _Items[index] = value;
 932        }
 933    } 
 934}
 935";
 936            var tree = Parse(text);
 937            var comp = CreateCompilationWithMscorlib(tree);
 938            var model = comp.GetSemanticModel(tree);
 939            var descendants = tree.GetCompilationUnitRoot().DescendantNodes();
 940
 941            var paras = descendants.OfType<ParameterSyntax>();
 942            Assert.Equal(1, paras.Count());
 943            var parasym = model.GetDeclaredSymbol(paras.First());
 944            var ploc = parasym.Locations[0];
 945            
 946            var args = descendants.OfType<ArgumentSyntax>().Where(s => s.ToString() == "index").Select(s => s);
 947            Assert.Equal(2, args.Count());
 948            var argsym1 = model.GetSymbolInfo(args.First().Expression).Symbol;
 949            var argsym2 = model.GetSymbolInfo(args.Last().Expression).Symbol;
 950            Assert.NotNull(argsym1);
 951            Assert.NotNull(argsym2);
 952
 953            Assert.Equal(ploc, argsym1.Locations[0]);
 954            Assert.Equal(ploc, argsym2.Locations[0]);
 955
 956            Assert.Equal(parasym.Kind, argsym1.Kind);
 957            Assert.Equal(parasym.Kind, argsym2.Kind);
 958
 959            // SourceSimpleParameterSymbol vs. SourceClonedComplexParameterSymbol
 960            Assert.NotEqual(parasym, argsym1);
 961            Assert.NotEqual(parasym, argsym2);
 962        }
 963
 964        [WorkItem(545648, "DevDiv")]
 965        [Fact]
 966        public void AliasDeclaredSymbolWithConflict()
 967        {
 968            var source = @"
 969using X = System;
 970 
 971class X { }
 972";
 973
 974            var comp = CreateCompilationWithMscorlib(source);
 975            var tree = comp.SyntaxTrees.Single();
 976            var model = comp.GetSemanticModel(tree);
 977
 978            var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
 979
 980            var symbol = model.GetDeclaredSymbol(aliasSyntax);
 981            Assert.Equal(symbol.Target, comp.GlobalNamespace.GetMember<NamespaceSymbol>("System"));
 982
 983            comp.VerifyDiagnostics(
 984                // (2,1): info CS8019: Unnecessary using directive.
 985                // using X = System;
 986                Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using X = System;"));
 987        }
 988
 989        [WorkItem(529751, "DevDiv")]
 990        [Fact]
 991        public void ExternAlias()
 992        {
 993            var source = @"
 994extern alias X;
 995
 996class Test
 997{
 998    static void Main()
 999    {
1000        X::C c = null;
1001    }
1002}
1003";
1004            var comp1 = CreateCompilationWithMscorlib("public class C { }");
1005            var ref1 = new MetadataImageReference(CompileAndVerify(comp1).EmittedAssemblyData, aliases: ImmutableArray.Create("X"));
1006
1007            var comp2 = CreateCompilationWithMscorlib(source, new[] { ref1 });
1008            var tree = comp2.SyntaxTrees.Single();
1009            var model = comp2.GetSemanticModel(tree);
1010
1011            var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ExternAliasDirectiveSyntax>().Single();
1012
1013            // Compilation.GetExternAliasTarget defines this behavior: the target is a merged namespace
1014            // with the same name as the alias, contained in the global namespace of the compilation.
1015            var aliasSymbol = model.GetDeclaredSymbol(aliasSyntax);
1016            var aliasTarget = (NamespaceSymbol)aliasSymbol.Target;
1017            Assert.Equal(NamespaceKind.Module, aliasTarget.Extent.Kind);
1018            Assert.Equal("", aliasTarget.Name);
1019            Assert.True(aliasTarget.IsGlobalNamespace);
1020            Assert.Null(aliasTarget.ContainingNamespace);
1021
1022            Assert.Equal(0, comp2.GlobalNamespace.GetMembers("X").Length); //Doesn't contain the alias target namespace as a child.
1023
1024            var aliasQualifiedSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AliasQualifiedNameSyntax>().Single();
1025            Assert.Equal(aliasSymbol, model.GetAliasInfo(aliasQualifiedSyntax.Alias));
1026
1027            comp2.VerifyDiagnostics(
1028                // (8,14): warning CS0219: The variable 'c' is assigned but its value is never used
1029                //         X::C c = null;
1030                Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "c").WithArguments("c"));
1031        }
1032
1033        [Fact, WorkItem(546687, "DevDiv"), WorkItem(529751, "DevDiv")]
1034        public void ExternAliasWithoutTarget()
1035        {
1036            var source = @"
1037extern alias X;
1038
1039class Test
1040{
1041    static void Main()
1042    {
1043        X::C c = null;
1044    }
1045}
1046";
1047
1048            var comp = CreateCompilationWithMscorlib(source);
1049            var tree = comp.SyntaxTrees.Single();
1050            var model = comp.GetSemanticModel(tree);
1051
1052            var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ExternAliasDirectiveSyntax>().Single();
1053
1054            var aliasSymbol = model.GetDeclaredSymbol(aliasSyntax);
1055            Assert.IsType<MissingNamespaceSymbol>(aliasSymbol.Target);
1056
1057            var aliasQualifiedSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AliasQualifiedNameSyntax>().Single();
1058            Assert.Equal(aliasSymbol, model.GetAliasInfo(aliasQualifiedSyntax.Alias));
1059
1060            comp.VerifyDiagnostics(
1061                // (2,14): error CS0430: The extern alias 'X' was not specified in a /reference option
1062                // extern alias X;
1063                Diagnostic(ErrorCode.ERR_BadExternAlias, "X").WithArguments("X"),
1064                // (8,12): error CS0234: The type or namespace name 'C' does not exist in the namespace 'X' (are you missing an assembly reference?)
1065                //         X::C c = null;
1066                Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "C").WithArguments("C", "X"));
1067        }
1068
1069        [WorkItem(545648, "DevDiv")]
1070        [Fact]
1071        public void UsingDirectiveAliasSemanticInfo()
1072        {
1073            var source = "using X = System;";
1074
1075            var comp = CreateCompilationWithMscorlib(source);
1076            comp.VerifyDiagnostics(
1077                // (1,1): info CS8019: Unnecessary using directive.
1078                // using X = System;
1079                Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using X = System;"));
1080
1081            var tree = comp.SyntaxTrees.Single();
1082            var model = comp.GetSemanticModel(tree);
1083
1084            var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<NameEqualsSyntax>().Single().Name;
1085            Assert.Equal(SymbolInfo.None, model.GetSymbolInfo(aliasSyntax));
1086
1087            var usingSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
1088            Assert.Equal(SymbolKind.Alias, model.GetDeclaredSymbol(usingSyntax).Kind);
1089        }
1090
1091        [WorkItem(545882, "DevDiv")]
1092        [Fact]
1093        public void SpeculativelyBindConstructorInitializerInPlaceOfActual()
1094        {
1095            var source = @"class C
1096{
1097    C(int x) { }
1098    C() : this((int) 1) { }
1099}";
1100
1101            var comp = CreateCompilationWithMscorlib(source);
1102            comp.VerifyDiagnostics();
1103            
1104            var tree = comp.SyntaxTrees.Single();
1105            var model = comp.GetSemanticModel(tree);
1106            
1107            var oldSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ConstructorInitializerSyntax>().Single();
1108
1109            var newSyntax = SyntaxFactory.ConstructorInitializer(SyntaxKind.ThisConstructorInitializer);
1110
1111            var info = model.GetSpeculativeSymbolInfo(oldSyntax.SpanStart, newSyntax);
1112            var symbol = info.Symbol;
1113            Assert.NotNull(symbol);
1114            Assert.Equal(comp.GlobalNamespace.GetMember<NamedTypeSymbol>("C"), symbol.ContainingType);
1115            Assert.Equal(SymbolKind.Method, symbol.Kind);
1116            var method = (MethodSymbol)symbol;
1117            Assert.Equal(MethodKind.Constructor, method.MethodKind);
1118            Assert.Equal(0, method.ParameterCount);
1119        }
1120
1121        [WorkItem(545882, "DevDiv")]
1122        [Fact]
1123        public void SpeculativelyBindConstructorInitializerInNewLocation()
1124        {
1125            var source = @"class C
1126{
1127    C() { }
1128}";
1129
1130            var comp = CreateCompilationWithMscorlib(source);
1131            comp.VerifyDiagnostics();
1132
1133            var tree = comp.SyntaxTrees.Single();
1134            var model = comp.GetSemanticModel(tree);
1135
1136            var oldSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().Single();
1137
1138            var newSyntax = SyntaxFactory.ConstructorInitializer(SyntaxKind.ThisConstructorInitializer);
1139
1140            var info = model.GetSpeculativeSymbolInfo(oldSyntax.ParameterList.Span.End, newSyntax);
1141            Assert.Equal(SymbolInfo.None, info);
1142        }
1143
1144        [Fact]
1145        public void TestGetSpeculativeSemanticModelInFieldInitializer()
1146        {
1147            var compilation = CreateCompilationWithMscorlib(@"
1148class C 
1149{
1150  object y = 1;
1151}
1152");
1153            var tree = compilation.SyntaxTrees[0];
1154            var root = tree.GetCompilationUnitRoot();
1155            var typeDecl = (TypeDeclarationSyntax)root.Members[0];
1156            var fieldDecl = (FieldDeclarationSyntax)typeDecl.Members[0];
1157            var varDecl = fieldDecl.Declaration.Variables.First();
1158
1159            var model = compilation.GetSemanticModel(tree);
1160            Assert.False(model.IsSpeculativeSemanticModel);
1161            Assert.Null(model.ParentModel);
1162            Assert.Equal(0, model.OriginalPositionForSpeculation);
1163
1164            // Speculate on the equals value syntax (initializer)
1165            // Conversion info available, ConvertedType: Object.
1166            var equalsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(@"(string)""Hello"""));
1167            var expr = equalsValue.Value;
1168            int position = varDecl.Initializer.SpanStart;
1169
1170            SemanticModel speculativeModel;
1171            bool success = model.TryGetSpeculativeSemanticModel(position, equalsValue, out speculativeModel);
1172            Assert.True(success);
1173            Assert.NotNull(speculativeModel);
1174            Assert.True(speculativeModel.IsSpeculativeSemanticModel);
1175            Assert.Equal(model, speculativeModel.ParentModel);
1176            Assert.Equal(position, speculativeModel.OriginalPositionForSpeculation);
1177
1178            var typeInfo = speculativeModel.GetTypeInfo(expr);
1179            Assert.NotNull(typeInfo.Type);
1180            Assert.Equal("String", typeInfo.Type.Name);
1181            Assert.Equal("Object", typeInfo.ConvertedType.Name);
1182
1183            var constantInfo = speculativeModel.GetConstantValue(expr);
1184            Assert.True(constantInfo.HasValue, "must be a constant");
1185            Assert.Equal("Hello", constantInfo.Value);
1186        }
1187
1188        [Fact]
1189        public void TestGetSpeculativeSemanticModelInEnumMember()
1190        {
1191            var compilation = CreateCompilationWithMscorlib(@"
1192enum C 
1193{
1194  y = 1
1195}
1196");
1197            var tree = compilation.SyntaxTrees[0];
1198            var root = tree.GetCompilationUnitRoot();
1199            var typeDecl = (EnumDeclarationSyntax)root.Members[0];
1200            var enumMemberDecl = (EnumMemberDeclarationSyntax)typeDecl.Members[0];
1201            var equalsValue = enumMemberDecl.EqualsValue;
1202            var initializer = equalsValue.Value;
1203
1204            var model = compilation.GetSemanticModel(tree);
1205
1206            // Speculate on the equals value syntax (initializer)
1207            // Conversion info available, ConvertedType: Int32.
1208            var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
1209            var expr = newEqualsValue.Value;
1210            SemanticModel speculativeModel;
1211            bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
1212            Assert.True(success);
1213            Assert.NotNull(speculativeModel);
1214
1215            var typeInfo = speculativeModel.GetTypeInfo(expr);
1216            Assert.NotNull(typeInfo.Type);
1217            Assert.Equal("Int16", typeInfo.Type.Name);
1218            Assert.Equal("Int32", typeInfo.ConvertedType.Name);
1219
1220            var constantInfo = speculativeModel.GetConstantValue(expr);
1221            Assert.True(constantInfo.HasValue, "must be a constant");
1222            Assert.Equal((short)0, constantInfo.Value);
1223        }
1224
1225        [Fact]
1226        [WorkItem(648305, "DevDiv")]
1227        public void TestGetSpeculativeSemanticModelInDefaultValueArgument()
1228        {
1229            var compilation = CreateCompilationWithMscorlib(@"
1230class C 
1231{
1232  void M(int x = 1)
1233  {
1234    string y = ""Hello"";     
1235  }
1236}
1237");
1238            var tree = compilation.SyntaxTrees[0];
1239            var root = tree.GetCompilationUnitRoot();
1240            var typeDecl = (TypeDeclarationSyntax)root.Members[0];
1241            var methodDecl = (MethodDeclarationSyntax)typeDecl.Members[0];
1242            var equalsValue = methodDecl.ParameterList.Parameters[0].Default;
1243            var paramDefaultArg = equalsValue.Value;
1244
1245            var model = compilation.GetSemanticModel(tree);
1246
1247            var ti = model.GetTypeInfo(paramDefaultArg);
1248
1249            // Speculate on the equals value syntax (initializer)
1250            // Conversion info available, ConvertedType: Int32.
1251            var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
1252            var expr = newEqualsValue.Value;
1253
1254            SemanticModel speculativeModel;
1255            bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
1256            Assert.True(success);
1257            Assert.NotNull(speculativeModel);
1258
1259            var typeInfo = speculativeModel.GetTypeInfo(expr);
1260            Assert.NotNull(typeInfo.Type);
1261            Assert.Equal("Int16", typeInfo.Type.Name);
1262            Assert.Equal("Int32", typeInfo.ConvertedType.Name);
1263
1264            var constantInfo = speculativeModel.GetConstantValue(expr);
1265            Assert.True(constantInfo.HasValue, "must be a constant");
1266            Assert.Equal((short)0, constantInfo.Value);
1267        }
1268
1269        [Fact]
1270        [WorkItem(746002, "DevDiv")]
1271        public void TestGetSpeculativeSemanticModelInDefaultValueArgument2()
1272        {
1273            var compilation = CreateCompilationWithMscorlib(@"
1274using System;
1275
1276enum E
1277{
1278   A = 1,
1279   B = 2  
1280}
1281
1282interface I
1283{
1284    void M1(E e = E.A);
1285}
1286");
1287            var tree = compilation.SyntaxTrees[0];
1288            var root = tree.GetCompilationUnitRoot();
1289            var interfaceDecl = (TypeDeclarationSyntax)root.Members[1];
1290            var methodDecl = (MethodDeclarationSyntax)interfaceDecl.Members[0];
1291            var param = methodDecl.ParameterList.Parameters[0];
1292            var equalsValue = param.Default;
1293            var paramDefaultArg = equalsValue.Value;
1294
1295            var model = compilation.GetSemanticModel(tree);
1296
1297            // Speculate on the equals value syntax (initializer) with a non-null parent
1298            var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("E.B | E.A"));
1299            newEqualsValue = param.ReplaceNode(equalsValue, newEqualsValue).Default;
1300            var binaryExpr = newEqualsValue.Value;
1301
1302            SemanticModel speculativeModel;
1303            bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
1304            Assert.True(success);
1305            Assert.NotNull(speculativeModel);
1306
1307            var typeInfo = speculativeModel.GetTypeInfo(binaryExpr);
1308            Assert.NotNull(typeInfo.Type);
1309            Assert.Equal("E", typeInfo.Type.Name);
1310            Assert.Equal("E", typeInfo.ConvertedType.Name);
1311        }
1312
1313        [Fact]
1314        [WorkItem(657701, "DevDiv")]
1315        public void TestGetSpeculativeSemanticModelInConstructorDefaultValueArgument()
1316        {
1317            var compilation = CreateCompilationWithMscorlib(@"
1318class C 
1319{
1320  C(int x = 1)
1321  {
1322    string y = ""Hello"";     
1323  }
1324}
1325");
1326            var tree = compilation.SyntaxTrees[0];
1327            var root = tree.GetCompilationUnitRoot();
1328            var typeDecl = (TypeDeclarationSyntax)root.Members[0];
1329            var constructorDecl = (ConstructorDeclarationSyntax)typeDecl.Members[0];
1330            var equalsValue = constructorDecl.ParameterList.Parameters[0].Default;
1331            var paramDefaultArg = equalsValue.Value;
1332
1333            var model = compilation.GetSemanticModel(tree);
1334
1335            var ti = model.GetTypeInfo(paramDefaultArg);
1336
1337            // Speculate on the equals value syntax (initializer)
1338            // Conversion info available, ConvertedType: Int32.
1339            var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
1340            var expr = newEqualsValue.Value;
1341
1342            SemanticModel speculativeModel;
1343            bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
1344            Assert.True(success);
1345            Assert.NotNull(speculativeModel);
1346
1347            var typeInfo = speculativeModel.GetTypeInfo(expr);
1348            Assert.NotNull(typeInfo.Type);
1349            Assert.Equal("Int16", typeInfo.Type.Name);
1350            Assert.Equal("Int32", typeInfo.ConvertedType.Name);
1351
1352            var constantInfo = speculativeModel.GetConstantValue(expr);
1353            Assert.True(constantInfo.HasValue, "must be a constant");
1354            Assert.Equal((short)0, constantInfo.Value);
1355        }
1356
1357        [WorkItem(529893, "DevDiv")]
1358        [Fact]
1359        public void AliasCalledVar()
1360        {
1361            var source = @"
1362using var = Q;
1363
1364class Q
1365{
1366    var q;
1367}
1368";
1369
1370            var comp = CreateCompilationWithMscorlib(source);
1371            comp.VerifyDiagnostics(
1372                // (6,9): warning CS0169: The field 'Q.q' is never used
1373                //     var q;
1374                Diagnostic(ErrorCode.WRN_UnreferencedField, "q").WithArguments("Q.q"));
1375
1376            var classQ = comp.GlobalNamespace.GetMember<NamedTypeSymbol>("Q");
1377            var fieldQ = classQ.GetMember<FieldSymbol>("q");
1378
1379            Assert.Equal(classQ, fieldQ.Type);
1380
1381            var tree = comp.SyntaxTrees.Single();
1382            var model = comp.GetSemanticModel(tree);
1383
1384            var aliasDecl = tree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
1385
1386           

Large files files are truncated, but you can click here to view the full file