Skip to content

Allow let! and use! binding with type annotation without parentheses. #18508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c4b40cb
new parser production rule.
edgarfgp Apr 25, 2025
0b489a1
Add let! and use! syntax tree tests
edgarfgp Apr 26, 2025
be13fe0
Update existing syntax tree tests
edgarfgp Apr 26, 2025
4e00653
try update mkSynMemberDefnGetSet
edgarfgp Apr 26, 2025
b223cd4
format code
edgarfgp Apr 26, 2025
7c592a8
try different parser rule
edgarfgp Apr 26, 2025
e3f7b8c
Add new test
edgarfgp Apr 26, 2025
43e9ea2
Revert "new parser production rule."
edgarfgp Apr 26, 2025
b26be8b
Revert "try update mkSynMemberDefnGetSet"
edgarfgp Apr 26, 2025
cbe3d48
Revert "Update existing syntax tree tests"
edgarfgp Apr 26, 2025
acb8742
update ce let! and use! test
edgarfgp Apr 26, 2025
00e687c
Merge branch 'main' into fix-10697
edgarfgp Apr 28, 2025
3034ffb
more tests
edgarfgp Apr 28, 2025
99cc8d4
reduce diff
edgarfgp Apr 28, 2025
dd416dc
release notes
edgarfgp Apr 28, 2025
5eb3102
Merge branch 'main' into fix-10697
edgarfgp Apr 30, 2025
e54cb12
more tests
edgarfgp Apr 30, 2025
9e65095
Merge branch 'main' into fix-10697
edgarfgp Apr 30, 2025
f7837a4
Merge branch 'main' into fix-10697
edgarfgp May 1, 2025
e608ce6
try headBindingPattern
edgarfgp May 1, 2025
50114d1
Update tests
edgarfgp May 1, 2025
b16bbb5
more tests
edgarfgp May 1, 2025
84ea9ba
opt_topReturnTypeWithTypeConstraints
edgarfgp May 2, 2025
2b09352
Merge branch 'main' into fix-10697
edgarfgp May 2, 2025
420a92d
Merge branch 'main' into fix-10697
edgarfgp May 5, 2025
afcfc17
Merge branch 'main' into fix-10697
edgarfgp May 6, 2025
4b22d90
Merge branch 'main' into fix-10697
edgarfgp May 7, 2025
a15afa2
Merge branch 'main' into fix-10697
edgarfgp May 12, 2025
2669c9b
Merge branch 'main' into fix-10697
edgarfgp May 13, 2025
71788e5
Merge branch 'main' into fix-10697
edgarfgp May 14, 2025
6b6e86f
Update SyntaxTree tests to include WarnDirectives
edgarfgp May 14, 2025
85a3401
Add LanguageFeature
edgarfgp May 14, 2025
fda8a38
Language release notes
edgarfgp May 14, 2025
e404fa1
Update CheckLanguageFeatureAndRecover in pars.fsy
edgarfgp May 14, 2025
05d11d8
update tests
edgarfgp May 14, 2025
0d83611
Merge branch 'main' into fix-10697
edgarfgp May 15, 2025
03f1ced
update release notes
edgarfgp May 15, 2025
64d72d8
Better naming
edgarfgp May 16, 2025
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
3 changes: 3 additions & 0 deletions docs/release-notes/.FSharp.Compiler.Service/10.0.100.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

