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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

edgarfgp
Copy link
Contributor

@edgarfgp edgarfgp commented Apr 26, 2025

Description

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

Fixes #10697

BEFORE

let f =
    async {
        let! (a: int) = async { return 1 } // Valid
        let! b: int = async { return 1 } // Error
        use! (a: int) = ...  //Error
        use! b: int = ... //Error
        return a
    }

AFTER

let f =
    async {
        let! (a: int) = ... // Valid
        let! b: int = ... // Valid
        return a
    }

Once #18487 is merged

let f =
    async {
        use! (a: int) = ...  // Valid
        use! b: int = ... // Valid
        return a
    }

Checklist

  • Test cases added
  • Release notes entry updated

Copy link
Contributor

github-actions bot commented Apr 26, 2025

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.300.md

@@ -4462,6 +4462,30 @@ declExpr:
let trivia: SynExprLetOrUseBangTrivia = { LetOrUseBangKeyword = rhs parseState 1 ; EqualsRange = Some mEquals }
SynExpr.LetOrUseBang(spBind, ($1 = "use"), true, $2, $4, $7, $8, m, trivia) }

| BINDER atomicPatternLongIdent COLON typeWithTypeConstraints EQUALS typedSequentialExprBlock IN opt_OBLOCKSEP moreBinders typedSequentialExprBlock %prec expr_let
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new rule allows type annotations without requiring parentheses in let!/use! bindings

let! x: int = ...
use! x: int = ...

It's worth noting that this is consistent with how patterns are handled in regular let bindings as well - e.g. when you want to annotate individual elements of a tuple, you need parentheses

let (a: int, b: string) = (1, "hello")  // Valid
let a: int, b: string = (1, "hello")  // Not valid

let! (a: int , b: int) = async { return 1, 3 } // Valid
let! a: int , b: int = async { return 1, 3 }  // Invalid

let trivia: SynExprLetOrUseBangTrivia = { LetOrUseBangKeyword = rhs parseState 1 ; EqualsRange = Some mEquals }
SynExpr.LetOrUseBang(spBind, ($1 = "use"), true, pat, $6, $9, $10, m, trivia) }

| OBINDER atomicPatternLongIdent COLON typeWithTypeConstraints EQUALS typedSequentialExprBlock hardwhiteDefnBindingsTerminator opt_OBLOCKSEP moreBinders typedSequentialExprBlock %prec expr_let
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the associated offside-sensitive version of the new type annotation rule. e.g. in cases where let! or use! bindings are used outside of the CE context. This is invalid code but we will want the tokens to be parsed.

let example=
    // No braces, indentation defines scope
    let! x = async { return 1 }
    let! y = async { return 2 }
    return x + y

@edgarfgp edgarfgp requested a review from auduchinok April 30, 2025 16:24
@edgarfgp
Copy link
Contributor Author

edgarfgp commented Apr 30, 2025

It might be interesting in a follow up PR to add error recovery for cases where we have a headBindingPattern COLON error

let! x: 
use! stream: 
let x: 
use x:

Where we can use reportParseErrorAt mColon (FSComp.SR.parsExpectingType())

Copy link
Member

@baronfel baronfel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the depth of test coverage here.

@edgarfgp edgarfgp marked this pull request as ready for review May 1, 2025 20:11
@edgarfgp edgarfgp requested a review from a team as a code owner May 1, 2025 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

let! binding with type annotation without parentheses yields error FS0010: Unexpected symbol ':' in expression
3 participants