Skip to content

Commit

Permalink
Attach C# 9 features to LanguageVersion.CSharp9 (#46645)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Aug 15, 2020
1 parent 7ccfbe0 commit 1ed3ef5
Show file tree
Hide file tree
Showing 107 changed files with 2,556 additions and 2,489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public sealed class IsExternalInit
{
TestCode = source,
FixedCode = fixedSource,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.Preview,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9,
};

await test.RunAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ public async Task TopLevelStatement()
{
TestCode = source,
FixedCode = fixedSource,
LanguageVersion = LanguageVersion.Preview,
LanguageVersion = LanguageVersion.CSharp9,
};

test.ExpectedDiagnostics.Add(
Expand Down Expand Up @@ -1962,7 +1962,7 @@ public async Task TopLevelStatement_FollowedWithThrow()
{
TestCode = source,
FixedCode = fixedSource,
LanguageVersion = LanguageVersion.Preview,
LanguageVersion = LanguageVersion.CSharp9,
};

test.ExpectedDiagnostics.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2420,7 +2420,37 @@ void M(object p)
break;
};
}
}", options: PreferDiscard);
}", options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
public async Task DeclarationPatternInSwitchCase_WithOnlyWriteReference_PreferDiscard_CSharp9()
{
await TestInRegularAndScriptAsync(
@"class C
{
void M(object p)
{
switch (p)
{
case int [|x|]:
x = 1;
break;
};
}
}",
@"class C
{
void M(object p)
{
switch (p)
{
case int:
int x = 1;
break;
};
}
}", options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp9));
}

[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
Expand Down Expand Up @@ -4918,7 +4948,7 @@ void M(object p)
break;
}}
}}
}}", optionName);
}}", optionName, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
Expand Down Expand Up @@ -7684,7 +7714,7 @@ void M(object p)
break;
};
}
}", options: PreferDiscard);
}", options: PreferDiscard, parseOptions: new CSharpParseOptions(LanguageVersion.CSharp8));
}

[WorkItem(32856, "https://github.com/dotnet/roslyn/issues/33312")]
Expand Down Expand Up @@ -8131,7 +8161,7 @@ void M()
using var [|x|] = new C();
}
}", options: PreferDiscard,
parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview));
parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
Expand All @@ -8153,7 +8183,7 @@ void M()
using var [|x|] = new C() { P = 1 };
}
}", options: PreferDiscard,
parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview));
parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ string M(object o)
if (!(o is string x)) return null;
return x;
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1090,7 +1090,7 @@ string M(object o, bool b)
return x;
}
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1330,7 +1330,7 @@ void M(object e)
{
for (; !(!(e is C c));) { }
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand All @@ -1357,7 +1357,7 @@ void M(object e)
M(c);
}
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1406,7 +1406,7 @@ void M(object e, bool b)
M(c);
}
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1434,7 +1434,7 @@ void M(object e)
M(c);
}
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1489,7 +1489,7 @@ void M(object e)
{
C F() => !(e is C c) ? null : c;
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down Expand Up @@ -1525,7 +1525,7 @@ void M(object e)
{
System.Func<C> f = () => !(e is C c) ? null : c;
}
}");
}", parameters: new TestParameters(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -61,7 +62,60 @@ int M()
return o is string e ? 1 : 0;
}
}");
}", parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8));
}

