Skip to content

Allow _ in use! bindings values #18487

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 17 commits into from
Apr 28, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
UnwrapUseBang
  • Loading branch information
edgarfgp committed Apr 21, 2025
commit a3036ba0265735d2de95e131f90f0c8ca20466eb
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,17 @@ let (|OptionalSequential|) e =
| SynExpr.Sequential(debugPoint = _sp; isTrueSeq = true; expr1 = dataComp1; expr2 = dataComp2) -> (dataComp1, Some dataComp2)
| _ -> (e, None)

[<return: Struct>]
let rec (|UnwrapUseBang|_|) supportsUseBangBindingValueDiscard pat =
match pat with
| SynPat.Named(ident = SynIdent(id, _); isThisVal = false) -> ValueSome(id, pat)
| SynPat.LongIdent(longDotId = SynLongIdent(id = [ id ])) -> ValueSome(id, pat)
| SynPat.Wild(m) when supportsUseBangBindingValueDiscard ->
let tmpIdent = mkSynId m "_"
ValueSome(tmpIdent, SynPat.Named(SynIdent(tmpIdent, None), false, None, m))
| SynPat.Paren(pat = UnwrapUseBang supportsUseBangBindingValueDiscard (id, pat)) -> ValueSome(id, pat)
| _ -> ValueNone

[<return: Struct>]
let (|ExprAsUseBang|_|) expr =
match expr with
Expand Down Expand Up @@ -1823,14 +1834,12 @@ let rec TryTranslateComputationExpression
requireBuilderMethod "Using" mBind cenv ceenv.env ceenv.ad ceenv.builderTy mBind
requireBuilderMethod "Bind" mBind cenv ceenv.env ceenv.ad ceenv.builderTy mBind

let supportsUseBangBindingValueDiscard =
ceenv.cenv.g.langVersion.SupportsFeature LanguageFeature.UseBangBindingValueDiscard

let ident, pat =
match pat with
| SynPat.Named(ident = SynIdent(id, _); isThisVal = false) -> id, pat
| SynPat.LongIdent(longDotId = SynLongIdent(id = [ id ])) -> id, pat
| SynPat.Wild(m) when ceenv.cenv.g.langVersion.SupportsFeature LanguageFeature.UseBangBindingValueDiscard ->
// Special handling for wildcard (_) patterns
let tmpIdent = mkSynId m "_"
tmpIdent, SynPat.Named(SynIdent(tmpIdent, None), false, None, m)
| UnwrapUseBang supportsUseBangBindingValueDiscard (ident, pat) -> ident, pat
| _ -> error (Error(FSComp.SR.tcInvalidUseBangBinding (), pat.Range))

let bindExpr =
Expand Down