Skip to content

Fix generic List conversion from Boolean scalar - Issue #17731#27079

Open
Thatgfsj wants to merge 1 commit intoPowerShell:masterfrom
Thatgfsj:fix/issue-17731-boolean-list-conversion
Open

Fix generic List conversion from Boolean scalar - Issue #17731#27079
Thatgfsj wants to merge 1 commit intoPowerShell:masterfrom
Thatgfsj:fix/issue-17731-boolean-list-conversion

Conversation

@Thatgfsj
Copy link
Copy Markdown

Summary

Fixes Issue #17731 - Cannot directly construct a generic list from a Boolean scalar

Problem

The following statements return an error:
\\powershell
[System.Collections.Generic.List[bool]]$False
[System.Collections.Generic.List[bool]]$True
[System.Collections.Generic.List[psobject]]$True
\\

While these statements work fine:
\\powershell
[System.Collections.Generic.List[int]]3
[System.Collections.Generic.List[string]]'Test'
[Array]$True
\\

Root Cause

The \ConvertViaIEnumerableConstructor.Convert\ method was directly adding the scalar value to the list without converting it to the element type first. This worked for types like \int\ where the value's runtime type matches the expected element type, but failed for \�ool\ because the PowerShell Boolean wrapper type differs from \System.Boolean.

Fix

Use \LanguagePrimitives.ConvertTo\ to convert the scalar value to the element type before adding to the list, similar to how non-scalar values are handled.

Changed Files

  • \src/System.Management.Automation/engine/LanguagePrimitives.cs\
  • \ est/powershell/engine/Api/LanguagePrimitive.Tests.ps1\

Testing

Added tests for:

  • [List[bool]]$true\
  • [List[bool]]$false\
  • [List[bool]]$boolVar\
  • [List[psobject]]$true\

Fixes #17731

Issue: PowerShell#17731 - Cannot directly construct a generic list from a Boolean scalar

The ConvertViaIEnumerableConstructor.Convert method was directly adding the
scalar value to the list without converting it to the element type first.
This worked for types like int where the value's runtime type matches the
expected element type, but failed for bool because the PowerShell Boolean
wrapper type is different from System.Boolean.

Fix: Use LanguagePrimitives.ConvertTo to convert the scalar value to the
element type before adding to the list, similar to how non-scalar values
are handled.

Added tests for:
- [List[bool]]$true
- [List[bool]]$false
- [List[bool]]$boolVar
- [List[psobject]]$true
@Thatgfsj Thatgfsj requested a review from a team as a code owner March 22, 2026 05:03
@Thatgfsj
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Mar 29, 2026
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't directly construct a generic list from a Boolean scalar

1 participant