CompilationCreationTests.cs 149.9 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6

using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
7
using System.Text;
P
Pilchie 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
using Microsoft.CodeAnalysis.CSharp.Symbols.Retargeting;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
    public class CompilationCreationTests : CSharpTestBase
    {
21
        #region Helpers
P
Pilchie 已提交
22

23 24 25 26
        private static SyntaxTree CreateSyntaxTree(string className)
        {
            var text = string.Format("public partial class {0} {{ }}", className);
            var path = string.Format("{0}.cs", className);
27
            return SyntaxFactory.ParseSyntaxTree(text, path: path);
P
Pilchie 已提交
28 29
        }

30
        private static void CheckCompilationSyntaxTrees(CSharpCompilation compilation, params SyntaxTree[] expectedSyntaxTrees)
P
Pilchie 已提交
31
        {
32
            ImmutableArray<SyntaxTree> actualSyntaxTrees = compilation.SyntaxTrees;
P
Pilchie 已提交
33

34
            int numTrees = expectedSyntaxTrees.Length;
P
Pilchie 已提交
35

36 37
            Assert.Equal(numTrees, actualSyntaxTrees.Length);
            for (int i = 0; i < numTrees; i++)
P
Pilchie 已提交
38
            {
39
                Assert.Equal(expectedSyntaxTrees[i], actualSyntaxTrees[i]);
P
Pilchie 已提交
40 41
            }

42 43 44
            for (int i = 0; i < numTrees; i++)
            {
                for (int j = 0; j < numTrees; j++)
P
Pilchie 已提交
45
                {
46
                    Assert.Equal(Math.Sign(compilation.CompareSyntaxTreeOrdering(expectedSyntaxTrees[i], expectedSyntaxTrees[j])), Math.Sign(i.CompareTo(j)));
P
Pilchie 已提交
47 48 49
                }
            }

50 51
            var types = expectedSyntaxTrees.Select(tree => compilation.GetSemanticModel(tree).GetDeclaredSymbol(tree.GetCompilationUnitRoot().Members.Single())).ToArray();
            for (int i = 0; i < numTrees; i++)
P
Pilchie 已提交
52
            {
53
                for (int j = 0; j < numTrees; j++)
P
Pilchie 已提交
54
                {
55
                    Assert.Equal(Math.Sign(compilation.CompareSourceLocations(types[i].Locations[0], types[j].Locations[0])), Math.Sign(i.CompareTo(j)));
P
Pilchie 已提交
56 57 58 59
                }
            }
        }

60 61
        #endregion

P
Pilchie 已提交
62
        [Fact]
63
        public void CorLibTypes()
P
Pilchie 已提交
64
        {
65
            var mdTestLib1 = TestReferences.SymbolsTests.MDTestLib1;
P
Pilchie 已提交
66

67
            var c1 = CSharpCompilation.Create("Test", references: new MetadataReference[] { MscorlibRef_v4_0_30316_17626, mdTestLib1 });
P
Pilchie 已提交
68

69
            TypeSymbol c107 = c1.GlobalNamespace.GetTypeMembers("C107").Single();
P
Pilchie 已提交
70

71
            Assert.Equal(SpecialType.None, c107.SpecialType);
P
Pilchie 已提交
72

73 74 75 76 77 78
            for (int i = 1; i <= (int)SpecialType.Count; i++)
            {
                NamedTypeSymbol type = c1.GetSpecialType((SpecialType)i);
                Assert.False(type.IsErrorType());
                Assert.Equal((SpecialType)i, type.SpecialType);
            }
P
Pilchie 已提交
79

80
            Assert.Equal(SpecialType.None, c107.SpecialType);
P
Pilchie 已提交
81

82
            var arrayOfc107 = new ArrayTypeSymbol(c1.Assembly, c107);
P
Pilchie 已提交
83

84
            Assert.Equal(SpecialType.None, arrayOfc107.SpecialType);
P
Pilchie 已提交
85

86
            var c2 = CSharpCompilation.Create("Test", references: new[] { mdTestLib1 });
P
Pilchie 已提交
87

88
            Assert.Equal(SpecialType.None, c2.GlobalNamespace.GetTypeMembers("C107").Single().SpecialType);
P
Pilchie 已提交
89 90 91 92 93 94 95 96 97
        }

        [Fact]
        public void CyclicReference()
        {
            var mscorlibRef = TestReferences.NetFx.v4_0_30319.mscorlib;
            var cyclic2Ref = TestReferences.SymbolsTests.Cyclic.Cyclic2.dll;

            var tc1 = CSharpCompilation.Create("Cyclic1", references: new[] { mscorlibRef, cyclic2Ref });
98
            Assert.NotNull(tc1.Assembly); // force creation of SourceAssemblySymbol
P
Pilchie 已提交
99

100 101
            var cyclic1Asm = (SourceAssemblySymbol)tc1.Assembly;
            var cyclic1Mod = (SourceModuleSymbol)cyclic1Asm.Modules[0];
P
Pilchie 已提交
102

103 104
            var cyclic2Asm = (PEAssemblySymbol)tc1.GetReferencedAssemblySymbol(cyclic2Ref);
            var cyclic2Mod = (PEModuleSymbol)cyclic2Asm.Modules[0];
P
Pilchie 已提交
105

106 107 108
            Assert.Same(cyclic2Mod.GetReferencedAssemblySymbols()[1], cyclic1Asm);
            Assert.Same(cyclic1Mod.GetReferencedAssemblySymbols()[1], cyclic2Asm);
        }
P
Pilchie 已提交
109 110 111 112 113 114

        [Fact]
        public void MultiTargeting1()
        {
            var varV1MTTestLib2Ref = TestReferences.SymbolsTests.V1.MTTestLib2.dll;
            var asm1 = MetadataTestHelpers.GetSymbolsForReferences(mrefs: new[]
115 116 117
                {
                    TestReferences.NetFx.v4_0_30319.mscorlib,
                    varV1MTTestLib2Ref
P
Pilchie 已提交
118 119
                });

120 121 122 123 124 125 126 127
            Assert.Equal("mscorlib", asm1[0].Identity.Name);
            Assert.Equal(0, asm1[0].BoundReferences().Length);
            Assert.Equal("MTTestLib2", asm1[1].Identity.Name);
            Assert.Equal(1, (from a in asm1[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm1[1].BoundReferences() where object.ReferenceEquals(a, asm1[0]) select a).Count());
            Assert.Equal(SymbolKind.ErrorType, asm1[1].GlobalNamespace.GetTypeMembers("Class4").
                                  Single().
                                  GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType.Kind);
P
Pilchie 已提交
128 129 130

            var asm2 = MetadataTestHelpers.GetSymbolsForReferences(new[]
                {
131 132 133
                    TestReferences.NetFx.v4_0_30319.mscorlib,
                    varV1MTTestLib2Ref,
                    TestReferences.SymbolsTests.V1.MTTestLib1.dll
P
Pilchie 已提交
134 135
                });

136
            Assert.Same(asm2[0], asm1[0]);
P
Pilchie 已提交
137

138 139 140 141 142 143
            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.NotSame(asm2[1], asm1[1]);
            Assert.Same(((PEAssemblySymbol)asm2[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[2]) select a).Count());
P
Pilchie 已提交
144

145 146 147
            var retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
148

149 150
            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
151

152 153 154 155
            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
P
Pilchie 已提交
156 157

            var varV2MTTestLib3Ref = TestReferences.SymbolsTests.V2.MTTestLib3.dll;
158
            var asm3 = MetadataTestHelpers.GetSymbolsForReferences(new[]
P
Pilchie 已提交
159
                {
160 161 162 163 164
                    TestReferences.NetFx.v4_0_30319.mscorlib,
                    varV1MTTestLib2Ref,
                    TestReferences.SymbolsTests.V2.MTTestLib1.dll,
                    varV2MTTestLib3Ref
                });
P
Pilchie 已提交
165

166
            Assert.Same(asm3[0], asm1[0]);
P
Pilchie 已提交
167

168 169 170 171 172 173 174
            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.NotSame(asm3[1], asm1[1]);
            Assert.NotSame(asm3[1], asm2[1]);
            Assert.Same(((PEAssemblySymbol)asm3[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
175

176 177 178
            var retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
179

180 181
            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
182

183 184 185 186 187 188
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(((PEAssemblySymbol)asm3[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
P
Pilchie 已提交
189

190 191 192 193 194
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(3, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
195

196 197
            var type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
198

199
            var retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
200

201 202
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
203

204
            var retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
205

206 207
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
208

209
            var retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
210

211 212
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
213 214 215 216 217 218 219 220 221 222 223

            var varV3MTTestLib4Ref = TestReferences.SymbolsTests.V3.MTTestLib4.dll;
            var asm4 = MetadataTestHelpers.GetSymbolsForReferences(new MetadataReference[]
                {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV1MTTestLib2Ref,
                TestReferences.SymbolsTests.V3.MTTestLib1.dll,
                varV2MTTestLib3Ref,
                varV3MTTestLib4Ref
            });

224
            Assert.Same(asm3[0], asm1[0]);
P
Pilchie 已提交
225

226 227 228 229 230 231 232 233
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], asm1[1]);
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((PEAssemblySymbol)asm4[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
234

235 236 237
            var retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
238

239 240
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
241

242 243 244 245 246 247 248 249
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm3[2]).Assembly);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
P
Pilchie 已提交
250

251 252 253 254 255 256 257
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((PEAssemblySymbol)asm4[3]).Assembly, ((PEAssemblySymbol)asm3[3]).Assembly);
            Assert.Equal(3, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
258

259 260
            var type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
261

262
            var retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
263

264 265
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
266

267
            var retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
268

269 270
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
271

272
            var retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
273

274 275
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
276

277 278 279 280 281 282
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(4, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[3]) select a).Count());
P
Pilchie 已提交
283

284 285
            var type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
286

287
            var retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
288

289 290
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
291

292
            var retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
293

294 295
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
296

297
            var retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
298

299 300
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
301

302
            var retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
303

304 305
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
306

307
            var retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
308

309 310
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
311 312 313 314 315 316 317

            var asm5 = MetadataTestHelpers.GetSymbolsForReferences(new[]
                {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV2MTTestLib3Ref
            });

318 319
            Assert.Same(asm5[0], asm1[0]);
            Assert.True(asm5[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm3[3]));
P
Pilchie 已提交
320 321 322 323 324 325 326

            var asm6 = MetadataTestHelpers.GetSymbolsForReferences(new[]
                {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV1MTTestLib2Ref
            });

327 328
            Assert.Same(asm6[0], asm1[0]);
            Assert.Same(asm6[1], asm1[1]);
P
Pilchie 已提交
329 330 331 332 333 334 335 336 337

            var asm7 = MetadataTestHelpers.GetSymbolsForReferences(new[]
                {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV1MTTestLib2Ref,
                varV2MTTestLib3Ref,
                varV3MTTestLib4Ref
            });

338 339 340 341 342
            Assert.Same(asm7[0], asm1[0]);
            Assert.Same(asm7[1], asm1[1]);
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
343

344 345 346 347 348
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((PEAssemblySymbol)asm7[2]).Assembly, ((PEAssemblySymbol)asm3[3]).Assembly);
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
P
Pilchie 已提交
349

350 351
            var type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
352

353
            var retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
354

355
            Assert.Equal(SymbolKind.ErrorType, retval15.Kind);
P
Pilchie 已提交
356

357
            var retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
358

359
            Assert.Equal(SymbolKind.ErrorType, retval16.Kind);
P
Pilchie 已提交
360

361
            var retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
362

363 364
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
365

366 367 368 369 370 371
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((PEAssemblySymbol)asm7[3]).Assembly, ((PEAssemblySymbol)asm4[4]).Assembly);
            Assert.Equal(3, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[2]) select a).Count());
P
Pilchie 已提交
372

373 374
            var type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
375

376
            var retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
377

378
            Assert.Equal(SymbolKind.ErrorType, retval18.Kind);
P
Pilchie 已提交
379

380
            var retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
381

382
            Assert.Equal(SymbolKind.ErrorType, retval19.Kind);
P
Pilchie 已提交
383

384
            var retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
385

386
            Assert.Equal(SymbolKind.ErrorType, retval20.Kind);
P
Pilchie 已提交
387

388
            var retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
389

390 391
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
392

393
            var retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
394

395 396
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
397

398
            // This test shows that simple reordering of references doesn't pick different set of assemblies
P
Pilchie 已提交
399 400 401 402 403 404 405 406
            var asm8 = MetadataTestHelpers.GetSymbolsForReferences(new[]
                {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV3MTTestLib4Ref,
                varV1MTTestLib2Ref,
                varV2MTTestLib3Ref
            });

407 408 409 410 411 412 413
            Assert.Same(asm8[0], asm1[0]);
            Assert.Same(asm8[0], asm1[0]);
            Assert.Same(asm8[2], asm7[1]);
            Assert.True(asm8[3].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[3]));
            Assert.Same(asm8[3], asm7[2]);
            Assert.True(asm8[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
            Assert.Same(asm8[1], asm7[3]);
P
Pilchie 已提交
414 415 416 417 418 419 420

            var asm9 = MetadataTestHelpers.GetSymbolsForReferences(new[]
            {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV3MTTestLib4Ref
            });

421 422
            Assert.Same(asm9[0], asm1[0]);
            Assert.True(asm9[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
P
Pilchie 已提交
423 424 425 426 427 428 429 430 431 432

            var asm10 = MetadataTestHelpers.GetSymbolsForReferences(new[]
            {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                varV1MTTestLib2Ref,
                TestReferences.SymbolsTests.V3.MTTestLib1.dll,
                varV2MTTestLib3Ref,
                varV3MTTestLib4Ref
            });

433 434 435 436 437
            Assert.Same(asm10[0], asm1[0]);
            Assert.Same(asm10[1], asm4[1]);
            Assert.Same(asm10[2], asm4[2]);
            Assert.Same(asm10[3], asm4[3]);
            Assert.Same(asm10[4], asm4[4]);
P
Pilchie 已提交
438

439 440 441
            // Run the same tests again to make sure we didn't corrupt prior state by loading additional assemblies
            Assert.Equal("MTTestLib2", asm1[1].Identity.Name);
            Assert.Equal(1, (from a in asm1[1].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
442
            Assert.Equal(1, (from a in asm1[1].BoundReferences() where ReferenceEquals(a, asm1[0]) select a).Count());
443 444 445
            Assert.Equal(SymbolKind.ErrorType, asm1[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType.Kind);
P
Pilchie 已提交
446

447
            Assert.Same(asm2[0], asm1[0]);
P
Pilchie 已提交
448

449 450 451 452
            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.NotSame(asm2[1], asm1[1]);
            Assert.Same(((PEAssemblySymbol)asm2[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
453 454 455
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where ReferenceEquals(a, asm2[2]) select a).Count());

456 457 458
            retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
459

460 461
            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
462

463 464 465
            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
466 467
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where ReferenceEquals(a, asm2[0]) select a).Count());

468
            Assert.Same(asm3[0], asm1[0]);
P
Pilchie 已提交
469

470 471 472 473 474
            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.NotSame(asm3[1], asm1[1]);
            Assert.NotSame(asm3[1], asm2[1]);
            Assert.Same(((PEAssemblySymbol)asm3[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
475 476 477
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where ReferenceEquals(a, asm3[2]) select a).Count());

478 479 480
            retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
481

482 483
            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
484

485 486 487 488 489
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(((PEAssemblySymbol)asm3[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
490 491
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where ReferenceEquals(a, asm3[0]) select a).Count());

492 493
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(3, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
494 495 496 497
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where ReferenceEquals(a, asm3[2]) select a).Count());

498 499
            type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
500

501
            retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
502

503 504
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
505

506
            retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
507

508 509
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
510

511
            retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
512

513 514
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
515

516
            Assert.Same(asm3[0], asm1[0]);
P
Pilchie 已提交
517

518 519 520 521 522 523
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], asm1[1]);
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((PEAssemblySymbol)asm4[1]).Assembly, ((PEAssemblySymbol)asm1[1]).Assembly);
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
524 525 526
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where ReferenceEquals(a, asm4[2]) select a).Count());

527 528 529
            retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
530

531 532
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
533

534 535 536 537 538 539 540
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm3[2]).Assembly);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
541 542
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where ReferenceEquals(a, asm4[0]) select a).Count());

