dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.84k stars 777 forks source link

`use!` doesn't make value considered used #7775

Closed auduchinok closed 2 years ago

auduchinok commented 4 years ago
open System



do
    let d = { new IDisposable with member x.Dispose() = () }



    let x1 = d
    use x2 = d

    async {

        use! x3 = async { return d }
        return ()

    } |> ignore
Screenshot 2019-10-28 at 12 53 04
cartermp commented 4 years ago

Interestingly, in FSAC it's reported as unused:

image

But the compiler definitely ignores x2:

/Users/phillip/scratch/console/Program.fs(6,9): warning FS1182: The value 'x1' is unused [/Users/phillip/scratch/console/console.fsproj]
/Users/phillip/scratch/console/Program.fs(10,14): warning FS1182: The value 'x3' is unused [/Users/phillip/scratch/console/console.fsproj]
/Users/phillip/scratch/console/Program.fs(15,10): warning FS1182: The value 'argv' is unused [/Users/phillip/scratch/console/console.fsproj]
/Users/phillip/scratch/console/Program.fs(16,9): warning FS1182: The value 'x' is unused [/Users/phillip/scratch/console/console.fsproj]
    4 Warning(s)

@Krzysztof-Cieslak any ideas why this would report correctly in Ionide?

auduchinok commented 4 years ago

@Krzysztof-Cieslak any ideas why this would report correctly in Ionide?

@cartermp Please note it's about binding in a CE, i.e. when use! is used. The warning about x2 looks like a bug in Ionide/FSAC.

cartermp commented 4 years ago

I see, sorry. Looks I misread this as use and was confused about why use! was emphasized in your screenshot.

TysonMN commented 3 years ago

Worse yet, the use! binding cannot be the wildcard pattern _.

With identifier d

2020-11-29_20-33-16_399

use! d = async { return { new IDisposable with member x.Dispose() = () } }

FS1182: The value is unused

With identifier _

2020-11-29_20-33-39_400

use! _ = async { return { new IDisposable with member x.Dispose() = () } }

FS1228: use! bindings must be of the form use! <var> = <expr>

cmeeren commented 3 years ago

Worse yet, the use! binding cannot be the wildcard pattern _.

I always use __ (double underscores). That works fine and communicates the same intent. Until recently the same thing was required for this in instance members, e.g. member __.Foo = ... (now you can write member _.Foo).

TysonMN commented 3 years ago

Nice comparison.

I just wanted to add that information to this issue. I would not like if this issue were fixed in such a way that the identifier caused FS1182 (the unused value warning) and it was still not possible to use the discard pattern.

dsyme commented 2 years ago

I checked and this is now consistent in VS2022 - must have been fixed somewhere along the way

image