public void TestCompilationReferenceLocation()
        {
            var source = @"public class C { }";

            var libRef = new CSharpCompilationReference(CreateCompilationWithMscorlib(Parse(source, "file1.cs"), assemblyName: "Metadata"));
            var comp = CreateCompilationWithMscorlib(Parse(source, "file2.cs"), new[] { libRef }, assemblyName: "Source");

            var sourceAssembly = comp.SourceAssembly;
            var referencedAssembly = (AssemblySymbol)comp.GetAssemblyOrModuleSymbol(libRef);

            var sourceType = sourceAssembly.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
            var referencedType = referencedAssembly.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
            var distinguisher = new SymbolDistinguisher(comp, sourceType, referencedType);
            Assert.Equal("C [file2.cs(1)]", distinguisher.First.ToString());
            Assert.Equal("C [file1.cs(1)]", distinguisher.Second.ToString());
        }
        public void IvtVirtualCall2()
        {
            var source1 = @"
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm4"")]

public class A
{
    internal virtual void M() { }
    internal virtual int P { get { return 0; } }
    internal virtual event System.Action E { add { } remove { } }
}
";
            var source2 = @"
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm3"")]

public class B : A
{
    internal override void M() { }
    internal override int P { get { return 0; } }
    internal override event System.Action E { add { } remove { } }
}
";
            var source3 = @"
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm4"")]

public class C : B
{
    internal override void M() { }
    internal override int P { get { return 0; } }
    internal override event System.Action E { add { } remove { } }
}
";
            var source4 = @"
using System;
using System.Linq.Expressions;

public class D : C
{
    internal override void M() { }

    void Test()
    {
        D d = new D();
        d.M();
        int x = d.P;
        d.E += null;
    }

    void TestET() 
    {
        D d = new D();
        Expression<Action> expr = () => d.M();
    }
}
";

            var comp1 = CreateCompilationWithMscorlib(source1, options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider), assemblyName: "asm1");
            comp1.VerifyDiagnostics();
            var ref1 = new CSharpCompilationReference(comp1);

            var comp2 = CreateCompilationWithMscorlib(source2, new[] { ref1 }, options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider), assemblyName: "asm2");
            comp2.VerifyDiagnostics();
            var ref2 = new CSharpCompilationReference(comp2);

            var comp3 = CreateCompilationWithMscorlib(source3, new[] { ref1, ref2 }, options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider), assemblyName: "asm3");
            comp3.VerifyDiagnostics();
            var ref3 = new CSharpCompilationReference(comp3);

            var comp4 = CreateCompilationWithMscorlib(source4, new[] { SystemCoreRef, ref1, ref2, ref3 }, options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider), assemblyName: "asm4");
            comp4.VerifyDiagnostics();

            // Note: calls C.M, not A.M, since asm2 is not accessible (stops search).
            // Confirmed in Dev11.
            var verifier = CompileAndVerify(comp4, emitOptions: TestEmitters.CCI);

            verifier.VerifyIL("D.Test", @"
{
  // Code size       25 (0x19)
  .maxstack  2
  IL_0000:  newobj     ""D..ctor()""
  IL_0005:  dup
  IL_0006:  callvirt   ""void C.M()""
  IL_000b:  dup
  IL_000c:  callvirt   ""int C.P.get""
  IL_0011:  pop
  IL_0012:  ldnull
  IL_0013:  callvirt   ""void C.E.add""
  IL_0018:  ret
}");

            verifier.VerifyIL("D.TestET", @"
{
  // Code size       85 (0x55)
  .maxstack  3
  IL_0000:  newobj     ""D.<>c__DisplayClass2_0..ctor()""
  IL_0005:  dup
  IL_0006:  newobj     ""D..ctor()""
  IL_000b:  stfld      ""D D.<>c__DisplayClass2_0.d""
  IL_0010:  ldtoken    ""D.<>c__DisplayClass2_0""
  IL_0015:  call       ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)""
  IL_001a:  call       ""System.Linq.Expressions.ConstantExpression System.Linq.Expressions.Expression.Constant(object, System.Type)""
  IL_001f:  ldtoken    ""D D.<>c__DisplayClass2_0.d""
  IL_0024:  call       ""System.Reflection.FieldInfo System.Reflection.FieldInfo.GetFieldFromHandle(System.RuntimeFieldHandle)""
  IL_0029:  call       ""System.Linq.Expressions.MemberExpression System.Linq.Expressions.Expression.Field(System.Linq.Expressions.Expression, System.Reflection.FieldInfo)""
  IL_002e:  ldtoken    ""void C.M()""
  IL_0033:  call       ""System.Reflection.MethodBase System.Reflection.MethodBase.GetMethodFromHandle(System.RuntimeMethodHandle)""
  IL_0038:  castclass  ""System.Reflection.MethodInfo""
  IL_003d:  ldc.i4.0
  IL_003e:  newarr     ""System.Linq.Expressions.Expression""
  IL_0043:  call       ""System.Linq.Expressions.MethodCallExpression System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression, System.Reflection.MethodInfo, params System.Linq.Expressions.Expression[])""
  IL_0048:  ldc.i4.0
  IL_0049:  newarr     ""System.Linq.Expressions.ParameterExpression""
  IL_004e:  call       ""System.Linq.Expressions.Expression<System.Action> System.Linq.Expressions.Expression.Lambda<System.Action>(System.Linq.Expressions.Expression, params System.Linq.Expressions.ParameterExpression[])""
  IL_0053:  pop
  IL_0054:  ret
}");
        }
        public void SameErrorTypeArgumentsDifferentSourceAssemblies()
        {
            var source0 =
@"public class A
{
    public static void M(System.Collections.Generic.IEnumerable<E> e)
    {
    }
}";
            var source1 =
@"class B
{
    static void M(System.Collections.Generic.IEnumerable<E> e)
    {
        A.M(e);
    }
}";
            var comp0 = CreateCompilationWithMscorlib(source0);
            comp0.VerifyDiagnostics(
                // (3,65): error CS0246: The type or namespace name 'E' could not be found (are you missing a using directive or an assembly reference?)
                //     public static void M(System.Collections.Generic.IEnumerable<E> e)
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "E").WithArguments("E").WithLocation(3, 65));
            var ref0 = new CSharpCompilationReference(comp0);
            var comp1 = CreateCompilationWithMscorlib(Parse(source1), new[] { ref0 });
            comp1.VerifyDiagnostics(
                // (3,58): error CS0246: The type or namespace name 'E' could not be found (are you missing a using directive or an assembly reference?)
                //     static void M(System.Collections.Generic.IEnumerable<E> e)
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "E").WithArguments("E").WithLocation(3, 58),
                // (5,13): error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<E>' to 'System.Collections.Generic.IEnumerable<E>'
                //         A.M(e);
                Diagnostic(ErrorCode.ERR_BadArgType, "e").WithArguments("1", "System.Collections.Generic.IEnumerable<E>", "System.Collections.Generic.IEnumerable<E>").WithLocation(5, 13));
        }
        public void ERR_BadArgType_SameSourceLocation()
        {
            var libSource = @"
public class C { }

public static class Lib
{
    public static void M(ref C c) { }
}
";

            var source = @"
public class C
{
    public static void Main()
    {
        var c = new C();
        Lib.M(ref c);
    }
}
";

            var libRef = new CSharpCompilationReference(CreateCompilationWithMscorlib(Parse(libSource, "file.cs"), assemblyName: "Metadata"));
            CreateCompilationWithMscorlib(Parse(source, "file.cs"), new[] { libRef }, assemblyName: "Source").VerifyDiagnostics(
                // file.cs(6,21): warning CS0436: The type 'C' in 'file.cs' conflicts with the imported type 'C' in 'Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'file.cs'.
                //         var c = new C();
                Diagnostic(ErrorCode.WRN_SameFullNameThisAggAgg, "C").WithArguments("file.cs", "C", "Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "C").WithLocation(6, 21),
                // file.cs(7,19): error CS1503: Argument 1: cannot convert from 'ref C [Source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'ref C [Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
                //         Lib.M(ref c);
                Diagnostic(ErrorCode.ERR_BadArgType, "c").WithArguments("1", "ref C [Source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]", "ref C [Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]").WithLocation(7, 19));
        }
Example #5
0
        public void SymbolInfoForExternAliasInAliasTarget()
        {
            var libSource = @"
namespace N
{
    public class C { }
}
";

            var source = @"
extern alias A;

using C = A::N.C;

class Test
{
    static void Main()
    {
        C c = new C();
    }
}";
            var libRef = new CSharpCompilationReference(CreateCompilationWithMscorlib(libSource, assemblyName: "lib"), aliases: ImmutableArray.Create("A"));
            var comp = CreateCompilationWithMscorlib(source, new[] { libRef });
            comp.VerifyDiagnostics();

            var tree = comp.SyntaxTrees.Single();
            var model = comp.GetSemanticModel(tree);

            var root = tree.GetRoot();
            var externAliasSyntax = root.DescendantNodes().OfType<ExternAliasDirectiveSyntax>().Single();
            var usingSyntax = root.DescendantNodes().OfType<UsingDirectiveSyntax>().Single();
            var usingTargetSyntax = (QualifiedNameSyntax)usingSyntax.Name;
            var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)usingTargetSyntax.Left;

            var aliasedGlobalNamespace = comp.GetReferencedAssemblySymbol(libRef).GlobalNamespace;
            var namespaceN = aliasedGlobalNamespace.GetMember<NamespaceSymbol>("N");
            var typeC = namespaceN.GetMember<NamedTypeSymbol>("C");

            var externAliasSymbol = model.GetDeclaredSymbol(externAliasSyntax);
            Assert.Equal("A", externAliasSymbol.Name);
            Assert.Equal(aliasedGlobalNamespace, externAliasSymbol.Target);

            var usingAliasSymbol = model.GetDeclaredSymbol(usingSyntax);
            Assert.Equal("C", usingAliasSymbol.Name);
            Assert.Equal(typeC, usingAliasSymbol.Target);

            var qualifiedNameInfo = model.GetSymbolInfo(usingTargetSyntax);
            Assert.Equal(typeC, qualifiedNameInfo.Symbol);

            var aliasQualifiedNameInfo = model.GetSymbolInfo(aliasQualifiedNameSyntax);
            Assert.Equal(typeC.ContainingNamespace, aliasQualifiedNameInfo.Symbol);

            var aliasNameInfo = model.GetSymbolInfo(aliasQualifiedNameSyntax.Alias);
            Assert.Equal(aliasedGlobalNamespace, aliasNameInfo.Symbol);
        }