543 544 545 546
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((PEAssemblySymbol)asm4[3]).Assembly, ((PEAssemblySymbol)asm3[3]).Assembly);
            Assert.Equal(3, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
547 548 549 550
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where ReferenceEquals(a, asm4[2]) select a).Count());

551 552
            type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
553

554
            retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
555

556 557
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
558

559
            retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
560

561 562
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
563

564
            retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
565

566 567
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
568

569 570
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(4, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
571 572 573 574 575
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where ReferenceEquals(a, asm4[3]) select a).Count());

576 577
            type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
578

579
            retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
580

581 582
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
583

584
            retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
585

586 587
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
588

589
            retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
590

591 592
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
593

594
            retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
595

596 597
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
598

599
            retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
600

601 602
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
603

604 605 606 607 608
            Assert.Same(asm7[0], asm1[0]);
            Assert.Same(asm7[1], asm1[1]);
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
609

610 611 612
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((PEAssemblySymbol)asm7[2]).Assembly, ((PEAssemblySymbol)asm3[3]).Assembly);
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
613 614 615
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where ReferenceEquals(a, asm7[1]) select a).Count());

616 617
            type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
618

619
            retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
620

621
            Assert.Equal(SymbolKind.ErrorType, retval15.Kind);
P
Pilchie 已提交
622

623
            retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
624

625
            Assert.Equal(SymbolKind.ErrorType, retval16.Kind);
P
Pilchie 已提交
626

627
            retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
628

629 630
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
631

632 633 634
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((PEAssemblySymbol)asm7[3]).Assembly, ((PEAssemblySymbol)asm4[4]).Assembly);
            Assert.Equal(3, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
P
Pilchie 已提交
635 636 637 638
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where ReferenceEquals(a, asm7[2]) select a).Count());

639 640
            type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
641

642
            retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
643

644
            Assert.Equal(SymbolKind.ErrorType, retval18.Kind);
P
Pilchie 已提交
645

646
            retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
647

648
            Assert.Equal(SymbolKind.ErrorType, retval19.Kind);
P
Pilchie 已提交
649

650
            retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
651

652
            Assert.Equal(SymbolKind.ErrorType, retval20.Kind);
P
Pilchie 已提交
653

654
            retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
655

656 657
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
658

659
            retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
660

661 662 663
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
        }
P
Pilchie 已提交
664 665 666 667

        [Fact]
        public void MultiTargeting2()
        {
668
            var varMTTestLib1_V1_Name = new AssemblyIdentity("MTTestLib1", new Version("1.0.0.0"));
P
Pilchie 已提交
669

670 671 672 673
            var varC_MTTestLib1_V1 = CreateCompilation(varMTTestLib1_V1_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.V1.MTTestModule1.netmodule
                               @"
P
Pilchie 已提交
674 675 676 677 678
public class Class1
{
}
"
                               },
679
                           new[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
680

681
            var asm_MTTestLib1_V1 = varC_MTTestLib1_V1.SourceAssembly().BoundReferences();
P
Pilchie 已提交
682

683
            var varMTTestLib2_Name = new AssemblyIdentity("MTTestLib2");
P
Pilchie 已提交
684

685 686 687 688
            var varC_MTTestLib2 = CreateCompilation(varMTTestLib2_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.V1.MTTestModule2.netmodule
                               @"
P
Pilchie 已提交
689 690 691 692 693 694 695 696 697 698 699 700
public class Class4
{
    Class1 Foo()
    {
        return null;
    }

    public Class1 Bar;

}
"
                               },
701
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib1_V1.ToMetadataReference() });
P
Pilchie 已提交
702

703
            var asm_MTTestLib2 = varC_MTTestLib2.SourceAssembly().BoundReferences();
P
Pilchie 已提交
704

705 706
            Assert.Same(asm_MTTestLib2[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm_MTTestLib2[1], varC_MTTestLib1_V1.SourceAssembly());
P
Pilchie 已提交
707

708 709 710 711 712 713 714 715
            var c2 = CreateCompilation(new AssemblyIdentity("c2"),
                           null,
                           new MetadataReference[]
                               {
                                   TestReferences.NetFx.v4_0_30319.mscorlib,
                                   varC_MTTestLib2.ToMetadataReference(),
                                   varC_MTTestLib1_V1.ToMetadataReference()
                               });
P
Pilchie 已提交
716

717
            var asm2 = c2.SourceAssembly().BoundReferences();
P
Pilchie 已提交
718

719 720 721
            Assert.Same(asm2[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm2[1], varC_MTTestLib2.SourceAssembly());
            Assert.Same(asm2[2], varC_MTTestLib1_V1.SourceAssembly());
P
Pilchie 已提交
722

723 724 725 726
            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[2]) select a).Count());
P
Pilchie 已提交
727

728 729 730
            var retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
731

732 733
            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
734

735 736 737 738
            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
P
Pilchie 已提交
739

740
            var varMTTestLib1_V2_Name = new AssemblyIdentity("MTTestLib1", new Version("2.0.0.0"));
P
Pilchie 已提交
741

742 743 744 745
            var varC_MTTestLib1_V2 = CreateCompilation(varMTTestLib1_V2_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.V2.MTTestModule1.netmodule
                               @"
P
Pilchie 已提交
746 747 748 749 750 751 752 753 754
public class Class1
{
}

public class Class2
{
}
"
                               },
755
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
756

757
            var asm_MTTestLib1_V2 = varC_MTTestLib1_V2.SourceAssembly().BoundReferences();
P
Pilchie 已提交
758

759
            var varMTTestLib3_Name = new AssemblyIdentity("MTTestLib3");
P
Pilchie 已提交
760

761 762
            var varC_MTTestLib3 = CreateCompilation(varMTTestLib3_Name,
                new string[] {
P
Pilchie 已提交
763
                    // AssemblyPaths.SymbolsTests.V2.MTTestModule3.netmodule
764
                    @"
P
Pilchie 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
public class Class5
{
    Class1 Foo1()
    {
        return null;
    }

    Class2 Foo2()
    {
        return null;
    }

    Class4 Foo3()
    {
        return null;
    }

    Class1 Bar1;
    Class2 Bar2;
    Class4 Bar3;
}
"
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
                   },
               new MetadataReference[]
                   {
                       TestReferences.NetFx.v4_0_30319.mscorlib,
                       varC_MTTestLib2.ToMetadataReference(),
                       varC_MTTestLib1_V2.ToMetadataReference()
                   });

            var asm_MTTestLib3 = varC_MTTestLib3.SourceAssembly().BoundReferences();

            Assert.Same(asm_MTTestLib3[0], asm_MTTestLib1_V1[0]);
            Assert.NotSame(asm_MTTestLib3[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm_MTTestLib3[2], varC_MTTestLib1_V1.SourceAssembly());

            var c3 = CreateCompilation(new AssemblyIdentity("c3"),
                null,
                new MetadataReference[]
                    {
                        TestReferences.NetFx.v4_0_30319.mscorlib,
                        varC_MTTestLib2.ToMetadataReference(),
                        varC_MTTestLib1_V2.ToMetadataReference(),
                        varC_MTTestLib3.ToMetadataReference()
                    });
P
Pilchie 已提交
810

811
            var asm3 = c3.SourceAssembly().BoundReferences();
P
Pilchie 已提交
812

813 814 815 816
            Assert.Same(asm3[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm3[1], asm_MTTestLib3[1]);
            Assert.Same(asm3[2], asm_MTTestLib3[2]);
            Assert.Same(asm3[3], varC_MTTestLib3.SourceAssembly());
P
Pilchie 已提交
817

818 819 820 821 822
            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm3[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
823

824 825 826
            var retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
827

828 829
            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
830

831 832 833 834 835 836
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(asm3[2].DeclaringCompilation, asm2[2].DeclaringCompilation);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
P
Pilchie 已提交
837

838 839 840 841 842
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(3, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
843

844 845
            var type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
846

847
            var retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
848

849 850
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
851

852
            var retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
853

854 855
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
856

857
            var retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
858

859 860
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
861

862
            var varMTTestLib1_V3_Name = new AssemblyIdentity("MTTestLib1", new Version("3.0.0.0"));
P
Pilchie 已提交
863

864 865 866 867
            var varC_MTTestLib1_V3 = CreateCompilation(varMTTestLib1_V3_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.V3.MTTestModule1.netmodule
                               @"
P
Pilchie 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880
public class Class1
{
}

public class Class2
{
}

public class Class3
{
}
"
                               },
881
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
882

883
            var asm_MTTestLib1_V3 = varC_MTTestLib1_V3.SourceAssembly().BoundReferences();
P
Pilchie 已提交
884

885
            var varMTTestLib4_Name = new AssemblyIdentity("MTTestLib4");
P
Pilchie 已提交
886

887 888 889 890
            var varC_MTTestLib4 = CreateCompilation(varMTTestLib4_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.V3.MTTestModule4.netmodule
                               @"
P
Pilchie 已提交
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
public class Class6
{
    Class1 Foo1()
    {
        return null;
    }

    Class2 Foo2()
    {
        return null;
    }

    Class3 Foo3()
    {
        return null;
    }

    Class4 Foo4()
    {
        return null;
    }

    Class5 Foo5()
    {
        return null;
    }

    Class1 Bar1;
    Class2 Bar2;
    Class3 Bar3;
    Class4 Bar4;
    Class5 Bar5;

}
"
                               },
927 928
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib,
                                    varC_MTTestLib2.ToMetadataReference(), varC_MTTestLib1_V3.ToMetadataReference(), varC_MTTestLib3.ToMetadataReference() });
P
Pilchie 已提交
929

930
            var asm_MTTestLib4 = varC_MTTestLib4.SourceAssembly().BoundReferences();
P
Pilchie 已提交
931

932 933 934 935
            Assert.Same(asm_MTTestLib4[0], asm_MTTestLib1_V1[0]);
            Assert.NotSame(asm_MTTestLib4[1], varC_MTTestLib2.SourceAssembly());
            Assert.Same(asm_MTTestLib4[2], varC_MTTestLib1_V3.SourceAssembly());
            Assert.NotSame(asm_MTTestLib4[3], varC_MTTestLib3.SourceAssembly());
P
Pilchie 已提交
936

937 938 939 940 941 942 943 944 945 946
            var c4 = CreateCompilation(new AssemblyIdentity("c4"),
                           null,
                           new MetadataReference[]
                               {
                                   TestReferences.NetFx.v4_0_30319.mscorlib,
                                   varC_MTTestLib2.ToMetadataReference(),
                                   varC_MTTestLib1_V3.ToMetadataReference(),
                                   varC_MTTestLib3.ToMetadataReference(),
                                   varC_MTTestLib4.ToMetadataReference()
                               });
P
Pilchie 已提交
947

948
            var asm4 = c4.SourceAssembly().BoundReferences();
P
Pilchie 已提交
949

950 951 952 953 954
            Assert.Same(asm4[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm4[1], asm_MTTestLib4[1]);
            Assert.Same(asm4[2], asm_MTTestLib4[2]);
            Assert.Same(asm4[3], asm_MTTestLib4[3]);
            Assert.Same(asm4[4], varC_MTTestLib4.SourceAssembly());
P
Pilchie 已提交
955

956 957 958 959 960 961 962 963
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
964

965 966 967
            var retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
968

969 970
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
971

972 973 974 975 976 977 978 979
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(asm4[2].DeclaringCompilation, asm2[2].DeclaringCompilation);
            Assert.NotSame(asm4[2].DeclaringCompilation, asm3[2].DeclaringCompilation);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
P
Pilchie 已提交
980

981 982 983 984 985 986 987
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[3]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(3, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
988

989 990
            var type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
991

992
            var retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
993

994 995
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
996

997
            var retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
998

999 1000
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1001

1002
            var retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1003

1004 1005
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1006

1007 1008 1009 1010 1011 1012
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(4, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[3]) select a).Count());
P
Pilchie 已提交
1013

1014 1015
            var type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1016

1017
            var retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1018

1019 1020
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1021

1022
            var retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1023

1024 1025
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1026

1027
            var retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1028

1029 1030
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
1031

1032
            var retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1033

1034 1035
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1036

1037
            var retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1038

1039 1040
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1041

1042 1043 1044
            var c5 = CreateCompilation(new AssemblyIdentity("c5"),
                           null,
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib3.ToMetadataReference() });
P
Pilchie 已提交
1045

1046
            var asm5 = c5.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1047

1048 1049
            Assert.Same(asm5[0], asm2[0]);
            Assert.True(asm5[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm3[3]));
P
Pilchie 已提交
1050

1051 1052 1053
            var c6 = CreateCompilation(new AssemblyIdentity("c6"),
                           null,
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib2.ToMetadataReference() });
P
Pilchie 已提交
1054

