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
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ internal static ITypeName ScanType(string typename, bool ignoreErrors)
var result = parser.TypeNameRule(allowAssemblyQualifiedNames: true, firstTypeNameToken: out _);

SemanticChecks.CheckArrayTypeNameDepth(result, PositionUtilities.EmptyExtent, parser);
if (!ignoreErrors && parser.ErrorList.Count > 0)
if (!ignoreErrors && result is not null && (parser.ErrorList.Count > 0 || !result.Extent.Text.Equals(typename, StringComparison.OrdinalIgnoreCase)))
{
result = null;
}
Expand Down
8 changes: 8 additions & 0 deletions test/powershell/Language/Parser/Conversions.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ Describe 'conversion syntax' -Tags "CI" {
$result -join ";" | Should -Be ($Elements -join ";")
}
}

It 'Should not convert invalid strings to type name using -as operator' {
'int]whatever' -as [type] | Should -Be $null
Comment thread
andyleejordan marked this conversation as resolved.
}
Comment thread
MartinGC94 marked this conversation as resolved.

It 'Should not convert invalid strings to type name using left-hand side operator' {
{[Type] 'int]whatever'} | Should -Throw
}
}

Describe "Type resolution should prefer assemblies in powershell assembly cache" -Tags "Feature" {
Expand Down