Example #6
0
        public void EmptyStructsFromMetadata()
        {
            var comp1 = CreateCompilation(
                @"public struct StructWithReference
{
    string PrivateData;
}
public struct StructWithValue
{
    int PrivateData;
}");
            var sourceReference   = new CSharpCompilationReference(comp1);
            var metadataReference = MetadataReference.CreateFromStream(comp1.EmitToStream());

            var source2 =
                @"class Program
{
    public static void Main()
    {
        StructWithReference r1;
        var r2 = r1;

        StructWithValue v1;
        var v2 = v1;
    }
}";

            CreateCompilation(source2,
                              options: TestOptions.ReleaseDll.WithWarningLevel(5),
                              references: new MetadataReference[] { sourceReference }).VerifyDiagnostics(
                // (6,18): warning CS8829: Use of unassigned local variable 'r1'
                //         var r2 = r1;
                Diagnostic(ErrorCode.WRN_UseDefViolation, "r1").WithArguments("r1").WithLocation(6, 18),
                // (9,18): error CS0165: Use of unassigned local variable 'v1'
                //         var v2 = v1;
                Diagnostic(ErrorCode.ERR_UseDefViolation, "v1").WithArguments("v1").WithLocation(9, 18)
                );
            CreateCompilation(source2,
                              options: TestOptions.ReleaseDll.WithWarningLevel(5),
                              references: new MetadataReference[] { metadataReference }).VerifyDiagnostics(
                // (6,18): warning CS8829: Use of unassigned local variable 'r1'
                //         var r2 = r1;
                Diagnostic(ErrorCode.WRN_UseDefViolation, "r1").WithArguments("r1").WithLocation(6, 18),
                // (9,18): error CS0165: Use of unassigned local variable 'v1'
                //         var v2 = v1;
                Diagnostic(ErrorCode.ERR_UseDefViolation, "v1").WithArguments("v1").WithLocation(9, 18)
                );
            CreateCompilation(source2,
                              options: TestOptions.ReleaseDll,
                              references: new MetadataReference[] { sourceReference }).VerifyDiagnostics(
                // (9,18): error CS0165: Use of unassigned local variable 'v1'
                //         var v2 = v1;
                Diagnostic(ErrorCode.ERR_UseDefViolation, "v1").WithArguments("v1").WithLocation(9, 18)
                );
            CreateCompilation(source2,
                              options: TestOptions.ReleaseDll,
                              references: new MetadataReference[] { metadataReference }).VerifyDiagnostics(
                // (9,18): error CS0165: Use of unassigned local variable 'v1'
                //         var v2 = v1;
                Diagnostic(ErrorCode.ERR_UseDefViolation, "v1").WithArguments("v1").WithLocation(9, 18)
                );
        }
        public void NoBridgeMethodForVirtualImplementation()
        {
            var source1 = @"
public interface I
{
    void Virtual();
    void NonVirtual();
}

public class B
{
    public virtual void Virtual() { }
    public void NonVirtual() { }
}
";

            var source2 = @"
class D : B, I
{
}
";
            var comp1 = CreateCompilationWithMscorlib(source1, options: TestOptions.ReleaseDll, assemblyName: "asm1");
            comp1.VerifyDiagnostics();
            var ref1 = new CSharpCompilationReference(comp1);

            var comp2 = CreateCompilationWithMscorlib(source2, new[] { ref1 }, options: TestOptions.ReleaseDll, assemblyName: "asm2");
            comp2.VerifyDiagnostics();

            var derivedType = comp2.GlobalNamespace.GetMember<SourceNamedTypeSymbol>("D");
            var bridgeMethod = derivedType.GetSynthesizedExplicitImplementations(CancellationToken.None).Single();
            Assert.Equal("NonVirtual", bridgeMethod.ImplementingMethod.Name);
        }
Example #8
0
        public void CS1757ERR_InteropStructContainsMethods()
        {
            var textdll = @"using System;
using System.Runtime.InteropServices;
[assembly: ImportedFromTypeLib(""NoPiaTestLib"")]
[assembly: Guid(""A7721B07-2448-447A-BA36-64682CBEF136"")]
namespace NS
{
    public struct MyStruct
    {
        private int _age;
        public string Name;
    }
}
";
            var text = @"public class Test
{
    public static void Main()
    {
        NS.MyStruct S = new NS.MyStruct();
        System.Console.Write(S);
    }
}
";
            var comp = CreateCompilationWithMscorlib(textdll);
            var ref1 = new CSharpCompilationReference(comp, embedInteropTypes: true);
            var comp1 = CreateCompilationWithMscorlib(text, new[] { ref1 });
            comp1.VerifyEmitDiagnostics(
                // (5,24): error CS1757: Embedded interop struct 'NS.MyStruct' can contain only public instance fields.
                //         NS.MyStruct S = new NS.MyStruct();
                Diagnostic(ErrorCode.ERR_InteropStructContainsMethods, "new NS.MyStruct()").WithArguments("NS.MyStruct"));
        }
Example #9
0
            public Test01()
            {
                string source       = @"
using System.Diagnostics;

[assembly: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
[module: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]


[DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
class TestClass
{
    [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
    public int testField;


    [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
    public int TestProperty
    {
        [return: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
        get
        {
            return testField;
        }
    }

    [return: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
    [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )]
    public T TestMethod<[DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] T>
        ([DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] T testParameter)
    {
        return testParameter;
    }
}";
                var    compilation1 = CSharpCompilation.Create("C1", new[] { Parse(source) }, new[] { OldMsCorLib }, TestOptions.ReleaseDll);

                c1 = new CSharpCompilationReference(compilation1);

                var c1Assembly = compilation1.Assembly;

                var compilation2 = CSharpCompilation.Create("C2", references: new MetadataReference[] { NewMsCorLib, c1 });

                c2 = new CSharpCompilationReference(compilation2);

                var c1AsmRef = compilation2.GetReferencedAssemblySymbol(c1);

                Assert.NotSame(c1Assembly, c1AsmRef);

                c1MscorLibAssemblyRef = compilation1.GetReferencedAssemblySymbol(OldMsCorLib);
                c2MscorlibAssemblyRef = compilation2.GetReferencedAssemblySymbol(NewMsCorLib);
                Assert.NotSame(c1MscorLibAssemblyRef, c2MscorlibAssemblyRef);

                oldMsCorLib_systemType = c1MscorLibAssemblyRef.GetTypeByMetadataName("System.Type");
                newMsCorLib_systemType = c2MscorlibAssemblyRef.GetTypeByMetadataName("System.Type");
                Assert.NotSame(oldMsCorLib_systemType, newMsCorLib_systemType);

                oldMsCorLib_debuggerTypeProxyAttributeType = c1MscorLibAssemblyRef.GetTypeByMetadataName("System.Diagnostics.DebuggerTypeProxyAttribute");
                newMsCorLib_debuggerTypeProxyAttributeType = c2MscorlibAssemblyRef.GetTypeByMetadataName("System.Diagnostics.DebuggerTypeProxyAttribute");
                Assert.NotSame(oldMsCorLib_debuggerTypeProxyAttributeType, newMsCorLib_debuggerTypeProxyAttributeType);

                oldMsCorLib_debuggerTypeProxyAttributeCtor = (MethodSymbol)oldMsCorLib_debuggerTypeProxyAttributeType.GetMembers(".ctor").Single(
                    m => ((MethodSymbol)m).ParameterCount == 1 && ((MethodSymbol)m).ParameterTypes[0] == oldMsCorLib_systemType);

                newMsCorLib_debuggerTypeProxyAttributeCtor = (MethodSymbol)newMsCorLib_debuggerTypeProxyAttributeType.GetMembers(".ctor").Single(
                    m => ((MethodSymbol)m).ParameterCount == 1 && ((MethodSymbol)m).ParameterTypes[0] == newMsCorLib_systemType);

                Assert.NotSame(oldMsCorLib_debuggerTypeProxyAttributeCtor, newMsCorLib_debuggerTypeProxyAttributeCtor);
            }
Example #10
0
        public void MultiModulesNamespaceCorLibraries()
        {
            var text1 =
                @"namespace N1
{
    class A {}
}
";
            var text2 =
                @"namespace N1
{
    interface IGoo {}
}
";
            var text3 =
                @"namespace N1
{
    struct SGoo {}
}
";

            var comp1 = CSharpCompilation.Create(
                assemblyName: "Test1",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(text1) },
                references: new MetadataReference[] { }
                );
            var comp2 = CSharpCompilation.Create(
                assemblyName: "Test2",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(text2) },
                references: new MetadataReference[] { }
                );

            var compRef1 = new CSharpCompilationReference(comp1);
            var compRef2 = new CSharpCompilationReference(comp2);

            var comp = CSharpCompilation.Create(
                assemblyName: "Test3",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(text3) },
                references: new MetadataReference[] { compRef1, compRef2 }
                );

            var global = comp.GlobalNamespace; // throw
            var ns     = global.GetMembers("N1").Single() as NamespaceSymbol;

            Assert.Equal(3, ns.GetTypeMembers().Length); // A, IGoo & SGoo
            Assert.Equal(NamespaceKind.Compilation, ns.Extent.Kind);

            var constituents = ns.ConstituentNamespaces;

            Assert.Equal(3, constituents.Length);
            Assert.True(
                constituents.Contains(
                    comp.SourceAssembly.GlobalNamespace.GetMembers("N1").Single() as NamespaceSymbol
                    )
                );
            Assert.True(
                constituents.Contains(
                    comp.GetReferencedAssemblySymbol(compRef1)
                    .GlobalNamespace.GetMembers("N1")
                    .Single() as NamespaceSymbol
                    )
                );
            Assert.True(
                constituents.Contains(
                    comp.GetReferencedAssemblySymbol(compRef2)
                    .GlobalNamespace.GetMembers("N1")
                    .Single() as NamespaceSymbol
                    )
                );
        }
Example #11
0
        public void MultiModulesNamespace()
        {
            var text1 =
                @"namespace N1
{
    class A {}
}
";
            var text2 =
                @"namespace N1
{
    interface IGoo {}
}
";
            var text3 =
                @"namespace N1
{
    struct SGoo {}
}
";
            var comp1 = CreateCompilation(text1, assemblyName: "Compilation1");
            var comp2 = CreateCompilation(text2, assemblyName: "Compilation2");

            var compRef1 = new CSharpCompilationReference(comp1);
            var compRef2 = new CSharpCompilationReference(comp2);

            var comp = CreateEmptyCompilation(
                new string[] { text3 },
                references: new MetadataReference[] { compRef1, compRef2 }.ToList(),
                assemblyName: "Test3"
                );
            //Compilation.Create(outputName: "Test3", options: CompilationOptions.Default,
            //                        syntaxTrees: new SyntaxTree[] { SyntaxTree.ParseCompilationUnit(text3) },
            //                        references: new MetadataReference[] { compRef1, compRef2 });

            var global = comp.GlobalNamespace; // throw
            var ns     = global.GetMembers("N1").Single() as NamespaceSymbol;

            Assert.Equal(3, ns.GetTypeMembers().Length); // A, IGoo & SGoo
            Assert.Equal(NamespaceKind.Compilation, ns.Extent.Kind);

            var constituents = ns.ConstituentNamespaces;

            Assert.Equal(3, constituents.Length);
            Assert.True(
                constituents.Contains(
                    comp.SourceAssembly.GlobalNamespace.GetMembers("N1").Single() as NamespaceSymbol
                    )
                );
            Assert.True(
                constituents.Contains(
                    comp.GetReferencedAssemblySymbol(compRef1)
                    .GlobalNamespace.GetMembers("N1")
                    .Single() as NamespaceSymbol
                    )
                );
            Assert.True(
                constituents.Contains(
                    comp.GetReferencedAssemblySymbol(compRef2)
                    .GlobalNamespace.GetMembers("N1")
                    .Single() as NamespaceSymbol
                    )
                );

            foreach (var constituentNs in constituents)
            {
                Assert.Equal(NamespaceKind.Module, constituentNs.Extent.Kind);
                Assert.Equal(ns.ToTestDisplayString(), constituentNs.ToTestDisplayString());
            }
        }
Example #12
0
        public void UsingAliasForNamespace()
        {
            var text =
                @"using Gen = System.Collections.Generic;

namespace NS {
    public interface IGoo {}
}

namespace NS.NS1 {
    using F = NS.IGoo;
    class A : F { }
}
";
            var text1 =
                @"namespace NS.NS1 {
    public class B {
        protected Gen.List<int> field;
    }
}
";
            var text2 =
                @"namespace NS {
    namespace NS2 {
        using NN = NS.NS1;
        class C : NN.B { }
    }
}
";
            var comp1   = CreateCompilation(text);
            var compRef = new CSharpCompilationReference(comp1);

            var comp = CSharpCompilation.Create(
                assemblyName: "Test1",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[]
            {
                SyntaxFactory.ParseSyntaxTree(text1),
                SyntaxFactory.ParseSyntaxTree(text2)
            },
                references: new MetadataReference[] { compRef }
                );

            var global = comp.GlobalNamespace;
            var ns     = global.GetMembers("NS").Single() as NamespaceSymbol;

            Assert.Equal(1, ns.GetTypeMembers().Length); // IGoo
            Assert.Equal(3, ns.GetMembers().Length);     // NS1, NS2, IGoo

            var ns1   = ns.GetMembers("NS1").Single() as NamespaceSymbol;
            var type1 = ns1.GetTypeMembers("A").SingleOrDefault() as NamedTypeSymbol;

            Assert.Equal(1, type1.Interfaces().Length);
            Assert.Equal("IGoo", type1.Interfaces()[0].Name);

            var ns2   = ns.GetMembers("NS2").Single() as NamespaceSymbol;
            var type2 = ns2.GetTypeMembers("C").SingleOrDefault() as NamedTypeSymbol;

            Assert.NotNull(type2.BaseType());
            Assert.Equal("NS.NS1.B", type2.BaseType().ToTestDisplayString());
        }
Example #13
0
        public void SimpleNamespace()
        {
            var text =
                @"namespace N1
{
    namespace N11 {
        namespace N111 {
            class A {}
        }
    }
}

namespace N1 {
    struct S {}
}
";
            var text1 =
                @"namespace N1
{
    namespace N11 {
        namespace N111 {
            class B {}
        }
    }
}
";
            var text2 =
                @"namespace N1
    namespace N12 {
         struct S {}
    }
}
";
            var comp1 = CSharpCompilation.Create(
                assemblyName: "Test",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[] { SyntaxFactory.ParseSyntaxTree(text) },
                references: new MetadataReference[] { }
                );
            var compRef = new CSharpCompilationReference(comp1);

            var comp = CSharpCompilation.Create(
                assemblyName: "Test1",
                options: TestOptions.DebugExe,
                syntaxTrees: new SyntaxTree[]
            {
                SyntaxFactory.ParseSyntaxTree(text1),
                SyntaxFactory.ParseSyntaxTree(text2)
            },
                references: new MetadataReference[] { compRef }
                );

            var global = comp.GlobalNamespace;
            var ns     = global.GetMembers("N1").Single() as NamespaceSymbol;

            Assert.Equal(1, ns.GetTypeMembers().Length); // S
            Assert.Equal(3, ns.GetMembers().Length);     // N11, N12, S

            var ns1 =
                (ns.GetMembers("N11").Single() as NamespaceSymbol).GetMembers("N111").Single()
                as NamespaceSymbol;

            Assert.Equal(2, ns1.GetTypeMembers().Length); // A & B
        }
Example #14
0
        public void ForwardedTypesAreEmittedInADeterministicOrder()
        {
            var forwardedToCode         = @"
namespace Namespace2 {
    public class GenericType1<T> {}
    public class GenericType3<T> {}
    public class GenericType2<T> {}
}
namespace Namespace1 {
    public class Type3 {}
    public class Type2 {}
    public class Type1 {}
}
namespace Namespace4 {
    namespace Embedded {
        public class Type2 {}
        public class Type1 {}
    }
}
namespace Namespace3 {
    public class GenericType {}
    public class GenericType<T> {}
    public class GenericType<T, U> {}
}
";
            var forwardedToCompilation1 = CreateCompilation(forwardedToCode, assemblyName: "ForwardedTo");
            var forwardedToReference1   = new CSharpCompilationReference(forwardedToCompilation1);

            var forwardingCode = @"
using System.Runtime.CompilerServices;
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType1<int>))]
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType3<int>))]
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType2<int>))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type3))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type2))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type1))]
[assembly: TypeForwardedTo(typeof(Namespace4.Embedded.Type2))]
[assembly: TypeForwardedTo(typeof(Namespace4.Embedded.Type1))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType<int>))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType<int, int>))]
";

            var forwardingCompilation = CreateCompilation(forwardingCode, new MetadataReference[] { forwardedToReference1 });
            var forwardingReference   = new CSharpCompilationReference(forwardingCompilation);

            var sortedFullNames = new string[]
            {
                "Namespace1.Type1",
                "Namespace1.Type2",
                "Namespace1.Type3",
                "Namespace2.GenericType1`1",
                "Namespace2.GenericType2`1",
                "Namespace2.GenericType3`1",
                "Namespace3.GenericType",
                "Namespace3.GenericType`1",
                "Namespace3.GenericType`2",
                "Namespace4.Embedded.Type1",
                "Namespace4.Embedded.Type2"
            };

            Action <ModuleSymbol> metadataValidator = module =>
            {
                var assembly = module.ContainingAssembly;
                Assert.Equal(sortedFullNames, getNamesOfForwardedTypes(assembly));
            };

            CompileAndVerify(forwardingCompilation, symbolValidator: metadataValidator, sourceSymbolValidator: metadataValidator, verify: Verification.Skipped);

            using (var stream = forwardingCompilation.EmitToStream())
            {
                using (var block = ModuleMetadata.CreateFromStream(stream))
                {
                    var metadataFullNames = MetadataValidation.GetExportedTypesFullNames(block.MetadataReader);
                    Assert.Equal(sortedFullNames, metadataFullNames);
                }
            }

            var forwardedToCompilation2 = CreateCompilation(forwardedToCode, assemblyName: "ForwardedTo");
            var forwardedToReference2   = new CSharpCompilationReference(forwardedToCompilation2);

            var withRetargeting = CreateCompilation("", new MetadataReference[] { forwardedToReference2, forwardingReference });

            var retargeting = (RetargetingAssemblySymbol)withRetargeting.GetReferencedAssemblySymbol(forwardingReference);

            Assert.Equal(sortedFullNames, getNamesOfForwardedTypes(retargeting));

            foreach (var type in getForwardedTypes(retargeting))
            {
                Assert.Same(forwardedToCompilation2.Assembly.GetPublicSymbol(), type.ContainingAssembly);
            }
