提交 2c899e44 编写于 作者: S shyamn

Fixes extension method error reporting for the following issue.

interface I
{
static void M(this object o);
}

Result:
(3,17): error CS0106: The modifier 'static' is not valid for this item
(3,17): error CS1105: Extension methods must be static

Expected:
(3,17): error CS0106: The modifier 'static' is not valid for this item
(3,17): error CS1106: Extension method must be defined in a non-generic static class

The fix was simple - I simply switched the order in which the above extension method errors are reported. However, this has a small side-effect in that it changes the error reporting for the following case. The change seems acceptable (i.e. user would anyways have hit the new error that we report now as soon as they fixed the error that the old compiler was reporting in this case). I've fixed up the couple of tests that were validating the old error for this case.

class C
{
void M(this object o) { }
}

Old (Native as well as Roslyn):
Test.cs(3,10): error CS1105: Extension method must be static

New:
Test.cs(1,7): error CS1106: Extension method must be defined in a non-generic static class (changeset 1250408)
上级 9af62a06
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Diagnostics;
......@@ -230,10 +230,6 @@ private void MethodChecks(MethodDeclarationSyntax syntax, Binder withTypeParamsB
{
diagnostics.Add(ErrorCode.ERR_ExtensionMethodsDecl, location, ContainingType.Name);
}
else if (!IsStatic)
{
diagnostics.Add(ErrorCode.ERR_BadExtensionMeth, location);
}
else if (!ContainingType.IsStatic || ContainingType.Arity != 0)
{
// Duplicate Dev10 behavior by selecting the containing type identifier. However if there
......@@ -243,6 +239,10 @@ private void MethodChecks(MethodDeclarationSyntax syntax, Binder withTypeParamsB
var loc = identifier.GetLocation();
diagnostics.Add(ErrorCode.ERR_BadExtensionAgg, loc);
}
else if (!IsStatic)
{
diagnostics.Add(ErrorCode.ERR_BadExtensionMeth, location);
}
else
{
// Verify ExtensionAttribute is available.
......
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Symbols;
......@@ -1080,12 +1080,12 @@ class C : I
internal static virtual void M4<T>() { }
}";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (5,7): error CS1106: Extension method must be defined in a non-generic static class
// class C : I
Diagnostic(ErrorCode.ERR_BadExtensionAgg, "C").WithLocation(5, 7),
// (7,17): error CS0425: The constraints for type parameter 'T' of method 'C.M1<T>()' must match the constraints for type parameter 'T' of interface method 'I.M1<T>()'. Consider using an explicit interface implementation instead.
// public void M1<T>() { }
Diagnostic(ErrorCode.ERR_ImplBadConstraints, "M1").WithArguments("T", "C.M1<T>()", "T", "I.M1<T>()").WithLocation(7, 17),
// (8,10): error CS1105: Extension method must be static
// void M2<T>(this object o) { }
Diagnostic(ErrorCode.ERR_BadExtensionMeth, "M2").WithLocation(8, 10),
// (9,17): error CS0238: 'C.M3<T>()' cannot be sealed because it is not an override
// sealed void M3<T>() { }
Diagnostic(ErrorCode.ERR_SealedNonOverride, "M3").WithArguments("C.M3<T>()").WithLocation(9, 17),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册