/Src/Compilers/CSharp/Test/Symbol/Compilation/SemanticModelAPITests.cs
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
- // 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.
- using System;
- using System.Collections.Generic;
- using System.Collections.Immutable;
- 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 Microsoft.CodeAnalysis.Text;
- using Roslyn.Test.Utilities;
- using Xunit;
- namespace Microsoft.CodeAnalysis.CSharp.UnitTests
- {
- public class SemanticTests : CSharpTestBase
- {
- [Fact]
- public void LocalSymbolsAreEquivalentAcrossSemanticModelsFromTheSameCompilation()
- {
- var text = @"public class C { public void M() { int x = 10; } }";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model1 = comp.GetSemanticModel(tree);
- var model2 = comp.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(vardecl);
- var symbol2 = model2.GetDeclaredSymbol(vardecl);
- Assert.NotSame(symbol1, symbol2);
- Assert.Equal(symbol1, symbol2);
- }
- [Fact]
- public void LocalSymbolsAreDifferentArossSemanticModelsFromDifferentCompilations()
- {
- var text = @"public class C { public void M() { int x = 10; } }";
- var tree = Parse(text);
- var comp1 = CreateCompilationWithMscorlib(tree);
- var comp2 = CreateCompilationWithMscorlib(tree);
- var model1 = comp1.GetSemanticModel(tree);
- var model2 = comp2.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<VariableDeclaratorSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(vardecl);
- var symbol2 = model2.GetDeclaredSymbol(vardecl);
- Assert.NotSame(symbol1, symbol2);
- Assert.NotEqual(symbol1, symbol2);
- }
- [Fact]
- public void RangeVariableSymbolsAreEquivalentAcrossSemanticModelsFromTheSameCompilation()
- {
- var text = @"using System.Linq; public class C { public void M() { var q = from c in string.Empty select c; } }";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
- var model1 = comp.GetSemanticModel(tree);
- var model2 = comp.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryClauseSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(vardecl);
- var symbol2 = model2.GetDeclaredSymbol(vardecl);
- Assert.NotSame(symbol1, symbol2);
- Assert.Equal(symbol1, symbol2);
- }
- [Fact]
- public void RangeVariableSymbolsAreDifferentArossSemanticModelsFromDifferentCompilations()
- {
- var text = @"using System.Linq; public class C { public void M() { var q = from c in string.Empty select c; } }";
- var tree = Parse(text);
- var comp1 = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
- var comp2 = CreateCompilationWithMscorlibAndSystemCore(new[] { tree });
- var model1 = comp1.GetSemanticModel(tree);
- var model2 = comp2.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var vardecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<QueryClauseSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(vardecl);
- var symbol2 = model2.GetDeclaredSymbol(vardecl);
- Assert.NotSame(symbol1, symbol2);
- Assert.NotEqual(symbol1, symbol2);
- }
- [Fact]
- public void LabelSymbolsAreEquivalentAcrossSemanticModelsFromSameCompilation()
- {
- var text = @"public class C { public void M() { label: goto label; } }";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model1 = comp.GetSemanticModel(tree);
- var model2 = comp.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var statement = tree.GetCompilationUnitRoot().DescendantNodes().OfType<GotoStatementSyntax>().First();
- var symbol1 = model1.GetSymbolInfo(statement.Expression).Symbol;
- var symbol2 = model2.GetSymbolInfo(statement.Expression).Symbol;
- Assert.Equal(false, ReferenceEquals(symbol1, symbol2));
- Assert.Equal(symbol1, symbol2);
- }
- [Fact]
- public void LambdaParameterSymbolsAreEquivalentAcrossSemanticModelsFromSameCompilation()
- {
- var text = @"using System; public class C { public void M() { Func<int,int> f = (p) => p; } }";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model1 = comp.GetSemanticModel(tree);
- var model2 = comp.GetSemanticModel(tree);
- Assert.NotEqual(model1, model2);
- var paramdecl = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(paramdecl);
- var symbol2 = model2.GetDeclaredSymbol(paramdecl);
- Assert.NotSame(symbol1, symbol2);
- Assert.Equal(symbol1.ContainingSymbol, symbol2.ContainingSymbol);
- Assert.Equal(symbol1, symbol2);
- }
- [Fact]
- public void LambdaParameterSymbolsAreDifferentAcrossSemanticModelsFromDifferentCompilations()
- {
- var text = @"using System; public class C { public void M() { Func<int,int> f = (p) => p; } }";
- var tree1 = Parse(text);
- var tree2 = Parse(text);
- var comp1 = CreateCompilationWithMscorlib(tree1);
- var comp2 = CreateCompilationWithMscorlib(tree2);
- var model1 = comp1.GetSemanticModel(tree1);
- var model2 = comp2.GetSemanticModel(tree2);
- Assert.NotEqual(model1, model2);
- var paramdecl1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var symbol1 = model1.GetDeclaredSymbol(paramdecl1);
- var paramdecl2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var symbol2 = model2.GetDeclaredSymbol(paramdecl2);
- Assert.NotSame(symbol1, symbol2);
- Assert.NotEqual(symbol1.ContainingSymbol, symbol2.ContainingSymbol);
- Assert.NotEqual(symbol1, symbol2);
- }
- [WorkItem(539740, "DevDiv")]
- [Fact]
- public void NamespaceWithoutName()
- {
- var text = "namespace";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var errors = comp.GetDiagnostics().ToArray();
- Assert.Equal(3, errors.Length);
- var nsArray = tree.GetCompilationUnitRoot().DescendantNodes().Where(node => node.IsKind(SyntaxKind.NamespaceDeclaration)).ToArray();
- Assert.Equal(1, nsArray.Length);
- var nsSyntax = nsArray[0] as NamespaceDeclarationSyntax;
- var symbol = model.GetDeclaredSymbol(nsSyntax);
- Assert.Equal(string.Empty, symbol.Name);
- }
- [Fact]
- public void LazyBoundUsings1()
- {
- var text =
- @"
- // Peter Golde[7/19/2010]: I managed to construct the following interesting example today,
- // which Dev10 does compile. Interestingly, the resolution of one “using” can depend
- // on the resolution of another “using” later in the same namespace.
- using K = A.Q;
- using L = B.R;
-
- class B
- {
- public class R
- {
- public class Q
- {
- public class S : K { }
- }
- }
- }
-
- class A : L
- {
- public K.S v = null;
- }
- ";
- var comp = CreateCompilationWithMscorlib(text);
- var global = comp.GlobalNamespace;
- var a = global.GetTypeMembers("A", 0).Single();
- var abase = a.BaseType;
- Assert.Equal("B.R", abase.ToTestDisplayString());
- var b = global.GetTypeMembers("B", 0).Single();
- var r = b.GetTypeMembers("R", 0).Single();
- var q = r.GetTypeMembers("Q", 0).Single();
- var v = a.GetMembers("v").Single() as FieldSymbol;
- var s = v.Type;
- Assert.Equal("B.R.Q.S", s.ToTestDisplayString());
- var sbase = s.BaseType;
- Assert.Equal("B.R.Q", sbase.ToTestDisplayString());
- }
- [Fact]
- public void Diagnostics1()
- {
- var text =
- @"
- class A : A {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var errs = comp.GetSemanticModel(tree).GetDeclarationDiagnostics();
- Assert.Equal(1, errs.Count());
- }
- [Fact]
- public void DiagnosticsInOneTree()
- {
- var partial1 =
- @"
- partial class A
- {
- void foo() { int x = y; }
- }
- class C : B {}
- ";
- var partial2 =
- @"
- partial class A
- {
- int q; //an unused field in a partial type
- void bar() { int x = z; }
- }
- class B : NonExistent {}
- ";
- var partial1Tree = Parse(partial1);
- var partial2Tree = Parse(partial2);
- var comp = CreateCompilationWithMscorlib(new SyntaxTree[] { partial1Tree, partial2Tree });
- var errs = comp.GetSemanticModel(partial1Tree).GetDiagnostics();
- Assert.Equal(1, errs.Count());
- }
- [Fact]
- public void Bindings1()
- {
- var text =
- @"
- class B : A {}
- class A {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var bbase = bdecl.BaseList.Types[0] as TypeSyntax;
- var model = comp.GetSemanticModel(tree);
- var info = model.GetSymbolInfo(bbase);
- Assert.NotNull(info.Symbol);
- var a = comp.GlobalNamespace.GetTypeMembers("A", 0).Single();
- Assert.Equal(a, info.Symbol);
- Assert.Equal(a, model.GetTypeInfo(bbase).Type);
- }
- [Fact]
- public void BaseScope1()
- {
- // ensure the base clause is not bound in the scope of the class
- var text =
- @"
- public class C : B {}
- public class A {
- public class B {}
- }
- public class B : A {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var cdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var cbase = cdecl.BaseList.Types[0] as TypeSyntax;
- var model = comp.GetSemanticModel(tree);
- var info = model.GetSymbolInfo(cbase);
- Assert.NotNull(info.Symbol);
- var b = comp.GlobalNamespace.GetTypeMembers("B", 0).Single();
- Assert.Equal(b, info.Symbol);
- Assert.Equal(b, model.GetTypeInfo(cbase).Type);
- }
- [Fact]
- public void BaseScope2()
- {
- // ensure type parameters are in scope in the base clause
- var text =
- @"
- public class C<T> : A<T> { }
- public class A<T> : B { }
- public class B {
- public class T { }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var cdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var cbase = cdecl.BaseList.Types[0] as TypeSyntax;
- var model = comp.GetSemanticModel(tree);
- var info = model.GetSymbolInfo(cbase);
- Assert.NotNull(info.Symbol);
- var cbasetype = info.Symbol as NamedTypeSymbol;
- var c = comp.GlobalNamespace.GetTypeMembers("C", 1).Single();
- Assert.Equal(c.BaseType, cbasetype);
- }
- [Fact]
- public void Bindings2()
- {
- var text =
- @"
- class B<T> : A<T> {}
- class A<T> {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var bbase = bdecl.BaseList.Types[0] as TypeSyntax; // A<T>
- var model = comp.GetSemanticModel(tree);
- var info = model.GetSymbolInfo(bbase);
- Assert.NotNull(info.Symbol);
- var at2 = info.Symbol as NamedTypeSymbol;
- Assert.Equal(at2, model.GetTypeInfo(bbase).Type);
- var a = comp.GlobalNamespace.GetTypeMembers("A", 1).Single();
- var at = a.TypeParameters.First();
- var b = comp.GlobalNamespace.GetTypeMembers("B", 1).Single();
- var bt = b.TypeParameters.First();
- Assert.Equal(a.OriginalDefinition, at2.OriginalDefinition);
- Assert.Equal(b.TypeParameters.First(), at2.TypeArguments.First());
- }
- [Fact]
- public void Bindings3()
- {
- var text =
- @"using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static Program Field;
- }";
- var tree1 = Parse(text);
- var compilation = CreateCompilationWithMscorlib(tree1);
- var tree2 = Parse(text);
- var classProgram = tree2.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var staticProgramField = classProgram.Members[0] as FieldDeclarationSyntax;
- var program = staticProgramField.Declaration.Type;
- var model = compilation.GetSemanticModel(tree1);
- Assert.Throws<ArgumentException>(() =>
- {
- // tree2 not in the compilation
- var lookup = model.GetSymbolInfo(program);
- });
- }
- [Fact]
- public void Bindings4()
- {
- var text =
- @"using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- Program p;
- static void Main(string[] args)
- {
- }
- }";
- var tree1 = Parse(text);
- var compilation = CreateCompilationWithMscorlib(tree1);
- var decl = tree1.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var field = decl.Members[0] as FieldDeclarationSyntax;
- var type = field.Declaration.Type;
- var model = compilation.GetSemanticModel(tree1);
- var info = model.GetSymbolInfo(type);
- Assert.Equal(compilation.GlobalNamespace.GetTypeMembers("Program", 0).Single(), info.Symbol);
- }
- [Fact]
- public void Bindings5()
- {
- var text =
- @"using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- }
- }";
- var tree1 = Parse(text);
- var tree2 = Parse(text);
- var compilation = CreateCompilationWithMscorlib(new SyntaxTree[] { tree1, tree2 });
- var decl = tree1.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var method = decl.Members[0] as MethodDeclarationSyntax;
- var type = method.ParameterList.Parameters[0].Type;
- var model = compilation.GetSemanticModel(tree1);
- var info = model.GetSymbolInfo(type);
- Assert.Equal<Symbol>(compilation.GetSpecialType(SpecialType.System_String), (info.Symbol as ArrayTypeSymbol).ElementType);
- }
- [Fact]
- public void Speculative1()
- {
- var text =
- @"
- class B {
- object x;
- }
- class A {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var bdecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var xdecl = bdecl.Members[0] as FieldDeclarationSyntax;
- var model = comp.GetSemanticModel(tree);
- TypeSyntax speculate = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier("A"));
- var symbolInfo = model.GetSpeculativeSymbolInfo(xdecl.SpanStart, speculate, SpeculativeBindingOption.BindAsTypeOrNamespace);
- var lookup = symbolInfo.Symbol as TypeSymbol;
- Assert.NotNull(lookup);
- var a = comp.GlobalNamespace.GetTypeMembers("A", 0).Single();
- Assert.Equal(a, lookup);
- }
- [Fact]
- public void GetType1()
- {
- var text =
- @"
- class A {
- class B {}
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var adecl = tree.GetCompilationUnitRoot().Members[0] as TypeDeclarationSyntax;
- var bdecl = adecl.Members[0] as TypeDeclarationSyntax;
- var model = comp.GetSemanticModel(tree);
- var a1 = model.GetDeclaredSymbol(adecl);
- var b1 = model.GetDeclaredSymbol(bdecl);
- var global = comp.GlobalNamespace;
- var a2 = global.GetTypeMembers("A", 0).Single();
- var b2 = a2.GetTypeMembers("B", 0).Single();
- Assert.Equal(a2, a1);
- Assert.Equal(b2, b1);
- }
- [Fact]
- public void DottedName()
- {
- var text =
- @"
- class Main {
- A.B x; // this refers to the B within A.
- }
- class A {
- public class B {}
- }
- class B {}
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
- var root = tree.GetCompilationUnitRoot();
- var mainDecl = root.Members[0] as TypeDeclarationSyntax;
- var mainType = model.GetDeclaredSymbol(mainDecl);
- var aDecl = root.Members[1] as TypeDeclarationSyntax;
- var aType = model.GetDeclaredSymbol(aDecl);
- var abDecl = aDecl.Members[0] as TypeDeclarationSyntax;
- var abType = model.GetDeclaredSymbol(abDecl);
- var bDecl = root.Members[2] as TypeDeclarationSyntax;
- var bType = model.GetDeclaredSymbol(bDecl);
- var xDecl = mainDecl.Members[0] as FieldDeclarationSyntax;
- var xSym = mainType.GetMembers("x").Single() as FieldSymbol;
- Assert.Equal<ISymbol>(abType, xSym.Type);
- var info = model.GetSymbolInfo((xDecl.Declaration.Type as QualifiedNameSyntax).Right);
- Assert.Equal(abType, info.Symbol);
- }
- [Fact]
- public void AliasQualifiedName()
- {
- var text =
- @"
- class B {}
- namespace N {
- class C : global::B {}
- class B {}
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var root = tree.GetCompilationUnitRoot();
- var nDecl = root.Members[0] as NamespaceDeclarationSyntax;
- var n2Decl = root.Members[1] as NamespaceDeclarationSyntax;
- var cDecl = n2Decl.Members[0] as TypeDeclarationSyntax;
- var cBase = (cDecl.BaseList.Types[0] as AliasQualifiedNameSyntax).Name;
- var cBaseType = model.GetSymbolInfo(cBase).Symbol;
- var bOuter = comp.GlobalNamespace.GetTypeMembers("B", 0).Single();
- var bInner = (comp.GlobalNamespace.GetMembers("N").Single() as NamespaceSymbol).GetTypeMembers("B", 0).Single();
- Assert.Equal(bOuter, cBaseType);
- }
- [Fact, WorkItem(528655, "DevDiv")]
- public void ErrorSymbolForInvalidCode()
- {
- var text = @"
- public class A
- {
- int foo { void foo() {} } // Error
- static int Main() { return 1; }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var mems = comp.SourceModule.GlobalNamespace.GetMembers();
- var typeA = mems.Where(s => s.Name == "A").Select(s => s);
- Assert.Equal(1, typeA.Count());
- var invalid = mems.Where(s => s.Name == "<invalid-global-code>").Select(s => s);
- Assert.Equal(1, invalid.Count());
- }
- [Fact, WorkItem(543225, "DevDiv"), WorkItem(529057, "DevDiv")]
- public void MergePartialMethodAndParameterSymbols()
- {
- var text = @"
- using System;
- partial class PC
- {
- partial void PM(int pp);
- }
- partial class PC
- {
- partial void PM(int pp) {}
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var pTypeSym = comp.SourceModule.GlobalNamespace.GetTypeMembers("PC").Single();
- var pMethodSym = pTypeSym.GetMembers("PM").Single();
- var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
- var pType01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();
- var pType02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Last();
- Assert.NotEqual(pType01, pType02);
- var ptSym01 = model.GetDeclaredSymbol(pType01);
- var ptSym02 = model.GetDeclaredSymbol(pType02);
- // same partial type symbol
- Assert.Same(ptSym01, ptSym02);
- Assert.Equal(2, ptSym01.Locations.Length);
- var pMethod01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First();
- var pMethod02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Last();
- Assert.NotEqual(pMethod01, pMethod02);
- var pmSym01 = model.GetDeclaredSymbol(pMethod01);
- var pmSym02 = model.GetDeclaredSymbol(pMethod02);
- // different partial method symbols:(
- Assert.NotSame(pmSym01, pmSym02);
- // the declaration one is what one can get from GetMembers()
- Assert.Same(pMethodSym, pmSym01);
- // with decl|impl point to each nother
- Assert.Null(pmSym01.PartialDefinitionPart);
- Assert.Same(pmSym02, pmSym01.PartialImplementationPart);
- Assert.Same(pmSym01, pmSym02.PartialDefinitionPart);
- Assert.Null(pmSym02.PartialImplementationPart);
- var pParam01 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var pParam02 = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().Last();
- Assert.NotEqual(pParam01, pParam02);
- var ppSym01 = model.GetDeclaredSymbol(pParam01);
- var ppSym02 = model.GetDeclaredSymbol(pParam02);
- Assert.NotSame(ppSym01, ppSym02);
- Assert.Equal(1, ppSym01.Locations.Length);
- Assert.Equal(1, ppSym02.Locations.Length);
- }
- [Fact, WorkItem(544221, "DevDiv")]
- public void GetTypeInfoForOptionalParameterDefaultValueInDelegate()
- {
- var text = @"
- using System;
- class Test
- {
- public delegate void DFoo(byte i = 1);
- protected internal void MFoo(sbyte j = 2) { }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var root = tree.GetCompilationUnitRoot();
- var exprs = root.DescendantNodes().OfType<LiteralExpressionSyntax>().ToArray();
- Assert.Equal(2, exprs.Length);
- var type1 = model.GetTypeInfo(exprs[0]);
- var type2 = model.GetTypeInfo(exprs[1]);
- Assert.NotNull(type1.Type);
- Assert.Equal("System.Int32", type1.Type.ToTestDisplayString());
- Assert.NotNull(type2.Type);
- Assert.Equal("System.Int32", type2.Type.ToTestDisplayString());
- }
- [Fact, WorkItem(544231, "DevDiv")]
- public void GetDeclSymbolForParameterOfPartialMethod()
- {
- var text1 = @"
- using System;
- partial class Partial001
- {
- static partial void Foo(ulong x);
- }
- ";
- var text2 = @"
- using System;
- partial class Partial001
- {
- static partial void Foo(ulong x) { }
- static int Main() { return 1; }
- }
- ";
- var tree1 = Parse(text1);
- var tree2 = Parse(text2);
- var comp = CreateCompilationWithMscorlib(new List<SyntaxTree> {tree1, tree2});
- var model1 = comp.GetSemanticModel(tree1);
- var model2 = comp.GetSemanticModel(tree2);
- var root1 = tree1.GetCompilationUnitRoot();
- var root2 = tree1.GetCompilationUnitRoot();
- var para1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var para2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<ParameterSyntax>().First();
- var sym1 = model1.GetDeclaredSymbol(para1);
- var sym2 = model2.GetDeclaredSymbol(para2);
- Assert.NotNull(sym1);
- Assert.NotNull(sym2);
- Assert.Equal("System.UInt64 x", sym1.ToTestDisplayString());
- Assert.Equal("System.UInt64 x", sym2.ToTestDisplayString());
- Assert.NotEqual(sym1.Locations[0], sym2.Locations[0]);
- }
- [Fact, WorkItem(544473, "DevDiv")]
- public void GetDeclSymbolForTypeParameterOfPartialMethod()
- {
- var text1 = @"
- using System;
- partial class Partial001
- {
- static partial void Foo<T>(T x);
- }
- ";
- var text2 = @"
- using System;
- partial class Partial001
- {
- static partial void Foo<T>(T x) { }
- static int Main() { return 1; }
- }
- ";
- var tree1 = Parse(text1);
- var tree2 = Parse(text2);
- var comp = CreateCompilationWithMscorlib(new List<SyntaxTree> { tree1, tree2 });
- var model1 = comp.GetSemanticModel(tree1);
- var model2 = comp.GetSemanticModel(tree2);
- var root1 = tree1.GetCompilationUnitRoot();
- var root2 = tree1.GetCompilationUnitRoot();
- var para1 = tree1.GetCompilationUnitRoot().DescendantNodes().OfType<TypeParameterSyntax>().First();
- var para2 = tree2.GetCompilationUnitRoot().DescendantNodes().OfType<TypeParameterSyntax>().First();
- var sym1 = model1.GetDeclaredSymbol(para1);
- var sym2 = model2.GetDeclaredSymbol(para2);
- Assert.NotNull(sym1);
- Assert.NotNull(sym2);
- Assert.Equal("T", sym1.ToTestDisplayString());
- Assert.Equal("T", sym2.ToTestDisplayString());
- Assert.NotEqual(sym1.Locations[0], sym2.Locations[0]);
- }
- [Fact]
- public void GetDeclaredSymbolForAnonymousTypeProperty01()
- {
- var text = @"
- using System;
- struct AnonTypeTest
- {
- static long Prop
- {
- get
- {
- short @short = -1;
- var anonType = new { id = 123, @do = ""QC"", @short, Prop };
- return anonType.id + anonType.@short;
- }
- }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
- Assert.Equal(4, anonProps.Count());
- var symList = from ap in anonProps
- let apsym = model.GetDeclaredSymbol(ap)
- orderby apsym.Name
- select apsym.Name;
- var results = string.Join(", ", symList);
- Assert.Equal("do, id, Prop, short", results);
- }
- [Fact]
- public void GetDeclaredSymbolForAnonymousTypeProperty02()
- {
- var text = @"
- using System;
- class AnonTypeTest
- {
- long field = 111;
- void M(byte p1, ref sbyte p2, out string p3, params string[] ary)
- {
- ulong local = 12345;
- var anonType = new { local, this.field, p1, p2, ary };
- p3 = anonType.ary.Length > 0 ? anonType.ary[0] : """";
- }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
- Assert.Equal(5, anonProps.Count());
- var symList = from ap in anonProps
- let apsym = model.GetDeclaredSymbol(ap)
- orderby apsym.Name
- select apsym.Name;
- var results = string.Join(", ", symList);
- Assert.Equal("ary, field, local, p1, p2", results);
- }
- [Fact]
- public void GetDeclaredSymbolForAnonymousTypeProperty03()
- {
- var text = @"
- using System;
- enum E { a, b, c }
- class Base
- {
- protected E baseField = E.b;
- protected virtual Base BaseProp { get { return this; } }
- public Func<string, char> deleField;
- }
- class AnonTypeTest : Base
- {
- protected override Base BaseProp { get { return null; } }
- char this[string @string]
- {
- get
- {
- var anonType = new { id = deleField, base.BaseProp, base.baseField, ret = @string };
- return anonType.id(anonType.ret);
- }
- }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
- Assert.Equal(4, anonProps.Count());
- var symList = from ap in anonProps
- let apsym = model.GetDeclaredSymbol(ap)
- orderby apsym.Name
- select apsym.Name;
- var results = string.Join(", ", symList);
- Assert.Equal("baseField, BaseProp, id, ret", results);
- }
- [Fact]
- public void GetDeclaredSymbolForAnonymousTypeProperty04()
- {
- var text = @"
- using System;
- enum E { a, b, c }
- struct S
- {
- public static E sField;
- public interface IFoo { }
- public IFoo GetFoo { get; set; }
- public IFoo GetFoo2() { return null; }
- }
- class AnonTypeTest
- {
- event Action<ushort> Eve
- {
- add
- {
- var anonType = new { a1 = new { S.sField, ifoo = new { new S().GetFoo } } };
- }
- remove
- {
- var anonType = new { a1 = new { a2 = new { a2 = S.sField, a3 = new { a3 = new S().GetFoo2() } } } };
- }
- }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var anonProps = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AnonymousObjectMemberDeclaratorSyntax>();
- Assert.Equal(9, anonProps.Count());
- var symList = from ap in anonProps
- let apsym = model.GetDeclaredSymbol(ap)
- orderby apsym.Name
- select apsym.Name;
- var results = string.Join(", ", symList);
- Assert.Equal("a1, a1, a2, a2, a3, a3, GetFoo, ifoo, sField", results);
- }
- [Fact(), WorkItem(542861, "DevDiv"), WorkItem(529673, "DevDiv")]
- public void GetSymbolInfoForAccessorParameters()
- {
- var text = @"
- using System;
- public class Test
- {
- object[] _Items = new object[3];
- public object this[int index]
- {
- get
- {
- return _Items[index];
- }
- set
- {
- _Items[index] = value;
- }
- }
- }
- ";
- var tree = Parse(text);
- var comp = CreateCompilationWithMscorlib(tree);
- var model = comp.GetSemanticModel(tree);
- var descendants = tree.GetCompilationUnitRoot().DescendantNodes();
- var paras = descendants.OfType<ParameterSyntax>();
- Assert.Equal(1, paras.Count());
- var parasym = model.GetDeclaredSymbol(paras.First());
- var ploc = parasym.Locations[0];
-
- var args = descendants.OfType<ArgumentSyntax>().Where(s => s.ToString() == "index").Select(s => s);
- Assert.Equal(2, args.Count());
- var argsym1 = model.GetSymbolInfo(args.First().Expression).Symbol;
- var argsym2 = model.GetSymbolInfo(args.Last().Expression).Symbol;
- Assert.NotNull(argsym1);
- Assert.NotNull(argsym2);
- Assert.Equal(ploc, argsym1.Locations[0]);
- Assert.Equal(ploc, argsym2.Locations[0]);
- Assert.Equal(parasym.Kind, argsym1.Kind);
- Assert.Equal(parasym.Kind, argsym2.Kind);
- // SourceSimpleParameterSymbol vs. SourceClonedComplexParameterSymbol
- Assert.NotEqual(parasym, argsym1);
- Assert.NotEqual(parasym, argsym2);
- }
- [WorkItem(545648, "DevDiv")]
- [Fact]
- public void AliasDeclaredSymbolWithConflict()
- {
- var source = @"
- using X = System;
-
- class X { }
- ";
- var comp = CreateCompilationWithMscorlib(source);
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
- var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
- var symbol = model.GetDeclaredSymbol(aliasSyntax);
- Assert.Equal(symbol.Target, comp.GlobalNamespace.GetMember<NamespaceSymbol>("System"));
- comp.VerifyDiagnostics(
- // (2,1): info CS8019: Unnecessary using directive.
- // using X = System;
- Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using X = System;"));
- }
- [WorkItem(529751, "DevDiv")]
- [Fact]
- public void ExternAlias()
- {
- var source = @"
- extern alias X;
- class Test
- {
- static void Main()
- {
- X::C c = null;
- }
- }
- ";
- var comp1 = CreateCompilationWithMscorlib("public class C { }");
- var ref1 = new MetadataImageReference(CompileAndVerify(comp1).EmittedAssemblyData, aliases: ImmutableArray.Create("X"));
- var comp2 = CreateCompilationWithMscorlib(source, new[] { ref1 });
- var tree = comp2.SyntaxTrees.Single();
- var model = comp2.GetSemanticModel(tree);
- var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ExternAliasDirectiveSyntax>().Single();
- // Compilation.GetExternAliasTarget defines this behavior: the target is a merged namespace
- // with the same name as the alias, contained in the global namespace of the compilation.
- var aliasSymbol = model.GetDeclaredSymbol(aliasSyntax);
- var aliasTarget = (NamespaceSymbol)aliasSymbol.Target;
- Assert.Equal(NamespaceKind.Module, aliasTarget.Extent.Kind);
- Assert.Equal("", aliasTarget.Name);
- Assert.True(aliasTarget.IsGlobalNamespace);
- Assert.Null(aliasTarget.ContainingNamespace);
- Assert.Equal(0, comp2.GlobalNamespace.GetMembers("X").Length); //Doesn't contain the alias target namespace as a child.
- var aliasQualifiedSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AliasQualifiedNameSyntax>().Single();
- Assert.Equal(aliasSymbol, model.GetAliasInfo(aliasQualifiedSyntax.Alias));
- comp2.VerifyDiagnostics(
- // (8,14): warning CS0219: The variable 'c' is assigned but its value is never used
- // X::C c = null;
- Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "c").WithArguments("c"));
- }
- [Fact, WorkItem(546687, "DevDiv"), WorkItem(529751, "DevDiv")]
- public void ExternAliasWithoutTarget()
- {
- var source = @"
- extern alias X;
- class Test
- {
- static void Main()
- {
- X::C c = null;
- }
- }
- ";
- var comp = CreateCompilationWithMscorlib(source);
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
- var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ExternAliasDirectiveSyntax>().Single();
- var aliasSymbol = model.GetDeclaredSymbol(aliasSyntax);
- Assert.IsType<MissingNamespaceSymbol>(aliasSymbol.Target);
- var aliasQualifiedSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<AliasQualifiedNameSyntax>().Single();
- Assert.Equal(aliasSymbol, model.GetAliasInfo(aliasQualifiedSyntax.Alias));
- comp.VerifyDiagnostics(
- // (2,14): error CS0430: The extern alias 'X' was not specified in a /reference option
- // extern alias X;
- Diagnostic(ErrorCode.ERR_BadExternAlias, "X").WithArguments("X"),
- // (8,12): error CS0234: The type or namespace name 'C' does not exist in the namespace 'X' (are you missing an assembly reference?)
- // X::C c = null;
- Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "C").WithArguments("C", "X"));
- }
- [WorkItem(545648, "DevDiv")]
- [Fact]
- public void UsingDirectiveAliasSemanticInfo()
- {
- var source = "using X = System;";
- var comp = CreateCompilationWithMscorlib(source);
- comp.VerifyDiagnostics(
- // (1,1): info CS8019: Unnecessary using directive.
- // using X = System;
- Diagnostic(ErrorCode.INF_UnusedUsingDirective, "using X = System;"));
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
- var aliasSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<NameEqualsSyntax>().Single().Name;
- Assert.Equal(SymbolInfo.None, model.GetSymbolInfo(aliasSyntax));
- var usingSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
- Assert.Equal(SymbolKind.Alias, model.GetDeclaredSymbol(usingSyntax).Kind);
- }
- [WorkItem(545882, "DevDiv")]
- [Fact]
- public void SpeculativelyBindConstructorInitializerInPlaceOfActual()
- {
- var source = @"class C
- {
- C(int x) { }
- C() : this((int) 1) { }
- }";
- var comp = CreateCompilationWithMscorlib(source);
- comp.VerifyDiagnostics();
-
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
-
- var oldSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ConstructorInitializerSyntax>().Single();
- var newSyntax = SyntaxFactory.ConstructorInitializer(SyntaxKind.ThisConstructorInitializer);
- var info = model.GetSpeculativeSymbolInfo(oldSyntax.SpanStart, newSyntax);
- var symbol = info.Symbol;
- Assert.NotNull(symbol);
- Assert.Equal(comp.GlobalNamespace.GetMember<NamedTypeSymbol>("C"), symbol.ContainingType);
- Assert.Equal(SymbolKind.Method, symbol.Kind);
- var method = (MethodSymbol)symbol;
- Assert.Equal(MethodKind.Constructor, method.MethodKind);
- Assert.Equal(0, method.ParameterCount);
- }
- [WorkItem(545882, "DevDiv")]
- [Fact]
- public void SpeculativelyBindConstructorInitializerInNewLocation()
- {
- var source = @"class C
- {
- C() { }
- }";
- var comp = CreateCompilationWithMscorlib(source);
- comp.VerifyDiagnostics();
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
- var oldSyntax = tree.GetCompilationUnitRoot().DescendantNodes().OfType<ConstructorDeclarationSyntax>().Single();
- var newSyntax = SyntaxFactory.ConstructorInitializer(SyntaxKind.ThisConstructorInitializer);
- var info = model.GetSpeculativeSymbolInfo(oldSyntax.ParameterList.Span.End, newSyntax);
- Assert.Equal(SymbolInfo.None, info);
- }
- [Fact]
- public void TestGetSpeculativeSemanticModelInFieldInitializer()
- {
- var compilation = CreateCompilationWithMscorlib(@"
- class C
- {
- object y = 1;
- }
- ");
- var tree = compilation.SyntaxTrees[0];
- var root = tree.GetCompilationUnitRoot();
- var typeDecl = (TypeDeclarationSyntax)root.Members[0];
- var fieldDecl = (FieldDeclarationSyntax)typeDecl.Members[0];
- var varDecl = fieldDecl.Declaration.Variables.First();
- var model = compilation.GetSemanticModel(tree);
- Assert.False(model.IsSpeculativeSemanticModel);
- Assert.Null(model.ParentModel);
- Assert.Equal(0, model.OriginalPositionForSpeculation);
- // Speculate on the equals value syntax (initializer)
- // Conversion info available, ConvertedType: Object.
- var equalsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression(@"(string)""Hello"""));
- var expr = equalsValue.Value;
- int position = varDecl.Initializer.SpanStart;
- SemanticModel speculativeModel;
- bool success = model.TryGetSpeculativeSemanticModel(position, equalsValue, out speculativeModel);
- Assert.True(success);
- Assert.NotNull(speculativeModel);
- Assert.True(speculativeModel.IsSpeculativeSemanticModel);
- Assert.Equal(model, speculativeModel.ParentModel);
- Assert.Equal(position, speculativeModel.OriginalPositionForSpeculation);
- var typeInfo = speculativeModel.GetTypeInfo(expr);
- Assert.NotNull(typeInfo.Type);
- Assert.Equal("String", typeInfo.Type.Name);
- Assert.Equal("Object", typeInfo.ConvertedType.Name);
- var constantInfo = speculativeModel.GetConstantValue(expr);
- Assert.True(constantInfo.HasValue, "must be a constant");
- Assert.Equal("Hello", constantInfo.Value);
- }
- [Fact]
- public void TestGetSpeculativeSemanticModelInEnumMember()
- {
- var compilation = CreateCompilationWithMscorlib(@"
- enum C
- {
- y = 1
- }
- ");
- var tree = compilation.SyntaxTrees[0];
- var root = tree.GetCompilationUnitRoot();
- var typeDecl = (EnumDeclarationSyntax)root.Members[0];
- var enumMemberDecl = (EnumMemberDeclarationSyntax)typeDecl.Members[0];
- var equalsValue = enumMemberDecl.EqualsValue;
- var initializer = equalsValue.Value;
- var model = compilation.GetSemanticModel(tree);
- // Speculate on the equals value syntax (initializer)
- // Conversion info available, ConvertedType: Int32.
- var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
- var expr = newEqualsValue.Value;
- SemanticModel speculativeModel;
- bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
- Assert.True(success);
- Assert.NotNull(speculativeModel);
- var typeInfo = speculativeModel.GetTypeInfo(expr);
- Assert.NotNull(typeInfo.Type);
- Assert.Equal("Int16", typeInfo.Type.Name);
- Assert.Equal("Int32", typeInfo.ConvertedType.Name);
- var constantInfo = speculativeModel.GetConstantValue(expr);
- Assert.True(constantInfo.HasValue, "must be a constant");
- Assert.Equal((short)0, constantInfo.Value);
- }
- [Fact]
- [WorkItem(648305, "DevDiv")]
- public void TestGetSpeculativeSemanticModelInDefaultValueArgument()
- {
- var compilation = CreateCompilationWithMscorlib(@"
- class C
- {
- void M(int x = 1)
- {
- string y = ""Hello"";
- }
- }
- ");
- var tree = compilation.SyntaxTrees[0];
- var root = tree.GetCompilationUnitRoot();
- var typeDecl = (TypeDeclarationSyntax)root.Members[0];
- var methodDecl = (MethodDeclarationSyntax)typeDecl.Members[0];
- var equalsValue = methodDecl.ParameterList.Parameters[0].Default;
- var paramDefaultArg = equalsValue.Value;
- var model = compilation.GetSemanticModel(tree);
- var ti = model.GetTypeInfo(paramDefaultArg);
- // Speculate on the equals value syntax (initializer)
- // Conversion info available, ConvertedType: Int32.
- var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
- var expr = newEqualsValue.Value;
- SemanticModel speculativeModel;
- bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
- Assert.True(success);
- Assert.NotNull(speculativeModel);
- var typeInfo = speculativeModel.GetTypeInfo(expr);
- Assert.NotNull(typeInfo.Type);
- Assert.Equal("Int16", typeInfo.Type.Name);
- Assert.Equal("Int32", typeInfo.ConvertedType.Name);
- var constantInfo = speculativeModel.GetConstantValue(expr);
- Assert.True(constantInfo.HasValue, "must be a constant");
- Assert.Equal((short)0, constantInfo.Value);
- }
- [Fact]
- [WorkItem(746002, "DevDiv")]
- public void TestGetSpeculativeSemanticModelInDefaultValueArgument2()
- {
- var compilation = CreateCompilationWithMscorlib(@"
- using System;
- enum E
- {
- A = 1,
- B = 2
- }
- interface I
- {
- void M1(E e = E.A);
- }
- ");
- var tree = compilation.SyntaxTrees[0];
- var root = tree.GetCompilationUnitRoot();
- var interfaceDecl = (TypeDeclarationSyntax)root.Members[1];
- var methodDecl = (MethodDeclarationSyntax)interfaceDecl.Members[0];
- var param = methodDecl.ParameterList.Parameters[0];
- var equalsValue = param.Default;
- var paramDefaultArg = equalsValue.Value;
- var model = compilation.GetSemanticModel(tree);
- // Speculate on the equals value syntax (initializer) with a non-null parent
- var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("E.B | E.A"));
- newEqualsValue = param.ReplaceNode(equalsValue, newEqualsValue).Default;
- var binaryExpr = newEqualsValue.Value;
- SemanticModel speculativeModel;
- bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
- Assert.True(success);
- Assert.NotNull(speculativeModel);
- var typeInfo = speculativeModel.GetTypeInfo(binaryExpr);
- Assert.NotNull(typeInfo.Type);
- Assert.Equal("E", typeInfo.Type.Name);
- Assert.Equal("E", typeInfo.ConvertedType.Name);
- }
- [Fact]
- [WorkItem(657701, "DevDiv")]
- public void TestGetSpeculativeSemanticModelInConstructorDefaultValueArgument()
- {
- var compilation = CreateCompilationWithMscorlib(@"
- class C
- {
- C(int x = 1)
- {
- string y = ""Hello"";
- }
- }
- ");
- var tree = compilation.SyntaxTrees[0];
- var root = tree.GetCompilationUnitRoot();
- var typeDecl = (TypeDeclarationSyntax)root.Members[0];
- var constructorDecl = (ConstructorDeclarationSyntax)typeDecl.Members[0];
- var equalsValue = constructorDecl.ParameterList.Parameters[0].Default;
- var paramDefaultArg = equalsValue.Value;
- var model = compilation.GetSemanticModel(tree);
- var ti = model.GetTypeInfo(paramDefaultArg);
- // Speculate on the equals value syntax (initializer)
- // Conversion info available, ConvertedType: Int32.
- var newEqualsValue = SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("(short)0"));
- var expr = newEqualsValue.Value;
- SemanticModel speculativeModel;
- bool success = model.TryGetSpeculativeSemanticModel(equalsValue.SpanStart, newEqualsValue, out speculativeModel);
- Assert.True(success);
- Assert.NotNull(speculativeModel);
- var typeInfo = speculativeModel.GetTypeInfo(expr);
- Assert.NotNull(typeInfo.Type);
- Assert.Equal("Int16", typeInfo.Type.Name);
- Assert.Equal("Int32", typeInfo.ConvertedType.Name);
- var constantInfo = speculativeModel.GetConstantValue(expr);
- Assert.True(constantInfo.HasValue, "must be a constant");
- Assert.Equal((short)0, constantInfo.Value);
- }
- [WorkItem(529893, "DevDiv")]
- [Fact]
- public void AliasCalledVar()
- {
- var source = @"
- using var = Q;
- class Q
- {
- var q;
- }
- ";
- var comp = CreateCompilationWithMscorlib(source);
- comp.VerifyDiagnostics(
- // (6,9): warning CS0169: The field 'Q.q' is never used
- // var q;
- Diagnostic(ErrorCode.WRN_UnreferencedField, "q").WithArguments("Q.q"));
- var classQ = comp.GlobalNamespace.GetMember<NamedTypeSymbol>("Q");
- var fieldQ = classQ.GetMember<FieldSymbol>("q");
- Assert.Equal(classQ, fieldQ.Type);
- var tree = comp.SyntaxTrees.Single();
- var model = comp.GetSemanticModel(tree);
- var aliasDecl = tree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
- …
Large files files are truncated, but you can click here to view the full file