Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.100.md
Original file line number Diff line number Diff line change
@@ -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 `[<return: X>]` prefix attributes being silently dropped on class members, and fix false-positive `AllowMultiple=false` errors when `[<X>]` and `[<return: X>]` 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))
Expand Down
11 changes: 8 additions & 3 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11863,9 +11863,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)
| _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,123 @@ module AddressOf =
|> shouldFail
|> withErrorCode 431
|> withDiagnosticMessageMatches "byref"

// Issue #18841: let _ = &expr should compile (parity with let x = &expr)

[<Fact>]
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

[<Fact>]
let ``Issue 18841 - let discard with address-of mutable local compiles`` () =
Fsx """
module Test
let test () =
let mutable x = 42
let _ = &x
()
"""
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Issue 18841 - let discard with address-of struct field compiles`` () =
Fsx """
module Test

[<Struct>]
type S = { mutable Field: int }

let test () =
let mutable s = { Field = 0 }
let _ = &s.Field
()
"""
|> typecheck
|> shouldSucceed

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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

[<Fact>]
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
Loading