Skip to content

Commit

Permalink
Merge pull request #3812 from amcasey/GH3795
Browse files Browse the repository at this point in the history
Report diagnostics when short-circuiting emit
  • Loading branch information
amcasey committed Jul 2, 2015
2 parents aaf608f + 516db69 commit f165907
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2250,12 +2250,7 @@ internal override CommonPEModuleBuilder CreateModuleBuilder(
DiagnosticBag diagnostics,
CancellationToken cancellationToken)
{
// Do not waste a slot in the submission chain for submissions that contain no executable code
// (they may only contain #r directives, usings, etc.)
if (IsSubmission && !HasCodeToEmit())
{
return null;
}
Debug.Assert(!IsSubmission || HasCodeToEmit());

string runtimeMDVersion = GetRuntimeMetadataVersion(emitOptions, diagnostics);
if (runtimeMDVersion == null)
Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/Core/Portable/Compilation/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,15 @@ internal EmitResult Emit(
return ToEmitResultAndFree(diagnostics, success: false, entryPointOpt: null);
}

// Do not waste a slot in the submission chain for submissions that contain no executable code
// (they may only contain #r directives, usings, etc.)
if (IsSubmission && !HasCodeToEmit())
{
// Still report diagnostics since downstream submissions will assume there are no errors.
diagnostics.AddRange(this.GetDiagnostics());
return ToEmitResultAndFree(diagnostics, success: false, entryPointOpt: null);
}

var moduleBeingBuilt = this.CreateModuleBuilder(
options,
manifestResources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2147,12 +2147,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
cancellationToken As CancellationToken) As CommonPEModuleBuilder

Debug.Assert(diagnostics.IsEmptyWithoutResolution) ' True, but not required.

' Do not waste a slot in the submission chain for submissions that contain no executable code
' (they may only contain #r directives, usings, etc.)
If IsSubmission AndAlso Not HasCodeToEmit() Then
Return Nothing
End If
Debug.Assert(Not IsSubmission OrElse HasCodeToEmit())

' Get the runtime metadata version from the cor library. If this fails we have no reasonable value to give.
Dim runtimeMetadataVersion = GetRuntimeMetadataVersion()
Expand Down
27 changes: 27 additions & 0 deletions src/Scripting/CSharpTest/InteractiveSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,33 @@ public void SubmissionCompilation_Errors()
Assert.Throws<ArgumentException>(() => CSharpCompilation.CreateSubmission("a", options: TestOptions.ReleaseDll.WithDelaySign(false)));
}

[WorkItem(3795, "https://github.com/dotnet/roslyn/issues/3795")]
[Fact]
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),
// (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).
using (var stream = new MemoryStream())
{
var emitResult = submission.Emit(stream);
Assert.False(emitResult.Success);
emitResult.Diagnostics.Verify(expectedDiagnostics);
}

submission.GetDiagnostics().Verify(expectedDiagnostics);
}

private CSharpCompilation CreateSubmission(string code, CSharpParseOptions options, int expectedErrorCount = 0)
{
var submission = CSharpCompilation.CreateSubmission("sub",
Expand Down
21 changes: 21 additions & 0 deletions src/Scripting/VisualBasicTest/InteractiveSessionTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,27 @@ End Function")
'Assert.False(symbols.Any(Function(s) s.Name = "Roslyn"))
End Sub

<WorkItem(3795, "https:'github.com/dotnet/roslyn/issues/3795")>
<Fact>
Public Sub ErrorInUsing()
Dim submission = VisualBasicCompilation.CreateSubmission("sub1", Parse("Imports Unknown", options:=TestOptions.Script), {MscorlibRef})

Dim expectedErrors = <errors><![CDATA[
BC40056: Namespace or type specified in the Imports 'Unknown' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Imports Unknown
~~~~~~~
]]></errors>

' Emit produces the same diagnostics as GetDiagnostics (below).
Using stream As New MemoryStream()
Dim emitResult = submission.Emit(stream)
Assert.False(emitResult.Success)
emitResult.Diagnostics.AssertTheseDiagnostics(expectedErrors)
End Using

submission.GetDiagnostics().AssertTheseDiagnostics(expectedErrors)
End Sub

#End Region

#Region "Anonymous types"
Expand Down

0 comments on commit f165907

Please sign in to comment.