-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "#error version" to C# and udpate UpgradeProject to support 7.1 #18045
Changes from 2 commits
3c4893f
5d81ef0
9da0117
0c04bef
da46683
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,13 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Roslyn.Test.Utilities; | ||
using Xunit; | ||
|
@@ -58,7 +61,7 @@ internal struct MemberInfo | |
|
||
#endregion | ||
|
||
public class PreprocessorTests | ||
public class PreprocessorTests : TestBase | ||
{ | ||
public PreprocessorTests() | ||
{ | ||
|
@@ -2962,6 +2965,71 @@ private void CheckDiagnosticStringFileName(string compilationFileName, string li | |
Assert.Equal(expectedErrorStringFileName, actualErrorStringFileName); | ||
} | ||
|
||
[Fact] | ||
public void TestErrorWithVersion() | ||
{ | ||
var text = "#error version"; | ||
var node = Parse(text, SourceCodeKind.Regular); | ||
TestRoundTripping(node, text, disallowErrors: false); | ||
VerifyDirectivesSpecial(node, new DirectiveInfo | ||
{ | ||
Kind = SyntaxKind.ErrorDirectiveTrivia, | ||
Status = NodeStatus.IsActive, | ||
Text = "version" | ||
}); | ||
|
||
node.GetDiagnostics().Verify( | ||
// (1,8): error CS1029: #error: 'version' | ||
// #error version | ||
Diagnostic(ErrorCode.ERR_ErrorDirective, "version").WithArguments("version").WithLocation(1, 8), | ||
// (1,8): error CS8304: Compiler version: '42.42.42.42424 (<developer build>)'. Language version: 4. | ||
// #error version | ||
Diagnostic(ErrorCode.ERR_CompilerAndLanguageVersion, "version").WithArguments(GetExpectedVersion(), "4").WithLocation(1, 8) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void TestErrorWithVersionNumber() | ||
{ | ||
var text = "#error version:7.1"; | ||
var node = Parse(text, SourceCodeKind.Regular); | ||
TestRoundTripping(node, text, disallowErrors: false); | ||
VerifyDirectivesSpecial(node, new DirectiveInfo | ||
{ | ||
Kind = SyntaxKind.ErrorDirectiveTrivia, | ||
Status = NodeStatus.IsActive, | ||
Text = "7.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There must be a bug in the test method (the test passed). I'll investigate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, the verification method was silently dropping some expectations :-S |
||
}); | ||
|
||
node.GetDiagnostics().Verify( | ||
// (1,8): error CS1029: #error: 'version:7.1' | ||
// #error version:7.1 | ||
Diagnostic(ErrorCode.ERR_ErrorDirective, "version:7.1").WithArguments("version:7.1").WithLocation(1, 8), | ||
// (1,8): error CS8302: Feature 'version' is not available in C# 7.1. Please use language version 7.1 or greater. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this instead say that feature 'version' is not available in C# 4? Because Parse uses version 4? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense and still fulfills the need. Fixed. |
||
// #error version:7.1 | ||
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "version:7.1").WithArguments("version", "7.1").WithLocation(1, 8) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void TestErrorWithInvalidVersion() | ||
{ | ||
var text = "#error version:A.B"; | ||
var node = Parse(text, SourceCodeKind.Regular); | ||
TestRoundTripping(node, text, disallowErrors: false); | ||
VerifyDirectivesSpecial(node, new DirectiveInfo | ||
{ | ||
Kind = SyntaxKind.ErrorDirectiveTrivia, | ||
Status = NodeStatus.IsActive, | ||
Text = "A.B" | ||
}); | ||
|
||
node.GetDiagnostics().Verify( | ||
// (1,8): error CS1029: #error: 'version:A.B' | ||
// #error version:A.B | ||
Diagnostic(ErrorCode.ERR_ErrorDirective, "version:A.B").WithArguments("version:A.B").WithLocation(1, 8) | ||
); | ||
} | ||
#endregion | ||
|
||
#region #line | ||
|
@@ -3874,5 +3942,13 @@ public void TestLoadWithComment() | |
} | ||
|
||
#endregion | ||
|
||
private static string GetExpectedVersion() | ||
{ | ||
Assembly assembly = typeof(CSharpCompiler).GetTypeInfo().Assembly; | ||
string fileVersion = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version; | ||
string hash = CommonCompiler.ExtractShortCommitHash(assembly.GetCustomAttribute<CommitHashAttribute>().Hash); | ||
return $"{fileVersion} ({hash})"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ private async Task TestLanguageVersionUpgradedAsync( | |
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUpgradeProject)] | ||
public async Task UpgradeProjectToDefault() | ||
public async Task UpgradeProjectFromCSharp6ToDefault() | ||
{ | ||
await TestLanguageVersionUpgradedAsync( | ||
@" | ||
|
@@ -55,7 +55,7 @@ void A() | |
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUpgradeProject)] | ||
public async Task UpgradeProjectToCSharp7() | ||
public async Task UpgradeProjectFromCSharp6ToCSharp7() | ||
{ | ||
await TestLanguageVersionUpgradedAsync( | ||
@" | ||
|
@@ -71,6 +71,33 @@ void A() | |
index: 1); | ||
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUpgradeProject)] | ||
public async Task UpgradeProjectFromCSharp7ToLatest() | ||
{ | ||
await TestLanguageVersionUpgradedAsync( | ||
@" | ||
class Program | ||
{ | ||
#error version:[|7.1|] | ||
}", | ||
LanguageVersion.Latest, | ||
new CSharpParseOptions(LanguageVersion.CSharp7)); | ||
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUpgradeProject)] | ||
public async Task UpgradeProjectFromCSharp7ToCSharp7_1() | ||
{ | ||
await TestLanguageVersionUpgradedAsync( | ||
@" | ||
class Program | ||
{ | ||
#error [|version:7.1|] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, i'm not getting what the user scenario is for this. Can you clarify? I did understand what the use of "#error version" was though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the bug reports we're getting are the result of using older versions of the compiler. When the compiler is hosted by a framework (asp.net, azure functions, CLI, etc) it will be convenient for troubleshooting to be able to check the compiler version with a source change. It's similar to "phpversion". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get the reason for "#error version". I'm curious what the purposes of "#error version:number" is though... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I had misread your comment. |
||
}", | ||
LanguageVersion.CSharp7_1, | ||
new CSharpParseOptions(LanguageVersion.CSharp7), | ||
index: 1); | ||
} | ||
|
||
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUpgradeProject)] | ||
public async Task UpgradeAllProjectsToDefault() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -69,7 +70,9 @@ protected bool TryGetDocumentAndSelectSpan(TestWorkspace workspace, out Document | |
|
||
protected Document GetDocumentAndAnnotatedSpan(TestWorkspace workspace, out string annotation, out TextSpan span) | ||
{ | ||
var hostDocument = workspace.Documents.Single(d => d.AnnotatedSpans.Any()); | ||
var annotatedDocuments = workspace.Documents.Where(d => d.AnnotatedSpans.Any()); | ||
Debug.Assert(!annotatedDocuments.IsEmpty(), "No annotated span found"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes an annoyance I ran into when working on the original PR. |
||
var hostDocument = annotatedDocuments.Single(); | ||
var annotatedSpan = hostDocument.AnnotatedSpans.Single(); | ||
annotation = annotatedSpan.Key; | ||
span = annotatedSpan.Value.Single(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,10 @@ internal class CSharpUpgradeProjectCodeFixProvider : AbstractUpgradeProjectCodeF | |
private const string CS8025 = nameof(CS8025); // error CS8025: Feature is not available in C# 4. Please use language version X or greater. | ||
private const string CS8026 = nameof(CS8026); // error CS8026: Feature is not available in C# 5. Please use language version X or greater. | ||
private const string CS8059 = nameof(CS8059); // error CS8059: Feature is not available in C# 6. Please use language version X or greater. | ||
private const string CS8302 = nameof(CS8302); // error CS8302: Feature is not available in C# 7.0. Please use language version X or greater. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will break when we compact the set of newly assigned error codes? Or are we committing to this particular number for the error now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this will need to be updated when we compact error codes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like |
||
|
||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = | ||
ImmutableArray.Create(CS8022, CS8023, CS8024, CS8025, CS8026, CS8059); | ||
ImmutableArray.Create(CS8022, CS8023, CS8024, CS8025, CS8026, CS8059, CS8302); | ||
|
||
public override string UpgradeThisProjectResource => CSharpFeaturesResources.Upgrade_this_project_to_csharp_language_version_0; | ||
public override string UpgradeAllProjectsResource => CSharpFeaturesResources.Upgrade_all_csharp_projects_to_language_version_0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,7 @@ Public Class ParseTree | |
Return Enumerations(enumString) | ||
End If | ||
|
||
ReportError(referencingElement, "{0} is not a valid field type", enumString) | ||
ReportError(referencingElement, "{0} is not a valid field type. You should add a node-kind entry in the syntax.xml.", enumString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes an annoyance I ran into when working on the original PR. |
||
Return Nothing | ||
End Function | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this showing language version 4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you were talking about the formatting ('4' vs. 4) in your previous comment :-S
The reason is because
Parse
uses language version 4: