diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md index 51c14214f3c..84971c0df11 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.100.md @@ -1,5 +1,6 @@ ### Fixed +* Fix FS0421 "The address of the variable cannot be used at this point" incorrectly raised for the discard pattern `let _ = &expr` when `let x = &expr` compiles. ([Issue #18841](https://github.com/dotnet/fsharp/issues/18841), [PR #19811](https://github.com/dotnet/fsharp/pull/19811)) * Honor `--nowarn` and `--warnaserror` for warnings emitted during command-line option parsing ([Issue #19576](https://github.com/dotnet/fsharp/issues/19576), [PR #19776](https://github.com/dotnet/fsharp/pull/19776)) * Fix `[]` prefix attributes being silently dropped on class members, and fix false-positive `AllowMultiple=false` errors when `[]` and `[]` are applied to the same binding. ([Issue #17904](https://github.com/dotnet/fsharp/issues/17904), [Issue #19020](https://github.com/dotnet/fsharp/issues/19020), [PR #19738](https://github.com/dotnet/fsharp/pull/19738)) * Fix attributes on return type of unparenthesized tuple methods being silently dropped from IL. ([Issue #462](https://github.com/dotnet/fsharp/issues/462), [PR #19714](https://github.com/dotnet/fsharp/pull/19714)) diff --git a/src/Compiler/Checking/Expressions/CheckExpressions.fs b/src/Compiler/Checking/Expressions/CheckExpressions.fs index d3b1e26497f..9a14f0980a1 100644 --- a/src/Compiler/Checking/Expressions/CheckExpressions.fs +++ b/src/Compiler/Checking/Expressions/CheckExpressions.fs @@ -11875,9 +11875,14 @@ and TcLetBinding (cenv: cenv) isUse env containerInfo declKind tpenv (synBinds, let rhsExpr = mkTypeLambda m generalizedTypars (rhsExpr, tauTy) match checkedPat with - // Don't introduce temporary or 'let' for 'match against wild' or 'match against unit' - - | TPat_wild _ | TPat_const (Const.Unit, _) when not isUse && not isFixed && isNil generalizedTypars -> + // Don't introduce temporary or 'let' for 'match against wild' or 'match against unit', + // unless the RHS is a byref-like value (e.g. `let _ = &s`). For byref-like RHS we + // must keep a real `Expr.Let` so that PostInferenceChecks treats the binding as + // permitting byref expressions, matching the behaviour of `let v = &s`. + // See issue dotnet/fsharp#18841. + + | TPat_wild _ | TPat_const (Const.Unit, _) + when not isUse && not isFixed && isNil generalizedTypars && not (isByrefLikeTy g m tauTy) -> let mkSequentialBind (tm, tmty) = mkSequential m rhsExpr tm, tmty (buildExpr >> mkSequentialBind, env, tpenv) | _ -> diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/DataExpressions/AddressOf/AddressOf.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/DataExpressions/AddressOf/AddressOf.fs index 4f9ceb2067d..4005d80bf5d 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/DataExpressions/AddressOf/AddressOf.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/DataExpressions/AddressOf/AddressOf.fs @@ -27,3 +27,123 @@ module AddressOf = |> shouldFail |> withErrorCode 431 |> withDiagnosticMessageMatches "byref" + + // Issue #18841: let _ = &expr should compile (parity with let x = &expr) + + [] + let ``Issue 18841 - let discard with address-of struct value compiles`` () = + Fsx """ +module Test + +type S = + struct + val Field: int + end + +let test () = + let s = S() + let _ = &s + let a = &s + ignore a + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - let discard with address-of mutable local compiles`` () = + Fsx """ +module Test +let test () = + let mutable x = 42 + let _ = &x + () + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - let discard with address-of struct field compiles`` () = + Fsx """ +module Test + +[] +type S = { mutable Field: int } + +let test () = + let mutable s = { Field = 0 } + let _ = &s.Field + () + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - let discard with parenthesized address-of compiles`` () = + Fsx """ +module Test + +type S = + struct + val Field: int + end + +let test () = + let s = S() + let _ = &(s) + () + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - let discard with address-of inside function parameter compiles`` () = + Fsx """ +module Test + +type S = + struct + val Field: int + end + +let f (s: S) = + let _ = &s + () + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - named binding let x = &s still compiles (regression guard)`` () = + Fsx """ +module Test + +type S = + struct + val Field: int + end + +let test () = + let s = S() + let a = &s + ignore a + """ + |> typecheck + |> shouldSucceed + + [] + let ``Issue 18841 - plain let discard with no address-of still compiles`` () = + Fsx """ +module Test + +type S = + struct + val Field: int + end + +let test () = + let s = S() + let _ = s + () + """ + |> typecheck + |> shouldSucceed