Skip to content

Commit c350cf3

Browse files
authored
Merge pull request #38980 from alrz/fix-38486
Check language version on relaxed ordering of parameter modifiers
2 parents 02f408e + ca67eb9 commit c350cf3

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

docs/compilers/CSharp/Compiler Breaking Changes - post VS2019.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ In Visual Studio version 16.4, the nullable analysis will be more stringent for
8080
class C<T> where T : I<(int a, int b)>, I<(int c, int d)> { } // error
8181
```
8282

83+
12. Previously, the language version was not checked for `this ref` and `this in` orderings of parameter modifiers. In *Visual Studio 2019 version 16.4* these orderings produce an error with langversion below 7.2. See https://github.com/dotnet/roslyn/issues/38486

src/Compilers/CSharp/Portable/Parser/LanguageParser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3670,6 +3670,11 @@ private void ParseParameterModifiers(SyntaxListBuilder modifiers)
36703670
{
36713671
case SyntaxKind.ThisKeyword:
36723672
modifier = CheckFeatureAvailability(modifier, MessageID.IDS_FeatureExtensionMethod);
3673+
if (this.CurrentToken.Kind == SyntaxKind.RefKeyword ||
3674+
this.CurrentToken.Kind == SyntaxKind.InKeyword)
3675+
{
3676+
modifier = CheckFeatureAvailability(modifier, MessageID.IDS_FeatureRefExtensionMethods);
3677+
}
36733678
break;
36743679

36753680
case SyntaxKind.RefKeyword:

src/Compilers/CSharp/Test/Semantic/Semantics/RefExtensionMethodsTests.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,33 +2346,61 @@ public void UsingRefExtensionMethodsBeforeVersion7_2ProducesDiagnostics_InSyntax
23462346
var code = @"
23472347
public static class Ext
23482348
{
2349-
public static void Print(in this int p)
2350-
{
2351-
System.Console.WriteLine(p);
2352-
}
2349+
public static void Print0(in this int p)
2350+
=> System.Console.Write(p);
2351+
public static void Print1(this in int p)
2352+
=> System.Console.Write(p);
2353+
public static void Print2(ref this int p)
2354+
=> System.Console.Write(p);
2355+
public static void Print3(this ref int p)
2356+
=> System.Console.Write(p);
23532357
}
23542358
public static class Program
23552359
{
23562360
public static void Main()
23572361
{
23582362
int p = 5;
2359-
p.Print();
2363+
p.Print0();
2364+
p.Print1();
2365+
p.Print2();
2366+
p.Print3();
23602367
}
23612368
}";
23622369

23632370
CreateCompilationWithMscorlib40AndSystemCore(code, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_1)).VerifyDiagnostics(
2364-
// (4,30): error CS8302: Feature 'readonly references' is not available in C# 7.1. Please use language version 7.2 or greater.
2365-
// public static void Print(in this int p)
2366-
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "in").WithArguments("readonly references", "7.2").WithLocation(4, 30),
2367-
// (4,30): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2368-
// public static void Print(in this int p)
2369-
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "in").WithArguments("ref extension methods", "7.2").WithLocation(4, 30),
2370-
// (14,9): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2371-
// p.Print();
2372-
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "p").WithArguments("ref extension methods", "7.2").WithLocation(14, 9)
2373-
);
2374-
2375-
CompileAndVerify(code, expectedOutput: "5");
2371+
// (4,31): error CS8302: Feature 'readonly references' is not available in C# 7.1. Please use language version 7.2 or greater.
2372+
// public static void Print0(in this int p)
2373+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "in").WithArguments("readonly references", "7.2").WithLocation(4, 31),
2374+
// (4,31): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2375+
// public static void Print0(in this int p)
2376+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "in").WithArguments("ref extension methods", "7.2").WithLocation(4, 31),
2377+
// (6,31): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2378+
// public static void Print1(this in int p)
2379+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "this").WithArguments("ref extension methods", "7.2").WithLocation(6, 31),
2380+
// (6,36): error CS8302: Feature 'readonly references' is not available in C# 7.1. Please use language version 7.2 or greater.
2381+
// public static void Print1(this in int p)
2382+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "in").WithArguments("readonly references", "7.2").WithLocation(6, 36),
2383+
// (8,31): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2384+
// public static void Print2(ref this int p)
2385+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "ref").WithArguments("ref extension methods", "7.2").WithLocation(8, 31),
2386+
// (10,31): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2387+
// public static void Print3(this ref int p)
2388+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "this").WithArguments("ref extension methods", "7.2").WithLocation(10, 31),
2389+
// (18,9): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2390+
// p.Print0();
2391+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "p").WithArguments("ref extension methods", "7.2").WithLocation(18, 9),
2392+
// (19,9): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2393+
// p.Print1();
2394+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "p").WithArguments("ref extension methods", "7.2").WithLocation(19, 9),
2395+
// (20,9): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2396+
// p.Print2();
2397+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "p").WithArguments("ref extension methods", "7.2").WithLocation(20, 9),
2398+
// (21,9): error CS8302: Feature 'ref extension methods' is not available in C# 7.1. Please use language version 7.2 or greater.
2399+
// p.Print3();
2400+
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7_1, "p").WithArguments("ref extension methods", "7.2").WithLocation(21, 9)
2401+
);
2402+
2403+
CompileAndVerify(code, expectedOutput: "5555");
23762404
}
23772405

23782406
[Fact]

0 commit comments

Comments
 (0)