提交 eb816cc2 编写于 作者: Y Youssef Victor

Enhance error message for multiple constructors

上级 e7bad77b
......@@ -1727,7 +1727,8 @@ private void ReportMethodSignatureCollision(DiagnosticBag diagnostics, SourceMem
// Special case: if there are two destructors, use the destructor syntax instead of "Finalize"
var methodName = (method1.MethodKind == MethodKind.Destructor && method2.MethodKind == MethodKind.Destructor) ?
"~" + this.Name :
method1.Name;
(method1.IsConstructor() ? this.Name : method1.Name);
// Type '{1}' already defines a member called '{0}' with the same parameter types
diagnostics.Add(ErrorCode.ERR_MemberAlreadyExists, method1.Locations[0], methodName, this);
}
......
......@@ -608,9 +608,9 @@ public static void Main()
}";
var comp = CreateCompilation(src);
comp.VerifyDiagnostics(
// (5,12): error CS0111: Type 'C' already defines a member called '.ctor' with the same parameter types
// (5,12): error CS0111: Type 'C' already defines a member called 'C' with the same parameter types
// public C(int a, int b)
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments(".ctor", "C").WithLocation(5, 12),
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments("C", "C").WithLocation(5, 12),
// (5,12): error CS8862: A constructor declared in a record with parameters must have 'this' constructor initializer.
// public C(int a, int b)
Diagnostic(ErrorCode.ERR_UnexpectedOrMissingConstructorInitializerInRecord, "C").WithLocation(5, 12),
......
......@@ -5822,12 +5822,12 @@ partial class C
model1.GetDeclarationDiagnostics().Verify();
model2.GetDeclarationDiagnostics().Verify(
// (4,5): error CS0111: Type 'C' already defines a member called '.ctor' with the same parameter types
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments(".ctor", "C").WithLocation(4, 5));
// (4,5): error CS0111: Type 'C' already defines a member called 'C' with the same parameter types
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments("C", "C").WithLocation(4, 5));
model3.GetDeclarationDiagnostics().Verify(
// (4,5): error CS0111: Type 'C' already defines a member called '.ctor' with the same parameter types
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments(".ctor", "C").WithLocation(4, 5));
// (4,5): error CS0111: Type 'C' already defines a member called 'C' with the same parameter types
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments("C", "C").WithLocation(4, 5));
Assert.Equal(3, comp.GlobalNamespace.GetMember<NamedTypeSymbol>("C").InstanceConstructors.Length);
}
......
......@@ -74,9 +74,9 @@ public C(int a, string b)
}
}");
comp.VerifyDiagnostics(
// (4,12): error CS0111: Type 'C' already defines a member called '.ctor' with the same parameter types
// (4,12): error CS0111: Type 'C' already defines a member called 'C' with the same parameter types
// public C(int a, string b)
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments(".ctor", "C").WithLocation(4, 12),
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "C").WithArguments("C", "C").WithLocation(4, 12),
// (4,12): error CS8862: A constructor declared in a record with parameters must have 'this' constructor initializer.
// public C(int a, string b)
Diagnostic(ErrorCode.ERR_UnexpectedOrMissingConstructorInitializerInRecord, "C").WithLocation(4, 12)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册