* Allow `let!` and `use!` type annotations without requiring parentheses ([PR #18508](https://github.com/dotnet/fsharp/pull/18508))
1 change: 1 addition & 0 deletions docs/release-notes/.Language/preview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Allow `_` in `use!` bindings values (lift FS1228 restriction) ([PR #18487](https://github.com/dotnet/fsharp/pull/18487))
* Warn when `unit` is passed to an `obj`-typed argument ([PR #18330](https://github.com/dotnet/fsharp/pull/18330))
* Scoped Nowarn: added the #warnon compiler directive ([Language suggestion #278](https://github.com/fsharp/fslang-suggestions/issues/278), [RFC FS-1146 PR](https://github.com/fsharp/fslang-design/pull/782), [PR #18049](https://github.com/dotnet/fsharp/pull/18049))
* Allow `let!` and `use!` type annotations without requiring parentheses. ([PR #18508](https://github.com/dotnet/fsharp/pull/18508))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,9 @@ let rec TryTranslateComputationExpression
let supportsUseBangBindingValueDiscard =
ceenv.cenv.g.langVersion.SupportsFeature LanguageFeature.UseBangBindingValueDiscard

let supportsTypedLetOrUseBang =
ceenv.cenv.g.langVersion.SupportsFeature LanguageFeature.AllowTypedLetOrUseBang

// use! x = ...
// use! (x) = ...
// use! (__) = ...
Expand All @@ -1802,6 +1805,7 @@ let rec TryTranslateComputationExpression
match pat with
| SynPat.Named(ident = SynIdent(id, _); isThisVal = false) -> id, pat
| SynPat.LongIdent(longDotId = SynLongIdent(id = [ id ])) -> id, pat
| SynPat.Typed(pat = pat) when supportsTypedLetOrUseBang -> extractIdentifierFromPattern pat
| SynPat.Wild(m) when supportsUseBangBindingValueDiscard ->
// To properly call the Using(disposable) CE member, we need to convert the wildcard to a SynPat.Named
let tmpIdent = mkSynId m "_"
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,7 @@ featureSupportValueOptionsAsOptionalParameters,"Support ValueOption as valid typ
featureSupportWarnWhenUnitPassedToObjArg,"Warn when unit is passed to a member accepting `obj` argument, e.g. `Method(o:obj)` will warn if called via `Method()`."
featureUseBangBindingValueDiscard,"Allows use! _ = ... in computation expressions"
featureScopedNowarn,"Support for scoped enabling / disabling of warnings by #warn and #nowarn directives, also inside modules"
featureAllowLetOrUseBangTypeAnnotationWithoutParens,"Allow let! and use! type annotations without requiring parentheses"
3874,lexWarnDirectiveMustBeFirst,"#nowarn/#warnon directives must appear as the first non-whitespace characters on a line"
3875,lexWarnDirectiveMustHaveArgs,"Warn directives must have warning number(s) as argument(s)"
3876,lexWarnDirectivesMustMatch,"There is another %s for this warning already in line %d."
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Facilities/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type LanguageFeature =
| WarnWhenUnitPassedToObjArg
| UseBangBindingValueDiscard
| ScopedNowarn
| AllowTypedLetOrUseBang

/// LanguageVersion management
type LanguageVersion(versionText) =
Expand Down Expand Up @@ -233,6 +234,7 @@ type LanguageVersion(versionText) =
LanguageFeature.WarnWhenUnitPassedToObjArg, previewVersion
LanguageFeature.UseBangBindingValueDiscard, previewVersion
LanguageFeature.ScopedNowarn, previewVersion
LanguageFeature.AllowTypedLetOrUseBang, previewVersion
]

static let defaultLanguageVersion = LanguageVersion("default")
Expand Down Expand Up @@ -397,6 +399,7 @@ type LanguageVersion(versionText) =
| LanguageFeature.WarnWhenUnitPassedToObjArg -> FSComp.SR.featureSupportWarnWhenUnitPassedToObjArg ()
| LanguageFeature.UseBangBindingValueDiscard -> FSComp.SR.featureUseBangBindingValueDiscard ()
| LanguageFeature.ScopedNowarn -> FSComp.SR.featureScopedNowarn ()
| LanguageFeature.AllowTypedLetOrUseBang -> FSComp.SR.featureAllowLetOrUseBangTypeAnnotationWithoutParens ()

/// Get a version string associated with the given feature.
static member GetFeatureVersionString feature =
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Facilities/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type LanguageFeature =
| WarnWhenUnitPassedToObjArg
| UseBangBindingValueDiscard
| ScopedNowarn
| AllowTypedLetOrUseBang

/// LanguageVersion management
type LanguageVersion =
Expand Down
33 changes: 33 additions & 0 deletions src/Compiler/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -4466,6 +4466,39 @@ declExpr:
let trivia: SynExprLetOrUseBangTrivia = { LetOrUseBangKeyword = rhs parseState 1 ; EqualsRange = Some mEquals }
SynExpr.LetOrUseBang(spBind, ($1 = "use"), true, $2, $4, $7, $8, m, trivia) }

| BINDER headBindingPattern opt_topReturnTypeWithTypeConstraints EQUALS typedSequentialExprBlock IN opt_OBLOCKSEP moreBinders typedSequentialExprBlock %prec expr_let
{ // Handle type annotations on patterns in let!/use! bindings
// Examples: let! x: int = async { return 1 }
// use! _: IDisposable = async { return new MemoryStream() }
let spBind = DebugPointAtBinding.Yes(rhs2 parseState 1 7)
let pat =
match $3 with
| None -> $2
| Some (_, SynReturnInfo((ty, _), _)) ->
SynPat.Typed($2, ty, unionRanges $2.Range ty.Range)
parseState.LexBuffer.CheckLanguageFeatureAndRecover LanguageFeature.AllowTypedLetOrUseBang pat.Range
let mEquals = rhs parseState 4
let m = unionRanges (rhs parseState 1) $9.Range
let trivia: SynExprLetOrUseBangTrivia = { LetOrUseBangKeyword = rhs parseState 1 ; EqualsRange = Some mEquals }
SynExpr.LetOrUseBang(spBind, ($1 = "use"), true, pat, $5, $8, $9, m, trivia) }

| OBINDER headBindingPattern opt_topReturnTypeWithTypeConstraints EQUALS typedSequentialExprBlock hardwhiteDefnBindingsTerminator opt_OBLOCKSEP moreBinders typedSequentialExprBlock %prec expr_let
{ // Handle type annotations on patterns in let!/use! bindings (offside-sensitive version)
// This rule maintains consistent handling of binding constructs across different syntactic contexts
let report, mIn, _ = $6
report (if $1 = "use" then "use!" else "let!") (rhs parseState 1) // report unterminated error
let spBind = DebugPointAtBinding.Yes(unionRanges (rhs parseState 1) $5.Range)
let pat =
match $3 with
| None -> $2
| Some (_, SynReturnInfo((ty, _), _)) ->
SynPat.Typed($2, ty, unionRanges $2.Range ty.Range)
parseState.LexBuffer.CheckLanguageFeatureAndRecover LanguageFeature.AllowTypedLetOrUseBang pat.Range
let mEquals = rhs parseState 4
let m = unionRanges (rhs parseState 1) $9.Range
let trivia: SynExprLetOrUseBangTrivia = { LetOrUseBangKeyword = rhs parseState 1 ; EqualsRange = Some mEquals }
SynExpr.LetOrUseBang(spBind, ($1 = "use"), true, pat, $5, $8, $9, m, trivia) }

| OBINDER headBindingPattern EQUALS typedSequentialExprBlock hardwhiteDefnBindingsTerminator opt_OBLOCKSEP error %prec expr_let
{ // error recovery that allows intellisense when writing incomplete computation expressions
let spBind = DebugPointAtBinding.Yes(unionRanges (rhs parseState 1) $4.Range)
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
open System

open System

type Disposable(id: int) =
static let mutable disposedIds = Set.empty<int>
static let mutable constructedIds = Set.empty<int>

do constructedIds <- constructedIds.Add(id)

member _.Id = id

static member GetDisposed() = disposedIds
static member GetConstructed() = constructedIds
static member Reset() =
disposedIds <- Set.empty
constructedIds <- Set.empty

interface IDisposable with
member this.Dispose() = disposedIds <- disposedIds.Add(this.Id)

type DisposableBuilder() =
member _.Using(resource: #IDisposable, f) =
async {
use res = resource
return! f res
}

member _.Bind(disposable: Disposable, f) = async.Bind(async.Return(disposable), f)
member _.Return(x) = async.Return x
member _.ReturnFrom(x) = x
member _.Bind(task, f) = async.Bind(task, f)

let counterDisposable = DisposableBuilder()

let testBindingPatterns() =
Disposable.Reset()

counterDisposable {
use! res:IDisposable = new Disposable(1)
use! __:IDisposable = new Disposable(2)
use! (res1: IDisposable) = new Disposable(3)
use! _: IDisposable = new Disposable(4)
use! (_: IDisposable) = new Disposable(5)
return ()
} |> Async.RunSynchronously

let constructed = Disposable.GetConstructed()
let disposed = Disposable.GetDisposed()
let undisposed = constructed - disposed

if not undisposed.IsEmpty then
printfn $"Undisposed instances: %A{undisposed}"
failwithf "Not all disposables were properly disposed"
else
printfn $"Success! All %d{constructed.Count} disposables were properly disposed"

testBindingPatterns()
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ module UseBangBindingsVersion9 =
|> withDiagnostics [
(Error 1228, Line 47, Col 14, Line 47, Col 15, "'use!' bindings must be of the form 'use! <var> = <expr>'")
]

[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBang05.fs"|])>]
let ``UseBangBindings - UseBang05_fs - Current LangVersion`` compilation =
compilation
|> asFsx
|> withLangVersion90
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 3350, Line 43, Col 14, Line 43, Col 28, "Feature 'Allow let! and use! type annotations without requiring parentheses' is not available in F# 9.0. Please use language version 'PREVIEW' or greater.")
(Error 3350, Line 41, Col 14, Line 41, Col 28, "Feature 'Allow let! and use! type annotations without requiring parentheses' is not available in F# 9.0. Please use language version 'PREVIEW' or greater.")
(Error 3350, Line 40, Col 14, Line 40, Col 29, "Feature 'Allow let! and use! type annotations without requiring parentheses' is not available in F# 9.0. Please use language version 'PREVIEW' or greater.")
]

module UseBangBindingsPreview =
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBang01.fs"|])>]
Expand Down Expand Up @@ -83,4 +96,13 @@ module UseBangBindingsPreview =
|> withLangVersionPreview
|> compileAndRun
|> shouldSucceed

[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBang05.fs"|])>]
let ``UseBangBindings - UseBang05_fs - Preview LangVersion`` compilation =
compilation
|> asExe
|> withLangVersionPreview
|> compileAndRun
|> shouldSucceed


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// #DeclarationElements #LetBindings
//<Expects status="success"></Expects>
open System

let answer =
use x:IDisposable = new System.IO.MemoryStream()
42
Loading