1055
            var asm6 = c6.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1056

1057 1058
            Assert.Same(asm6[0], asm2[0]);
            Assert.True(asm6[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
P
Pilchie 已提交
1059

1060 1061 1062
            var c7 = CreateCompilation(new AssemblyIdentity("c7"),
                           null,
                          new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib2.ToMetadataReference(), varC_MTTestLib3.ToMetadataReference(), varC_MTTestLib4.ToMetadataReference() });
P
Pilchie 已提交
1063

1064
            var asm7 = c7.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1065

1066 1067 1068 1069 1070
            Assert.Same(asm7[0], asm2[0]);
            Assert.True(asm7[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
1071

1072 1073 1074 1075 1076
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[2]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
P
Pilchie 已提交
1077

1078 1079
            var type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1080

1081
            var retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1082

1083 1084
            Assert.Equal("MTTestLib1", retval15.ContainingAssembly.Name);
            Assert.Equal(0, (from a in asm7 where a != null && a.Name == "MTTestLib1" select a).Count());
P
Pilchie 已提交
1085

1086
            var retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1087

1088
            Assert.Equal("MTTestLib1", retval16.ContainingAssembly.Name);
P
Pilchie 已提交
1089

1090
            var retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1091

1092 1093
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1094

1095 1096 1097 1098 1099 1100
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[3]).UnderlyingAssembly, asm4[4]);
            Assert.Equal(3, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[2]) select a).Count());
P
Pilchie 已提交
1101

1102 1103
            var type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1104

1105
            var retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1106

1107
            Assert.Equal("MTTestLib1", retval18.ContainingAssembly.Name);
P
Pilchie 已提交
1108

1109
            var retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1110

1111
            Assert.Equal("MTTestLib1", retval19.ContainingAssembly.Name);
P
Pilchie 已提交
1112

1113
            var retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1114

1115
            Assert.Equal("MTTestLib1", retval20.ContainingAssembly.Name);
P
Pilchie 已提交
1116

1117
            var retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1118

1119 1120
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1121

1122
            var retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1123

1124 1125
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1126

1127 1128 1129 1130
            // This test shows that simple reordering of references doesn't pick different set of assemblies
            var c8 = CreateCompilation(new AssemblyIdentity("c8"),
                           null,
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib4.ToMetadataReference(), varC_MTTestLib2.ToMetadataReference(), varC_MTTestLib3.ToMetadataReference() });
P
Pilchie 已提交
1131

1132
            var asm8 = c8.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1133

