Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting a (single) number to System.Collections.IEnumerable inappropriately converts to a (single) string #18987

Closed
5 tasks done
mklement0 opened this issue Jan 19, 2023 · 6 comments
Assignees
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors

Comments

@mklement0
Copy link
Contributor

mklement0 commented Jan 19, 2023

Prerequisites

Steps to reproduce

Note:

  • Casting to an interface should never change the operand data type, yet that is what happens here with numeric input types.
  • A cast from a type that does not implement IEnumerable should fail, consistently.
  • (As an aside, because it may be surprising: casting to [IEnumerable] (or [IEnumerable[char]]) from a string works correctly, given that [string] implements those interfaces, for enumeration of a string's characters)
using namespace System.Collections
using namespace System.Collections.Generic

# With [int] input, for instance.
# !! -> 'System.String'
# Other (non-enumerable) types, e.g. [datetime], cause an error, as expected, and so does use of the
# the generic interface; e.g. 
#    [IEnumerable] (Get-Date)  
#    [IEnumerable[int]] 42
([IEnumerable] 42).GetType().FullName

Expected behavior

The cast should fail, given that [int] doesn't implement IEnumerable.

Actual behavior

'System.String' is output, implying that the cast converted the [int] operand to [string]

Error details

No response

Environment data

PowerShell Core 7.3.1
PowerShell Core 7.4.0-preview.1
(Also WinPS)

Visuals

No response

@mklement0 mklement0 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Jan 19, 2023
@237dmitry
Copy link

In 7.3.1 (Linux) it returned error

$  [System.Collections.Generic.IEnumerable] 42                             
InvalidOperation: Unable to find type [System.Collections.Generic.IEnumerable].

$  [System.Collections.Generic.IEnumerable[int]] [int[]] 42
42

@mklement0
Copy link
Contributor Author

You have a typo: [System.Collections.Generic.IEnumerable] 42 -> [System.Collections.IEnumerable] 42; the behavior isn't platform-specific.

@daxian-dbw daxian-dbw added the WG-Engine core PowerShell engine, interpreter, and runtime label Jan 27, 2023
@rkeithhill rkeithhill added Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors and removed WG-Engine core PowerShell engine, interpreter, and runtime labels Jul 24, 2023
@rkeithhill rkeithhill self-assigned this Jul 24, 2023
@rkeithhill rkeithhill added Issue-Bug Issue has been identified as a bug in the product and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Jul 24, 2023
@rkeithhill
Copy link
Collaborator

rkeithhill commented Jul 24, 2023

The Engine WG reviewed this issue today and agree that it is a bug that needs to be investigated for a potential fix.

@ninmonkey
Copy link

Here is a similar error where @( String ) fails to coerce to [string[]] when using a List[String]

# works
[Collections.Generic.List[Object]]$names = 'bob', 'jen'
$names.AddRange(@( 'sue', 'bill'))

# errors
[Collections.Generic.List[String]]$names2 = 'bob', 'jen'
$names2.AddRange(@( 'sue', 'bill'))
Cannot convert argument "collection" with 
     value: "Object[]", 
     for "AddRange" 
     to type "IEnumerable`1[String]": 

"Cannot convert the "Object[]" value 
    of type "Object[]" 
    to type "IEnumerable`1[String]"."

@scriptingstudio
Copy link

This works $names2.AddRange([string[]]( 'sue', 'bill'))
You have to use explicit type because you have a typed list

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Mar 16, 2024
Copy link
Contributor

This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors
Projects
None yet
Development

No branches or pull requests

6 participants