dotnet / dotnet/roslyn

Error message for non-collection typed `params` is unclear

Open
#75,623 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Compilers Concept-Diagnostic Clarity Feature - ParamsCollections
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

Version Used: 4.13.0-1.24477.2 (3da6b6d8)

In certain situations, the error message when using the wrong type for params parameters is unclear.

For example, trying use params string args will complain string's constructor not taking 0 arguments. On the surface, this doesn't seem to have anything to do w/ params, and there's no obvious parameter-less ctor call here, so the user might be confused as to what the actual cause is, let alone the fix.

However, params object args surfaces the "right" error (CS0225), about object not being a collection
type.

Steps to Reproduce:

class C {
    void M(params string args) {} // CS1729: 'string' does not contain a constructor that takes 0 arguments
}

Sharplab link

Diagnostic Id: CS1729

Expected Behavior:

Line 2 produces error CS0225, or a similar one explaining that string cannot be used for params.

When briefly mentioned in the Discord, some pointed out that CS0225's current message (The params parameter must have a valid collection type) could be countered with the fact that string is a "collection type," in the most common meaning, just not one that can be collection intialized, as seems to be the requirement for params type. This could be a little confusing to some users, so maybe the error message needs to be reworded, or a separate error needs to be created, linking the missing ctor with the params feature.

Actual Behavior:

Line 2 produces CS1729 'string' does not contain a constructor that takes 0 arguments, which doesn't really indicate to the user that the problem lies with the lack with the params part of the declaration, and it doesn't guide towards any fix for that problem

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer void M(params string args) {} and compare its CS1729 diagnostic with the CS0225 behavior from params object args. Trace how the compiler validates the params parameter type and make the result explain that string is invalid for params, then verify that the reported diagnostic and wording match the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.