1134 1135 1136 1137 1138 1139 1140
            Assert.Same(asm8[0], asm2[0]);
            Assert.True(asm8[2].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[1]));
            Assert.Same(asm8[2], asm7[1]);
            Assert.True(asm8[3].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[3]));
            Assert.Same(asm8[3], asm7[2]);
            Assert.True(asm8[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
            Assert.Same(asm8[1], asm7[3]);
P
Pilchie 已提交
1141

1142 1143 1144
            var c9 = CreateCompilation(new AssemblyIdentity("c9"),
                           null,
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib, varC_MTTestLib4.ToMetadataReference() });
P
Pilchie 已提交
1145

1146
            var asm9 = c9.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1147

1148 1149
            Assert.Same(asm9[0], asm2[0]);
            Assert.True(asm9[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
P
Pilchie 已提交
1150

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
            var c10 = CreateCompilation(new AssemblyIdentity("c10"),
                           null,
                           new MetadataReference[] {
                                   TestReferences.NetFx.v4_0_30319.mscorlib,
                                   varC_MTTestLib2.ToMetadataReference(),
                                   varC_MTTestLib1_V3.ToMetadataReference(),
                                   varC_MTTestLib3.ToMetadataReference(),
                                   varC_MTTestLib4.ToMetadataReference() });

            var asm10 = c10.SourceAssembly().BoundReferences();

            Assert.Same(asm10[0], asm2[0]);
            Assert.Same(asm10[1], asm4[1]);
            Assert.Same(asm10[2], asm4[2]);
            Assert.Same(asm10[3], asm4[3]);
            Assert.Same(asm10[4], asm4[4]);

            // Run the same tests again to make sure we didn't corrupt prior state by loading additional assemblies
            Assert.Same(asm2[0], asm_MTTestLib1_V1[0]);

            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(1, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[2]) select a).Count());

            retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;

            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());

            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());

            Assert.Same(asm_MTTestLib3[0], asm_MTTestLib1_V1[0]);
            Assert.NotSame(asm_MTTestLib3[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm_MTTestLib3[2], varC_MTTestLib1_V1.SourceAssembly());

            Assert.Same(asm3[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm3[1], asm_MTTestLib3[1]);
            Assert.Same(asm3[2], asm_MTTestLib3[2]);
            Assert.Same(asm3[3], varC_MTTestLib3.SourceAssembly());

            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm3[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());

            retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;

            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1209

1210 1211 1212 1213 1214 1215
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(asm3[2].DeclaringCompilation, asm2[2].DeclaringCompilation);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
P
Pilchie 已提交
1216

1217 1218 1219 1220 1221
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(3, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(1, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
1222

1223 1224
            type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1225

1226
            retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1227

1228 1229
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1230

1231
            retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1232

1233 1234
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1235

1236
            retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1237

1238 1239
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1240

1241 1242 1243 1244 1245
            Assert.Same(asm4[0], asm_MTTestLib1_V1[0]);
            Assert.Same(asm4[1], asm_MTTestLib4[1]);
            Assert.Same(asm4[2], asm_MTTestLib4[2]);
            Assert.Same(asm4[3], asm_MTTestLib4[3]);
            Assert.Same(asm4[4], varC_MTTestLib4.SourceAssembly());
P
Pilchie 已提交
1246

1247 1248 1249 1250 1251 1252 1253 1254
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1255

1256 1257 1258
            retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1259

1260 1261
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1262

1263 1264 1265 1266 1267 1268 1269 1270
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(asm4[2].DeclaringCompilation, asm2[2].DeclaringCompilation);
            Assert.NotSame(asm4[2].DeclaringCompilation, asm3[2].DeclaringCompilation);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
P
Pilchie 已提交
1271

1272 1273 1274 1275 1276 1277 1278
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[3]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(3, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1279

1280 1281
            type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1282

1283
            retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1284

1285 1286
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1287

1288
            retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1289

1290 1291
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1292

1293
            retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1294

1295 1296
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1297

1298 1299 1300 1301 1302 1303
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(4, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(1, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[3]) select a).Count());
P
Pilchie 已提交
1304

1305 1306
            type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1307

1308
            retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1309

1310 1311
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1312

1313
            retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1314

1315 1316
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1317

1318
            retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1319

1320 1321
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
1322

1323
            retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1324

1325 1326
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1327

1328
            retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1329

1330 1331
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1332

1333 1334
            Assert.Same(asm5[0], asm2[0]);
            Assert.True(asm5[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm3[3]));
P
Pilchie 已提交
1335

1336 1337
            Assert.Same(asm6[0], asm2[0]);
            Assert.True(asm6[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
P
Pilchie 已提交
1338

1339 1340 1341 1342 1343
            Assert.Same(asm7[0], asm2[0]);
            Assert.True(asm7[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
1344

1345 1346 1347 1348 1349
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[2]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
P
Pilchie 已提交
1350

1351 1352
            type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1353

1354
            retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1355

1356 1357
            Assert.Equal("MTTestLib1", retval15.ContainingAssembly.Name);
            Assert.Equal(0, (from a in asm7 where a != null && a.Name == "MTTestLib1" select a).Count());
P
Pilchie 已提交
1358

1359
            retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1360

1361
            Assert.Equal("MTTestLib1", retval16.ContainingAssembly.Name);
P
Pilchie 已提交
1362

1363
            retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1364

1365 1366
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1367

1368 1369 1370 1371 1372 1373
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[3]).UnderlyingAssembly, asm4[4]);
            Assert.Equal(3, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(1, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[2]) select a).Count());
P
Pilchie 已提交
1374

1375 1376
            type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1377

1378
            retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1379

1380
            Assert.Equal("MTTestLib1", retval18.ContainingAssembly.Name);
P
Pilchie 已提交
1381

1382
            retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1383

1384
            Assert.Equal("MTTestLib1", retval19.ContainingAssembly.Name);
P
Pilchie 已提交
1385

1386
            retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1387

1388
            Assert.Equal("MTTestLib1", retval20.ContainingAssembly.Name);
P
Pilchie 已提交
1389

1390
            retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1391

1392 1393
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1394

1395
            retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1396

1397 1398
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1399 1400 1401 1402 1403
        }

        [Fact]
        public void MultiTargeting3()
        {
1404
            var varMTTestLib2_Name = new AssemblyIdentity("MTTestLib2");
P
Pilchie 已提交
1405

1406 1407
            var varC_MTTestLib2 = CreateCompilation(varMTTestLib2_Name, (string[])null,
                           new[] {
P
Pilchie 已提交
1408 1409 1410 1411 1412
                                        TestReferences.NetFx.v4_0_30319.mscorlib,
                                        TestReferences.SymbolsTests.V1.MTTestLib1.dll,
                                        TestReferences.SymbolsTests.V1.MTTestModule2.netmodule
                                     });

1413
            var asm_MTTestLib2 = varC_MTTestLib2.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1414

1415 1416 1417
            var c2 = CreateCompilation(new AssemblyIdentity("c2"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1418 1419 1420 1421 1422
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           TestReferences.SymbolsTests.V1.MTTestLib1.dll,
                                                           new CSharpCompilationReference(varC_MTTestLib2)
                                                       });

1423 1424
            var asm2Prime = c2.SourceAssembly().BoundReferences();
            var asm2 = new AssemblySymbol[] { asm2Prime[0], asm2Prime[2], asm2Prime[1] };
P
Pilchie 已提交
1425

1426 1427 1428
            Assert.Same(asm2[0], asm_MTTestLib2[0]);
            Assert.Same(asm2[1], varC_MTTestLib2.SourceAssembly());
            Assert.Same(asm2[2], asm_MTTestLib2[1]);
P
Pilchie 已提交
1429

1430 1431 1432 1433
            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.Equal(4, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[2]) select a).Count());
P
Pilchie 已提交
1434

1435 1436 1437
            var retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1438

1439 1440 1441
            Assert.Same(retval1, asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Bar").OfType<FieldSymbol>().Single().Type);
P
Pilchie 已提交
1442

1443 1444
            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1445

1446 1447 1448 1449
            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
P
Pilchie 已提交
1450

1451
            var varMTTestLib3_Name = new AssemblyIdentity("MTTestLib3");
P
Pilchie 已提交
1452

1453 1454 1455
            var varC_MTTestLib3 = CreateCompilation(varMTTestLib3_Name,
                           null,
                           new MetadataReference[]
P
Pilchie 已提交
1456 1457 1458 1459 1460 1461 1462
                               {
                                    TestReferences.NetFx.v4_0_30319.mscorlib,
                                    TestReferences.SymbolsTests.V2.MTTestLib1.dll,
                                    new CSharpCompilationReference(varC_MTTestLib2),
                                    TestReferences.SymbolsTests.V2.MTTestModule3.netmodule
                               });

1463 1464
            var asm_MTTestLib3Prime = varC_MTTestLib3.SourceAssembly().BoundReferences();
            var asm_MTTestLib3 = new AssemblySymbol[] { asm_MTTestLib3Prime[0], asm_MTTestLib3Prime[2], asm_MTTestLib3Prime[1] };
P
Pilchie 已提交
1465

1466 1467 1468
            Assert.Same(asm_MTTestLib3[0], asm_MTTestLib2[0]);
            Assert.NotSame(asm_MTTestLib3[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm_MTTestLib3[2], asm_MTTestLib2[1]);
P
Pilchie 已提交
1469

1470 1471 1472
            var c3 = CreateCompilation(new AssemblyIdentity("c3"),
                           null,
                           new MetadataReference[]
P
Pilchie 已提交
1473 1474 1475 1476 1477 1478 1479
                               {
                                   TestReferences.NetFx.v4_0_30319.mscorlib,
                                   TestReferences.SymbolsTests.V2.MTTestLib1.dll,
                                   new CSharpCompilationReference(varC_MTTestLib2),
                                   new CSharpCompilationReference(varC_MTTestLib3)
                               });

1480 1481
            var asm3Prime = c3.SourceAssembly().BoundReferences();
            var asm3 = new AssemblySymbol[] { asm3Prime[0], asm3Prime[2], asm3Prime[1], asm3Prime[3] };
P
Pilchie 已提交
1482

1483 1484 1485 1486
            Assert.Same(asm3[0], asm_MTTestLib2[0]);
            Assert.Same(asm3[1], asm_MTTestLib3[1]);
            Assert.Same(asm3[2], asm_MTTestLib3[2]);
            Assert.Same(asm3[3], varC_MTTestLib3.SourceAssembly());
P
Pilchie 已提交
1487

1488 1489 1490 1491 1492
            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm3[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(4, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
1493

1494 1495 1496
            var retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1497

1498 1499 1500
            Assert.Same(retval2, asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Bar").OfType<FieldSymbol>().Single().Type);
P
Pilchie 已提交
1501

1502 1503
            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1504

1505 1506 1507 1508 1509 1510 1511
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(((PEAssemblySymbol)asm3[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
P
Pilchie 已提交
1512

1513 1514 1515 1516 1517
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(6, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
1518

1519
            var type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").Single();
P
Pilchie 已提交
1520

1521
            var retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1522

1523 1524
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1525

1526
            var retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1527

1528 1529
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1530

1531
            var retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1532

1533 1534
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1535

1536
            var varMTTestLib4_Name = new AssemblyIdentity("MTTestLib4");
P
Pilchie 已提交
1537

1538 1539 1540
            var varC_MTTestLib4 = CreateCompilation(varMTTestLib4_Name,
                           null,
                           new MetadataReference[]
P
Pilchie 已提交
1541 1542 1543 1544 1545 1546 1547 1548
                                {
                                    TestReferences.NetFx.v4_0_30319.mscorlib,
                                    TestReferences.SymbolsTests.V3.MTTestLib1.dll,
                                    new CSharpCompilationReference(varC_MTTestLib2),
                                    new CSharpCompilationReference(varC_MTTestLib3),
                                    TestReferences.SymbolsTests.V3.MTTestModule4.netmodule
                                });

1549 1550
            var asm_MTTestLib4Prime = varC_MTTestLib4.SourceAssembly().BoundReferences();
            var asm_MTTestLib4 = new AssemblySymbol[] { asm_MTTestLib4Prime[0], asm_MTTestLib4Prime[2], asm_MTTestLib4Prime[1], asm_MTTestLib4Prime[3] };
P
Pilchie 已提交
1551

1552 1553 1554 1555 1556
            Assert.Same(asm_MTTestLib4[0], asm_MTTestLib2[0]);
            Assert.NotSame(asm_MTTestLib4[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm_MTTestLib4[2], asm3[2]);
            Assert.NotSame(asm_MTTestLib4[2], asm2[2]);
            Assert.NotSame(asm_MTTestLib4[3], varC_MTTestLib3.SourceAssembly());
P
Pilchie 已提交
1557

1558 1559 1560
            var c4 = CreateCompilation(new AssemblyIdentity("c4"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1561 1562 1563 1564 1565 1566 1567
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           TestReferences.SymbolsTests.V3.MTTestLib1.dll,
                                                           new CSharpCompilationReference(varC_MTTestLib2),
                                                           new CSharpCompilationReference(varC_MTTestLib3),
                                                           new CSharpCompilationReference(varC_MTTestLib4)
                                                       });

1568 1569
            var asm4Prime = c4.SourceAssembly().BoundReferences();
            var asm4 = new AssemblySymbol[] { asm4Prime[0], asm4Prime[2], asm4Prime[1], asm4Prime[3], asm4Prime[4] };
P
Pilchie 已提交
1570

1571 1572 1573 1574 1575
            Assert.Same(asm4[0], asm_MTTestLib2[0]);
            Assert.Same(asm4[1], asm_MTTestLib4[1]);
            Assert.Same(asm4[2], asm_MTTestLib4[2]);
            Assert.Same(asm4[3], asm_MTTestLib4[3]);
            Assert.Same(asm4[4], varC_MTTestLib4.SourceAssembly());
P
Pilchie 已提交
1576

1577 1578 1579 1580 1581 1582 1583 1584
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(4, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1585

1586 1587 1588
            var retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1589

1590 1591
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1592

1593 1594 1595 1596 1597 1598 1599 1600
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm3[2]).Assembly);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
P
Pilchie 已提交
1601

1602 1603 1604 1605 1606 1607 1608
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[3]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(6, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1609

1610 1611
            var type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1612

1613
            var retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1614

1615 1616
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1617

1618
            var retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1619

1620 1621
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1622

1623
            var retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1624

1625 1626
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1627

1628 1629 1630 1631 1632 1633
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(8, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[3]) select a).Count());
P
Pilchie 已提交
1634

1635 1636
            var type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1637

1638
            var retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1639

1640 1641
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1642

1643
            var retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1644

1645 1646
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1647

1648
            var retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1649

1650 1651
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
1652

1653
            var retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1654

1655 1656
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1657

1658
            var retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1659

1660 1661
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1662

1663 1664 1665
            var c5 = CreateCompilation(new AssemblyIdentity("c5"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1666 1667 1668 1669
                                                            TestReferences.NetFx.v4_0_30319.mscorlib,
                                                            new CSharpCompilationReference(varC_MTTestLib3)
                                                       });

1670
            var asm5 = c5.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1671

1672 1673
            Assert.Same(asm5[0], asm2[0]);
            Assert.True(asm5[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm3[3]));
P
Pilchie 已提交
1674

1675 1676 1677
            var c6 = CreateCompilation(new AssemblyIdentity("c6"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1678 1679 1680 1681
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           new CSharpCompilationReference(varC_MTTestLib2)
                                                       });

1682
            var asm6 = c6.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1683

1684 1685
            Assert.Same(asm6[0], asm2[0]);
            Assert.True(asm6[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
P
Pilchie 已提交
1686

1687 1688 1689
            var c7 = CreateCompilation(new AssemblyIdentity("c7"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1690 1691 1692 1693 1694 1695
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           new CSharpCompilationReference(varC_MTTestLib2),
                                                           new CSharpCompilationReference(varC_MTTestLib3),
                                                           new CSharpCompilationReference(varC_MTTestLib4)
                                                       });

1696
            var asm7 = c7.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1697

1698 1699 1700 1701 1702
            Assert.Same(asm7[0], asm2[0]);
            Assert.True(asm7[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
1703

1704 1705 1706 1707 1708
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[2]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(4, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
P
Pilchie 已提交
1709

1710 1711
            var type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1712

1713
            var retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1714

1715
            AssemblySymbol missingAssembly;
P
Pilchie 已提交
1716

1717
            missingAssembly = retval15.ContainingAssembly;
P
Pilchie 已提交
1718

1719 1720
            Assert.True(missingAssembly.IsMissing);
            Assert.Equal("MTTestLib1", missingAssembly.Identity.Name);
P
Pilchie 已提交
1721

1722
            var retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1723

1724
            Assert.Same(missingAssembly, retval16.ContainingAssembly);
P
Pilchie 已提交
1725

1726
            var retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1727

1728 1729
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1730

1731 1732 1733 1734 1735 1736
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[3]).UnderlyingAssembly, asm4[4]);
            Assert.Equal(6, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[2]) select a).Count());
P
Pilchie 已提交
1737

1738 1739
            var type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1740

1741
            var retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1742

1743
            Assert.Equal("MTTestLib1", ((MissingMetadataTypeSymbol)retval18).ContainingAssembly.Identity.Name);
P
Pilchie 已提交
1744

1745
            var retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1746

1747
            Assert.Same(retval18.ContainingAssembly, retval19.ContainingAssembly);
P
Pilchie 已提交
1748

1749
            var retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1750

1751
            Assert.Same(retval18.ContainingAssembly, retval20.ContainingAssembly);
P
Pilchie 已提交
1752

1753
            var retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1754

1755 1756
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1757

1758
            var retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1759

1760 1761
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1762

1763 1764 1765 1766
            // This test shows that simple reordering of references doesn't pick different set of assemblies
            var c8 = CreateCompilation(new AssemblyIdentity("c8"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1767 1768 1769 1770 1771 1772
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           new CSharpCompilationReference(varC_MTTestLib4),
                                                           new CSharpCompilationReference(varC_MTTestLib2),
                                                           new CSharpCompilationReference(varC_MTTestLib3)
                                                       });

1773
            var asm8 = c8.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1774

1775 1776 1777 1778 1779 1780 1781
            Assert.Same(asm8[0], asm2[0]);
            Assert.True(asm8[2].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[1]));
            Assert.Same(asm8[2], asm7[1]);
            Assert.True(asm8[3].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[3]));
            Assert.Same(asm8[3], asm7[2]);
            Assert.True(asm8[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
            Assert.Same(asm8[1], asm7[3]);
P
Pilchie 已提交
1782

1783 1784 1785
            var c9 = CreateCompilation(new AssemblyIdentity("c9"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1786 1787 1788 1789
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           new CSharpCompilationReference(varC_MTTestLib4)
                                                       });

1790
            var asm9 = c9.SourceAssembly().BoundReferences();
P
Pilchie 已提交
1791

1792 1793
            Assert.Same(asm9[0], asm2[0]);
            Assert.True(asm9[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm4[4]));
P
Pilchie 已提交
1794

1795 1796 1797
            var c10 = CreateCompilation(new AssemblyIdentity("c10"),
                           null,
                           new MetadataReference[] {
P
Pilchie 已提交
1798 1799 1800 1801 1802 1803 1804
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           TestReferences.SymbolsTests.V3.MTTestLib1.dll,
                                                           new CSharpCompilationReference(varC_MTTestLib2),
                                                           new CSharpCompilationReference(varC_MTTestLib3),
                                                           new CSharpCompilationReference(varC_MTTestLib4)
                                                       });

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
            var asm10Prime = c10.SourceAssembly().BoundReferences();
            var asm10 = new AssemblySymbol[] { asm10Prime[0], asm10Prime[2], asm10Prime[1], asm10Prime[3], asm10Prime[4] };

            Assert.Same(asm10[0], asm2[0]);
            Assert.Same(asm10[1], asm4[1]);
            Assert.Same(asm10[2], asm4[2]);
            Assert.Same(asm10[3], asm4[3]);
            Assert.Same(asm10[4], asm4[4]);

            // Run the same tests again to make sure we didn't corrupt prior state by loading additional assemblies
            Assert.Same(asm2[0], asm_MTTestLib2[0]);

            Assert.Equal("MTTestLib2", asm2[1].Identity.Name);
            Assert.Equal(4, (from a in asm2[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());
            Assert.Equal(2, (from a in asm2[1].BoundReferences() where object.ReferenceEquals(a, asm2[2]) select a).Count());

            retval1 = asm2[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;

            Assert.NotEqual(SymbolKind.ErrorType, retval1.Kind);
            Assert.Same(retval1, asm2[2].GlobalNamespace.GetMembers("Class1").Single());

            Assert.Equal("MTTestLib1", asm2[2].Identity.Name);
            Assert.Equal(1, asm2[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm2[2].BoundReferences() where object.ReferenceEquals(a, asm2[0]) select a).Count());

            Assert.Same(asm_MTTestLib3[0], asm_MTTestLib2[0]);
            Assert.NotSame(asm_MTTestLib3[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm_MTTestLib3[2], asm_MTTestLib2[1]);

            Assert.Same(asm3[0], asm_MTTestLib2[0]);
            Assert.Same(asm3[1], asm_MTTestLib3[1]);
            Assert.Same(asm3[2], asm_MTTestLib3[2]);
            Assert.Same(asm3[3], varC_MTTestLib3.SourceAssembly());
P
Pilchie 已提交
1842

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
            Assert.Equal("MTTestLib2", asm3[1].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm3[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(4, (from a in asm3[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(2, (from a in asm3[1].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());

            retval2 = asm3[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;

            Assert.NotEqual(SymbolKind.ErrorType, retval2.Kind);
            Assert.Same(retval2, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1855

1856 1857 1858 1859 1860 1861
            Assert.Equal("MTTestLib1", asm3[2].Identity.Name);
            Assert.NotSame(asm3[2], asm2[2]);
            Assert.NotSame(((PEAssemblySymbol)asm3[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.Equal(2, asm3[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm3[2].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
P
Pilchie 已提交
1862

1863 1864 1865 1866 1867
            Assert.Equal("MTTestLib3", asm3[3].Identity.Name);
            Assert.Equal(6, (from a in asm3[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[0]) select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[1]) select a).Count());
            Assert.Equal(2, (from a in asm3[3].BoundReferences() where object.ReferenceEquals(a, asm3[2]) select a).Count());
P
Pilchie 已提交
1868

1869 1870
            type1 = asm3[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1871

1872
            retval3 = type1.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1873

1874 1875
            Assert.NotEqual(SymbolKind.ErrorType, retval3.Kind);
            Assert.Same(retval3, asm3[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1876

1877
            retval4 = type1.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1878

1879 1880
            Assert.NotEqual(SymbolKind.ErrorType, retval4.Kind);
            Assert.Same(retval4, asm3[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1881

1882
            retval5 = type1.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1883

1884 1885
            Assert.NotEqual(SymbolKind.ErrorType, retval5.Kind);
            Assert.Same(retval5, asm3[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1886

1887 1888 1889 1890 1891
            Assert.Same(asm4[0], asm_MTTestLib2[0]);
            Assert.Same(asm4[1], asm_MTTestLib4[1]);
            Assert.Same(asm4[2], asm_MTTestLib4[2]);
            Assert.Same(asm4[3], asm_MTTestLib4[3]);
            Assert.Same(asm4[4], varC_MTTestLib4.SourceAssembly());
P
Pilchie 已提交
1892

1893 1894 1895 1896 1897 1898 1899 1900
            Assert.Equal("MTTestLib2", asm4[1].Identity.Name);
            Assert.NotSame(asm4[1], varC_MTTestLib2.SourceAssembly());
            Assert.NotSame(asm4[1], asm2[1]);
            Assert.NotSame(asm4[1], asm3[1]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[1]).UnderlyingAssembly, varC_MTTestLib2.SourceAssembly());
            Assert.Equal(4, (from a in asm4[1].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[1].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1901

1902 1903 1904
            retval6 = asm4[1].GlobalNamespace.GetTypeMembers("Class4").
                          Single().
                          GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1905

1906 1907
            Assert.NotEqual(SymbolKind.ErrorType, retval6.Kind);
            Assert.Same(retval6, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1908

1909 1910 1911 1912 1913 1914 1915 1916
            Assert.Equal("MTTestLib1", asm4[2].Identity.Name);
            Assert.NotSame(asm4[2], asm2[2]);
            Assert.NotSame(asm4[2], asm3[2]);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm2[2]).Assembly);
            Assert.NotSame(((PEAssemblySymbol)asm4[2]).Assembly, ((PEAssemblySymbol)asm3[2]).Assembly);
            Assert.Equal(3, asm4[2].Identity.Version.Major);
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(1, (from a in asm4[2].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
P
Pilchie 已提交
1917

1918 1919 1920 1921 1922 1923 1924
            Assert.Equal("MTTestLib3", asm4[3].Identity.Name);
            Assert.NotSame(asm4[3], asm3[3]);
            Assert.Same(((RetargetingAssemblySymbol)asm4[3]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(6, (from a in asm4[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(2, (from a in asm4[3].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
P
Pilchie 已提交
1925

1926 1927
            type2 = asm4[3].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1928

1929
            retval7 = type2.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1930

1931 1932
            Assert.NotEqual(SymbolKind.ErrorType, retval7.Kind);
            Assert.Same(retval7, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1933

1934
            retval8 = type2.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1935

1936 1937
            Assert.NotEqual(SymbolKind.ErrorType, retval8.Kind);
            Assert.Same(retval8, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1938

1939
            retval9 = type2.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1940

1941 1942
            Assert.NotEqual(SymbolKind.ErrorType, retval9.Kind);
            Assert.Same(retval9, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1943

1944 1945 1946 1947 1948 1949
            Assert.Equal("MTTestLib4", asm4[4].Identity.Name);
            Assert.Equal(8, (from a in asm4[4].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[0]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[1]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[2]) select a).Count());
            Assert.Equal(2, (from a in asm4[4].BoundReferences() where object.ReferenceEquals(a, asm4[3]) select a).Count());
P
Pilchie 已提交
1950

1951 1952
            type3 = asm4[4].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
1953

1954
            retval10 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1955

1956 1957
            Assert.NotEqual(SymbolKind.ErrorType, retval10.Kind);
            Assert.Same(retval10, asm4[2].GlobalNamespace.GetMembers("Class1").Single());
P
Pilchie 已提交
1958

1959
            retval11 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1960

1961 1962
            Assert.NotEqual(SymbolKind.ErrorType, retval11.Kind);
            Assert.Same(retval11, asm4[2].GlobalNamespace.GetMembers("Class2").Single());
P
Pilchie 已提交
1963

1964
            retval12 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1965

1966 1967
            Assert.NotEqual(SymbolKind.ErrorType, retval12.Kind);
            Assert.Same(retval12, asm4[2].GlobalNamespace.GetMembers("Class3").Single());
P
Pilchie 已提交
1968

1969
            retval13 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1970

1971 1972
            Assert.NotEqual(SymbolKind.ErrorType, retval13.Kind);
            Assert.Same(retval13, asm4[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
1973

1974
            retval14 = type3.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
1975

1976 1977
            Assert.NotEqual(SymbolKind.ErrorType, retval14.Kind);
            Assert.Same(retval14, asm4[3].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
1978

1979 1980
            Assert.Same(asm5[0], asm2[0]);
            Assert.True(asm5[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(asm3[3]));
P
Pilchie 已提交
1981

1982 1983
            Assert.Same(asm6[0], asm2[0]);
            Assert.True(asm6[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
P
Pilchie 已提交
1984

1985 1986 1987 1988 1989
            Assert.Same(asm7[0], asm2[0]);
            Assert.True(asm7[1].RepresentsTheSameAssemblyButHasUnresolvedReferencesByComparisonTo(varC_MTTestLib2.SourceAssembly()));
            Assert.NotSame(asm7[2], asm3[3]);
            Assert.NotSame(asm7[2], asm4[3]);
            Assert.NotSame(asm7[3], asm4[4]);
P
Pilchie 已提交
1990

1991 1992 1993 1994 1995
            Assert.Equal("MTTestLib3", asm7[2].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[2]).UnderlyingAssembly, asm3[3]);
            Assert.Equal(4, (from a in asm7[2].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(2, (from a in asm7[2].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
P
Pilchie 已提交
1996

1997 1998
            type4 = asm7[2].GlobalNamespace.GetTypeMembers("Class5").
                          Single();
P
Pilchie 已提交
1999

2000
            retval15 = type4.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2001

2002
            missingAssembly = retval15.ContainingAssembly;
P
Pilchie 已提交
2003

2004 2005
            Assert.True(missingAssembly.IsMissing);
            Assert.Equal("MTTestLib1", missingAssembly.Identity.Name);
P
Pilchie 已提交
2006

2007
            retval16 = type4.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2008

2009
            Assert.Same(missingAssembly, retval16.ContainingAssembly);
P
Pilchie 已提交
2010

2011
            retval17 = type4.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2012

2013 2014
            Assert.NotEqual(SymbolKind.ErrorType, retval17.Kind);
            Assert.Same(retval17, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
2015

2016 2017 2018 2019 2020 2021
            Assert.Equal("MTTestLib4", asm7[3].Identity.Name);
            Assert.Same(((RetargetingAssemblySymbol)asm7[3]).UnderlyingAssembly, asm4[4]);
            Assert.Equal(6, (from a in asm7[3].BoundReferences() where !a.IsMissing select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[0]) select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[1]) select a).Count());
            Assert.Equal(2, (from a in asm7[3].BoundReferences() where object.ReferenceEquals(a, asm7[2]) select a).Count());
P
Pilchie 已提交
2022

2023 2024
            type5 = asm7[3].GlobalNamespace.GetTypeMembers("Class6").
                          Single();
P
Pilchie 已提交
2025

2026
            retval18 = type5.GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2027

2028
            Assert.Equal("MTTestLib1", ((MissingMetadataTypeSymbol)retval18).ContainingAssembly.Identity.Name);
P
Pilchie 已提交
2029

2030
            retval19 = type5.GetMembers("Foo2").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2031

2032
            Assert.Same(retval18.ContainingAssembly, retval19.ContainingAssembly);
P
Pilchie 已提交
2033

2034
            retval20 = type5.GetMembers("Foo3").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2035

2036
            Assert.Same(retval18.ContainingAssembly, retval20.ContainingAssembly);
P
Pilchie 已提交
2037

2038
            retval21 = type5.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2039

2040 2041
            Assert.NotEqual(SymbolKind.ErrorType, retval21.Kind);
            Assert.Same(retval21, asm7[1].GlobalNamespace.GetMembers("Class4").Single());
P
Pilchie 已提交
2042

2043
            retval22 = type5.GetMembers("Foo5").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2044

2045 2046
            Assert.NotEqual(SymbolKind.ErrorType, retval22.Kind);
            Assert.Same(retval22, asm7[2].GlobalNamespace.GetMembers("Class5").Single());
P
Pilchie 已提交
2047 2048 2049 2050 2051
        }

        [Fact]
        public void MultiTargeting4()
        {
2052
            var localC1_V1_Name = new AssemblyIdentity("c1", new Version("1.0.0.0"));
P
Pilchie 已提交
2053

2054 2055 2056 2057
            var localC1_V1 = CreateCompilation(localC1_V1_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source1Module.netmodule
                               @"
P
Pilchie 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
public class C1<T>
{
    public class C2<S>
    {
        public C1<T>.C2<S> Foo()
        {
            return null;
        }
    }
}
"
                               },
2070
                           new[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
2071

2072
            var asm1_V1 = localC1_V1.SourceAssembly();
P
Pilchie 已提交
2073

2074
            var localC1_V2_Name = new AssemblyIdentity("c1", new Version("2.0.0.0"));
P
Pilchie 已提交
2075

2076 2077 2078 2079
            var localC1_V2 = CreateCompilation(localC1_V2_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source1Module.netmodule
                               @"
P
Pilchie 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
public class C1<T>
{
    public class C2<S>
    {
        public C1<T>.C2<S> Foo()
        {
            return null;
        }
    }
}
"
                               },
2092
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
2093

2094
            var asm1_V2 = localC1_V2.SourceAssembly();
P
Pilchie 已提交
2095

2096
            var localC4_V1_Name = new AssemblyIdentity("c4", new Version("1.0.0.0"));
P
Pilchie 已提交
2097

2098 2099 2100 2101
            var localC4_V1 = CreateCompilation(localC4_V1_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source4Module.netmodule
                               @"
P
Pilchie 已提交
2102 2103 2104 2105 2106
public class C4
{
}
"
                               },
2107
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
2108

2109
            var asm4_V1 = localC4_V1.SourceAssembly();
P
Pilchie 已提交
2110

2111
            var localC4_V2_Name = new AssemblyIdentity("c4", new Version("2.0.0.0"));
P
Pilchie 已提交
2112

2113 2114 2115 2116
            var localC4_V2 = CreateCompilation(localC4_V2_Name,
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source4Module.netmodule
                               @"
P
Pilchie 已提交
2117 2118 2119 2120 2121
public class C4
{
}
"
                               },
2122
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
2123

2124
            var asm4_V2 = localC4_V2.SourceAssembly();
P
Pilchie 已提交
2125

2126 2127 2128 2129
            var c7 = CreateCompilation(new AssemblyIdentity("C7"),
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source7Module.netmodule
                               @"
P
Pilchie 已提交
2130 2131 2132 2133 2134 2135 2136
public class C7
{}

public class C8<T>
{ }
"
                               },
2137
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib });
P
Pilchie 已提交
2138

2139
            var asm7 = c7.SourceAssembly();
P
Pilchie 已提交
2140

2141 2142 2143 2144
            var c3 = CreateCompilation(new AssemblyIdentity("C3"),
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source3Module.netmodule
                               @"
P
Pilchie 已提交
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
public class C3
{
    public C1<C3>.C2<C4> Foo()
    {
        return null;
    }

    public static C6<C4> Bar()
    {
        return null;
    }

    public C8<C7> Foo1()
    {
        return null;
    }

    public void Foo2(ref C300[,] x1,
                    out C4 x2,
                    ref C7[] x3,
                    C4 x4 = null)
    {
        x2 = null;
    }

    internal virtual TFoo3 Foo3<TFoo3>() where TFoo3: C4
    {
        return null;
    }

    public C8<C4> Foo4()
    {
        return null;
    }

    public abstract class C301 :
        I1
    {
    }

    internal class C302
    {
    }

}

public class C6<T> where T: new ()
{}

public class C300
{}

public interface I1
{}

namespace ns1
{
    namespace ns2
    {
        public class C303
        {}

    }

    public class C304
    {
        public class C305
        {}

    }

}
"
                               },
2219
                           new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib,
P
Pilchie 已提交
2220 2221 2222 2223 2224
                                                           new CSharpCompilationReference(localC1_V1),
                                                           new CSharpCompilationReference(localC4_V1),
                                                           new CSharpCompilationReference(c7)
                                                       });

2225
            var asm3 = c3.SourceAssembly();
P
Pilchie 已提交
2226

2227 2228
            var localC3Foo2 = asm3.GlobalNamespace.GetTypeMembers("C3").
                          Single().GetMembers("Foo2").OfType<MethodSymbol>().Single();
P
Pilchie 已提交
2229

2230 2231 2232 2233
            var c5 = CreateCompilation(new AssemblyIdentity("C5"),
                           new string[] {
                               // AssemblyPaths.SymbolsTests.MultiTargeting.Source5Module.netmodule
                               @"
P
Pilchie 已提交
2234 2235 2236 2237 2238
public class C5 :
    ns1.C304.C305
{}
"
                               },
2239
                           new MetadataReference[] {
P
Pilchie 已提交
2240 2241 2242 2243 2244 2245 2246
                                                           TestReferences.NetFx.v4_0_30319.mscorlib,
                                                           new CSharpCompilationReference(c3),
                                                           new CSharpCompilationReference(localC1_V2),
                                                           new CSharpCompilationReference(localC4_V2),
                                                           new CSharpCompilationReference(c7)
                                                       });

2247
            var asm5 = c5.SourceAssembly().BoundReferences();
P
Pilchie 已提交
2248

2249 2250 2251 2252 2253
            Assert.NotSame(asm5[1], asm3);
            Assert.Same(((RetargetingAssemblySymbol)asm5[1]).UnderlyingAssembly, asm3);
            Assert.Same(asm5[2], asm1_V2);
            Assert.Same(asm5[3], asm4_V2);
            Assert.Same(asm5[4], asm7);
P
Pilchie 已提交
2254

2255 2256
            var type3 = asm5[1].GlobalNamespace.GetTypeMembers("C3").
                          Single();
P
Pilchie 已提交
2257

2258 2259
            var type1 = asm1_V2.GlobalNamespace.GetTypeMembers("C1").
                          Single();
P
Pilchie 已提交
2260

2261 2262
            var type2 = type1.GetTypeMembers("C2").
                          Single();
P
Pilchie 已提交
2263

2264 2265
            var type4 = asm4_V2.GlobalNamespace.GetTypeMembers("C4").
                          Single();
P
Pilchie 已提交
2266

2267
            var retval1 = (NamedTypeSymbol)type3.GetMembers("Foo").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2268

2269
            Assert.Equal("C1<C3>.C2<C4>", retval1.ToTestDisplayString());
P
Pilchie 已提交
2270

2271
            Assert.Same(retval1.OriginalDefinition, type2);
P
Pilchie 已提交
2272

2273 2274
            var args1 = retval1.ContainingType.TypeArguments.Concat(retval1.TypeArguments);
            var params1 = retval1.ContainingType.TypeParameters.Concat(retval1.TypeParameters);
P
Pilchie 已提交
2275

2276 2277
            Assert.Same(params1[0], type1.TypeParameters[0]);
            Assert.Same(params1[1].OriginalDefinition, type2.TypeParameters[0].OriginalDefinition);
P
Pilchie 已提交
2278

2279 2280 2281
            Assert.Same(args1[0], type3);
            Assert.Same(args1[0].ContainingAssembly, asm5[1]);
            Assert.Same(args1[1], type4);
P
Pilchie 已提交
2282

2283
            var retval2 = retval1.ContainingType;
P
Pilchie 已提交
2284

2285 2286
            Assert.Equal("C1<C3>", retval2.ToTestDisplayString());
            Assert.Same(retval2.OriginalDefinition, type1);
P
Pilchie 已提交
2287

2288 2289 2290 2291
            var bar = type3.GetMembers("Bar").OfType<MethodSymbol>().Single();
            var retval3 = (NamedTypeSymbol)bar.ReturnType;
            var type6 = asm5[1].GlobalNamespace.GetTypeMembers("C6").
                          Single();
P
Pilchie 已提交
2292

2293
            Assert.Equal("C6<C4>", retval3.ToTestDisplayString());
P
Pilchie 已提交
2294

2295 2296
            Assert.Same(retval3.OriginalDefinition, type6);
            Assert.Same(retval3.ContainingAssembly, asm5[1]);
P
Pilchie 已提交
2297

2298 2299
            var args3 = retval3.TypeArguments;
            var params3 = retval3.TypeParameters;
P
Pilchie 已提交
2300

2301 2302 2303
            Assert.Same(params3[0], type6.TypeParameters[0]);
            Assert.Same(params3[0].ContainingAssembly, asm5[1]);
            Assert.Same(args3[0], type4);
P
Pilchie 已提交
2304

2305 2306
            var foo1 = type3.GetMembers("Foo1").OfType<MethodSymbol>().Single();
            var retval4 = foo1.ReturnType;
P
Pilchie 已提交
2307

2308
            Assert.Equal("C8<C7>", retval4.ToTestDisplayString());
P
Pilchie 已提交
2309

2310 2311 2312 2313
            Assert.Same(retval4,
                          asm3.GlobalNamespace.GetTypeMembers("C3").
                          Single().
                          GetMembers("Foo1").OfType<MethodSymbol>().Single().ReturnType);
P
Pilchie 已提交
2314

2315 2316
            var foo1Params = foo1.Parameters;
            Assert.Equal(0, foo1Params.Length);
P
Pilchie 已提交
2317

2318 2319 2320 2321
            var foo2 = type3.GetMembers("Foo2").OfType<MethodSymbol>().Single();
            Assert.NotEqual(localC3Foo2, foo2);
            Assert.Same(localC3Foo2, ((RetargetingMethodSymbol)foo2).UnderlyingMethod);
            Assert.Equal(1, ((RetargetingMethodSymbol)foo2).Locations.Length);
P
Pilchie 已提交
2322

2323 2324 2325 2326 2327 2328
            var foo2Params = foo2.Parameters;
            Assert.Equal(4, foo2Params.Length);
            Assert.Same(localC3Foo2.Parameters[0], ((RetargetingParameterSymbol)foo2Params[0]).UnderlyingParameter);
            Assert.Same(localC3Foo2.Parameters[1], ((RetargetingParameterSymbol)foo2Params[1]).UnderlyingParameter);
            Assert.Same(localC3Foo2.Parameters[2], ((RetargetingParameterSymbol)foo2Params[2]).UnderlyingParameter);
            Assert.Same(localC3Foo2.Parameters[3], ((RetargetingParameterSymbol)foo2Params[3]).UnderlyingParameter);
P
Pilchie 已提交
2329

2330 2331 2332 2333
            var x1 = foo2Params[0];
            var x2 = foo2Params[1];
            var x3 = foo2Params[2];
            var x4 = foo2Params[3];
P
Pilchie 已提交
2334

2335 2336 2337 2338 2339 2340 2341 2342 2343
            Assert.Equal("x1", x1.Name);
            Assert.NotEqual(localC3Foo2.Parameters[0].Type, x1.Type);
            Assert.Equal(localC3Foo2.Parameters[0].ToTestDisplayString(), x1.ToTestDisplayString());
            Assert.Same(asm5[1], x1.ContainingAssembly);
            Assert.Same(foo2, x1.ContainingSymbol);
            Assert.False(x1.HasExplicitDefaultValue);
            Assert.False(x1.IsOptional);
            Assert.Equal(RefKind.Ref, x1.RefKind);
            Assert.Equal(2, ((ArrayTypeSymbol)x1.Type).Rank);
P
Pilchie 已提交
2344

2345 2346 2347
            Assert.Equal("x2", x2.Name);
            Assert.NotEqual(localC3Foo2.Parameters[1].Type, x2.Type);
            Assert.Equal(RefKind.Out, x2.RefKind);
P
Pilchie 已提交
2348

2349 2350
            Assert.Equal("x3", x3.Name);
            Assert.Same(localC3Foo2.Parameters[2].Type, x3.Type);
P
Pilchie 已提交
2351

2352 2353 2354
            Assert.Equal("x4", x4.Name);
            Assert.True(x4.HasExplicitDefaultValue);
            Assert.True(x4.IsOptional);
P
Pilchie 已提交
2355

2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
            Assert.Equal("Foo2", foo2.Name);
            Assert.Equal(localC3Foo2.ToTestDisplayString(), foo2.ToTestDisplayString());
            Assert.Same(asm5[1], foo2.ContainingAssembly);
            Assert.Same(type3, foo2.ContainingSymbol);
            Assert.Equal(Accessibility.Public, foo2.DeclaredAccessibility);
            Assert.False(foo2.HidesBaseMethodsByName);
            Assert.False(foo2.IsAbstract);
            Assert.False(foo2.IsExtern);
            Assert.False(foo2.IsGenericMethod);
            Assert.False(foo2.IsOverride);
            Assert.False(foo2.IsSealed);
            Assert.False(foo2.IsStatic);
            Assert.False(foo2.IsVararg);
            Assert.False(foo2.IsVirtual);
            Assert.True(foo2.ReturnsVoid);
            Assert.Equal(0, foo2.TypeParameters.Length);
            Assert.Equal(0, foo2.TypeArguments.Length);
P
Pilchie 已提交
2373

2374 2375
            Assert.True(bar.IsStatic);
            Assert.False(bar.ReturnsVoid);
P
Pilchie 已提交
2376

2377
            var foo3 = type3.GetMembers("Foo3").OfType<MethodSymbol>().Single();
P
Pilchie 已提交
2378

2379 2380 2381
            Assert.Equal(Accessibility.Internal, foo3.DeclaredAccessibility);
            Assert.True(foo3.IsGenericMethod);
            Assert.True(foo3.IsVirtual);
P
Pilchie 已提交
2382

2383 2384 2385 2386
            var foo3TypeParams = foo3.TypeParameters;
            Assert.Equal(1, foo3TypeParams.Length);
            Assert.Equal(1, foo3.TypeArguments.Length);
            Assert.Same(foo3TypeParams[0], foo3.TypeArguments[0]);
P
Pilchie 已提交
2387

2388 2389 2390
            var typeC301 = type3.GetTypeMembers("C301").Single();
            var typeC302 = type3.GetTypeMembers("C302").Single();
            var typeC6 = asm5[1].GlobalNamespace.GetTypeMembers("C6").Single();
P
Pilchie 已提交
2391

2392 2393 2394
            Assert.Equal(typeC301.ToTestDisplayString(),
                asm3.GlobalNamespace.GetTypeMembers("C3").Single().
                        GetTypeMembers("C301").Single().ToTestDisplayString());
P
Pilchie 已提交
2395

2396 2397
            Assert.Equal(typeC6.ToTestDisplayString(),
                asm3.GlobalNamespace.GetTypeMembers("C6").Single().ToTestDisplayString());
P
Pilchie 已提交
2398

2399 2400 2401
            Assert.Equal(typeC301.ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat),
                asm3.GlobalNamespace.GetTypeMembers("C3").Single().
                        GetTypeMembers("C301").Single().ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat));
P
Pilchie 已提交
2402

2403 2404
            Assert.Equal(typeC6.ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat),
                asm3.GlobalNamespace.GetTypeMembers("C6").Single().ToDisplayString(SymbolDisplayFormat.QualifiedNameArityFormat));
P
Pilchie 已提交
2405

2406 2407
            Assert.Equal(type3.GetMembers().Length,
                asm3.GlobalNamespace.GetTypeMembers("C3").Single().GetMembers().Length);
P
Pilchie 已提交
2408

2409 2410
            Assert.Equal(type3.GetTypeMembers().Length,
                asm3.GlobalNamespace.GetTypeMembers("C3").Single().GetTypeMembers().Length);
P
Pilchie 已提交
2411

2412
            Assert.Same(typeC301, type3.GetTypeMembers("C301", 0).Single());
P
Pilchie 已提交
2413

2414 2415
            Assert.Equal(0, type3.Arity);
            Assert.Equal(1, typeC6.Arity);
P
Pilchie 已提交
2416

2417 2418
            Assert.NotNull(type3.BaseType);
            Assert.Equal("System.Object", type3.BaseType.ToTestDisplayString());
P
Pilchie 已提交
2419

2420 2421
            Assert.Equal(Accessibility.Public, type3.DeclaredAccessibility);
            Assert.Equal(Accessibility.Internal, typeC302.DeclaredAccessibility);
P
Pilchie 已提交
2422

2423 2424 2425
            Assert.Equal(0, type3.Interfaces.Length);
            Assert.Equal(1, typeC301.Interfaces.Length);
            Assert.Equal("I1", typeC301.Interfaces.Single().Name);
P
Pilchie 已提交
2426

2427 2428
            Assert.False(type3.IsAbstract);
            Assert.True(typeC301.IsAbstract);
P
Pilchie 已提交
2429

2430 2431
            Assert.False(type3.IsSealed);
            Assert.False(type3.IsStatic);
P
Pilchie 已提交
2432

2433 2434
            Assert.Equal(0, type3.TypeArguments.Length);
            Assert.Equal(0, type3.TypeParameters.Length);
P
Pilchie 已提交
2435

2436 2437 2438 2439
            var localC6Params = typeC6.TypeParameters;
            Assert.Equal(1, localC6Params.Length);
            Assert.Equal(1, typeC6.TypeArguments.Length);
            Assert.Same(localC6Params[0], typeC6.TypeArguments[0]);
P
Pilchie 已提交
2440

2441 2442 2443
            Assert.Same(((RetargetingNamedTypeSymbol)type3).UnderlyingNamedType,
                asm3.GlobalNamespace.GetTypeMembers("C3").Single());
            Assert.Equal(1, ((RetargetingNamedTypeSymbol)type3).Locations.Length);
P
Pilchie 已提交
2444

2445 2446
            Assert.Equal(TypeKind.Class, type3.TypeKind);
            Assert.Equal(TypeKind.Interface, asm5[1].GlobalNamespace.GetTypeMembers("I1").Single().TypeKind);
P
Pilchie 已提交
2447

2448 2449
            var localC6_T = localC6Params[0];
            var foo3TypeParam = foo3TypeParams[0];
P
Pilchie 已提交
2450

2451
            Assert.Equal(0, localC6_T.ConstraintTypes.Length);
P
Pilchie 已提交
2452

2453 2454
            Assert.Equal(1, foo3TypeParam.ConstraintTypes.Length);
            Assert.Same(type4, foo3TypeParam.ConstraintTypes.Single());
P
Pilchie 已提交
2455

2456 2457
            Assert.Same(typeC6, localC6_T.ContainingSymbol);
            Assert.False(foo3TypeParam.HasConstructorConstraint);
P
Pilchie 已提交
2458

2459
            Assert.True(localC6_T.HasConstructorConstraint);
P
Pilchie 已提交
2460

2461 2462
            Assert.False(foo3TypeParam.HasReferenceTypeConstraint);
            Assert.False(foo3TypeParam.HasValueTypeConstraint);
P
Pilchie 已提交
2463

2464 2465
            Assert.Equal("TFoo3", foo3TypeParam.Name);
            Assert.Equal("T", localC6_T.Name);
P
Pilchie 已提交
2466

2467 2468
            Assert.Equal(0, foo3TypeParam.Ordinal);
            Assert.Equal(0, localC6_T.Ordinal);
P
Pilchie 已提交
2469

2470 2471 2472
            Assert.Equal(VarianceKind.None, foo3TypeParam.Variance);
            Assert.Same(((RetargetingTypeParameterSymbol)localC6_T).UnderlyingTypeParameter,
                asm3.GlobalNamespace.GetTypeMembers("C6").Single().TypeParameters[0]);
P
Pilchie 已提交
2473

2474 2475
            var ns1 = asm5[1].GlobalNamespace.GetMembers("ns1").OfType<NamespaceSymbol>().Single();
            var ns2 = ns1.GetMembers("ns2").OfType<NamespaceSymbol>().Single();
P
Pilchie 已提交
2476

2477 2478
            Assert.Equal("ns1.ns2", ns2.ToTestDisplayString());
            Assert.Equal(2, ns1.GetMembers().Length);
P
Pilchie 已提交
2479

2480 2481
            Assert.Equal(1, ns1.GetTypeMembers().Length);
            Assert.Same(ns1.GetTypeMembers("C304").Single(), ns1.GetTypeMembers("C304", 0).Single());
P
Pilchie 已提交
2482

2483 2484 2485 2486 2487 2488 2489
            Assert.Same(asm5[1].Modules[0], asm5[1].Modules[0].GlobalNamespace.ContainingSymbol);
            Assert.Same(asm5[1].Modules[0].GlobalNamespace, ns1.ContainingSymbol);
            Assert.Same(asm5[1].Modules[0], ns1.Extent.Module);
            Assert.Equal(1, ns1.ConstituentNamespaces.Length);
            Assert.Same(ns1, ns1.ConstituentNamespaces[0]);
            Assert.False(ns1.IsGlobalNamespace);
            Assert.True(asm5[1].Modules[0].GlobalNamespace.IsGlobalNamespace);
P
Pilchie 已提交
2490

2491 2492 2493 2494
            Assert.Same(asm3.Modules[0].GlobalNamespace,
                ((RetargetingNamespaceSymbol)asm5[1].Modules[0].GlobalNamespace).UnderlyingNamespace);
            Assert.Same(asm3.Modules[0].GlobalNamespace.GetMembers("ns1").Single(),
                ((RetargetingNamespaceSymbol)ns1).UnderlyingNamespace);
P
Pilchie 已提交
2495

2496
            var module3 = (RetargetingModuleSymbol)asm5[1].Modules[0];
P
Pilchie 已提交
2497

2498 2499
            Assert.Equal("C3.dll", module3.ToTestDisplayString());
            Assert.Equal("C3.dll", module3.Name);
P
Pilchie 已提交
2500

2501 2502 2503
            Assert.Same(asm5[1], module3.ContainingSymbol);
            Assert.Same(asm5[1], module3.ContainingAssembly);
            Assert.Null(module3.ContainingType);
P
Pilchie 已提交
2504

2505
            var retval5 = type3.GetMembers("Foo4").OfType<MethodSymbol>().Single().ReturnType;
P
Pilchie 已提交
2506

2507
            Assert.Equal("C8<C4>", retval5.ToTestDisplayString());
P
Pilchie 已提交
2508

2509
            var typeC5 = c5.Assembly.GlobalNamespace.GetTypeMembers("C5").Single();
P
Pilchie 已提交
2510

2511 2512 2513
            Assert.Same(asm5[1], typeC5.BaseType.ContainingAssembly);
            Assert.Equal("ns1.C304.C305", typeC5.BaseType.ToTestDisplayString());
            Assert.NotEqual(SymbolKind.ErrorType, typeC5.Kind);
P
Pilchie 已提交
2514 2515 2516 2517 2518
        }

        [Fact]
        public void MultiTargeting5()
        {
2519
            var c1_Name = new AssemblyIdentity("c1");
P
Pilchie 已提交
2520

2521
            var text = @"
P
Pilchie 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
class Module1
{
    Class4 M1()
    {}

    Class4.Class4_1 M2()
    {}

    Class4 M3()
    {}
}
";
2534
            var tree = Parse(text);
P
Pilchie 已提交
2535 2536 2537 2538 2539 2540 2541

            var c1 = CreateCompilationWithMscorlib(tree, new MetadataReference[]
            {
                TestReferences.SymbolsTests.V1.MTTestLib1.dll,
                TestReferences.SymbolsTests.V1.MTTestModule2.netmodule
            });

2542
            var c2_Name = new AssemblyIdentity("MTTestLib2");
P
Pilchie 已提交
2543 2544 2545 2546 2547 2548 2549 2550

            var c2 = CreateCompilation(c2_Name, null, new MetadataReference[]
            {
                TestReferences.NetFx.v4_0_30319.mscorlib,
                TestReferences.SymbolsTests.V2.MTTestLib1.dll,
                new CSharpCompilationReference(c1)
            });

2551 2552 2553
            SourceAssemblySymbol c1AsmSource = (SourceAssemblySymbol)c1.Assembly;
            PEAssemblySymbol Lib1_V1 = (PEAssemblySymbol)c1AsmSource.Modules[0].GetReferencedAssemblySymbols()[1];
            PEModuleSymbol module1 = (PEModuleSymbol)c1AsmSource.Modules[1];
P
Pilchie 已提交
2554

2555 2556 2557 2558 2559
            Assert.Equal(LocationKind.MetadataFile, ((MetadataLocation)Lib1_V1.Locations[0]).Kind);
            SourceAssemblySymbol c2AsmSource = (SourceAssemblySymbol)c2.Assembly;
            RetargetingAssemblySymbol c1AsmRef = (RetargetingAssemblySymbol)c2AsmSource.Modules[0].GetReferencedAssemblySymbols()[2];
            PEAssemblySymbol Lib1_V2 = (PEAssemblySymbol)c2AsmSource.Modules[0].GetReferencedAssemblySymbols()[1];
            PEModuleSymbol module2 = (PEModuleSymbol)c1AsmRef.Modules[1];
P
Pilchie 已提交
2560

2561 2562
            Assert.Equal(1, Lib1_V1.Identity.Version.Major);
            Assert.Equal(2, Lib1_V2.Identity.Version.Major);
P
Pilchie 已提交
2563

2564 2565
            Assert.NotEqual(module1, module2);
            Assert.Same(module1.Module, module2.Module);
P
Pilchie 已提交
2566

2567 2568 2569 2570
            NamedTypeSymbol classModule1 = c1AsmRef.Modules[0].GlobalNamespace.GetTypeMembers("Module1").Single();
            MethodSymbol m1 = classModule1.GetMembers("M1").OfType<MethodSymbol>().Single();
            MethodSymbol m2 = classModule1.GetMembers("M2").OfType<MethodSymbol>().Single();
            MethodSymbol m3 = classModule1.GetMembers("M3").OfType<MethodSymbol>().Single();
P
Pilchie 已提交
2571

2572 2573 2574 2575
            Assert.Same(module2, m1.ReturnType.ContainingModule);
            Assert.Same(module2, m2.ReturnType.ContainingModule);
            Assert.Same(module2, m3.ReturnType.ContainingModule);
        }
P
Pilchie 已提交
2576

2577 2578
        // Very simplistic test if a compilation has a single type with the given full name. Does NOT handle generics.
        private bool HasSingleTypeOfKind(CSharpCompilation c, TypeKind kind, string fullName)
P
Pilchie 已提交
2579
        {
2580
            string[] names = fullName.Split('.');
P
Pilchie 已提交
2581

2582 2583
            NamespaceOrTypeSymbol current = c.GlobalNamespace;
            foreach (string name in names)
P
Pilchie 已提交
2584
            {
2585 2586 2587 2588 2589
                var matchingSym = current.GetMembers(name);
                if (matchingSym.Length != 1)
                {
                    return false;
                }
P
Pilchie 已提交
2590

2591
                current = (NamespaceOrTypeSymbol)matchingSym.First();
P
Pilchie 已提交
2592
            }
2593 2594

            return current is TypeSymbol && ((TypeSymbol)current).TypeKind == kind;
P
Pilchie 已提交
2595 2596 2597
        }

        [Fact]
2598
        public void AddRemoveReferences()
P
Pilchie 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
        {
            var mscorlibRef = TestReferences.NetFx.v4_0_30319.mscorlib;
            var systemCoreRef = TestReferences.NetFx.v4_0_30319.System_Core;
            var systemRef = TestReferences.NetFx.v4_0_30319.System;

            CSharpCompilation c = CSharpCompilation.Create("Test");
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Struct, "System.Int32"));
            c = c.AddReferences(mscorlibRef);
            Assert.True(HasSingleTypeOfKind(c, TypeKind.Struct, "System.Int32"));
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Class, "System.Linq.Enumerable"));
            c = c.AddReferences(systemCoreRef);
            Assert.True(HasSingleTypeOfKind(c, TypeKind.Class, "System.Linq.Enumerable"));
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Class, "System.Uri"));
            c = c.ReplaceReference(systemCoreRef, systemRef);
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Class, "System.Linq.Enumerable"));
            Assert.True(HasSingleTypeOfKind(c, TypeKind.Class, "System.Uri"));
            c = c.RemoveReferences(systemRef);
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Class, "System.Uri"));
            Assert.True(HasSingleTypeOfKind(c, TypeKind.Struct, "System.Int32"));
            c = c.RemoveReferences(mscorlibRef);
            Assert.False(HasSingleTypeOfKind(c, TypeKind.Struct, "System.Int32"));
        }

2622
        private sealed class Resolver : TestMetadataReferenceResolver
P
Pilchie 已提交
2623
        {
C
Charles Stoner 已提交
2624
            private readonly RelativePathReferenceResolver _pathResolver;
B
beep boop 已提交
2625
            private readonly string _data, _core, _system;
P
Pilchie 已提交
2626 2627 2628

            public Resolver(string data, string core, string system)
            {
C
Charles Stoner 已提交
2629
                _pathResolver = RelativePathReferenceResolver.Default;
2630 2631 2632
                _data = data;
                _core = core;
                _system = system;
P
Pilchie 已提交
2633 2634
            }

2635
            public override string ResolveReference(string reference, string baseFileName)
P
Pilchie 已提交
2636
            {
2637
                switch (reference)
P
Pilchie 已提交
2638 2639
                {
                    case "System.Data":
2640
                        return _data;
P
Pilchie 已提交
2641 2642

                    case "System.Core":
2643
                        return _core;
P
Pilchie 已提交
2644 2645

                    case "System":
2646
                        return _system;
P
Pilchie 已提交
2647 2648

                    default:
C
Charles Stoner 已提交
2649
                        return _pathResolver.ResolveReference(reference, baseFileName);
P
Pilchie 已提交
2650 2651 2652 2653 2654 2655 2656
                }
            }
        }

        [Fact]
        public void CompilationWithReferenceDirectives()
        {
2657 2658 2659
            var data = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System_Data).Path;
            var core = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System_Core).Path;
            var system = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System).Path;
P
Pilchie 已提交
2660

2661
            var trees = new[] {
P
Pilchie 已提交
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
                SyntaxFactory.ParseSyntaxTree(@"
#r ""System.Data""
#r """ + typeof(System.Xml.Serialization.IXmlSerializable).Assembly.Location + @"""
#r """ + typeof(System.Linq.Expressions.Expression).Assembly.Location + @"""
", options: TestOptions.Script),
                SyntaxFactory.ParseSyntaxTree(@"
#r ""System""
", options: TestOptions.Script),
            SyntaxFactory.ParseSyntaxTree(@"
new System.Data.DataSet();
System.Linq.Expressions.Expression.Constant(123);
System.Diagnostics.Process.GetCurrentProcess();
", options: TestOptions.Script)
            };

2677 2678 2679
            var compilation = CSharpCompilation.Create("foo",
                syntaxTrees: trees,
                references: new[] { MscorlibRef },
2680
                options: TestOptions.ReleaseDll.WithMetadataReferenceResolver(new AssemblyReferenceResolver(new Resolver(data, core, system), MetadataFileReferenceProvider.Default)));
P
Pilchie 已提交
2681

2682
            var boundRefs = compilation.Assembly.BoundReferences();
P
Pilchie 已提交
2683

2684 2685 2686 2687 2688 2689
            Assert.Equal(5, boundRefs.Length);
            Assert.NotNull(boundRefs.FirstOrDefault(sym => sym.Name == "mscorlib"));
            Assert.NotNull(boundRefs.FirstOrDefault(sym => sym.Name == "System"));
            Assert.NotNull(boundRefs.FirstOrDefault(sym => sym.Name == "System.Core"));
            Assert.NotNull(boundRefs.FirstOrDefault(sym => sym.Name == "System.Data"));
            Assert.NotNull(boundRefs.FirstOrDefault(sym => sym.Name == "System.Xml"));
P
Pilchie 已提交
2690

2691
            compilation.VerifyDiagnostics();
P
Pilchie 已提交
2692 2693 2694 2695 2696
        }

        [Fact]
        public void CompilationWithReferenceDirectives_Errors()
        {
2697 2698 2699
            var data = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System_Data).Path;
            var core = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System_Core).Path;
            var system = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System).Path;
2700
            var mscorlibRef = MetadataReference.CreateFromAssemblyInternal(typeof(object).Assembly);
P
Pilchie 已提交
2701

2702
            var trees = new[] {
2703
                    SyntaxFactory.ParseSyntaxTree(@"
P
Pilchie 已提交
2704 2705 2706 2707 2708 2709 2710
#r System
#r ""~!@#$%^&*():\?/""
#r ""non-existing-reference""
", options: TestOptions.Script),
                SyntaxFactory.ParseSyntaxTree(@"
#r ""System.Core""
")
2711
                };
P
Pilchie 已提交
2712

2713 2714 2715 2716
            var compilation = CSharpCompilation.Create("foo",
                syntaxTrees: trees,
                references: new[] { mscorlibRef },
                options: TestOptions.ReleaseDll
2717
                    .WithMetadataReferenceResolver(new AssemblyReferenceResolver(new Resolver(data, core, system), MetadataFileReferenceProvider.Default)));
P
Pilchie 已提交
2718

2719
            compilation.VerifyDiagnostics(
P
Pilchie 已提交
2720 2721 2722 2723 2724 2725 2726
                // (3,1): error CS0006: Metadata file '~!@#$%^&*():\?/' could not be found
                Diagnostic(ErrorCode.ERR_NoMetadataFile, @"#r ""~!@#$%^&*():\?/""").WithArguments(@"~!@#$%^&*():\?/"),
                // (4,1): error CS0006: Metadata file 'non-existing-reference' could not be found
                Diagnostic(ErrorCode.ERR_NoMetadataFile, @"#r ""non-existing-reference""").WithArguments("non-existing-reference"),
                // (2,4): error CS7010: Quoted file name expected
                Diagnostic(ErrorCode.ERR_ExpectedPPFile, "System"),
                // (2,1): error CS7011: #r is only allowed in scripts
2727
                Diagnostic(ErrorCode.ERR_ReferenceDirectiveOnlyAllowedInScripts, "r"));
P
Pilchie 已提交
2728 2729
        }

2730
        private static readonly string s_resolvedPath = Path.GetPathRoot(Directory.GetCurrentDirectory()) + "RESOLVED";
P
Pilchie 已提交
2731

2732
        private class DummyFileProvider : MetadataFileReferenceProvider
P
Pilchie 已提交
2733
        {
2734
            private readonly string _targetDll;
P
Pilchie 已提交
2735 2736 2737

            public DummyFileProvider(string targetDll)
            {
2738
                _targetDll = targetDll;
P
Pilchie 已提交
2739 2740 2741 2742
            }

            public override PortableExecutableReference GetReference(string fullPath, MetadataReferenceProperties properties = default(MetadataReferenceProperties))
            {
2743
                var path = fullPath == s_resolvedPath ? _targetDll : fullPath;
2744
                return MetadataReference.CreateFromFile(path, properties);
P
Pilchie 已提交
2745 2746 2747
            }
        }

2748
        private class DummyRelativePathResolver : TestMetadataReferenceResolver
P
Pilchie 已提交
2749
        {
2750
            public override string ResolveReference(string reference, string baseFilePath)
P
Pilchie 已提交
2751
            {
2752
                return reference.EndsWith("-resolve", StringComparison.Ordinal) ? s_resolvedPath : reference;
P
Pilchie 已提交
2753 2754 2755 2756 2757 2758
            }
        }

        [Fact]
        public void MetadataReferenceProvider()
        {
2759 2760
            var csClasses01 = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
            var csInterfaces01 = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSInterfaces01).Path;
P
Pilchie 已提交
2761

2762 2763
            var provider = new DummyFileProvider(csClasses01);
            var resolver = new DummyRelativePathResolver();
P
Pilchie 已提交
2764

2765
            var source = @"
P
Pilchie 已提交
2766 2767 2768 2769 2770
#r """ + typeof(object).Assembly.Location + @"""
#r """ + "!@#$%^/&*-resolve" + @"""
#r """ + csInterfaces01 + @"""
class C : Metadata.ICSPropImpl { }";

2771 2772
            var compilation = CSharpCompilation.Create("foo",
                syntaxTrees: new[]
2773 2774 2775
                    {
                        Parse(source, options: TestOptions.Script)
                    },
2776
                options: TestOptions.ReleaseDll.WithMetadataReferenceResolver(new AssemblyReferenceResolver(resolver, provider)));
P
Pilchie 已提交
2777

2778
            compilation.VerifyDiagnostics();
P
Pilchie 已提交
2779 2780
        }

J
jaredpar 已提交
2781
        [ClrOnlyFact(ClrOnlyReason.Unknown)]
P
Pilchie 已提交
2782 2783
        public void CompilationWithReferenceDirective_RelativeToBaseDirectory()
        {
2784 2785 2786
            string path = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
            string fileName = Path.GetFileName(path);
            string dir = Path.GetDirectoryName(path);
P
Pilchie 已提交
2787

2788
            var trees = new[]
2789 2790
                {
                    SyntaxFactory.ParseSyntaxTree(@"
P
Pilchie 已提交
2791
#r "".\" + fileName + @"""
2792
", path: Path.Combine(dir, "a.csx"), options: TestOptions.Script),
2793
                };
P
Pilchie 已提交
2794

2795 2796 2797 2798 2799
            var compilation = CSharpCompilation.Create(
                "foo",
                trees,
                new[] { MscorlibRef },
                TestOptions.ReleaseDll
2800
                    .WithMetadataReferenceResolver(new AssemblyReferenceResolver(MetadataFileReferenceResolver.Default, MetadataFileReferenceProvider.Default)));
P
Pilchie 已提交
2801

2802
            compilation.VerifyDiagnostics();
P
Pilchie 已提交
2803 2804
        }

2805 2806 2807 2808
        [Fact]
        public void CompilationWithReferenceDirective_NoResolver()
        {
            var compilation = CSharpCompilation.Create("foo",
2809
                new[] { SyntaxFactory.ParseSyntaxTree(@"#r ""bar""", TestOptions.Script, "a.csx", Encoding.UTF8) },
2810
                new[] { MscorlibRef },
2811
                TestOptions.ReleaseDll.WithMetadataReferenceResolver(null));
2812 2813 2814 2815 2816 2817 2818

            compilation.VerifyDiagnostics(
                // a.csx(1,1): error CS7099: Metadata references not supported.
                // #r "bar"
                Diagnostic(ErrorCode.ERR_MetadataReferencesNotSupported, @"#r ""bar"""));
        }

J
jaredpar 已提交
2819
        [ClrOnlyFact(ClrOnlyReason.Unknown)]
P
Pilchie 已提交
2820 2821
        public void CompilationWithReferenceDirective_RelativeToBaseParent()
        {
2822 2823 2824
            string path = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
            string fileName = Path.GetFileName(path);
            string dir = Path.Combine(Path.GetDirectoryName(path), "subdir");
P
Pilchie 已提交
2825

2826
            var trees = new[]
2827 2828
                {
                    SyntaxFactory.ParseSyntaxTree(@"
P
Pilchie 已提交
2829
#r ""..\" + fileName + @"""
2830
", path: Path.Combine(dir, "a.csx"), options: TestOptions.Script),
2831
                };
P
Pilchie 已提交
2832

2833 2834 2835 2836
            var compilation = CSharpCompilation.Create("foo",
                syntaxTrees: trees,
                references: new[] { MscorlibRef },
                options: TestOptions.ReleaseDll
2837
                    .WithMetadataReferenceResolver(new AssemblyReferenceResolver(MetadataFileReferenceResolver.Default, MetadataFileReferenceProvider.Default)));
P
Pilchie 已提交
2838

2839
            compilation.VerifyDiagnostics();
P
Pilchie 已提交
2840 2841
        }

J
jaredpar 已提交
2842
        [ClrOnlyFact(ClrOnlyReason.Unknown)]
P
Pilchie 已提交
2843 2844
        public void CompilationWithReferenceDirective_RelativeToBaseRoot()
        {
2845 2846 2847
            string path = Temp.CreateFile().WriteAllBytes(TestResources.MetadataTests.InterfaceAndClass.CSClasses01).Path;
            string root = Path.GetPathRoot(path);
            string unrooted = path.Substring(root.Length);
P
Pilchie 已提交
2848

2849
            string dir = Path.Combine(root, "foo", "bar", "baz");
P
Pilchie 已提交
2850

2851
            var trees = new[]
2852 2853
                {
                    SyntaxFactory.ParseSyntaxTree(@"
P
Pilchie 已提交
2854
#r ""\" + unrooted + @"""
2855
", path: Path.Combine(dir, "a.csx"), options: TestOptions.Script),
2856
                };
P
Pilchie 已提交
2857

2858 2859 2860 2861 2862
            var compilation = CSharpCompilation.Create(
                "foo",
                trees,
                new[] { MscorlibRef },
                TestOptions.ReleaseDll
2863
                    .WithMetadataReferenceResolver(new AssemblyReferenceResolver(MetadataFileReferenceResolver.Default, MetadataFileReferenceProvider.Default)));
P
Pilchie 已提交
2864

2865
            compilation.VerifyDiagnostics();
P
Pilchie 已提交
2866 2867 2868 2869 2870
        }

        [Fact]
        public void GlobalUsings1()
        {
2871 2872
            var trees = new[]
            {
P
Pilchie 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
                SyntaxFactory.ParseSyntaxTree(@"
WriteLine(1);
Console.WriteLine(2);
", options: TestOptions.Script),
                SyntaxFactory.ParseSyntaxTree(@"
class C 
{ 
    void Foo() { Console.WriteLine(3); }
}
")
            };

            var compilation = CSharpCompilation.Create(
                "foo",
2887
                options: TestOptions.ReleaseDll.WithUsings(ImmutableArray.Create("System.Console", "System")),
P
Pilchie 已提交
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
                syntaxTrees: trees,
                references: new[] { MscorlibRef });

            var diagnostics = compilation.GetDiagnostics().ToArray();

            // global usings are only visible in script code:
            DiagnosticsUtils.VerifyErrorCodes(diagnostics,
                // (4,18): error CS0103: The name 'Console' does not exist in the current context
                new ErrorDescription() { Code = (int)ErrorCode.ERR_NameNotInContext, Line = 4, Column = 18 });
        }

        [Fact]
        public void GlobalUsings_Errors()
        {
            var trees = new[] {
                SyntaxFactory.ParseSyntaxTree(@"
WriteLine(1);
Console.WriteLine(2);
", options: TestOptions.Script)
            };

            var compilation = CSharpCompilation.Create(
                "foo",
2911
                options: TestOptions.ReleaseDll.WithUsings("System.Console!", "Blah"),
P
Pilchie 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
                syntaxTrees: trees,
                references: new[] { MscorlibRef });

            compilation.VerifyDiagnostics(
                // error CS0234: The type or namespace name 'Console!' does not exist in the namespace 'System' (are you missing an assembly reference?)
                Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS).WithArguments("Console!", "System"),
                // error CS0246: The type or namespace name 'Blah' could not be found (are you missing a using directive or an assembly reference?)
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("Blah"),
                // (2,1): error CS0103: The name 'WriteLine' does not exist in the current context
                Diagnostic(ErrorCode.ERR_NameNotInContext, "WriteLine").WithArguments("WriteLine"),
                // (3,1): error CS0103: The name 'Console' does not exist in the current context
                Diagnostic(ErrorCode.ERR_NameNotInContext, "Console").WithArguments("Console"));
        }

        [Fact]
        public void ReferenceToAssemblyWithSpecialCharactersInName()
        {
            var r = TestReferences.SymbolsTests.Metadata.InvalidCharactersInAssemblyName;

            var st = SyntaxFactory.ParseSyntaxTree("class C { static void Main() { new lib.Class1(); } }");
            var compilation = CSharpCompilation.Create("foo", references: new[] { MscorlibRef, r }, syntaxTrees: new[] { st });
            var diags = compilation.GetDiagnostics().ToArray();
            Assert.Equal(0, diags.Length);

            using (var stream = new MemoryStream())
            {
                compilation.Emit(stream);
            }
        }

        [Fact]
        public void SyntaxTreeOrderConstruct()
        {
            var tree1 = CreateSyntaxTree("A");
            var tree2 = CreateSyntaxTree("B");

            SyntaxTree[] treeOrder1 = new[] { tree1, tree2 };
            var compilation1 = CSharpCompilation.Create("Compilation1", syntaxTrees: treeOrder1);
            CheckCompilationSyntaxTrees(compilation1, treeOrder1);

            SyntaxTree[] treeOrder2 = new[] { tree2, tree1 };
            var compilation2 = CSharpCompilation.Create("Compilation2", syntaxTrees: treeOrder2);
            CheckCompilationSyntaxTrees(compilation2, treeOrder2);
        }

        [Fact]
        public void SyntaxTreeOrderAdd()
        {
            var tree1 = CreateSyntaxTree("A");
            var tree2 = CreateSyntaxTree("B");
            var tree3 = CreateSyntaxTree("C");
            var tree4 = CreateSyntaxTree("D");

            SyntaxTree[] treeList1 = new[] { tree1, tree2 };
            var compilation1 = CSharpCompilation.Create("Compilation1", syntaxTrees: treeList1);
            CheckCompilationSyntaxTrees(compilation1, treeList1);

            SyntaxTree[] treeList2 = new[] { tree3, tree4 };
            var compilation2 = compilation1.AddSyntaxTrees(treeList2);
            CheckCompilationSyntaxTrees(compilation1, treeList1); //compilation1 untouched
            CheckCompilationSyntaxTrees(compilation2, treeList1.Concat(treeList2).ToArray());

            SyntaxTree[] treeList3 = new[] { tree4, tree3 };
            var compilation3 = CSharpCompilation.Create("Compilation3", syntaxTrees: treeList3);
            CheckCompilationSyntaxTrees(compilation3, treeList3);

            SyntaxTree[] treeList4 = new[] { tree2, tree1 };
            var compilation4 = compilation3.AddSyntaxTrees(treeList4);
            CheckCompilationSyntaxTrees(compilation3, treeList3); //compilation3 untouched
            CheckCompilationSyntaxTrees(compilation4, treeList3.Concat(treeList4).ToArray());
        }

        [Fact]
        public void SyntaxTreeOrderRemove()
        {
            var tree1 = CreateSyntaxTree("A");
            var tree2 = CreateSyntaxTree("B");
            var tree3 = CreateSyntaxTree("C");
            var tree4 = CreateSyntaxTree("D");

            SyntaxTree[] treeList1 = new[] { tree1, tree2, tree3, tree4 };
            var compilation1 = CSharpCompilation.Create("Compilation1", syntaxTrees: treeList1);
            CheckCompilationSyntaxTrees(compilation1, treeList1);

            SyntaxTree[] treeList2 = new[] { tree3, tree1 };
            var compilation2 = compilation1.RemoveSyntaxTrees(treeList2);
            CheckCompilationSyntaxTrees(compilation1, treeList1); //compilation1 untouched
            CheckCompilationSyntaxTrees(compilation2, tree2, tree4);

            SyntaxTree[] treeList3 = new[] { tree4, tree3, tree2, tree1 };
            var compilation3 = CSharpCompilation.Create("Compilation3", syntaxTrees: treeList3);
            CheckCompilationSyntaxTrees(compilation3, treeList3);

            SyntaxTree[] treeList4 = new[] { tree3, tree1 };
            var compilation4 = compilation3.RemoveSyntaxTrees(treeList4);
            CheckCompilationSyntaxTrees(compilation3, treeList3); //compilation3 untouched
            CheckCompilationSyntaxTrees(compilation4, tree4, tree2);
        }

        [Fact]
        public void SyntaxTreeOrderReplace()
        {
            var tree1 = CreateSyntaxTree("A");
            var tree2 = CreateSyntaxTree("B");
            var tree3 = CreateSyntaxTree("C");

            SyntaxTree[] treeList1 = new[] { tree1, tree2 };
            var compilation1 = CSharpCompilation.Create("Compilation1", syntaxTrees: treeList1);
            CheckCompilationSyntaxTrees(compilation1, treeList1);

            var compilation2 = compilation1.ReplaceSyntaxTree(tree1, tree3);
            CheckCompilationSyntaxTrees(compilation1, treeList1); //compilation1 untouched
            CheckCompilationSyntaxTrees(compilation2, tree3, tree2);

            SyntaxTree[] treeList3 = new[] { tree2, tree1 };
            var compilation3 = CSharpCompilation.Create("Compilation3", syntaxTrees: treeList3);
            CheckCompilationSyntaxTrees(compilation3, treeList3);

            var compilation4 = compilation3.ReplaceSyntaxTree(tree1, tree3);
            CheckCompilationSyntaxTrees(compilation3, treeList3); //compilation3 untouched
            CheckCompilationSyntaxTrees(compilation4, tree2, tree3);
        }

3035
        [WorkItem(578706, "DevDiv")]
P
Pilchie 已提交
3036 3037 3038 3039 3040 3041
        [Fact]
        public void DeclaringCompilationOfAddedModule()
        {
            var source1 = "public class C1 { }";
            var source2 = "public class C2 { }";

3042
            var lib1 = CreateCompilationWithMscorlib(source1, assemblyName: "Lib1", options: TestOptions.ReleaseModule);
P
Pilchie 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
            var ref1 = lib1.EmitToImageReference(); // NOTE: can't use a compilation reference for a module.

            var lib2 = CreateCompilationWithMscorlib(source2, new[] { ref1 }, assemblyName: "Lib2");
            lib2.VerifyDiagnostics();

            var sourceAssembly = lib2.Assembly;
            var sourceModule = sourceAssembly.Modules[0];
            var sourceType = sourceModule.GlobalNamespace.GetMember<NamedTypeSymbol>("C2");

            Assert.IsType<SourceAssemblySymbol>(sourceAssembly);
            Assert.Equal(lib2, sourceAssembly.DeclaringCompilation);

            Assert.IsType<SourceModuleSymbol>(sourceModule);
            Assert.Equal(lib2, sourceModule.DeclaringCompilation);

            Assert.IsType<SourceNamedTypeSymbol>(sourceType);
            Assert.Equal(lib2, sourceType.DeclaringCompilation);


            var addedModule = sourceAssembly.Modules[1];
            var addedModuleAssembly = addedModule.ContainingAssembly;
            var addedModuleType = addedModule.GlobalNamespace.GetMember<NamedTypeSymbol>("C1");

            Assert.IsType<SourceAssemblySymbol>(addedModuleAssembly);
            Assert.Equal(lib2, addedModuleAssembly.DeclaringCompilation); //NB: not lib1, not null

            Assert.IsType<PEModuleSymbol>(addedModule);
            Assert.Null(addedModule.DeclaringCompilation);

            Assert.IsAssignableFrom<PENamedTypeSymbol>(addedModuleType);
            Assert.Null(addedModuleType.DeclaringCompilation);
        }
    }
}