[Fact(Skip = "https://github.com/dotnet/roslyn/issues/46821"), Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task FixAllInDocument1_CSharp9()
{
await TestInRegularAndScriptAsync(
@"class C
{
int M()
{
string a;
{|FixAllInDocument:var|} x = o as string;
if (x != null)
{
}
var y = o as string;
if (y != null)
{
}
if ((a = o as string) == null)
{
}
var c = o as string;
var d = c != null ? 1 : 0;
var e = o as string;
return e != null ? 1 : 0;
}
}",
@"class C
{
int M()
{
if (o is string x)
{
}
if (o is string y)
{
}
if (o is not string a)
{
}
var d = o is string c ? 1 : 0;
return o is string e ? 1 : 0;
}
}", parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9));
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/Errors/MessageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
// Checks are in the LanguageParser unless otherwise noted.
switch (feature)
{
// C# preview features.
// C# 9.0 features.
case MessageID.IDS_FeatureLambdaDiscardParameters: // semantic check
case MessageID.IDS_FeatureFunctionPointers:
case MessageID.IDS_FeatureLocalFunctionAttributes: // syntax check
Expand All @@ -344,7 +344,7 @@ internal static LanguageVersion RequiredVersion(this MessageID feature)
case MessageID.IDS_FeatureStaticAnonymousFunction: // syntax check
case MessageID.IDS_FeatureModuleInitializers: // semantic check on method attribute
case MessageID.IDS_FeatureDefaultTypeParameterConstraint:
return LanguageVersion.Preview;
return LanguageVersion.CSharp9;

// C# 8.0 features.
case MessageID.IDS_FeatureAltInterpolatedVerbatimStrings:
Expand Down
4 changes: 1 addition & 3 deletions src/Compilers/CSharp/Portable/LanguageVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,7 @@ public static LanguageVersion MapSpecifiedToEffectiveVersion(this LanguageVersio
case LanguageVersion.Latest:
case LanguageVersion.Default:
case LanguageVersion.LatestMajor:
return LanguageVersion.CSharp8;
case LanguageVersion.CSharp9:
return LanguageVersion.Preview;
return LanguageVersion.CSharp9;
default:
return version;
}
Expand Down
13 changes: 7 additions & 6 deletions src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,8 @@ public void ArgumentParsing()
[InlineData("7.3", LanguageVersion.CSharp7_3)]
[InlineData("8", LanguageVersion.CSharp8)]
[InlineData("8.0", LanguageVersion.CSharp8)]
[InlineData("9", LanguageVersion.CSharp9)]
[InlineData("9.0", LanguageVersion.CSharp9)]
[InlineData("preview", LanguageVersion.Preview)]
public void LangVersion_CanParseCorrectVersions(string value, LanguageVersion expectedVersion)
{
Expand Down Expand Up @@ -1655,7 +1657,6 @@ public void LanguageVersionAdded_Canary()
{
// When a new version is added, this test will break. This list must be checked:
// - update the "UpgradeProject" codefixer
// - update the IDE drop-down for selecting Language Version (in project-systems repo)
// - update all the tests that call this canary
AssertEx.SetEqual(new[] { "default", "1", "2", "3", "4", "5", "6", "7.0", "7.1", "7.2", "7.3", "8.0", "9.0", "latest", "latestmajor", "preview" },
Enum.GetValues(typeof(LanguageVersion)).Cast<LanguageVersion>().Select(v => v.ToDisplayString()));
Expand Down Expand Up @@ -1709,10 +1710,11 @@ public void LanguageVersion_GetErrorCode()
InlineData(LanguageVersion.CSharp7_2, LanguageVersion.CSharp7_2),
InlineData(LanguageVersion.CSharp7_3, LanguageVersion.CSharp7_3),
InlineData(LanguageVersion.CSharp8, LanguageVersion.CSharp8),
InlineData(LanguageVersion.CSharp8, LanguageVersion.LatestMajor),
InlineData(LanguageVersion.CSharp8, LanguageVersion.Latest),
InlineData(LanguageVersion.CSharp8, LanguageVersion.Default),
InlineData(LanguageVersion.Preview, LanguageVersion.CSharp9),
InlineData(LanguageVersion.CSharp9, LanguageVersion.CSharp9),
InlineData(LanguageVersion.CSharp9, LanguageVersion.LatestMajor),
InlineData(LanguageVersion.CSharp9, LanguageVersion.Latest),
InlineData(LanguageVersion.CSharp9, LanguageVersion.Default),
InlineData(LanguageVersion.Preview, LanguageVersion.Preview),
]
public void LanguageVersion_MapSpecifiedToEffectiveVersion(LanguageVersion expectedMappedVersion, LanguageVersion input)
{
Expand Down Expand Up @@ -7210,7 +7212,6 @@ public void SpecifyProperCodePage()
a.cs(1,7): error CS1001: Identifier expected
a.cs(1,7): error CS1514: { expected
a.cs(1,7): error CS1513: } expected
a.cs(1,7): error CS8652: The feature 'top-level statements' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
a.cs(1,7): error CS8803: Top-level statements must precede namespace and type declarations.
a.cs(1,7): error CS1525: Invalid expression term '??'
a.cs(1,9): error CS1525: Invalid expression term '{'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static void log(
line: 13
";

var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularPreview);
var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.Regular9);
CompileAndVerify(compilation, expectedOutput: expected);
}

Expand Down Expand Up @@ -1133,7 +1133,7 @@ public static void Main()
var compilation = CreateCompilation(
source,
options: TestOptions.ReleaseExe,
parseOptions: TestOptions.RegularPreview);
parseOptions: TestOptions.Regular9);
CompileAndVerify(compilation, expectedOutput: expected);
}

Expand Down Expand Up @@ -1179,7 +1179,7 @@ public static void Main()
var compilation = CreateCompilation(
source,
options: TestOptions.ReleaseExe,
parseOptions: TestOptions.RegularPreview);
parseOptions: TestOptions.Regular9);
CompileAndVerify(compilation, expectedOutput: expected);
}

Expand Down Expand Up @@ -1228,7 +1228,7 @@ public static void Main()
var compilation = CreateCompilation(
source,
options: TestOptions.ReleaseExe,
parseOptions: TestOptions.RegularPreview);
parseOptions: TestOptions.Regular9);
CompileAndVerify(compilation, expectedOutput: expected);
}

Expand Down Expand Up @@ -1696,7 +1696,7 @@ static void log([System.Runtime.CompilerServices.CallerFilePathAttribute] string
var compilation = CreateCompilation(
new[]
{
SyntaxFactory.ParseSyntaxTree(source1, options: TestOptions.RegularPreview, path: @"C:\filename", encoding: Encoding.UTF8)
SyntaxFactory.ParseSyntaxTree(source1, options: TestOptions.Regular9, path: @"C:\filename", encoding: Encoding.UTF8)
},
options: TestOptions.ReleaseExe.WithSourceReferenceResolver(SourceFileResolver.Default));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void local3()
}
}
}";
var comp = CreateCompilationWithMscorlib40AndSystemCore(new[] { DeprecatedAttributeSource, ExperimentalAttributeSource, source }, parseOptions: TestOptions.RegularPreview);
var comp = CreateCompilationWithMscorlib40AndSystemCore(new[] { DeprecatedAttributeSource, ExperimentalAttributeSource, source }, parseOptions: TestOptions.Regular9);
comp.VerifyDiagnostics(
// (7,9): warning CS0618: 'local1()' is obsolete: ''
// local1(); // 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ int ByValTStr
}
}
";
CreateCompilation(source, parseOptions: TestOptions.RegularPreview).VerifyDiagnostics(
CreateCompilation(source, parseOptions: TestOptions.Regular9).VerifyDiagnostics(
// (11,24): error CS7055: Unmanaged type 'ByValArray' is only valid for fields.
// [MarshalAs(UnmanagedType.ByValArray)]
Diagnostic(ErrorCode.ERR_MarshalUnmanagedTypeOnlyValidForFields, "UnmanagedType.ByValArray").WithArguments("ByValArray").WithLocation(11, 24),
Expand Down
Loading

0 comments on commit 1ed3ef5

Please sign in to comment.