diff --git a/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs b/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs index 332a339f819cecf4fb5363a110049727acce2db3..1d3695345c9f243b1fcc4cd27d2a0f2e2f01a823 100644 --- a/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs +++ b/src/Compilers/CSharp/Test/Emit/Emit/ScriptTests.cs @@ -582,9 +582,6 @@ public void ErrorInUsing() // (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), - // (1,1): hidden CS8019: Unnecessary using directive. - // using Unknown; - Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using Unknown;").WithLocation(1, 1), }; // Emit produces the same diagnostics as GetDiagnostics (below). diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs index d04a41c85a917bac375ae0892a96d35f333dc160..d41f6b31653a388ded1d6030673a93adf1f2dbcc 100644 --- a/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Compilation/GetUnusedImportDirectivesTests.cs @@ -358,5 +358,26 @@ public void InfoAndWarnAsError() // using System; Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithWarningAsError(false)); } + + [Fact] + 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(); + } + + [Fact] + public void UnusedUsingScript() + { + var tree = Parse("using System;", options: TestOptions.Script); + var comp = CreateCompilationWithMscorlib(tree); + + comp.VerifyDiagnostics( + // (2,1): info CS8019: Unnecessary using directive. + // using System; + Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;")); + } } } diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs index f40b70b1b51edd55c1f77036344763fff0147dc2..1125fdbbf1c45838140331d26de7d434bb66cf7a 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/ExternAliasTests.cs @@ -111,10 +111,7 @@ public void ExternAliasInInteractive_Error() comp.VerifyDiagnostics( // (1,1): error CS7015: 'extern alias' is not valid in this context // extern alias Bar; - Diagnostic(ErrorCode.ERR_ExternAliasNotAllowed, "extern alias Bar;"), - // (1,1): info CS8020: Unused extern alias. - // extern alias Bar; - Diagnostic(ErrorCode.HDN_UnusedExternAlias, "extern alias Bar;")); + Diagnostic(ErrorCode.ERR_ExternAliasNotAllowed, "extern alias Bar;")); } [Fact] diff --git a/src/Compilers/Core/Portable/Compilation/Compilation.cs b/src/Compilers/Core/Portable/Compilation/Compilation.cs index 68d13ae50078ec2db83b3f1b2cf0debb9f0bf989..79de2b6cfae51e5885f94cc5af2112fe71f4be5a 100644 --- a/src/Compilers/Core/Portable/Compilation/Compilation.cs +++ b/src/Compilers/Core/Portable/Compilation/Compilation.cs @@ -2098,7 +2098,8 @@ internal void MarkImportDirectiveAsUsed(SyntaxNode node) internal void MarkImportDirectiveAsUsed(SyntaxTree syntaxTree, int position) { - if (syntaxTree != null) + // Optimization: Don't initialize TreeToUsedImportDirectivesMap in submissions. + if (!IsSubmission && syntaxTree != null) { var set = TreeToUsedImportDirectivesMap.GetOrAdd(syntaxTree, s_createSetCallback); set.Add(position); @@ -2107,8 +2108,13 @@ internal void MarkImportDirectiveAsUsed(SyntaxTree syntaxTree, int position) internal bool IsImportDirectiveUsed(SyntaxTree syntaxTree, int position) { - SmallConcurrentSetOfInts usedImports; + if (IsSubmission) + { + // Since usings apply to subsequent submissions, we have to assume they are used. + return true; + } + SmallConcurrentSetOfInts usedImports; return syntaxTree != null && TreeToUsedImportDirectivesMap.TryGetValue(syntaxTree, out usedImports) && usedImports.Contains(position); diff --git a/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb b/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb index f10361645543c7fee5b77cb72cbf8db78c1a422f..41d8519bd35ef53e92ad6967d8af8e709ff2e331 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/Semantics/GetUnusedImportDirectivesTests.vb @@ -300,5 +300,24 @@ Imports System ' With doc comments. CreateCompilationWithMscorlib(source, parseOptions:=New VisualBasicParseOptions(documentationMode:=DocumentationMode.Diagnose)).AssertTheseDiagnostics(, suppressInfos:=False) End Sub + + + Public Sub UnusedImportInteractive() + Dim tree = Parse("Imports System", options:=TestOptions.Interactive) + Dim compilation = VisualBasicCompilation.CreateSubmission("sub1", tree, {MscorlibRef_v4_0_30316_17626}) + compilation.AssertNoDiagnostics(suppressInfos:=False) + End Sub + + + Public Sub UnusedImportScript() + Dim tree = Parse("Imports System", options:=TestOptions.Script) + Dim compilation = CreateCompilationWithMscorlib(tree) + compilation.AssertTheseDiagnostics( + +BC50001: Unused import statement. +Imports System +~~~~~~~~~~~~~~ + , suppressInfos:=False) + End Sub End Class End Namespace