Example #15
0
        public void CS0730ERR_ForwardedTypeIsNested()
        {
            var text1 = @"public class C
{
    public class CC
    {
    }
}
";
            var text2 = @"
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(C.CC))]";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var compRef1 = new CSharpCompilationReference(comp1);

            var comp2 = CreateCompilationWithMscorlib(text2, new MetadataReference[] { compRef1 });
            comp2.VerifyDiagnostics(
                // (4,12): error CS0730: Cannot forward type 'C.CC' because it is a nested type of 'C'
                // [assembly: TypeForwardedTo(typeof(C.CC))]
                Diagnostic(ErrorCode.ERR_ForwardedTypeIsNested, "TypeForwardedTo(typeof(C.CC))").WithArguments("C.CC", "C"));
        }
Example #16
0
        public void CS1754ERR_NoPIANestedType()
        {
            var textdll = @"using System;
using System.Runtime.InteropServices;
[assembly: ImportedFromTypeLib(""NoPiaTestLib"")]
[assembly: Guid(""A7721B07-2448-447A-BA36-64682CBEF136"")]
namespace NS
{
    public struct MyClass
    {
        public struct NestedClass
        {
            public string Name;
        }
    }
}
";
            var text = @"public class Test
{
    public static void Main()
    {
        var S = new NS.MyClass.NestedClass();
        System.Console.Write(S);
    }
}
";
            var comp = CreateCompilationWithMscorlib(textdll);
            var ref1 = new CSharpCompilationReference(comp, embedInteropTypes: true);
            CreateCompilationWithMscorlib(text, new[] { ref1 }).VerifyDiagnostics(
               Diagnostic(ErrorCode.ERR_NoPIANestedType, "NestedClass").WithArguments("NS.MyClass.NestedClass"));
        }
Example #17
0
        public void RetargetExplicitImplementationDifferentModule()
        {
            var source1 =
                @"public interface I<T>
{
    void M<U>(I<U> o);
    void N(I<T> o);
    I<T> P { get; }
}
public class A
{
}";
            var compilation1_v1 = CreateCompilation(source1, assemblyName: "assembly1");
            var compilation1_v2 = CreateCompilation(source1, assemblyName: "assembly1");

            var source2 =
                @"class B : I<A>
{
    void I<A>.M<U>(I<U> o) { }
    void I<A>.N(I<A> o) {}
    I<A> I<A>.P { get { return null; } }
}

class C<CT> : I<CT>
{
    void I<CT>.M<U>(I<U> o) { }
    void I<CT>.N(I<CT> o) { }
    I<CT> I<CT>.P { get { return null; } }
}
";
            var compilation2 = CreateCompilation(source2, new[] { new CSharpCompilationReference(compilation1_v1) }, assemblyName: "assembly2");

            var compilation2Ref = new CSharpCompilationReference(compilation2);

            var compilation3 = CreateCompilation("", new[] { compilation2Ref, new CSharpCompilationReference(compilation1_v2) }, assemblyName: "assembly3");

            var            assembly2 = compilation3.GetReferencedAssemblySymbol(compilation2Ref);
            MethodSymbol   implemented_m;
            MethodSymbol   implemented_n;
            PropertySymbol implemented_p;

            var b = assembly2.GetTypeByMetadataName("B");
            var m = b.GetMethod("I<A>.M");

            implemented_m = m.ExplicitInterfaceImplementations[0];

            Assert.Equal("void I<A>.M<U>(I<U> o)", implemented_m.ToTestDisplayString());

            var a_v2     = compilation1_v2.GetTypeByMetadataName("A");
            var i_a_v2   = compilation1_v2.GetTypeByMetadataName("I`1").Construct(ImmutableArray.Create <TypeSymbol>(a_v2));
            var i_a_m_v2 = i_a_v2.GetMethod("M");

            Assert.Equal(i_a_m_v2, implemented_m);

            var n = b.GetMethod("I<A>.N");

            implemented_n = n.ExplicitInterfaceImplementations[0];

            Assert.Equal("void I<A>.N(I<A> o)", implemented_n.ToTestDisplayString());

            var i_a_n_v2 = i_a_v2.GetMethod("N");

            Assert.Equal(i_a_n_v2, implemented_n);

            var p = b.GetProperty("I<A>.P");

            implemented_p = p.ExplicitInterfaceImplementations[0];

            Assert.Equal("I<A> I<A>.P { get; }", implemented_p.ToTestDisplayString());

            var i_a_p_v2 = i_a_v2.GetProperty("P");

            Assert.Equal(i_a_p_v2, implemented_p);

            var c       = assembly2.GetTypeByMetadataName("C`1");
            var i_ct_v2 = compilation1_v2.GetTypeByMetadataName("I`1").Construct(ImmutableArray.Create <TypeSymbol>(c.TypeParameters[0]));

            implemented_m = c.GetMethod("I<CT>.M").ExplicitInterfaceImplementations[0];

            Assert.Equal("void I<CT>.M<U>(I<U> o)", implemented_m.ToTestDisplayString());

            var i_ct_m_v2 = i_ct_v2.GetMethod("M");

            Assert.Equal(i_ct_m_v2, implemented_m);

            implemented_n = c.GetMethod("I<CT>.N").ExplicitInterfaceImplementations[0];

            Assert.Equal("void I<CT>.N(I<CT> o)", implemented_n.ToTestDisplayString());

            var i_ct_n_v2 = i_ct_v2.GetMethod("N");

            Assert.Equal(i_ct_n_v2, implemented_n);

            implemented_p = c.GetProperty("I<CT>.P").ExplicitInterfaceImplementations[0];

            Assert.Equal("I<CT> I<CT>.P { get; }", implemented_p.ToTestDisplayString());

            var i_ct_p_v2 = i_ct_v2.GetProperty("P");

            Assert.Equal(i_ct_p_v2, implemented_p);
        }
