Skip to content

Commit 79b2e66

Browse files
committed
yield! 1..10
1 parent b3e463e commit 79b2e66

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

src/Compiler/Checking/Expressions/CheckArrayOrListComputedExpressions.fs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,22 @@ let TcArrayOrListComputedExpression (cenv: TcFileState) env (overallTy: OverallT
173173

174174
TcExprUndelayed cenv overallTy env tpenv replacementExpr
175175
| _ ->
176-
let containsExplicitYields =
177-
let rec hasYieldBang expr cont =
176+
let containsRangeMixedWithYields =
177+
let rec hasRangeAndYield expr hasRange hasYield cont =
178178
match expr with
179-
| SynExpr.YieldOrReturnFrom _ -> cont true
179+
| SynExpr.IndexRange _ -> cont true hasYield
180+
| SynExpr.YieldOrReturnFrom _ -> cont hasRange true
180181
| SynExpr.Sequential(_, _, e1, e2, _, _) ->
181-
hasYieldBang e1 (fun e1Has -> if e1Has then cont true else hasYieldBang e2 cont)
182-
| _ -> cont false
183-
184-
hasYieldBang comp id
185-
182+
hasRangeAndYield e1 hasRange hasYield (fun r1 y1 ->
183+
hasRangeAndYield e2 r1 y1 cont)
184+
| _ -> cont hasRange hasYield
185+
186+
hasRangeAndYield comp false false (fun r y -> r && y)
187+
186188
// Transform mixed expressions with explicit yields to ensure all elements are properly yielded
187189
let comp =
188190
if
189-
containsExplicitYields
191+
containsRangeMixedWithYields
190192
&& g.langVersion.SupportsFeature LanguageFeature.AllowMixedRangesAndValuesInSeqExpressions
191193
then
192194
match comp with
@@ -200,18 +202,8 @@ let TcArrayOrListComputedExpression (cenv: TcFileState) env (overallTy: OverallT
200202
let elems = getElems comp []
201203
transformMixedListWithExplicitYields elems m
202204
| _ -> comp
203-
else if containsExplicitYields then
204-
// Check if any element is a range expression (tail recursive)
205-
let rec hasRangeInMixedYield expr cont =
206-
match expr with
207-
| SynExpr.IndexRange _ -> cont true
208-
| SynExpr.Sequential(_, _, e1, e2, _, _) ->
209-
hasRangeInMixedYield e1 (fun e1Has -> if e1Has then cont true else hasRangeInMixedYield e2 cont)
210-
| _ -> cont false
211-
212-
if hasRangeInMixedYield comp id then
213-
checkLanguageFeatureAndRecover g.langVersion LanguageFeature.AllowMixedRangesAndValuesInSeqExpressions m
214-
205+
else if containsRangeMixedWithYields then
206+
checkLanguageFeatureAndRecover g.langVersion LanguageFeature.AllowMixedRangesAndValuesInSeqExpressions m
215207
comp
216208
else
217209
comp

src/Compiler/Checking/Expressions/CheckComputationExpressions.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,9 +2284,12 @@ let rec TryTranslateComputationExpression
22842284

22852285
// Rewrite range expressions in yield! to their sequence form
22862286
let synYieldExpr =
2287-
match RewriteRangeExpr synYieldExpr with
2288-
| Some rewrittenExpr -> rewrittenExpr
2289-
| None -> synYieldExpr
2287+
if isRangeExpr then
2288+
match RewriteRangeExpr synYieldExpr with
2289+
| Some rewrittenExpr -> rewrittenExpr
2290+
| None -> synYieldExpr
2291+
else
2292+
synYieldExpr
22902293

22912294
let yieldFromExpr =
22922295
mkSourceExpr synYieldExpr ceenv.sourceMethInfo ceenv.builderValName

src/Compiler/Checking/Expressions/CheckExpressionsOps.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,23 @@ let transformMixedListWithExplicitYields elems m =
452452
| RangeExpr rangeExpr -> cont (``yield!`` rangeExpr expr)
453453
| SynExpr.YieldOrReturnFrom _ -> cont expr
454454
| SynExpr.YieldOrReturn _ -> cont expr
455+
| SynExpr.IfThenElse(ifExpr, thenExpr, elseExprOpt, spIfToThen, isFromErrorRecovery, range, trivia) ->
456+
transformExpr thenExpr (fun transformedThen ->
457+
match elseExprOpt with
458+
| Some elseExpr ->
459+
transformExpr elseExpr (fun transformedElse ->
460+
cont (
461+
SynExpr.IfThenElse(
462+
ifExpr,
463+
transformedThen,
464+
Some transformedElse,
465+
spIfToThen,
466+
isFromErrorRecovery,
467+
range,
468+
trivia
469+
)
470+
))
471+
| None -> cont (SynExpr.IfThenElse(ifExpr, transformedThen, None, spIfToThen, isFromErrorRecovery, range, trivia)))
455472
| _ -> cont (``yield`` expr)
456473

457474
let rec loop elems cont =

src/Compiler/Checking/Expressions/CheckSequenceExpressions.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,12 @@ let TcSequenceExpression (cenv: TcFileState) env tpenv comp (overallTy: OverallT
381381

382382
// Rewrite range expressions in yield! to their sequence form
383383
let synYieldExpr =
384-
match RewriteRangeExpr synYieldExpr with
385-
| Some rewrittenExpr -> rewrittenExpr
386-
| None -> synYieldExpr
384+
if isRangeExpr then
385+
match RewriteRangeExpr synYieldExpr with
386+
| Some rewrittenExpr -> rewrittenExpr
387+
| None -> synYieldExpr
388+
else
389+
synYieldExpr
387390

388391
let resultExpr, genExprTy, tpenv = TcExprOfUnknownType cenv env tpenv synYieldExpr
389392

tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/MixedSequenceExpressionTests.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,22 @@ let f = [|-3; 1..10; 19|]
361361
// SOURCE=SequenceExpressions13.fs # SequenceExpressions13.fs
362362
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"SequenceExpressions13.fs"|])>]
363363
let ``Preview: SequenceExpressions13 fs`` compilation =
364+
compilation
365+
|> withLangVersionPreview
366+
|> verifyCompileAndRun
367+
|> shouldSucceed
368+
369+
// SOURCE=SequenceExpressions14.fs # SequenceExpressions14.fs
370+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"SequenceExpressions14.fs"|])>]
371+
let ``Version 9: SequenceExpressions14 fs`` compilation =
372+
compilation
373+
|> withLangVersion90
374+
|> verifyCompileAndRun
375+
|> shouldSucceed
376+
377+
// SOURCE=SequenceExpressions13.fs # SequenceExpressions13.fs
378+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"SequenceExpressions14.fs"|])>]
379+
let ``Preview: SequenceExpressions14 fs`` compilation =
364380
compilation
365381
|> withLangVersionPreview
366382
|> verifyCompileAndRun
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module BackwardCompatTest
2+
3+
let makeCustomAttributes() =
4+
[|
5+
if true then
6+
yield "attr1"
7+
yield! ["attr2"; "attr3"]
8+
yield! ["attr4"]
9+
|]
10+
11+
let attrs = makeCustomAttributes()
12+
let expected = [|"attr1"; "attr2"; "attr3"; "attr4"|]
13+
14+
if attrs <> expected then failwithf $"attrs failed: got {attrs}"
15+
16+
printfn "Backward compatibility test passed!"

0 commit comments

Comments
 (0)