From 80ce8a3e8bcecb146075a745ed218e86f5aee89a Mon Sep 17 00:00:00 2001 From: Tomas Grosup Date: Wed, 24 Jun 2026 14:50:04 +0200 Subject: [PATCH] Add computation-expression tests for warning 20 range (follow-up to #19896) Answers @auduchinok's open question on PR #19896: in a computation expression the loop body is desugared per-statement, so the trailing expression reaches TcStmt on its own and warning 20 highlights only the offending expression (e.g. 'x'), same as the non-CE case. - async CE: asserts the squiggle lands on 'x' alone (Line 6, Col 13-14). - seq CE: trailing non-unit value is an implicit yield, so no warning 20. The CE test originally requested in review was removed in 0127569b; this reinstates a genuine CE case plus the seq implicit-yield contrast. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ErrorMessages/WarnExpressionTests.fs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs index 59b6877698c..214c961741f 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/WarnExpressionTests.fs @@ -284,6 +284,34 @@ while x < 3 do |> withSingleDiagnostic (Warning 20, Line 6, Col 5, Line 6, Col 6, "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + [] + let ``Warn On Last Expression Inside async Computation Expression - highlights only offending expression``() = + FSharp """ +let _ = + async { + for _ in [1] do + let x = 1 + x + } + """ + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 20, Line 6, Col 13, Line 6, Col 14, + "The result of this expression has type 'int' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.") + + [] + let ``No Warning 20 For Trailing Non-unit Expression In seq Computation Expression (implicit yield)``() = + FSharp """ +let _ = + seq { + for _ in [1] do + let x = 1 + x + } + """ + |> typecheck + |> shouldSucceed + [] let ``Warn If Possible Property Setter``() = FSharp """