Example #18
0
        public void NameCollisionWithAddedModule_03()
        {
            var forwardedTypesSource =
@"
public class CF1
{}

namespace ns
{
    public class CF2
    {
    }
}

public class CF3<T>
{}
";

            var forwardedTypes1 = CreateCompilationWithMscorlib(forwardedTypesSource, options: TestOptions.ReleaseDll, assemblyName: "ForwardedTypes1");
            var forwardedTypes1Ref = new CSharpCompilationReference(forwardedTypes1);

            var forwardedTypes2 = CreateCompilationWithMscorlib(forwardedTypesSource, options: TestOptions.ReleaseDll, assemblyName: "ForwardedTypes2");
            var forwardedTypes2Ref = new CSharpCompilationReference(forwardedTypes2);

            var forwardedTypesModRef = CreateCompilationWithMscorlib(forwardedTypesSource,
                                                                options: TestOptions.ReleaseModule,
                                                                assemblyName: "forwardedTypesMod").
                                       EmitToImageReference();

            var modSource =
@"
[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(CF1))]
[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(ns.CF2))]
";

            var module1_FT1_Ref = CreateCompilationWithMscorlib(modSource,
                                                                options: TestOptions.ReleaseModule,
                                                                assemblyName: "module1_FT1",
                                                                references: new MetadataReference[] { forwardedTypes1Ref }).
                                  EmitToImageReference();

            var module2_FT1_Ref = CreateCompilationWithMscorlib(modSource,
                                                                options: TestOptions.ReleaseModule,
                                                                assemblyName: "module2_FT1",
                                                                references: new MetadataReference[] { forwardedTypes1Ref }).
                                  EmitToImageReference();

            var module3_FT2_Ref = CreateCompilationWithMscorlib(modSource,
                                                                options: TestOptions.ReleaseModule,
                                                                assemblyName: "module3_FT2",
                                                                references: new MetadataReference[] { forwardedTypes2Ref }).
                                  EmitToImageReference();

            var module4_Ref = CreateCompilationWithMscorlib("[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(CF3<int>))]",
                                                                options: TestOptions.ReleaseModule,
                                                                assemblyName: "module4_FT1",
                                                                references: new MetadataReference[] { forwardedTypes1Ref }).
                                  EmitToImageReference();

            var compilation = CreateCompilationWithMscorlib(forwardedTypesSource,
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8006: Forwarded type 'ns.CF2' conflicts with type declared in primary module of this assembly.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithDeclaration).WithArguments("ns.CF2"),
                // error CS8006: Forwarded type 'CF1' conflicts with type declared in primary module of this assembly.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithDeclaration).WithArguments("CF1"));

            compilation = CreateCompilationWithMscorlib(modSource,
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            // Exported types in .Net modules cause PEVerify to fail.
            CompileAndVerify(compilation, verify: false).VerifyDiagnostics();

            compilation = CreateCompilationWithMscorlib("[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(CF3<byte>))]",
                new List<MetadataReference>()
                {
                    module4_Ref,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            CompileAndVerify(compilation, verify: false).VerifyDiagnostics();

            compilation = CreateCompilationWithMscorlib(modSource,
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    forwardedTypes2Ref,
                    new CSharpCompilationReference(forwardedTypes1, aliases: ImmutableArray.Create("FT1"))
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8007: Type 'ns.CF2' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'ns.CF2' forwarded to assembly 'ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                Diagnostic(ErrorCode.ERR_ForwardedTypesConflict).WithArguments("ns.CF2", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "ns.CF2", "ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // error CS8007: Type 'CF1' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'CF1' forwarded to assembly 'ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                Diagnostic(ErrorCode.ERR_ForwardedTypesConflict).WithArguments("CF1", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "CF1", "ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"));

            compilation = CreateCompilationWithMscorlib(
@"
extern alias FT1; 

[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(FT1::CF1))]
[assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(FT1::ns.CF2))]
",
                new List<MetadataReference>()
                {
                    forwardedTypesModRef,
                    new CSharpCompilationReference(forwardedTypes1, ImmutableArray.Create("FT1"))
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8008: Type 'CF1' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'CF1' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("CF1", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "CF1", "forwardedTypesMod.netmodule"),
                // error CS8008: Type 'ns.CF2' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'ns.CF2' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("ns.CF2", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "ns.CF2", "forwardedTypesMod.netmodule"));

            compilation = CreateCompilationWithMscorlib("",
                new List<MetadataReference>()
                {
                    forwardedTypesModRef,
                    module1_FT1_Ref,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8008: Type 'ns.CF2' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'ns.CF2' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("ns.CF2", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "ns.CF2", "forwardedTypesMod.netmodule"),
                // error CS8008: Type 'CF1' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'CF1' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("CF1", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "CF1", "forwardedTypesMod.netmodule"));

            compilation = CreateCompilationWithMscorlib("",
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    forwardedTypesModRef,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8008: Type 'ns.CF2' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'ns.CF2' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("ns.CF2", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "ns.CF2", "forwardedTypesMod.netmodule"),
                // error CS8008: Type 'CF1' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'CF1' exported from module 'forwardedTypesMod.netmodule'.
                Diagnostic(ErrorCode.ERR_ForwardedTypeConflictsWithExportedType).WithArguments("CF1", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "CF1", "forwardedTypesMod.netmodule"));

            compilation = CreateCompilationWithMscorlib("",
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    module2_FT1_Ref,
                    forwardedTypes1Ref
                }, TestOptions.ReleaseDll);

            CompileAndVerify(compilation, verify: false).VerifyDiagnostics();

            compilation = CreateCompilationWithMscorlib("",
                new List<MetadataReference>()
                {
                    module1_FT1_Ref,
                    module3_FT2_Ref,
                    forwardedTypes1Ref,
                    forwardedTypes2Ref
                }, TestOptions.ReleaseDll);

            compilation.VerifyEmitDiagnostics(
                // error CS8007: Type 'ns.CF2' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'ns.CF2' forwarded to assembly 'ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                Diagnostic(ErrorCode.ERR_ForwardedTypesConflict).WithArguments("ns.CF2", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "ns.CF2", "ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // error CS8007: Type 'CF1' forwarded to assembly 'ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' conflicts with type 'CF1' forwarded to assembly 'ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                Diagnostic(ErrorCode.ERR_ForwardedTypesConflict).WithArguments("CF1", "ForwardedTypes1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "CF1", "ForwardedTypes2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"));
        }
        public void Test2()
        {
            var oldMsCorLib = TestReferences.NetFx.v4_0_21006.mscorlib;
            var newMsCorLib = MscorlibRef;

            var source = @"
public class Modifiers
{
    public volatile int volatileFld;

    void F1(System.DateTime* p)
    {
    }
}
";

            CSharpCompilation c1 = CSharpCompilation.Create("C1", new[] { Parse(source) }, new[] { oldMsCorLib });

            var c1Assembly = c1.Assembly;

            var r1 = new CSharpCompilationReference(c1);
            CSharpCompilation c2 = CSharpCompilation.Create("C2", references: new[] { newMsCorLib, r1 });
            var c1AsmRef         = c2.GetReferencedAssemblySymbol(r1);

            Assert.NotSame(c1Assembly, c1AsmRef);

            var mscorlibAssembly = c2.GetReferencedAssemblySymbol(newMsCorLib);

            Assert.NotSame(mscorlibAssembly, c1.GetReferencedAssemblySymbol(oldMsCorLib));

            var modifiers = c2.GlobalNamespace.GetTypeMembers("Modifiers").Single();

            Assert.IsType <RetargetingNamedTypeSymbol>(modifiers);

            FieldSymbol volatileFld = modifiers.GetMembers("volatileFld").OfType <FieldSymbol>().Single();

            Assert.Equal(1, volatileFld.TypeWithAnnotations.CustomModifiers.Length);

            var volatileFldMod = volatileFld.TypeWithAnnotations.CustomModifiers[0];

            Assert.False(volatileFldMod.IsOptional);
            Assert.Equal("System.Runtime.CompilerServices.IsVolatile", volatileFldMod.Modifier.ToTestDisplayString());
            Assert.Equal(SpecialType.System_Int32, volatileFld.Type.SpecialType);
            Assert.Same(mscorlibAssembly, ((CSharpCustomModifier)volatileFldMod).ModifierSymbol.ContainingAssembly);

            Assert.Equal("volatileFld", volatileFld.Name);
            Assert.True(volatileFld.IsVolatile);
            Assert.Same(volatileFld, volatileFld.OriginalDefinition);
            Assert.Null(volatileFld.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false));
            Assert.Null(volatileFld.ConstantValue);
            Assert.Null(volatileFld.AssociatedSymbol);
            Assert.Same(c1AsmRef, volatileFld.ContainingAssembly);
            Assert.Same(c1AsmRef.Modules[0], volatileFld.ContainingModule);
            Assert.Same(modifiers, volatileFld.ContainingSymbol);
            Assert.Equal(Accessibility.Public, volatileFld.DeclaredAccessibility);
            Assert.False(volatileFld.IsConst);
            Assert.False(volatileFld.IsReadOnly);
            Assert.False(volatileFld.IsStatic);
            Assert.Same(volatileFld.ContainingModule, ((RetargetingFieldSymbol)volatileFld).RetargetingModule);
            Assert.Same(c1Assembly, ((RetargetingFieldSymbol)volatileFld).UnderlyingField.ContainingAssembly);

            MethodSymbol m1 = modifiers.GetMembers("F1").OfType <MethodSymbol>().Single();

            Assert.Equal(0, m1.ReturnTypeWithAnnotations.CustomModifiers.Length);
            Assert.True(!m1.ExplicitInterfaceImplementations.IsDefault);
            Assert.Equal(0, m1.ExplicitInterfaceImplementations.Length);
            Assert.False(m1.HidesBaseMethodsByName);
            Assert.False(m1.IsExtensionMethod);
            Assert.Equal(((RetargetingMethodSymbol)m1).UnderlyingMethod.CallingConvention, m1.CallingConvention);
            Assert.Null(m1.AssociatedSymbol);
            Assert.Same(c1AsmRef.Modules[0], m1.ContainingModule);

            ParameterSymbol p1 = m1.Parameters[0];

            Assert.Equal(0, p1.TypeWithAnnotations.CustomModifiers.Length);
            Assert.Same(c1AsmRef.Modules[0], p1.ContainingModule);
            Assert.False(p1.HasExplicitDefaultValue, "Parameter has default value");
            Assert.Equal(0, p1.Ordinal);

            PointerTypeSymbol p1Type = (PointerTypeSymbol)p1.Type;

            Assert.Null(p1Type.ContainingAssembly);
            Assert.Same(mscorlibAssembly, p1Type.PointedAtType.ContainingAssembly);
            Assert.Equal(SpecialType.System_DateTime, p1Type.PointedAtType.SpecialType);
            Assert.Equal(0, p1.TypeWithAnnotations.CustomModifiers.Length);
        }
        public void TestImplicitIndexerImplementation()
        {
            var text1 = @"
public interface BaseInterface1
{
    int this[int x] { get; }
}

public interface BaseInterface2
{
    int this[int x] { get; }
}
";
            var text2 = @"
public interface Interface : BaseInterface1, BaseInterface2
{
    int this[int x, int y] { get; }
}
";
            var text3 = @"
class Class : Interface
{
    public int this[int x] { get { return 0; } }
    public int this[int x, int y] { get { return 0; } }
}
";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var refs = new System.Collections.Generic.List<MetadataReference>() { comp1ref };

            var comp2 = CreateCompilationWithMscorlib(text2, references: refs, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            refs.Add(comp2ref);
            var comp = CreateCompilationWithMscorlib(text3, refs, assemblyName: "Test3");

            var global = comp.GlobalNamespace;

            var baseInterface1 = (NamedTypeSymbol)global.GetMembers("BaseInterface1").Single();
            var baseInterface1Indexer = baseInterface1.Indexers.Single();

            var baseInterface2 = (NamedTypeSymbol)global.GetMembers("BaseInterface2").Single();
            var baseInterface2Indexer = baseInterface2.Indexers.Single();

            var @interface = (NamedTypeSymbol)global.GetMembers("Interface").Single();
            var interfaceIndexer = @interface.Indexers.Single();

            var @class = (NamedTypeSymbol)global.GetMembers("Class").Single();
            var classImplicitImplementation = @class.Indexers.Where(p => p.Parameters.Length == 2).Single();
            var classImplicitImplementationBase = @class.Indexers.Where(p => p.Parameters.Length == 1).Single();

            var implementingIndexer = @class.FindImplementationForInterfaceMember(interfaceIndexer);
            Assert.Same(classImplicitImplementation, implementingIndexer);

            var implementingIndexerBase1 = @class.FindImplementationForInterfaceMember(baseInterface1Indexer);
            Assert.Same(classImplicitImplementationBase, implementingIndexerBase1);

            var implementingIndexerBase2 = @class.FindImplementationForInterfaceMember(baseInterface2Indexer);
            Assert.Same(classImplicitImplementationBase, implementingIndexerBase2);
        }
Example #21
0
        public void ForwardedTypesAreEmittedInADeterministicOrder()
        {
            var forwardedToCode        = @"
namespace Namespace2 {
    public class GenericType1<T> {}
    public class GenericType3<T> {}
    public class GenericType2<T> {}
}
namespace Namespace1 {
    public class Type3 {}
    public class Type2 {}
    public class Type1 {}
}
namespace Namespace4 {
    namespace Embedded {
        public class Type2 {}
        public class Type1 {}
    }
}
namespace Namespace3 {
    public class GenericType {}
    public class GenericType<T> {}
    public class GenericType<T, U> {}
}
";
            var forwardedToCompilation = CreateEmptyCompilation(forwardedToCode);
            var forwardedToReference   = new CSharpCompilationReference(forwardedToCompilation);

            var forwardingCode = @"
using System.Runtime.CompilerServices;
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType1<int>))]
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType3<int>))]
[assembly: TypeForwardedTo(typeof(Namespace2.GenericType2<int>))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type3))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type2))]
[assembly: TypeForwardedTo(typeof(Namespace1.Type1))]
[assembly: TypeForwardedTo(typeof(Namespace4.Embedded.Type2))]
[assembly: TypeForwardedTo(typeof(Namespace4.Embedded.Type1))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType<int>))]
[assembly: TypeForwardedTo(typeof(Namespace3.GenericType<int, int>))]
";

            var forwardingCompilation = CreateCompilation(forwardingCode, new MetadataReference[] { forwardedToReference });

            var sortedFullNames = new string[]
            {
                "Namespace1.Type1",
                "Namespace1.Type2",
                "Namespace1.Type3",
                "Namespace2.GenericType1`1",
                "Namespace2.GenericType2`1",
                "Namespace2.GenericType3`1",
                "Namespace3.GenericType",
                "Namespace3.GenericType`1",
                "Namespace3.GenericType`2",
                "Namespace4.Embedded.Type1",
                "Namespace4.Embedded.Type2"
            };

            using (var stream = forwardingCompilation.EmitToStream())
            {
                using (var block = ModuleMetadata.CreateFromStream(stream))
                {
                    var metadataFullNames = MetadataValidation.GetExportedTypesFullNames(block.MetadataReader);
                    Assert.Equal(sortedFullNames, metadataFullNames);
                }
            }
        }
        public void TestClassHiddenOverrides()
        {
            var text1 = @"
public class DefiningClass
{
    public virtual void Method1() { }
    public virtual void Method2() { }
    public virtual int Property1 { get; set; }
    public virtual int Property2 { get; set; }
}
";
            var text2 = @"
public class HidingClass : DefiningClass
{
    public new int Method1;
    public new void Method2() { }
    public new int Property1;
    public new int Property2 { get; set; }
}
";
            var text3 = @"
class OverridingClass : HidingClass
{
    public override void Method1() { } //blocked by non-method
    public override void Method2() { } //blocked by non-virtual method
    public override int Property1 { get; set; } //blocked by non-property
    public override int Property2 { get; set; } //blocked by non-virtual property
}
";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var refs = new System.Collections.Generic.List<MetadataReference>() { comp1ref };

            var comp2 = CreateCompilationWithMscorlib(text2, references: refs, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            refs.Add(comp2ref);
            var comp = CreateCompilationWithMscorlib(text3, refs, assemblyName: "Test3");

            var global = comp.GlobalNamespace;

            var definingClass = (NamedTypeSymbol)global.GetMembers("DefiningClass").Single();
            var hidingClass = (NamedTypeSymbol)global.GetMembers("HidingClass").Single();
            var overridingClass = (NamedTypeSymbol)global.GetMembers("OverridingClass").Single();

            var definingClassMethod1 = (MethodSymbol)definingClass.GetMembers("Method1").Single();
            var definingClassMethod2 = (MethodSymbol)definingClass.GetMembers("Method2").Single();
            var definingClassProperty1 = (PropertySymbol)definingClass.GetMembers("Property1").Single();
            var definingClassProperty2 = (PropertySymbol)definingClass.GetMembers("Property2").Single();

            var hidingClassMethod1 = (FieldSymbol)hidingClass.GetMembers("Method1").Single();
            var hidingClassMethod2 = (MethodSymbol)hidingClass.GetMembers("Method2").Single();
            var hidingClassProperty1 = (FieldSymbol)hidingClass.GetMembers("Property1").Single();
            var hidingClassProperty2 = (PropertySymbol)hidingClass.GetMembers("Property2").Single();

            var overridingClassMethod1 = (MethodSymbol)overridingClass.GetMembers("Method1").Single();
            var overridingClassMethod2 = (MethodSymbol)overridingClass.GetMembers("Method2").Single();
            var overridingClassProperty1 = (PropertySymbol)overridingClass.GetMembers("Property1").Single();
            var overridingClassProperty2 = (PropertySymbol)overridingClass.GetMembers("Property2").Single();

            var overridingClassMethod1OverriddenOrHidden = overridingClassMethod1.OverriddenOrHiddenMembers;
            Assert.False(overridingClassMethod1OverriddenOrHidden.OverriddenMembers.Any());
            Assert.False(overridingClassMethod1OverriddenOrHidden.RuntimeOverriddenMembers.Any());
            Assert.Same(hidingClassMethod1, overridingClassMethod1OverriddenOrHidden.HiddenMembers.Single());
            Assert.Null(overridingClassMethod1.OverriddenMethod);

            //counts as overriding even though the overridden method isn't virtual - we'll check for that later
            var overridingClassMethod2OverriddenOrHidden = overridingClassMethod2.OverriddenOrHiddenMembers;
            Assert.False(overridingClassMethod2OverriddenOrHidden.HiddenMembers.Any());
            Assert.Same(hidingClassMethod2, overridingClassMethod2OverriddenOrHidden.OverriddenMembers.Single());
            Assert.Same(hidingClassMethod2, overridingClassMethod2OverriddenOrHidden.RuntimeOverriddenMembers.Single());
            Assert.Null(overridingClassMethod2.OverriddenMethod);

            var overridingClassProperty1OverriddenOrHidden = overridingClassProperty1.OverriddenOrHiddenMembers;
            Assert.False(overridingClassProperty1OverriddenOrHidden.OverriddenMembers.Any());
            Assert.False(overridingClassProperty1OverriddenOrHidden.RuntimeOverriddenMembers.Any());
            Assert.Null(overridingClassProperty1.OverriddenProperty);

            //counts as overriding even though the overridden property isn't virtual - we'll check for that later
            var overridingClassProperty2OverriddenOrHidden = overridingClassProperty2.OverriddenOrHiddenMembers;
            Assert.False(overridingClassProperty2OverriddenOrHidden.HiddenMembers.Any());
            Assert.Same(hidingClassProperty2, overridingClassProperty2OverriddenOrHidden.OverriddenMembers.Single());
            Assert.Same(hidingClassProperty2, overridingClassProperty2OverriddenOrHidden.RuntimeOverriddenMembers.Single());
            Assert.Null(overridingClassProperty2.OverriddenProperty);
        }
Example #23
0
        public void SymbolInfoForExternAliasInAliasTarget()
        {
            var libSource = @"
namespace N
{
    public class C { }
}
";

            var source = @"
extern alias A;

using C = A::N.C;

class Test
{
    static void Main()
    {
        C c = new C();
    }
}";
            var libRef = new CSharpCompilationReference(CreateCompilationWithMscorlib(libSource, assemblyName: "lib"), aliases: ImmutableArray.Create("A"));
            var comp   = CreateCompilationWithMscorlib(source, new[] { libRef });

            comp.VerifyDiagnostics();

            var tree  = comp.SyntaxTrees.Single();
            var model = comp.GetSemanticModel(tree);

            var root = tree.GetRoot();
            var externAliasSyntax        = root.DescendantNodes().OfType <ExternAliasDirectiveSyntax>().Single();
            var usingSyntax              = root.DescendantNodes().OfType <UsingDirectiveSyntax>().Single();
            var usingTargetSyntax        = (QualifiedNameSyntax)usingSyntax.Name;
            var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)usingTargetSyntax.Left;

            var aliasedGlobalNamespace = comp.GetReferencedAssemblySymbol(libRef).GlobalNamespace;
            var namespaceN             = aliasedGlobalNamespace.GetMember <NamespaceSymbol>("N");
            var typeC = namespaceN.GetMember <NamedTypeSymbol>("C");

            var externAliasSymbol = model.GetDeclaredSymbol(externAliasSyntax);

            Assert.Equal("A", externAliasSymbol.Name);
            Assert.Equal(aliasedGlobalNamespace, externAliasSymbol.Target);

            var usingAliasSymbol = model.GetDeclaredSymbol(usingSyntax);

            Assert.Equal("C", usingAliasSymbol.Name);
            Assert.Equal(typeC, usingAliasSymbol.Target);

            var qualifiedNameInfo = model.GetSymbolInfo(usingTargetSyntax);

            Assert.Equal(typeC, qualifiedNameInfo.Symbol);

            var aliasQualifiedNameInfo = model.GetSymbolInfo(aliasQualifiedNameSyntax);

            Assert.Equal(typeC.ContainingNamespace, aliasQualifiedNameInfo.Symbol);

            var aliasNameInfo = model.GetSymbolInfo(aliasQualifiedNameSyntax.Alias);

            Assert.Equal(aliasedGlobalNamespace, aliasNameInfo.Symbol);
        }
        public void TestDistinctSymbolsWithSameLocation()
        {
            var source = @"public class C { }";
            var tree = Parse(source, "file.cs");

            var libRef = new CSharpCompilationReference(CreateCompilationWithMscorlib(tree, assemblyName: "Metadata"));
            var comp = CreateCompilationWithMscorlib(tree, new[] { libRef }, assemblyName: "Source");

            var sourceAssembly = comp.SourceAssembly;
            var referencedAssembly = (AssemblySymbol)comp.GetAssemblyOrModuleSymbol(libRef);

            var sourceType = sourceAssembly.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
            var referencedType = referencedAssembly.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
            var distinguisher = new SymbolDistinguisher(comp, sourceType, referencedType);
            Assert.Equal("C [Source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]", distinguisher.First.ToString());
            Assert.Equal("C [Metadata, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]", distinguisher.Second.ToString());
        }
        public void TestIndirectHiding()
        {
            var text1 = @"public class Base
{
    public virtual int P { get; set; }
}
";

            var text2 = @"public class Derived1 : Base
{
    public new virtual int P { get { return 1; } }
}
";

            var text3 = @"class Derived2 : Derived1
{
    public override int P { set { } }
}
";

            var comp1    = CreateCompilation(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var refs     = new System.Collections.Generic.List <MetadataReference>()
            {
                comp1ref
            };

            var comp2    = CreateCompilation(text2, references: refs, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            refs.Add(comp2ref);
            var compilation = CreateCompilation(text3, refs, assemblyName: "Test3");

            var global = compilation.GlobalNamespace;

            var baseClass    = global.GetMember <NamedTypeSymbol>("Base");
            var baseProperty = baseClass.GetMember <PropertySymbol>("P");
            var baseGetter   = baseProperty.GetMethod;

            var derived1Class    = global.GetMember <NamedTypeSymbol>("Derived1");
            var derived1Property = derived1Class.GetMember <PropertySymbol>("P");
            var derived1Getter   = derived1Property.GetMethod;

            Assert.Null(derived1Property.SetMethod);

            var derived2Class    = global.GetMember <NamedTypeSymbol>("Derived2");
            var derived2Property = derived2Class.GetMember <PropertySymbol>("P");

            Assert.Null(derived2Property.GetMethod);
            var derived2Setter = derived2Property.SetMethod;

            OverriddenOrHiddenMembersResult derived1PropertyOverriddenOrHidden = derived1Property.OverriddenOrHiddenMembers;

            Assert.Equal(0, derived1PropertyOverriddenOrHidden.OverriddenMembers.Length);
            Assert.Same(baseProperty, derived1PropertyOverriddenOrHidden.HiddenMembers.Single());

            OverriddenOrHiddenMembersResult derived1GetterOverriddenOrHidden = derived1Getter.OverriddenOrHiddenMembers;

            Assert.Equal(0, derived1GetterOverriddenOrHidden.OverriddenMembers.Length);
            Assert.Same(baseGetter, derived1GetterOverriddenOrHidden.HiddenMembers.Single());

            OverriddenOrHiddenMembersResult derived2PropertyOverriddenOrHidden = derived2Property.OverriddenOrHiddenMembers;

            Assert.Equal(0, derived2PropertyOverriddenOrHidden.HiddenMembers.Length);
            Assert.Same(derived1Property, derived2PropertyOverriddenOrHidden.OverriddenMembers.Single());

            OverriddenOrHiddenMembersResult derived2SetterOverriddenOrHidden = derived2Setter.OverriddenOrHiddenMembers;

            Assert.Equal(0, derived2SetterOverriddenOrHidden.HiddenMembers.Length);
            Assert.Equal(0, derived2SetterOverriddenOrHidden.OverriddenMembers.Length);
        }
Example #26
0
        public void PlatformMismatch_03()
        {
            string refSource = @"
public interface ITestPlatform
{}
";
            var refCompilation = CreateCompilation(refSource, compOptions: TestOptions.Dll.WithPlatform(Platform.X86).WithRuntimeMetadataVersion("v4.0.31019"), assemblyName: "PlatformMismatch");

            refCompilation.VerifyEmitDiagnostics();
            var compRef = new CSharpCompilationReference(refCompilation);
            var imageRef = refCompilation.EmitToImageReference();

            string useSource = @"
public interface IUsePlatform
{
    ITestPlatform M();
}
";

            var useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { compRef },
                compOptions: TestOptions.Dll.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics(
    // warning CS8012: Referenced assembly 'PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' targets a different processor.
    Diagnostic(ErrorCode.WRN_ConflictingMachineAssembly).WithArguments("PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
                );

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { imageRef },
                compOptions: TestOptions.Dll.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics(
    // warning CS8012: Referenced assembly 'PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' targets a different processor.
    Diagnostic(ErrorCode.WRN_ConflictingMachineAssembly).WithArguments("PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
                );

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { compRef },
                compOptions: TestOptions.NetModule.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics(
    // warning CS8012: Referenced assembly 'PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' targets a different processor.
    Diagnostic(ErrorCode.WRN_ConflictingMachineAssembly).WithArguments("PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
                );

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { imageRef },
                compOptions: TestOptions.NetModule.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics(
    // warning CS8012: Referenced assembly 'PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' targets a different processor.
    Diagnostic(ErrorCode.WRN_ConflictingMachineAssembly).WithArguments("PlatformMismatch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
                );
        }
Example #27
0
        public void PlatformMismatch_07()
        {
            string refSource = @"
public interface ITestPlatform
{}
";
            var refCompilation = CreateCompilation(refSource, compOptions: TestOptions.Dll.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"), assemblyName: "PlatformMismatch");

            refCompilation.VerifyEmitDiagnostics();
            var compRef = new CSharpCompilationReference(refCompilation);
            var imageRef = refCompilation.EmitToImageReference();

            string useSource = @"
public interface IUsePlatform
{
    ITestPlatform M();
}
";

            var useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { compRef },
                compOptions: TestOptions.Dll.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics();

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { imageRef },
                compOptions: TestOptions.Dll.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics();

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { compRef },
                compOptions: TestOptions.NetModule.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics();

            useCompilation = CreateCompilation(useSource,
                new MetadataReference[] { imageRef },
                compOptions: TestOptions.NetModule.WithPlatform(Platform.Itanium).WithRuntimeMetadataVersion("v4.0.31019"));

            useCompilation.VerifyEmitDiagnostics();
        }
        public void TestAmbiguousOverrideError()
        {
            var text1 = @"
public class Base<TLong, TInt>
{
    public virtual void Method(TLong l, int i) { }
    public virtual void Method(long l, TInt i) { }
}
";
            var text2 = @"
public class Derived<TInt> : Base<long, TInt>
{
    public override void Method(long l, TInt i) { }
    public override void Method(long l, int i) { }
}
";
            var text3 = @"
public class Derived2 : Derived<int>
{
    public override void Method(long l, int i) { }  //CS0462 and CS1957
}
";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var ref1 = new List<MetadataReference>() { comp1ref };

            var comp2 = CreateCompilationWithMscorlib(text2, references: ref1, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            var ref2 = new List<MetadataReference>() { comp1ref, comp2ref };
            var comp = CreateCompilationWithMscorlib(text3, ref2, assemblyName: "Test3");
            var diagnostics = comp.GetDiagnostics();

            var diagStrings = new string[] {
"'Method' warning CS1957: Member 'Derived2.Method(long, int)' overrides 'Derived<int>.Method(long, int)'. There are multiple override candidates at run-time. It is implementation dependent which method will be called.",
"'Method' error CS0462: The inherited members 'Derived<TInt>.Method(long, TInt)' and 'Derived<TInt>.Method(long, int)' have the same signature in type 'Derived2', so they cannot be overridden"
           };

            TestDiagnostics(diagnostics, diagStrings);
        }
Example #29
0
        public void MissingBaseClass()
        {
            var lib1 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public class C1
    {
        public void M1()
        {}
    }

    public class C6<T> where T : C1
    {}
}", compOptions: TestOptions.Dll, assemblyName: "MissingBaseClass1");

            var lib1Ref = new CSharpCompilationReference(lib1);

            var lib2 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public class C2 : C1
    {}
}", new[] { lib1Ref }, TestOptions.Dll, assemblyName: "MissingBaseClass2");

            var lib2Ref = new CSharpCompilationReference(lib2);

            var lib3 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public class C4 : C2
    {}
}", new[] { lib1Ref, lib2Ref }, TestOptions.Dll, assemblyName: "MissingBaseClass3");

            var lib3Ref = new CSharpCompilationReference(lib3);

            var lib4Def = @"
namespace ErrorTest
{
    class Test
    {
        void _Test(C4 y)
        {
            C1 x = y;
        }
    }

    public class C5 : C4
    {}

    class Test2
    {
        void _Test2(C4 y)
        {
            y.M1();
        }
    }

    class Test3<T> where T : C4
    {
        void Test(T y3)
        {
            C1 x = y3;
            y3.M1();
        }
    }

    public class C7 : C6<C4>
    {}

    class Test4
    {
        void Test(C6<C4> x)
        { }
    }
}";

            var lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib3Ref }, TestOptions.Dll);

            DiagnosticDescription[] expectedErrors =
            {
                // (12,23): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C5 : C4
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C4").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (32,18): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C6<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.C1'.
                //     public class C7 : C6<C4>
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C7").WithArguments("ErrorTest.C6<T>", "ErrorTest.C1", "T", "ErrorTest.C4"),
                // (32,18): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C7 : C6<C4>
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C7").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (37,26): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C6<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.C1'.
                //         void Test(C6<C4> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C6<T>", "ErrorTest.C1", "T", "ErrorTest.C4"),
                // (37,26): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C6<C4> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (19,15): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             y.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (19,15): error CS1061: 'ErrorTest.C4' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'ErrorTest.C4' could be found (are you missing a using directive or an assembly reference?)
                //             y.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("ErrorTest.C4", "M1"),
                // (8,20): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             C1 x = y;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (8,20): error CS0029: Cannot implicitly convert type 'ErrorTest.C4' to 'ErrorTest.C1'
                //             C1 x = y;
                Diagnostic(ErrorCode.ERR_NoImplicitConv, "y").WithArguments("ErrorTest.C4", "ErrorTest.C1"),
                // (27,20): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             C1 x = y3;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y3").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (27,20): error CS0029: Cannot implicitly convert type 'T' to 'ErrorTest.C1'
                //             C1 x = y3;
                Diagnostic(ErrorCode.ERR_NoImplicitConv, "y3").WithArguments("T", "ErrorTest.C1"),
                // (28,16): error CS0012: The type 'ErrorTest.C2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             y3.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.C2", "MissingBaseClass2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (28,16): error CS1061: 'T' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
                //             y3.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("T", "M1")
            };

            lib4.VerifyDiagnostics(expectedErrors);

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib2Ref, lib3Ref }, TestOptions.Dll);

            CompileAndVerify(lib4).VerifyDiagnostics();

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1.EmitToImageReference(), lib3.EmitToImageReference() }, TestOptions.Dll);

            lib4.VerifyDiagnostics(expectedErrors);
        }
        public void IvtVirtual_ParamsAndDynamic()
        {
            var source1 = @"
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm2"")]

