Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit57dbde6

Browse files
authored
Update Named and Statement block type inference to not consider AssignmentStatements and Increment/decrement operators as part of their output (#21137)
1 parent10d1785 commit57dbde6

File tree

2 files changed

+91
-20
lines changed

2 files changed

+91
-20
lines changed

‎src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,20 @@ object ICustomAstVisitor.VisitParamBlock(ParamBlockAst paramBlockAst)
850850
objectICustomAstVisitor.VisitNamedBlock(NamedBlockAstnamedBlockAst)
851851
{
852852
varinferredTypes=newList<PSTypeName>();
853-
for(varindex=0;index<namedBlockAst.Statements.Count;index++)
853+
for(intindex=0;index<namedBlockAst.Statements.Count;index++)
854854
{
855-
varast=namedBlockAst.Statements[index];
855+
StatementAstast=namedBlockAst.Statements[index];
856+
if(astisAssignmentStatementAst
857+
||(astisPipelineAstpipe&&pipe.PipelineElements.Count==1&&pipe.PipelineElements[0]isCommandExpressionAstcmd
858+
&&cmd.Redirections.Count==0&&cmd.ExpressionisUnaryExpressionAstunary
859+
&&unary.TokenKindisTokenKind.PostfixPlusPlus orTokenKind.PlusPlus orTokenKind.PostfixMinusMinus orTokenKind.MinusMinus))
860+
{
861+
// Assignments don't output anything to the named block unless they are wrapped in parentheses.
862+
// When they are wrapped in parentheses, they are seen as PipelineAst.
863+
// Increment/decrement operators like $i++ also don't output anything unless there's a redirection, or they are wrapped in parentheses.
864+
continue;
865+
}
866+
856867
inferredTypes.AddRange(InferTypes(ast));
857868
}
858869

@@ -921,8 +932,19 @@ object ICustomAstVisitor.VisitFunctionDefinition(FunctionDefinitionAst functionD
921932
objectICustomAstVisitor.VisitStatementBlock(StatementBlockAststatementBlockAst)
922933
{
923934
varinferredTypes=newList<PSTypeName>();
924-
foreach(varastinstatementBlockAst.Statements)
935+
foreach(StatementAstastinstatementBlockAst.Statements)
925936
{
937+
if(astisAssignmentStatementAst
938+
||(astisPipelineAstpipe&&pipe.PipelineElements.Count==1&&pipe.PipelineElements[0]isCommandExpressionAstcmd
939+
&&cmd.Redirections.Count==0&&cmd.ExpressionisUnaryExpressionAstunary
940+
&&unary.TokenKindisTokenKind.PostfixPlusPlus orTokenKind.PlusPlus orTokenKind.PostfixMinusMinus orTokenKind.MinusMinus))
941+
{
942+
// Assignments don't output anything to the statement block unless they are wrapped in parentheses.
943+
// When they are wrapped in parentheses, they are seen as PipelineAst.
944+
// Increment operators like $i++ also don't output anything unless there's a redirection, or they are wrapped in parentheses.
945+
continue;
946+
}
947+
926948
inferredTypes.AddRange(InferTypes(ast));
927949
}
928950

‎test/powershell/engine/Api/TypeInference.Tests.ps1

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,8 @@ Describe "Type inference Tests" -tags "CI" {
361361

362362
It"Infers type from foreach-object of integer" {
363363
$res= [AstTypeInference]::InferTypeOf( { [int[]]$i=1..20;$i|ForEach-Object {$_*10} }.Ast)
364-
$res.Count| Should-Be2
365-
foreach ($rin$res) {
366-
$r.Name-In'System.Int32','System.Int32[]'| Should-BeTrue
367-
}
364+
$res.Count| Should-Be1
365+
$res.Name| Should-Be'System.Int32'
368366
}
369367

370368
It"Infers type from generic new" {
@@ -386,9 +384,9 @@ Describe "Type inference Tests" -tags "CI" {
386384

387385
It"Infers type from foreach-object with begin/end" {
388386
$res= [AstTypeInference]::InferTypeOf( { [int[]]$i=1..20;$i|ForEach-Object-Begin {"Hi"} {$_*10}-End {[int]} }.Ast)
389-
$res.Count| Should-Be4
387+
$res.Count| Should-Be3
390388
foreach ($rin$res) {
391-
$r.Name-In'System.Int32','System.Int32[]','System.String','System.Type'| Should-BeTrue
389+
$r.Name-In'System.Int32','System.String','System.Type'| Should-BeTrue
392390
}
393391
}
394392

@@ -618,16 +616,6 @@ Describe "Type inference Tests" -tags "CI" {
618616
$res.Name| Should-Be'System.Int32'
619617
}
620618

621-
It'Infers type from attributed expession' {
622-
$res= [AstTypeInference]::InferTypeOf( {
623-
[ValidateRange(1,2)]
624-
[int]$i=1
625-
}.Ast)
626-
627-
$res.Count| Should-Be1
628-
$res.Name| Should-Be System.Int32
629-
}
630-
631619
It'Infers type from if statement' {
632620
$res= [AstTypeInference]::InferTypeOf( {
633621
if ($true) {return1}
@@ -1399,7 +1387,8 @@ Describe "Type inference Tests" -tags "CI" {
13991387

14001388
It'Infers closest variable type' {
14011389
$res= [AstTypeInference]::InferTypeOf( { [string]$TestVar="";[hashtable]$TestVar=@{};$TestVar }.Ast)
1402-
$res.Name|Select-Object-Last1| Should-Be"System.Collections.Hashtable"
1390+
$res.Count| Should-Be1
1391+
$res.Name| Should-Be"System.Collections.Hashtable"
14031392
}
14041393

14051394
It'Infers closest variable type and ignores unrelated param blocks' {
@@ -1444,6 +1433,66 @@ Describe "Type inference Tests" -tags "CI" {
14441433
)
14451434
$null= [AstTypeInference]::InferTypeOf($FoundAst)
14461435
}
1436+
1437+
It'Should only consider assignments wrapped in parentheses to be a part of the output in a Named block' {
1438+
$res= [AstTypeInference]::InferTypeOf( { [string]$Assignment1="Hello"; ([int]$Assignment2=42) }.Ast)
1439+
$res.Count| Should-Be1
1440+
$res.Name| Should-Be'System.Int32'
1441+
}
1442+
1443+
It'Should only consider assignments wrapped in parentheses to be a part of the output in a Statement block' {
1444+
$res= [AstTypeInference]::InferTypeOf( {if ($true){ [string]$Assignment1="Hello"; ([int]$Assignment2=42) }}.Ast)
1445+
$res.Count| Should-Be1
1446+
$res.Name| Should-Be'System.Int32'
1447+
}
1448+
1449+
It'Should only consider increments/decrements wrapped in parentheses to be a part of the output in a Named block' {
1450+
$res= [AstTypeInference]::InferTypeOf( {
1451+
[Int16]$Int16=1; [Int32]$Int32=1; [Int64]$Int64=1; [System.Int128]$Int128=1;
1452+
1453+
$Int16++;$Int32--;++$Int64;--$Int128}.Ast)
1454+
$res.Count| Should-Be0
1455+
1456+
$res= [AstTypeInference]::InferTypeOf( {
1457+
[UInt16]$Uint16=1; [UInt32]$Uint32=1; [UInt64]$Uint64=1; [System.UInt128]$Uint128=1
1458+
1459+
($Uint16++); ($Uint32--); (++$Uint64); (--$Uint128) }.Ast)
1460+
$res.Count| Should-Be4
1461+
$res.Name-join','| Should-Be ('System.UInt16','System.UInt32','System.UInt64','System.UInt128'-join',')
1462+
}
1463+
1464+
It'Should only consider increments/decrements wrapped in parentheses to be a part of the output in a Statement block' {
1465+
$res= [AstTypeInference]::InferTypeOf( {if ($true){
1466+
[Int16]$Int16=1; [Int32]$Int32=1; [Int64]$Int64=1; [System.Int128]$Int128=1;
1467+
1468+
$Int16++;$Int32--;++$Int64;--$Int128}}.Ast)
1469+
$res.Count| Should-Be0
1470+
1471+
$res= [AstTypeInference]::InferTypeOf( {if ($true){
1472+
[UInt16]$Uint16=1; [UInt32]$Uint32=1; [UInt64]$Uint64=1; [System.UInt128]$Uint128=1
1473+
1474+
($Uint16++); ($Uint32--); (++$Uint64); (--$Uint128) }}.Ast)
1475+
$res.Count| Should-Be4
1476+
$res.Name-join','| Should-Be ('System.UInt16','System.UInt32','System.UInt64','System.UInt128'-join',')
1477+
}
1478+
1479+
It'Redirected increments/decrements should be considered part of the output in a Named block' {
1480+
$res= [AstTypeInference]::InferTypeOf( {
1481+
[Int16]$Int16=1; [Int32]$Int32=1; [Int64]$Int64=1; [System.Int128]$Int128=1;
1482+
1483+
$Int16++*>&1;$Int32--*>&1;++$Int64*>&1;--$Int128*>&1}.Ast)
1484+
$res.Count| Should-Be4
1485+
$res.Name-join','| Should-Be ('System.Int16','System.Int32','System.Int64','System.Int128'-join',')
1486+
}
1487+
1488+
It'Redirected increments/decrements should be considered part of the output in a Statement block' {
1489+
$res= [AstTypeInference]::InferTypeOf( {if ($true){
1490+
[Int16]$Int16=1; [Int32]$Int32=1; [Int64]$Int64=1; [System.Int128]$Int128=1;
1491+
1492+
$Int16++*>&1;$Int32--*>&1;++$Int64*>&1;--$Int128*>&1}}.Ast)
1493+
$res.Count| Should-Be4
1494+
$res.Name-join','| Should-Be ('System.Int16','System.Int32','System.Int64','System.Int128'-join',')
1495+
}
14471496
}
14481497

14491498
Describe"AstTypeInference tests"-Tags CI {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp