Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1263,9 +1263,15 @@ private static bool PreferExpandedFormOverNormalForm<TMember>(MemberResolutionRe
expandedResult.Result.ParamsElementTypeOpt.HasType &&
expandedResult.Result.ParamsElementTypeOpt.Type != (object)ErrorTypeSymbol.EmptyParamsCollectionElementTypeSentinel)
{
if (haveBadArgumentForLastParameter(normalResult) && haveBadArgumentForLastParameter(expandedResult))
// Prefer expanded form if the normal form has a bad argument for the last parameter.
// This handles two cases:
// 1. Both normal and expanded forms have bad arguments for the last parameter
// (e.g., both fail but expanded gives better error messages)
// 2. Normal form has a bad argument for the last parameter but expanded form doesn't
// (e.g., M(char c, params object[] args) called with M(string[], string) where
// string -> object[] is bad in normal form but string -> object is good in expanded form)
if (haveBadArgumentForLastParameter(normalResult))
{
// Errors are better if we use the expanded form in this case.
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11945,5 +11945,88 @@ readonly struct S
""";
CreateCompilation(source).VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/12933")]
public void ParamsErrorSuppression_StringConvertibleToObject()
{
var source = """
class Program
{
static void M(char c, params object[] args) { }

static void Test(string[] arr)
{
M(arr, "test");
}
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (7,11): error CS1503: Argument 1: cannot convert from 'string[]' to 'char'
// M(arr, "test");
Diagnostic(ErrorCode.ERR_BadArgType, "arr").WithArguments("1", "string[]", "char").WithLocation(7, 11));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/12933")]
public void ParamsErrorSuppression_IntNotConvertibleToString()
{
var source = """
class C
{
static void M(char c, params string[] args) { }

static void Test()
{
M(1, 2);
}
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (7,11): error CS1503: Argument 1: cannot convert from 'int' to 'char'
// M(1, 2);
Diagnostic(ErrorCode.ERR_BadArgType, "1").WithArguments("1", "int", "char").WithLocation(7, 11),
// (7,14): error CS1503: Argument 2: cannot convert from 'int' to 'string'
// M(1, 2);
Diagnostic(ErrorCode.ERR_BadArgType, "2").WithArguments("2", "int", "string").WithLocation(7, 14));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43920")]
public void ParamsErrorSuppression_Issue43920()
{
var source = """
class Program
{
static void M(int x, params object[] args) { }
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot #43920 uses this signature: static void A(string a, params string[] b) { } add a test case for this as well. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test case ParamsErrorSuppression_Issue43920_StringParams with the specific signature A(string a, params string[] b) from issue #43920. The test verifies that calling A(1, "test") only reports the first argument error (int -> string) and not a misleading params error. Commit eeb9e7b.


static void Test()
{
M("wrong", "test");
}
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (7,11): error CS1503: Argument 1: cannot convert from 'string' to 'int'
// M("wrong", "test");
Diagnostic(ErrorCode.ERR_BadArgType, @"""wrong""").WithArguments("1", "string", "int").WithLocation(7, 11));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43920")]
public void ParamsErrorSuppression_Issue43920_StringParams()
{
var source = """
class Program
{
static void A(string a, params string[] b) { }

static void Test()
{
A(1, "test");
}
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (7,11): error CS1503: Argument 1: cannot convert from 'int' to 'string'
// A(1, "test");
Diagnostic(ErrorCode.ERR_BadArgType, "1").WithArguments("1", "int", "string").WithLocation(7, 11));
}
}
}