public class A
{
    internal virtual void F(params int[] a) { }
    internal virtual void G(System.Action<dynamic> a) { }

    [System.Obsolete(""obsolete"", true)]
    internal virtual void H() { }

    internal virtual int this[int x, params int[] a] { get { return 0; } }
}
";
            // use IL to generate code that doesn't have synthesized ParamArrayAttribute on int[] parameters:

            // [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm3"")]
            // public class B : A
            // {
            //     internal override void F(int[] a) { }                            
            //     internal override void G(System.Action<object> a) { }
            //     internal override void H() { }
            //     internal override int this[int x, int[] a] { get { return 0; } }
            // }

            var source2 = @"
.assembly extern asm1
{
  .ver 0:0:0:0
}
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly asm2
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string) = ( 01 00 04 61 73 6D 33 00 00 )                      // ...asm3..
}

.class public auto ansi beforefieldinit B extends [asm1]A
{
  .custom instance void [mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(string) = ( 01 00 04 49 74 65 6D 00 00 )                      // ...Item..
  
  .method assembly hidebysig strict virtual instance void  F(int32[] a) cil managed 
  {
    nop
    ret
  }

  .method assembly hidebysig strict virtual instance void  G(class [mscorlib]System.Action`1<object> a) cil managed
  {
    nop
    ret
  }

  .method assembly hidebysig strict virtual instance void  H() cil managed
  {
    nop
    ret
  }

  .method assembly hidebysig specialname strict virtual instance int32  get_Item(int32 x, int32[] a) cil managed
  {
    ldloc.0
    ret
  }

  .method public hidebysig specialname rtspecialname instance void  .ctor() cil managed
  {
    ldarg.0
    call       instance void [asm1]A::.ctor()
    ret
  }

  .property instance int32 Item(int32, int32[])
  {
    .get instance int32 B::get_Item(int32,
                                    int32[])
  }
}";

            var source3 = @"
public class C : B
{
    void Test()
    {
        C c = new C();
        c.F();
        c.G(x => x.Bar());
        c.H();
        var z = c[1];
    }
}
";

            var comp1 = CreateCompilationWithMscorlib(source1,
                new[] { SystemCoreRef },
                options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider),
                assemblyName: "asm1");

            comp1.VerifyDiagnostics();
            var ref1 = new CSharpCompilationReference(comp1);

            var ref2 = CompileIL(source2, appendDefaultHeader: false);

            var comp3 = CreateCompilationWithMscorlib(source3,
                new[] { SystemCoreRef, ref1, ref2 },
                options: TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider),
                assemblyName: "asm3");

            comp3.VerifyDiagnostics(
                // (7,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'a' of 'B.F(int[])'
                Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "F").WithArguments("a", "B.F(int[])").WithLocation(7, 11),
                // (8,20): error CS1061: 'object' does not contain a definition for 'Bar' and no extension method 'Bar' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "Bar").WithArguments("object", "Bar").WithLocation(8, 20),
                // (10,17): error CS7036: There is no argument given that corresponds to the required formal parameter 'a' of 'B.this[int, int[]]'
                Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "c[1]").WithArguments("a", "B.this[int, int[]]").WithLocation(10, 17));
        }
Example #31
0
        public void MissingTypeInTypeArgumentsOfImplementedInterface()
        {
            var lib1 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public interface I1<out T1>
    {}

    public interface I2
    {}

    public interface I6<in T1>
    {}

    public class C10<T> where T : I1<I2>
    {}
}", compOptions: TestOptions.Dll, assemblyName: "MissingTypeInTypeArgumentsOfImplementedInterface1");

            var lib1Ref = new CSharpCompilationReference(lib1);

            var lib2 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public interface I3 : I2
    {}

}", new[] { lib1Ref }, TestOptions.Dll, assemblyName: "MissingTypeInTypeArgumentsOfImplementedInterface2");

            var lib2Ref = new CSharpCompilationReference(lib2);

            var lib3 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public class C4 : I1<I3>
    {}

    public interface I5 : I1<I3>
    {}

    public class C8<T> where T : I6<I3>
    {}
}", new[] { lib1Ref, lib2Ref }, TestOptions.Dll, assemblyName: "MissingTypeInTypeArgumentsOfImplementedInterface3");

            var lib3Ref = new CSharpCompilationReference(lib3);

            var lib4Def = @"
namespace ErrorTest
{
    class Test
    {
        void _Test(C4 y)
        {
            I1<I2> x = y;
        }
    }

    public class C6
        : I5
    {}

    public class C7
        : C4
    {}

    class Test3<T> where T : C4
    {
        void Test(T y3)
        {
            I1<I2> x = y3;
        }
    }

    class Test4<T> where T : I5
    {
        void Test(T y4)
        {
            I1<I2> x = y4;
        }
    }
    
    class Test5
    {
        void Test(I5 y5)
        {
            I1<I2> x = y5;
        }
    }

    public class C9
        : C8<I6<I2>>
    {}

    public class C11
        : C10<C4>
    {}

    public class C12
        : C10<I5>
    {}

    class Test6
    {
        void Test(C8<I6<I2>> x)
        {}
        void Test(C10<C4> x)
        {}
        void Test(C10<I5> x)
        {}
    }
}";

            var lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib3Ref }, TestOptions.Dll);

            DiagnosticDescription[] expectedErrors =
            {
                // (52,18): error CS0311: The type 'ErrorTest.I5' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C10<T>'. There is no implicit reference conversion from 'ErrorTest.I5' to 'ErrorTest.I1<ErrorTest.I2>'.
                //     public class C12
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C12").WithArguments("ErrorTest.C10<T>", "ErrorTest.I1<ErrorTest.I2>", "T", "ErrorTest.I5"),
                // (52,18): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C12
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C12").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (44,18): error CS0311: The type 'ErrorTest.I6<ErrorTest.I2>' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C8<T>'. There is no implicit reference conversion from 'ErrorTest.I6<ErrorTest.I2>' to 'ErrorTest.I6<ErrorTest.I3>'.
                //     public class C9
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C9").WithArguments("ErrorTest.C8<T>", "ErrorTest.I6<ErrorTest.I3>", "T", "ErrorTest.I6<ErrorTest.I2>"),
                // (44,18): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C9
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C9").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (48,18): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C10<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.I1<ErrorTest.I2>'.
                //     public class C11
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C11").WithArguments("ErrorTest.C10<T>", "ErrorTest.I1<ErrorTest.I2>", "T", "ErrorTest.C4"),
                // (48,18): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C11
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C11").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (12,18): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C6
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C6").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (60,27): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C10<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.I1<ErrorTest.I2>'.
                //         void Test(C10<C4> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C10<T>", "ErrorTest.I1<ErrorTest.I2>", "T", "ErrorTest.C4"),
                // (60,27): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C10<C4> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (62,27): error CS0311: The type 'ErrorTest.I5' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C10<T>'. There is no implicit reference conversion from 'ErrorTest.I5' to 'ErrorTest.I1<ErrorTest.I2>'.
                //         void Test(C10<I5> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C10<T>", "ErrorTest.I1<ErrorTest.I2>", "T", "ErrorTest.I5"),
                // (62,27): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C10<I5> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (58,30): error CS0311: The type 'ErrorTest.I6<ErrorTest.I2>' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C8<T>'. There is no implicit reference conversion from 'ErrorTest.I6<ErrorTest.I2>' to 'ErrorTest.I6<ErrorTest.I3>'.
                //         void Test(C8<I6<I2>> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C8<T>", "ErrorTest.I6<ErrorTest.I3>", "T", "ErrorTest.I6<ErrorTest.I2>"),
                // (58,30): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C8<I6<I2>> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (32,24): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1<I2> x = y4;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y4").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (32,24): error CS0266: Cannot implicitly convert type 'T' to 'ErrorTest.I1<ErrorTest.I2>'. An explicit conversion exists (are you missing a cast?)
                //             I1<I2> x = y4;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y4").WithArguments("T", "ErrorTest.I1<ErrorTest.I2>"),
                // (40,24): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1<I2> x = y5;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y5").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (40,24): error CS0266: Cannot implicitly convert type 'ErrorTest.I5' to 'ErrorTest.I1<ErrorTest.I2>'. An explicit conversion exists (are you missing a cast?)
                //             I1<I2> x = y5;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y5").WithArguments("ErrorTest.I5", "ErrorTest.I1<ErrorTest.I2>"),
                // (24,24): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1<I2> x = y3;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y3").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (24,24): error CS0266: Cannot implicitly convert type 'T' to 'ErrorTest.I1<ErrorTest.I2>'. An explicit conversion exists (are you missing a cast?)
                //             I1<I2> x = y3;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y3").WithArguments("T", "ErrorTest.I1<ErrorTest.I2>"),
                // (8,24): error CS0012: The type 'ErrorTest.I3' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1<I2> x = y;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y").WithArguments("ErrorTest.I3", "MissingTypeInTypeArgumentsOfImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (8,24): error CS0266: Cannot implicitly convert type 'ErrorTest.C4' to 'ErrorTest.I1<ErrorTest.I2>'. An explicit conversion exists (are you missing a cast?)
                //             I1<I2> x = y;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("ErrorTest.C4", "ErrorTest.I1<ErrorTest.I2>")
            };

            lib4.VerifyDiagnostics(expectedErrors);

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib2Ref, lib3Ref }, TestOptions.Dll);

            CompileAndVerify(lib4).VerifyDiagnostics();

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1.EmitToImageReference(), lib3.EmitToImageReference() }, TestOptions.Dll);

            lib4.VerifyDiagnostics(expectedErrors);
        }
Example #32
0
        public void CS1748ERR_NoCanonicalView()
        {
            var textdll = @"
using System.Runtime.InteropServices;
[assembly: ImportedFromTypeLib(""NoPiaTest"")]
[assembly: Guid(""A55E0B17-2558-447D-B786-84682CBEF136"")]
[assembly: BestFitMapping(false)]
[ComImport, Guid(""E245C65D-2448-447A-B786-64682CBEF133"")]
[TypeIdentifier(""E245C65D-2448-447A-B786-64682CBEF133"", ""IMyInterface"")]
public interface IMyInterface
{
    void Method(int n);
}
public delegate void DelegateWithInterface(IMyInterface value);
public delegate void DelegateWithInterfaceArray(IMyInterface[] ary);
public delegate IMyInterface DelegateRetInterface();
public delegate DelegateRetInterface DelegateRetDelegate(DelegateRetInterface d);
";
            var text = @"
class Test
{
    static void Main()
    {
    }
    public static void MyDelegate02(IMyInterface[] ary) { }
}
";
            var comp1 = CreateCompilationWithMscorlib(textdll);
            var ref1 = new CSharpCompilationReference(comp1);
            CreateCompilationWithMscorlib(text, references: new MetadataReference[] { ref1 }).VerifyDiagnostics(
                // (7,37): error CS0246: The type or namespace name 'IMyInterface' could not be found (are you missing a using directive or an assembly reference?)
                //     public static void MyDelegate02(IMyInterface[] ary) { }
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "IMyInterface").WithArguments("IMyInterface")
                );
        }
Example #33
0
        public void MissingImplementedInterface()
        {
            var lib1 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public interface I1
    {
        void M1();
    }

    public class C9<T> where T : I1
    { }
}
", compOptions: TestOptions.Dll, assemblyName: "MissingImplementedInterface1");

            var lib1Ref = new CSharpCompilationReference(lib1);

            var lib2 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public interface I2
        : I1
    {}

    public class C12
        : I2
    {
        private void M1() //Implements I1.M1
        {}

        void I1.M1()
        {
        }
    }
}
", new[] { lib1Ref }, TestOptions.Dll, assemblyName: "MissingImplementedInterface2");

            var lib2Ref = new CSharpCompilationReference(lib2);

            var lib3 = CreateCompilationWithMscorlib(@"
namespace ErrorTest
{
    public class C4
        : I2
    {
        private void M1() //Implements I1.M1
        {}

        void I1.M1()
        {
        }
    }

    public interface I5
        : I2
    {}

    public class C13
        : C12
    { }
}
", new[] { lib1Ref, lib2Ref }, TestOptions.Dll, assemblyName: "MissingImplementedInterface3");

            var lib3Ref = new CSharpCompilationReference(lib3);

            var lib4Def = @"
namespace ErrorTest
{
    class Test
    {
        void _Test(C4 y)
        {
            I1 x = y;
        }
    }

    public class C6 : I5
    {
        void I1.M1() 
        {}
    }

    public class C7 : C4
    {}

    class Test2
    {
        void _Test2(I5 x, C4 y)
        {
            x.M1();
            y.M1();
        }
    }

    class Test3<T> where T : C4
    {
        void Test(T y3)
        {
            I1 x = y3;
            y3.M1();
        }
    }

    class Test4<T> where T : I5
    {
        void Test(T y4)
        {
            I1 x = y4;
            y4.M1();
        }
    }

    public class C8 : I5 
    {
        void I1.M1()
        {}
    }

    public class C10 : C9<C4>
    {}

    public class C11 : C9<I5>
    {}

    class Test5
    {
        void Test(C9<C4> x)
        { }
        void Test(C9<I5> x)
        { }

        void Test(C13 c13)
        {
            I1 x = c13;
        }
    }
}
";

            var lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib3Ref }, TestOptions.Dll);

            lib4.VerifyDiagnostics(
                // (48,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C8 : I5 
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C8").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (12,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C6 : I5
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C6").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (50,14): error CS0540: 'ErrorTest.C8.ErrorTest.I1.M1()': containing type does not implement interface 'ErrorTest.I1'
                //         void I1.M1()
                Diagnostic(ErrorCode.ERR_ClassDoesntImplementInterface, "I1").WithArguments("ErrorTest.C8.ErrorTest.I1.M1()", "ErrorTest.I1"),
                // (14,14): error CS0540: 'ErrorTest.C6.ErrorTest.I1.M1()': containing type does not implement interface 'ErrorTest.I1'
                //         void I1.M1() 
                Diagnostic(ErrorCode.ERR_ClassDoesntImplementInterface, "I1").WithArguments("ErrorTest.C6.ErrorTest.I1.M1()", "ErrorTest.I1"),
                // (54,18): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C9<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.I1'.
                //     public class C10 : C9<C4>
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C10").WithArguments("ErrorTest.C9<T>", "ErrorTest.I1", "T", "ErrorTest.C4"),
                // (54,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C10 : C9<C4>
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C10").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (57,18): error CS0311: The type 'ErrorTest.I5' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C9<T>'. There is no implicit reference conversion from 'ErrorTest.I5' to 'ErrorTest.I1'.
                //     public class C11 : C9<I5>
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "C11").WithArguments("ErrorTest.C9<T>", "ErrorTest.I1", "T", "ErrorTest.I5"),
                // (57,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C11 : C9<I5>
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C11").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (64,26): error CS0311: The type 'ErrorTest.I5' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C9<T>'. There is no implicit reference conversion from 'ErrorTest.I5' to 'ErrorTest.I1'.
                //         void Test(C9<I5> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C9<T>", "ErrorTest.I1", "T", "ErrorTest.I5"),
                // (64,26): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C9<I5> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (62,26): error CS0311: The type 'ErrorTest.C4' cannot be used as type parameter 'T' in the generic type or method 'ErrorTest.C9<T>'. There is no implicit reference conversion from 'ErrorTest.C4' to 'ErrorTest.I1'.
                //         void Test(C9<C4> x)
                Diagnostic(ErrorCode.ERR_GenericConstraintNotSatisfiedRefType, "x").WithArguments("ErrorTest.C9<T>", "ErrorTest.I1", "T", "ErrorTest.C4"),
                // (62,26): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C9<C4> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (69,20): error CS0012: The type 'ErrorTest.C12' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = c13;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "c13").WithArguments("ErrorTest.C12", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (69,20): error CS0266: Cannot implicitly convert type 'ErrorTest.C13' to 'ErrorTest.I1'. An explicit conversion exists (are you missing a cast?)
                //             I1 x = c13;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "c13").WithArguments("ErrorTest.C13", "ErrorTest.I1"),
                // (8,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (8,20): error CS0266: Cannot implicitly convert type 'ErrorTest.C4' to 'ErrorTest.I1'. An explicit conversion exists (are you missing a cast?)
                //             I1 x = y;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("ErrorTest.C4", "ErrorTest.I1"),
                // (34,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y3;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y3").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (34,20): error CS0266: Cannot implicitly convert type 'T' to 'ErrorTest.I1'. An explicit conversion exists (are you missing a cast?)
                //             I1 x = y3;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y3").WithArguments("T", "ErrorTest.I1"),
                // (35,16): error CS0122: 'ErrorTest.C4.M1()' is inaccessible due to its protection level
                //             y3.M1();
                Diagnostic(ErrorCode.ERR_BadAccess, "M1").WithArguments("ErrorTest.C4.M1()"),
                // (25,15): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             x.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (25,15): error CS1061: 'ErrorTest.I5' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'ErrorTest.I5' could be found (are you missing a using directive or an assembly reference?)
                //             x.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("ErrorTest.I5", "M1"),
                // (26,15): error CS0122: 'ErrorTest.C4.M1()' is inaccessible due to its protection level
                //             y.M1();
                Diagnostic(ErrorCode.ERR_BadAccess, "M1").WithArguments("ErrorTest.C4.M1()"),
                // (43,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y4;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y4").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (43,20): error CS0266: Cannot implicitly convert type 'T' to 'ErrorTest.I1'. An explicit conversion exists (are you missing a cast?)
                //             I1 x = y4;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y4").WithArguments("T", "ErrorTest.I1"),
                // (44,16): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             y4.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (44,16): error CS1061: 'T' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
                //             y4.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("T", "M1")
                );

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1Ref, lib2Ref, lib3Ref }, TestOptions.Dll);

            lib4.VerifyDiagnostics(
                // (35,16): error CS0122: 'ErrorTest.C4.M1()' is inaccessible due to its protection level
                //             y3.M1();
                Diagnostic(ErrorCode.ERR_BadAccess, "M1").WithArguments("ErrorTest.C4.M1()"),
                // (26,15): error CS0122: 'ErrorTest.C4.M1()' is inaccessible due to its protection level
                //             y.M1();
                Diagnostic(ErrorCode.ERR_BadAccess, "M1").WithArguments("ErrorTest.C4.M1()")
                );

            lib4 = CreateCompilationWithMscorlib(lib4Def, new[] { lib1.EmitToImageReference(), lib3.EmitToImageReference() }, TestOptions.Dll);

            lib4.VerifyDiagnostics(
                // (12,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C6 : I5
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C6").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (48,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C8 : I5 
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C8").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (64,26): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C9<I5> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (62,26): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //         void Test(C9<C4> x)
                Diagnostic(ErrorCode.ERR_NoTypeDef, "x").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (54,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C10 : C9<C4>
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C10").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (57,18): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //     public class C11 : C9<I5>
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C11").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (8,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (69,20): error CS0012: The type 'ErrorTest.C12' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = c13;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "c13").WithArguments("ErrorTest.C12", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (69,20): error CS0266: Cannot implicitly convert type 'ErrorTest.C13' to 'ErrorTest.I1'. An explicit conversion exists (are you missing a cast?)
                //             I1 x = c13;
                Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "c13").WithArguments("ErrorTest.C13", "ErrorTest.I1"),
                // (34,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y3;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y3").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (35,16): error CS1061: 'T' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
                //             y3.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("T", "M1"),
                // (43,20): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             I1 x = y4;
                Diagnostic(ErrorCode.ERR_NoTypeDef, "y4").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (44,16): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             y4.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (25,15): error CS0012: The type 'ErrorTest.I2' is defined in an assembly that is not referenced. You must add a reference to assembly 'MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                //             x.M1();
                Diagnostic(ErrorCode.ERR_NoTypeDef, "M1").WithArguments("ErrorTest.I2", "MissingImplementedInterface2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"),
                // (26,15): error CS1061: 'ErrorTest.C4' does not contain a definition for 'M1' and no extension method 'M1' accepting a first argument of type 'ErrorTest.C4' could be found (are you missing a using directive or an assembly reference?)
                //             y.M1();
                Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "M1").WithArguments("ErrorTest.C4", "M1")
                );
        }
Example #34
0
        public void CS1755ERR_InvalidTypeIdentifierConstructor()
        {
            var textdll = @"using System;
using System.Runtime.InteropServices;
[assembly: ImportedFromTypeLib(""NoPiaTestLib"")]
[assembly: Guid(""A7721B07-2448-447A-BA36-64682CBEF136"")]
namespace NS
{
    [TypeIdentifier(""Foo2"", ""Bar2"")]
    public delegate void MyDel();
}
";
            var text = @"
public class Test
{
    event NS.MyDel e;
    public static void Main()
    {
    }
}
";
            var comp = CreateCompilationWithMscorlib(textdll);
            var ref1 = new CSharpCompilationReference(comp);
            CreateCompilationWithMscorlib(text, new[] { ref1 }).VerifyDiagnostics(
                // (4,14): error CS0234: The type or namespace name 'MyDel' does not exist in the namespace 'NS' (are you missing an assembly reference?)
                //     event NS.MyDel e;
                Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "MyDel").WithArguments("MyDel", "NS"),
                // (4,20): warning CS0067: The event 'Test.e' is never used
                //     event NS.MyDel e;
                Diagnostic(ErrorCode.WRN_UnreferencedEvent, "e").WithArguments("Test.e"));
            //var comp1 = DiagnosticsUtils.VerifyErrorsAndGetCompilationWithMscorlib(new List<string>() { text }, new List<MetadataReference>() { ref1 },
            //    new ErrorDescription { Code = 1755, Line = 4, Column = 14 });
        }
Example #35
0
        public void InheritedTypesCrossComps()
        {
            var text = @"namespace MT {
    public interface IFoo { void Foo(); }
    public interface IFoo<T, R> { R Foo(T t); }
    public interface IEmpty { }
}
";
            var text1 = @"namespace MT {
    public interface IBar<T> : IFoo, IEmpty { void Bar(T t); }
}
";
            var text2 = @"namespace NS {
    using MT;
    public class A<T> : IFoo<T, string>, IBar<T>, IFoo {
        void IFoo.Foo() { }
        public string Foo(T t) { return null; }
        void IBar<T>.Bar(T t) { }
    }

    public class B : A<ulong> {}
}
";
            var text3 = @"namespace NS {
    public class C : B {}
}
";
            var comp1 = CreateCompilationWithMscorlib(text);
            var compRef1 = new CSharpCompilationReference(comp1);

            var comp2 = CreateCompilationWithMscorlib(new string[] { text1, text2 }, assemblyName: "Test1",
                            references: new List<MetadataReference> { compRef1 });
            var compRef2 = new CSharpCompilationReference(comp2);

            var comp = CreateCompilationWithMscorlib(text3, assemblyName: "Test2",
                            references: new List<MetadataReference> { compRef2, compRef1 });

            Assert.Equal(0, comp1.GetDiagnostics().Count());
            Assert.Equal(0, comp2.GetDiagnostics().Count());
            Assert.Equal(0, comp.GetDiagnostics().Count());

            var global = comp.GlobalNamespace;
            var ns = global.GetMembers("NS").Single() as NamespaceSymbol;

            var type1 = ns.GetTypeMembers("C", 0).SingleOrDefault() as NamedTypeSymbol;
            Assert.Equal(0, type1.Interfaces.Length);
            //
            Assert.Equal(4, type1.AllInterfaces.Length);
            var sorted = (from i in type1.AllInterfaces
                          orderby i.Name
                          select i).ToArray();
            var i1 = sorted[0] as NamedTypeSymbol;
            var i2 = sorted[1] as NamedTypeSymbol;
            var i3 = sorted[2] as NamedTypeSymbol;
            var i4 = sorted[3] as NamedTypeSymbol;
            Assert.Equal("MT.IBar<System.UInt64>", i1.ToTestDisplayString());
            Assert.Equal(1, i1.Arity);
            Assert.Equal("MT.IEmpty", i2.ToTestDisplayString());
            Assert.Equal(0, i2.Arity);
            Assert.Equal("MT.IFoo<System.UInt64, System.String>", i3.ToTestDisplayString());
            Assert.Equal(2, i3.Arity);
            Assert.Equal("MT.IFoo", i4.ToTestDisplayString());
            Assert.Equal(0, i4.Arity);

            Assert.Equal("B", type1.BaseType.Name);
            // B
            var type2 = type1.BaseType as NamedTypeSymbol;
            //
            Assert.Equal(4, type2.AllInterfaces.Length);
            Assert.NotNull(type2.BaseType);
            // A<ulong>
            var type3 = type2.BaseType as NamedTypeSymbol;
            // T1?
            Assert.Equal("NS.A<System.UInt64>", type3.ToTestDisplayString());
            Assert.Equal(3, type3.Interfaces.Length);
            Assert.Equal(4, type3.AllInterfaces.Length);

            var type33 = ns.GetTypeMembers("A", 1).SingleOrDefault() as NamedTypeSymbol;
            Assert.Equal("NS.A<T>", type33.ToTestDisplayString());
            Assert.Equal(3, type33.Interfaces.Length);
            Assert.Equal(4, type33.AllInterfaces.Length);
        }
Example #36
0
        public void CS1768ERR_GenericsUsedInNoPIAType()
        {
            // add dll and make it embed
            var textdll = @"using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
[assembly: ImportedFromTypeLib(""NoPiaTestLib"")]
[assembly: Guid(""A7721B07-2448-447A-BA36-64682CBEF136"")]
namespace ClassLibrary3
{
    [ComImport, Guid(""b2496f7a-5d40-4abe-ad14-462f257a8ed5"")]
    public interface IFoo
    {
        IBar<string> foo();
    }
    [ComImport, Guid(""b2496f7a-5d40-4abe-ad14-462f257a8ed6"")]
    public interface IBar<T>
    {
        List<IFoo> GetList();
    }

}
";
            var text = @"
using System.Collections.Generic;
using ClassLibrary3;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IFoo x = (IFoo)new object();
        }
    }
    class foo : IBar<string>, IFoo
    {
        public List<string> GetList()
        {
            throw new NotImplementedException();
        }

        List<IFoo> IBar<string>.GetList()
        {
            throw new NotImplementedException();
        }
    }
  
}
";
            var comp = CreateCompilationWithMscorlib(textdll);
            var ref1 = new CSharpCompilationReference(comp, embedInteropTypes: true);
            CreateCompilationWithMscorlib(text, new[] { ref1 }).VerifyDiagnostics(
                Diagnostic(ErrorCode.ERR_GenericsUsedInNoPIAType));
        }
Example #37
0
        public void TypeCrossComps()
        {
            #region "Interface Impl"
            var text = @"
    public interface IFoo  {
        void M0();
    }
";

            var text1 = @"
    public class Foo : IFoo  {
        public void M0() {}
    }
";
            var comp1 = CreateCompilationWithMscorlib(text);
            var compRef1 = new CSharpCompilationReference(comp1);
            var comp = CreateCompilationWithMscorlib(text1, references: new List<MetadataReference> { compRef1 }, assemblyName: "Comp2");

            Assert.Equal(0, comp.GetDiagnostics().Count());
            #endregion

            #region "Interface Inherit"
            text = @"
    public interface IFoo  {
        void M0();
    }
";

            text1 = @"
    public interface IBar : IFoo  {
        void M1();
    }
";
            comp1 = CreateCompilationWithMscorlib(text);
            compRef1 = new CSharpCompilationReference(comp1);
            comp = CreateCompilationWithMscorlib(text1, references: new List<MetadataReference> { compRef1 }, assemblyName: "Comp2");

            Assert.Equal(0, comp.GetDiagnostics().Count());
            #endregion

            #region "Class Inherit"
            text = @"
public class A { 
    void M0() {}
}
";

            text1 = @"
public class B : A { 
    void M1() {}
}
";
            comp1 = CreateCompilationWithMscorlib(text);
            compRef1 = new CSharpCompilationReference(comp1);
            comp = CreateCompilationWithMscorlib(text1, references: new List<MetadataReference> { compRef1 }, assemblyName: "Comp2");

            Assert.Equal(0, comp.GetDiagnostics().Count());
            #endregion

            #region "Partial"
            text = @"
public partial interface IBar {
    void M0();
}

public partial class A { }
";

            text1 = @"
public partial interface IBar {
    void M1();
}

public partial class A { }
";
            comp1 = CreateCompilationWithMscorlib(text);
            compRef1 = new CSharpCompilationReference(comp1);
            comp = CreateCompilationWithMscorlib(text1, references: new List<MetadataReference> { compRef1 }, assemblyName: "Comp2");

            Assert.Equal(0, comp.GetDiagnostics().Count());
            #endregion
        }
Example #38
0
        public void Test2()
        {
            var oldMsCorLib = TestReferences.NetFx.v4_0_21006.mscorlib;
            var newMsCorLib = MscorlibRef;

            var source = @"
public class Modifiers
{
    public volatile int volatileFld;

    void F1(System.DateTime* p)
    {
    }
}
";

            CSharpCompilation c1 = CSharpCompilation.Create("C1", new[] { Parse(source) }, new[] { oldMsCorLib });

            var c1Assembly = c1.Assembly;

            var r1 = new CSharpCompilationReference(c1);
            CSharpCompilation c2 = CSharpCompilation.Create("C2", references: new[] { newMsCorLib, r1 });
            var c1AsmRef = c2.GetReferencedAssemblySymbol(r1);

            Assert.NotSame(c1Assembly, c1AsmRef);

            var mscorlibAssembly = c2.GetReferencedAssemblySymbol(newMsCorLib);

            Assert.NotSame(mscorlibAssembly, c1.GetReferencedAssemblySymbol(oldMsCorLib));

            var modifiers = c2.GlobalNamespace.GetTypeMembers("Modifiers").Single();

            Assert.IsType<RetargetingNamedTypeSymbol>(modifiers);

            FieldSymbol volatileFld = modifiers.GetMembers("volatileFld").OfType<FieldSymbol>().Single();

            Assert.Equal(1, volatileFld.CustomModifiers.Length);

            var volatileFldMod = volatileFld.CustomModifiers[0];

            Assert.False(volatileFldMod.IsOptional);
            Assert.Equal("System.Runtime.CompilerServices.IsVolatile", volatileFldMod.Modifier.ToTestDisplayString());
            Assert.Equal(SpecialType.System_Int32, volatileFld.Type.SpecialType);
            Assert.Same(mscorlibAssembly, volatileFldMod.Modifier.ContainingAssembly);

            Assert.Equal("volatileFld", volatileFld.Name);
            Assert.True(volatileFld.IsVolatile);
            Assert.Same(volatileFld, volatileFld.OriginalDefinition);
            Assert.Null(volatileFld.GetConstantValue(ConstantFieldsInProgress.Empty, earlyDecodingWellKnownAttributes: false));
            Assert.Null(volatileFld.ConstantValue);
            Assert.Null(volatileFld.AssociatedSymbol);
            Assert.Same(c1AsmRef, volatileFld.ContainingAssembly);
            Assert.Same(c1AsmRef.Modules[0], volatileFld.ContainingModule);
            Assert.Same(modifiers, volatileFld.ContainingSymbol);
            Assert.Equal(Accessibility.Public, volatileFld.DeclaredAccessibility);
            Assert.False(volatileFld.IsConst);
            Assert.False(volatileFld.IsReadOnly);
            Assert.False(volatileFld.IsStatic);
            Assert.Same(volatileFld.ContainingModule, ((RetargetingFieldSymbol)volatileFld).RetargetingModule);
            Assert.Same(c1Assembly, ((RetargetingFieldSymbol)volatileFld).UnderlyingField.ContainingAssembly);

            MethodSymbol m1 = modifiers.GetMembers("F1").OfType<MethodSymbol>().Single();

            Assert.Equal(0, m1.ReturnTypeCustomModifiers.Length);
            Assert.True(!m1.ExplicitInterfaceImplementations.IsDefault);
            Assert.Equal(0, m1.ExplicitInterfaceImplementations.Length);
            Assert.False(m1.HidesBaseMethodsByName);
            Assert.False(m1.IsExtensionMethod);
            Assert.Equal(((RetargetingMethodSymbol)m1).UnderlyingMethod.CallingConvention, m1.CallingConvention);
            Assert.Null(m1.AssociatedSymbol);
            Assert.Same(c1AsmRef.Modules[0], m1.ContainingModule);

            ParameterSymbol p1 = m1.Parameters[0];

            Assert.Equal(0, p1.CustomModifiers.Length);
            Assert.Same(c1AsmRef.Modules[0], p1.ContainingModule);
            Assert.False(p1.HasExplicitDefaultValue, "Parameter has default value");
            Assert.Equal(0, p1.Ordinal);

            //PointerTypeSymbol p1Type = (PointerTypeSymbol)p1.Type;

            //Assert.Same(mscorlibAssembly, p1Type.ContainingAssembly);
            //Assert.Equal(SpecialType.System_DateTime, p1Type.PointedAtType.SpecialType);
            //Assert.Equal(0, p1Type.CustomModifiers.Count);
        }
        public void PortableLibrary()
        {
            var plSource = @"public class C {}";
            var pl = CreateCompilation(plSource, new[] { MscorlibPP7Ref, SystemRuntimePP7Ref });
            var r1 = new CSharpCompilationReference(pl);

            var mainSource = @"public class D : C { }";

            // w/o facades:
            var main = CreateCompilation(mainSource, new MetadataReference[] { r1, MscorlibFacadeRef }, options: TestOptions.ReleaseDll);
            main.VerifyDiagnostics(
                // (1,18): error CS0012: The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
                Diagnostic(ErrorCode.ERR_NoTypeDef, "C").WithArguments("System.Object", "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));

            // facade specified:
            main = CreateCompilation(mainSource, new MetadataReference[] { r1, MscorlibFacadeRef, SystemRuntimeFacadeRef });
            main.VerifyDiagnostics();
        }
        public void TestImplicitMethodImplementation()
        {
            var text1 = @"
public interface BaseInterface1
{
    void BaseMethod();
}

public interface BaseInterface2
{
    void BaseMethod();
}
";
            var text2 = @"
public interface Interface : BaseInterface1, BaseInterface2
{
    void Method();
}
";
            var text3 = @"
class Class : Interface
{
    public void Method() { }
    public void BaseMethod() { }
}
";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var refs = new System.Collections.Generic.List<MetadataReference>() { comp1ref };

            var comp2 = CreateCompilationWithMscorlib(text2, references: refs, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            refs.Add(comp2ref);
            var comp = CreateCompilationWithMscorlib(text3, refs, assemblyName: "Test3");

            var global = comp.GlobalNamespace;

            var baseInterface1 = (NamedTypeSymbol)global.GetMembers("BaseInterface1").Single();
            var baseInterface1Method = baseInterface1.GetMembers("BaseMethod").Single();

            var baseInterface2 = (NamedTypeSymbol)global.GetMembers("BaseInterface2").Single();
            var baseInterface2Method = baseInterface2.GetMembers("BaseMethod").Single();

            var @interface = (NamedTypeSymbol)global.GetMembers("Interface").Single();
            var interfaceMethod = @interface.GetMembers("Method").Single();

            var @class = (NamedTypeSymbol)global.GetMembers("Class").Single();

            var classImplicitImplementation = (MethodSymbol)@class.GetMembers("Method").Single();
            Assert.Equal(MethodKind.Ordinary, classImplicitImplementation.MethodKind);

            var classImplicitImplementationBase = (MethodSymbol)@class.GetMembers("BaseMethod").Single();
            Assert.Equal(MethodKind.Ordinary, classImplicitImplementationBase.MethodKind);

            var implementingMethod = @class.FindImplementationForInterfaceMember(interfaceMethod);
            Assert.Same(classImplicitImplementation, implementingMethod);

            var implementingMethodBase1 = @class.FindImplementationForInterfaceMember(baseInterface1Method);
            Assert.Same(classImplicitImplementationBase, implementingMethodBase1);

            var implementingMethodBase2 = @class.FindImplementationForInterfaceMember(baseInterface2Method);
            Assert.Same(classImplicitImplementationBase, implementingMethodBase2);
        }
Example #41
0
        public void TestCyclicConstantEvalAcrossCompilations()
        {
            var source1 =
@"public class A
{
    public const string A1 = B.B1;
}
public class B
{
    public const string B1 = A.A1;
}
public class C
{
    public const string C1 = B.B1;
}";
            var source2 =
@"public class D
{
    public const string D1 = D1;
}";
            var source3 =
@"public class E
{
    public const string E1 = F.F1;
}
public class F
{
    public const string F1 = C.C1;
}";
            var source4 =
@"public class G
{
    public const string G1 = F.F1 + D.D1;
}";
            var compilation1 = CreateCompilationWithMscorlib(source1);
            var reference1 = new CSharpCompilationReference(compilation1);
            var compilation2 = CreateCompilationWithMscorlib(source2);
            var reference2 = new CSharpCompilationReference(compilation2);
            var compilation3 = CreateCompilationWithMscorlib(source3, new MetadataReference[] { reference1 });
            var reference3 = new CSharpCompilationReference(compilation3);
            var compilation4 = CreateCompilationWithMscorlib(source4, new MetadataReference[] { reference2, reference3 });
            compilation4.VerifyDiagnostics();
            compilation3.VerifyDiagnostics();
            compilation2.VerifyDiagnostics(
                // (3,25): error CS0110: The evaluation of the constant value for 'D.D1' involves a circular definition
                //     public const string D1 = D1;
                Diagnostic(ErrorCode.ERR_CircConstValue, "D1").WithArguments("D.D1").WithLocation(3, 25));
            compilation1.VerifyDiagnostics(
                // (3,25): error CS0110: The evaluation of the constant value for 'A.A1' involves a circular definition
                //     public const string A1 = B.B1;
                Diagnostic(ErrorCode.ERR_CircConstValue, "A1").WithArguments("A.A1").WithLocation(3, 25));
        }
        public void TestExplicitMethodImplementationInBase()
        {
            var text1 = @"
public interface BaseInterface
{
    void Method();
}

public interface Interface : BaseInterface
{
    new void Method();
}
";
            var text2 = @"
public class BaseClass : Interface
{
    void BaseInterface.Method() { }
    void Interface.Method() { }
    public void Method() { }
}
";
            var text3 = @"
class Class1 : BaseClass, Interface //declares Interface
{
}

class Class2 : BaseClass //does not declare interface
{
}
";

            var comp1 = CreateCompilationWithMscorlib(text1);
            var comp1ref = new CSharpCompilationReference(comp1);
            var refs = new System.Collections.Generic.List<MetadataReference>() { comp1ref };

            var comp2 = CreateCompilationWithMscorlib(text2, references: refs, assemblyName: "Test2");
            var comp2ref = new CSharpCompilationReference(comp2);

            refs.Add(comp2ref);
            var comp = CreateCompilationWithMscorlib(text3, refs, assemblyName: "Test3");

            var global = comp.GlobalNamespace;

            var baseInterface = (NamedTypeSymbol)global.GetMembers("BaseInterface").Single();
            var baseInterfaceMethod = baseInterface.GetMembers("Method").Single();

            var @interface = (NamedTypeSymbol)global.GetMembers("Interface").Single();
            var interfaceMethod = @interface.GetMembers("Method").Single();

            var baseClass = (NamedTypeSymbol)global.GetMembers("BaseClass").Single();

            var baseClassExplicitImplementationBase = (MethodSymbol)baseClass.GetMembers("BaseInterface.Method").Single();
            Assert.Equal(MethodKind.ExplicitInterfaceImplementation, baseClassExplicitImplementationBase.MethodKind);

            var baseClassExplicitImplementation = (MethodSymbol)baseClass.GetMembers("Interface.Method").Single();
            Assert.Equal(MethodKind.ExplicitInterfaceImplementation, baseClassExplicitImplementation.MethodKind);

            var baseClassImplicitImplementation = (MethodSymbol)baseClass.GetMembers("Method").Single();
            Assert.Equal(MethodKind.Ordinary, baseClassImplicitImplementation.MethodKind);

            Assert.NotSame(baseClassImplicitImplementation, baseClassExplicitImplementation);
            Assert.NotSame(baseClassImplicitImplementation, baseClassExplicitImplementationBase);

            var class1 = (NamedTypeSymbol)global.GetMembers("Class1").Single();

            var class1ImplementingMethodBase = class1.FindImplementationForInterfaceMember(baseInterfaceMethod);
            Assert.Same(baseClassExplicitImplementationBase, class1ImplementingMethodBase);

            var class1ImplementingMethod = class1.FindImplementationForInterfaceMember(interfaceMethod);
            Assert.Same(baseClassExplicitImplementation, class1ImplementingMethod);

            var class2 = (NamedTypeSymbol)global.GetMembers("Class2").Single();

            var class2ImplementingMethodBase = class2.FindImplementationForInterfaceMember(baseInterfaceMethod);
            Assert.Same(baseClassExplicitImplementationBase, class2ImplementingMethodBase);

            var class2ImplementingMethod = class2.FindImplementationForInterfaceMember(interfaceMethod);
            Assert.Same(baseClassExplicitImplementation, class2ImplementingMethod);
        }
Example #43
0
        public void IgnoreEffectivelyInternalStructFieldsOfReferenceTypeFromMetadata()
        {
            var comp1 = CreateCompilation(
                @"
internal class C1
{
    public struct S
    {
        public string data;
    }
}
public struct Struct
{
    internal C1.S data;
}
"
                );
            var sourceReference   = new CSharpCompilationReference(comp1);
            var metadataReference = MetadataReference.CreateFromStream(comp1.EmitToStream());

            var source2 =
                @"class Program
{
    public static void Main()
    {
        Struct r1;
        var r2 = r1;
    }
}";

            CreateCompilation(
                source2,
                references: new MetadataReference[] { sourceReference },
                options: TestOptions.ReleaseDll.WithWarningLevel(
                    CodeAnalysis.Diagnostic.DefaultWarningLevel
                    )
                )
            .VerifyDiagnostics();
            CreateCompilation(
                source2,
                references: new MetadataReference[] { metadataReference },
                options: TestOptions.ReleaseDll.WithWarningLevel(
                    CodeAnalysis.Diagnostic.DefaultWarningLevel
                    )
                )
            .VerifyDiagnostics();
            CreateCompilation(
                source2,
                references: new MetadataReference[] { sourceReference },
                options: TestOptions.ReleaseDll.WithWarningLevel(5)
                )
            .VerifyDiagnostics(
                // (6,18): warning CS8829: Use of unassigned local variable 'r1'
                //         var r2 = r1;
                Diagnostic(ErrorCode.WRN_UseDefViolation, "r1")
                .WithArguments("r1")
                .WithLocation(6, 18)
                );
            CreateCompilation(
                source2,
                references: new MetadataReference[] { metadataReference },
                options: TestOptions.ReleaseDll.WithWarningLevel(5)
                )
            .VerifyDiagnostics(
                // (6,18): warning CS8829: Use of unassigned local variable 'r1'
                //         var r2 = r1;
                Diagnostic(ErrorCode.WRN_UseDefViolation, "r1")
                .WithArguments("r1")
                .WithLocation(6, 18)
                );
        }