public void CompilationChain_SystemObject_NotEquals() { // As in VS/ETA, make a new list of references for each submission. var options = new CSharpParseOptions(kind: SourceCodeKind.Interactive, documentationMode: DocumentationMode.None); var provider = new TestMetadataReferenceProvider() { MakeDocumentationProvider = () => new TestDocumentationProviderNoEquals() }; var s1 = CSharpCompilation.CreateSubmission("s1.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("struct S { }", options), references: MakeReferencesViaCommandLine(provider), returnType: typeof(object)); s1.GetDiagnostics().Verify(); var s2 = CSharpCompilation.CreateSubmission("s2.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("System.Collections.IEnumerable Iterator() { yield return new S(); }", options), previousSubmission: s1, references: MakeReferencesViaCommandLine(provider), returnType: typeof(object)); Assert.NotEqual(s1.GetSpecialType(SpecialType.System_Object), s2.GetSpecialType(SpecialType.System_Object)); s2.GetDiagnostics().Verify( // (1,58): error CS0029: Cannot implicitly convert type 'S' to 'object' // System.Collections.IEnumerable Iterator() { yield return new S(); } Diagnostic(ErrorCode.ERR_NoImplicitConv, "new S()").WithArguments("S", "object")); }
public void SubmissionCompilation_Errors() { var genericParameter = typeof(List <>).GetGenericArguments()[0]; var open = typeof(Dictionary <,>).MakeGenericType(typeof(int), genericParameter); var ptr = typeof(int).MakePointerType(); var byref = typeof(int).MakeByRefType(); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", returnType: genericParameter)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", returnType: open)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", returnType: typeof(void))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", returnType: byref)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: genericParameter)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: open)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: typeof(void))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: typeof(int))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: ptr)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", hostObjectType: byref)); var s0 = CSharpCompilation.CreateSubmission("a0", hostObjectType: typeof(List <int>)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a1", previousSubmission: s0, hostObjectType: typeof(List <bool>))); // invalid options: Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseExe)); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithOutputKind(OutputKind.NetModule))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithOutputKind(OutputKind.WindowsRuntimeMetadata))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithOutputKind(OutputKind.WindowsRuntimeApplication))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithOutputKind(OutputKind.WindowsApplication))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithCryptoKeyContainer("foo"))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithCryptoKeyFile("foo.snk"))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithDelaySign(true))); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithDelaySign(false))); }
public void CompilationChain_SystemObject_Equals() { // As in VS/ETA, make a new list of references for each submission. var options = new CSharpParseOptions(kind: SourceCodeKind.Interactive, documentationMode: DocumentationMode.None); var provider = new TestMetadataReferenceProvider() { MakeDocumentationProvider = () => new TestDocumentationProviderEquals() }; var s1 = CSharpCompilation.CreateSubmission("s1.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("struct S { }", options), references: MakeReferencesViaCommandLine(provider), returnType: typeof(object)); s1.GetDiagnostics().Verify(); var s2 = CSharpCompilation.CreateSubmission("s2.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("System.Collections.IEnumerable Iterator() { yield return new S(); }", options), previousSubmission: s1, references: MakeReferencesViaCommandLine(provider), returnType: typeof(object)); s2.GetDiagnostics().Verify(); Assert.Equal(s1.GetSpecialType(SpecialType.System_Object), s2.GetSpecialType(SpecialType.System_Object)); }
public void UnusedUsingInteractive() { var tree = Parse("using System;", options: TestOptions.Interactive); var comp = CSharpCompilation.CreateSubmission("sub1", tree, new[] { MscorlibRef_v4_0_30316_17626 }); comp.VerifyDiagnostics(); }
public void CompilationChain_SystemObject_NotEquals() { // As in VS/ETA, make a new list of references for each submission. var options = new CSharpParseOptions(kind: SourceCodeKind.Interactive, documentationMode: DocumentationMode.None); var corLib = AssemblyMetadata.CreateFromImage(TestResources.NetFX.v4_0_30319.mscorlib); var s1 = CSharpCompilation.CreateSubmission("s1.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("struct S { }", options), references: new[] { corLib.GetReference(documentation: new TestDocumentationProviderNoEquals()) }, returnType: typeof(object)); s1.VerifyDiagnostics(); var s2 = CSharpCompilation.CreateSubmission("s2.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("System.Collections.IEnumerable Iterator() { yield return new S(); }", options), previousSubmission: s1, references: new[] { corLib.GetReference(documentation: new TestDocumentationProviderNoEquals()) }, returnType: typeof(object)); Assert.NotEqual(s1.GetSpecialType(SpecialType.System_Object), s2.GetSpecialType(SpecialType.System_Object)); s2.VerifyDiagnostics( // (1,58): error CS0029: Cannot implicitly convert type 'S' to 'object' // System.Collections.IEnumerable Iterator() { yield return new S(); } Diagnostic(ErrorCode.ERR_NoImplicitConv, "new S()").WithArguments("S", "object")); }
public void LabelLookup() { const string source = "using System; 1"; var tree = Parse(source, options: TestOptions.Script); var submission = CSharpCompilation.CreateSubmission("sub1", tree, new[] { MscorlibRef }); var model = submission.GetSemanticModel(tree); Assert.Empty(model.LookupLabels(source.Length - 1)); // Used to assert. }
private CSharpCompilation CreateSubmission(string code, CSharpParseOptions options, int expectedErrorCount = 0) { var submission = CSharpCompilation.CreateSubmission("sub", references: new[] { MetadataReference.CreateFromAssemblyInternal(typeof(object).Assembly) }, syntaxTree: Parse(code, options: options)); Assert.Equal(expectedErrorCount, submission.GetDiagnostics(CompilationStage.Declare, true, CancellationToken.None).Count()); return(submission); }
public void CompilationDoesNotAcceptArbitrarilyRootedTree() { var arbitraryTree = SyntaxFactory.SyntaxTree(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Wooh"))); var parsedTree = SyntaxFactory.ParseSyntaxTree(""); Assert.Throws <ArgumentException>(() => CSharpCompilation.Create("Grrr", syntaxTrees: new[] { arbitraryTree })); Assert.Throws <ArgumentException>(() => CSharpCompilation.CreateSubmission("Wah").AddSyntaxTrees(arbitraryTree)); Assert.Throws <ArgumentException>(() => CSharpCompilation.Create("Bahh", syntaxTrees: new[] { parsedTree }).ReplaceSyntaxTree(parsedTree, arbitraryTree)); Assert.Throws <ArgumentException>(() => CSharpCompilation.Create("Woo").GetSemanticModel(arbitraryTree)); }
public void Submissions_ExecutionOrder3() { var references = new[] { MetadataReference.CreateFromAssemblyInternal(typeof(object).Assembly) }; CSharpCompilation s0 = CSharpCompilation.CreateSubmission("s0.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("int a = \"x\";", options: TestOptions.Interactive), references: references, returnType: typeof(object)); Assert.Throws <InvalidOperationException>(() => { CSharpCompilation.CreateSubmission("s11.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("a + 1", options: TestOptions.Interactive), previousSubmission: s0, references: references, returnType: typeof(object)); }); }
public void Submissions_EmitToPeStream() { var references = new[] { MetadataReference.CreateFromAssemblyInternal(typeof(object).Assembly) }; CSharpCompilation s0 = CSharpCompilation.CreateSubmission("s0", syntaxTree: SyntaxFactory.ParseSyntaxTree("int a = 1;", options: TestOptions.Interactive), references: references, returnType: typeof(object)); CSharpCompilation s11 = CSharpCompilation.CreateSubmission("s11", syntaxTree: SyntaxFactory.ParseSyntaxTree("a + 1", options: TestOptions.Interactive), previousSubmission: s0, references: references, returnType: typeof(object)); CSharpCompilation s12 = CSharpCompilation.CreateSubmission("s12", syntaxTree: SyntaxFactory.ParseSyntaxTree("a + 2", options: TestOptions.Interactive), previousSubmission: s0, references: references, returnType: typeof(object)); CompileAndVerify(s11); CompileAndVerify(s12); }
public void HostObjectBinding_Diagnostics() { var submission = CSharpCompilation.CreateSubmission("foo", syntaxTree: SyntaxFactory.ParseSyntaxTree("x", options: TestOptions.Interactive), references: new[] { MscorlibRef, MetadataReference.CreateFromAssemblyInternal(typeof(ScriptTests).Assembly) }, hostObjectType: typeof(B)); submission.VerifyDiagnostics(); }
public void ExternAliasInInteractive_Error() { var src = "extern alias Bar;"; var comp = CSharpCompilation.CreateSubmission( GetUniqueName(), syntaxTree: SyntaxFactory.ParseSyntaxTree(src, options: TestOptions.Script), references: new MetadataReference[] { MscorlibRef, ExternAliasTests.Foo1, ExternAliasTests.Foo2 }); comp.VerifyDiagnostics( // (1,1): error CS7015: 'extern alias' is not valid in this context // extern alias Bar; Diagnostic(ErrorCode.ERR_ExternAliasNotAllowed, "extern alias Bar;")); }
public void Bug870885() { var source = @"var o = o.F;"; var syntaxTree = SyntaxFactory.ParseSyntaxTree(source, options: TestOptions.Interactive); var compilation = CSharpCompilation.CreateSubmission( GetUniqueName(), syntaxTree: syntaxTree, references: new MetadataReference[] { MscorlibRef, SystemCoreRef }, returnType: typeof(object)); compilation.VerifyDiagnostics( // (1,5): error CS7019: Type of 'o' cannot be inferred since its initializer directly or indirectly refers to the definition. // var o = o.F; Diagnostic(ErrorCode.ERR_RecursivelyTypedVariable, "o").WithArguments("o").WithLocation(1, 5)); }
public override Compilation CreateSubmission(Script script) { Compilation previousSubmission = null; if (script.Previous != null) { previousSubmission = script.Previous.GetCompilation(); } var diagnostics = DiagnosticBag.GetInstance(); var references = script.GetReferencesForCompilation(MessageProvider.Instance, diagnostics); // TODO: report diagnostics diagnostics.Free(); var parseOptions = script.Options.IsInteractive ? s_defaultInteractive : s_defaultScript; var tree = SyntaxFactory.ParseSyntaxTree(script.Code, parseOptions, script.Options.Path); string assemblyName, submissionTypeName; script.Builder.GenerateSubmissionId(out assemblyName, out submissionTypeName); var compilation = CSharpCompilation.CreateSubmission( assemblyName, tree, references, new CSharpCompilationOptions( outputKind: OutputKind.DynamicallyLinkedLibrary, mainTypeName: null, scriptClassName: submissionTypeName, usings: script.Options.Namespaces, optimizationLevel: OptimizationLevel.Debug, // TODO checkOverflow: false, // TODO allowUnsafe: true, // TODO platform: Platform.AnyCpu, warningLevel: 4, xmlReferenceResolver: null, // don't support XML file references in interactive (permissions & doc comment includes) sourceReferenceResolver: SourceFileResolver.Default, metadataReferenceResolver: script.Options.MetadataResolver, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default ), previousSubmission, script.ReturnType, script.GlobalsType ); return(compilation); }
public void CompilationChain_AnonymousTypeTemplates() { MetadataReference[] references = { MscorlibRef_v4_0_30316_17626, SystemCoreRef }; var parseOptions = TestOptions.Interactive; var s0 = CSharpCompilation.CreateSubmission("s0.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var x = new { a = 1 }; ", options: parseOptions), references: references, returnType: typeof(object)); var sx = CSharpCompilation.CreateSubmission("sx.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var y = new { b = 2 }; ", options: parseOptions), previousSubmission: s0, references: references, returnType: typeof(object)); var s1 = CSharpCompilation.CreateSubmission("s1.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var y = new { b = new { a = 3 } }; ", options: parseOptions), previousSubmission: s0, references: references, returnType: typeof(object)); var s2 = CSharpCompilation.CreateSubmission("s2.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("x = y.b; ", options: parseOptions), previousSubmission: s1, references: references, returnType: typeof(object)); var diagnostics = s2.GetDiagnostics().ToArray(); Assert.Equal(0, diagnostics.Length); using (MemoryStream stream = new MemoryStream()) { s2.Emit(stream); } Assert.True(s2.AnonymousTypeManager.AreTemplatesSealed); Assert.Equal(0, s2.AnonymousTypeManager.GetAllCreatedTemplates().Length); Assert.True(s1.AnonymousTypeManager.AreTemplatesSealed); Assert.Equal(1, s1.AnonymousTypeManager.GetAllCreatedTemplates().Length); Assert.True(s0.AnonymousTypeManager.AreTemplatesSealed); Assert.Equal(1, s0.AnonymousTypeManager.GetAllCreatedTemplates().Length); Assert.False(sx.AnonymousTypeManager.AreTemplatesSealed); }
public static CSharpCompilation CreateSubmission( string code, IEnumerable <MetadataReference> references = null, CSharpCompilationOptions options = null, CSharpParseOptions parseOptions = null, CSharpCompilation previous = null, Type returnType = null, Type hostObjectType = null) { return(CSharpCompilation.CreateSubmission( GetUniqueName(), references: (references != null) ? new[] { MscorlibRef_v4_0_30316_17626 }.Concat(references) : new[] { MscorlibRef_v4_0_30316_17626 }, options: options, syntaxTree: Parse(code, options: parseOptions ?? TestOptions.Interactive), previousSubmission: previous, returnType: returnType, hostObjectType: hostObjectType)); }
public void SubmissionResultType() { var submission = CSharpCompilation.CreateSubmission("sub"); bool hasValue; Assert.Equal(SpecialType.System_Void, submission.GetSubmissionResultType(out hasValue).SpecialType); Assert.False(hasValue); TestResult(CreateSubmission("1", TestOptions.Script, expectedErrorCount: 1), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("1", TestOptions.Interactive), expectedType: SpecialType.System_Int32, expectedHasValue: true); TestResult(CreateSubmission("1;", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("void foo() { }", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("using System;", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("int i;", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("System.Console.WriteLine();", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: false); TestResult(CreateSubmission("System.Console.WriteLine()", TestOptions.Interactive), expectedType: SpecialType.System_Void, expectedHasValue: true); TestResult(CreateSubmission("null", TestOptions.Interactive), expectedType: null, expectedHasValue: true); TestResult(CreateSubmission("System.Console.WriteLine", TestOptions.Interactive), expectedType: null, expectedHasValue: true); }
public void NoReferences() { var submission = CSharpCompilation.CreateSubmission("test", syntaxTree: SyntaxFactory.ParseSyntaxTree("1", options: TestOptions.Interactive), returnType: typeof(int)); submission.VerifyDiagnostics( // (1,1): error CS0518: Predefined type 'System.Object' is not defined or imported // 1 Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1").WithArguments("System.Object").WithLocation(1, 1), // error CS0518: Predefined type 'System.Object' is not defined or imported Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Object").WithLocation(1, 1), // error CS0518: Predefined type 'System.Threading.Tasks.Task`1' is not defined or imported Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Threading.Tasks.Task`1").WithLocation(1, 1), // error CS0400: The type or namespace name 'System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' could not be found in the global namespace (are you missing an assembly reference?) Diagnostic(ErrorCode.ERR_GlobalSingleTypeNameNotFound).WithArguments("System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").WithLocation(1, 1), // (1,1): error CS0518: Predefined type 'System.Int32' is not defined or imported // 1 Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1").WithArguments("System.Int32").WithLocation(1, 1), // error CS0518: Predefined type 'System.Object' is not defined or imported Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound).WithArguments("System.Object").WithLocation(1, 1)); }
public void CompilationChain_DynamicSiteDelegates() { MetadataReference[] references = { MscorlibRef, SystemCoreRef, CSharpRef }; var parseOptions = TestOptions.Interactive; var s0 = CSharpCompilation.CreateSubmission("s0.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var i = 1; dynamic d = null; d.m(ref i);", options: parseOptions), references: references, returnType: typeof(object)); var sx = CSharpCompilation.CreateSubmission("sx.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var i = 1; dynamic d = null; d.m(ref i, ref i);", options: parseOptions), previousSubmission: s0, references: references, returnType: typeof(object)); var s1 = CSharpCompilation.CreateSubmission("s1.dll", syntaxTree: SyntaxFactory.ParseSyntaxTree("var i = 1; dynamic d = null; d.m(out i);", options: parseOptions), previousSubmission: s0, references: references, returnType: typeof(object)); var diagnostics = s1.GetDiagnostics().ToArray(); diagnostics.Verify(); using (MemoryStream stream = new MemoryStream()) { s1.Emit(stream); } // no new delegates should have been created: Assert.True(s1.AnonymousTypeManager.AreTemplatesSealed); Assert.Equal(0, s1.AnonymousTypeManager.GetAllCreatedTemplates().Length); // delegate for (ref) Assert.True(s0.AnonymousTypeManager.AreTemplatesSealed); Assert.Equal(1, s0.AnonymousTypeManager.GetAllCreatedTemplates().Length); Assert.False(sx.AnonymousTypeManager.AreTemplatesSealed); }
public void ErrorInUsing() { var submission = CSharpCompilation.CreateSubmission("sub1", Parse("using Unknown;", options: TestOptions.Script), new[] { MscorlibRef }); var expectedDiagnostics = new[] { // (1,7): error CS0246: The type or namespace name 'Unknown' could not be found (are you missing a using directive or an assembly reference?) // using Unknown; Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Unknown").WithArguments("Unknown").WithLocation(1, 7), }; // Emit produces the same diagnostics as GetDiagnostics (below). using (var stream = new MemoryStream()) { var emitResult = submission.Emit(stream); Assert.False(emitResult.Success); emitResult.Diagnostics.Verify(expectedDiagnostics); } submission.GetDiagnostics().Verify(expectedDiagnostics); }