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
22 changes: 20 additions & 2 deletions src/System.Management.Automation/engine/parser/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3200,7 +3200,7 @@ public object VisitPipelineChain(PipelineChainAst pipelineChainAst)
ExpressionCache.Constant(0)));
tryBodyExprs.Add(Expression.Label(label0));
tryBodyExprs.Add(Expression.Assign(dispatchIndex, ExpressionCache.Constant(1)));
tryBodyExprs.Add(Compile(currentChain.LhsPipelineChain));
tryBodyExprs.Add(CompilePipelineChainElement((PipelineAst)currentChain.LhsPipelineChain));

// Remainder of try statement body
// L1: dispatchIndex = 2; if ($?) pipeline2;
Expand Down Expand Up @@ -3234,7 +3234,7 @@ public object VisitPipelineChain(PipelineChainAst pipelineChainAst)
? s_getDollarQuestion
: s_notDollarQuestion;

tryBodyExprs.Add(Expression.IfThen(dollarQuestionCheck, Compile(currentChain.RhsPipeline)));
tryBodyExprs.Add(Expression.IfThen(dollarQuestionCheck, CompilePipelineChainElement(currentChain.RhsPipeline)));

currentChain = currentChain.Parent as PipelineChainAst;
}
Expand Down Expand Up @@ -3277,6 +3277,24 @@ public object VisitPipelineChain(PipelineChainAst pipelineChainAst)
return fullyExpandedBlock;
}

/// <summary>
/// Compile a pipeline as an element in a pipeline chain.
/// Needed since pure expressions won't set $? after them.
/// <seealso cref="CompileTrappableExpression"/> which does something similar.
/// </summary>
/// <param name="pipelineAst">The pipeline in the pipeline chain to compile to an expression.</param>
/// <returns>The compiled expression to execute the pipeline.</returns>
private Expression CompilePipelineChainElement(PipelineAst pipelineAst)
{
if (pipelineAst.PipelineElements.Count == 1 && pipelineAst.PipelineElements[0] is CommandExpressionAst)
{
// A single expression - must set $? after the expression.
return Expression.Block(Compile(pipelineAst), s_setDollarQuestionToTrue);
}

return Compile(pipelineAst);
}

public object VisitPipeline(PipelineAst pipelineAst)
{
var temps = new List<ParameterExpression>();
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/engine/parser/token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ public static class TokenTraits

#region Flags for operators

/* AndAnd */ TokenFlags.BinaryOperator | TokenFlags.ParseModeInvariant,
/* OrOr */ TokenFlags.BinaryOperator | TokenFlags.ParseModeInvariant,
/* AndAnd */ TokenFlags.ParseModeInvariant,
/* OrOr */ TokenFlags.ParseModeInvariant,
/* Ampersand */ TokenFlags.SpecialOperator | TokenFlags.ParseModeInvariant,
/* Pipe */ TokenFlags.SpecialOperator | TokenFlags.ParseModeInvariant,
/* Comma */ TokenFlags.UnaryOperator | TokenFlags.ParseModeInvariant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Describe "Experimental Feature: && and || operators - Feature-Enabled" -Tag CI {
@{ Statement = '"hi" && testexe -returncode 1'; Output = @('hi', '1') }
@{ Statement = 'testexe -returncode 1 && "Hi"'; Output = @('1') }
@{ Statement = 'testexe -returncode 1 || "hi"'; Output = @('1', 'hi') }
@{ Statement = '"hello" && get-item macarena_doesnotexist || "there"; "hello" || "there"'; Output = @('hello', 'there', 'hello') }
@{ Statement = '1 + 1 && "Hi"'; Output = @(2, 'Hi') }

# Pipeline and native command
@{ Statement = '1,2,3 | % { $_ + 1 } && testexe -returncode 0'; Output = @('2','3','4','0') }
Expand Down