提交 c123c6a9 编写于 作者: O Omar Tawfik 提交者: GitHub

Merge pull request #17787 from OmarTawfik/error-on-extension-methods-in-legacy-code

Produce error on extension methods in language versions earlier than C# 3
......@@ -4025,7 +4025,14 @@ private void ParseParameterModifiers(SyntaxListBuilder modifiers)
{
while (IsParameterModifier(this.CurrentToken.Kind))
{
modifiers.Add(this.EatToken());
var modifier = this.EatToken();
if (modifier.Kind == SyntaxKind.ThisKeyword)
{
modifier = CheckFeatureAvailability(modifier, MessageID.IDS_FeatureExtensionMethod);
}
modifiers.Add(modifier);
}
}
......
......@@ -4736,6 +4736,23 @@ void Foo()
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "=>").WithArguments("lambda expression", "3"));
}
[Fact]
public void ExtensionMethodsAreNotAvailableInEarlierCSharpVersions()
{
var code = @"
public static class Test
{
public static void DoSomething(this int x) { }
}";
ParseAndValidate(code, new CSharpParseOptions(LanguageVersion.CSharp2),
// (4,37): error CS8023: Feature 'extension method' is not available in C# 2. Please use language version 3 or greater.
// public static void DoSomething(this int x) { }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion2, "this").WithArguments("extension method", "3").WithLocation(4, 37));
ParseAndValidate(code, new CSharpParseOptions(LanguageVersion.Latest));
}
[Fact]
public void ExceptionFilterBeforeVersionSix()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册