Skip to content

Inconsistent treatment of <- with tuples #16987

Closed
@brianrourkeboll

Description

@brianrourkeboll

Repro steps

Parentheses are not normally required around a tuple when using set/assignment syntax, including with an indexer, since , has higher precedence than <-.

When the assignment operation happens to translate to a method call like set_Item(key: …, value: …*…), however, parentheses are required — but only when using the x.[…] <- … syntax (i.e., with a dot .); when using the x[…] <- … syntax (without a dot), parentheses are never required.

open System.Collections.Generic

let xs = Array.zeroCreate 1

// OK.
xs[0] <- (2, 3, 4)
// OK.
xs[0] <- 2, 3, 4

// OK.
xs.[0] <- (2, 3, 4)
// OK.
xs.[0] <- 2, 3, 4

let ys = Dictionary ()

// OK.
ys[0] <- (2, 3, 4)
// OK.
ys[0] <- 2, 3, 4

// OK.
ys.[0] <- (2, 3, 4)
// error FS0501: The member or object constructor 'Item' takes 2 argument(s) but is here given 4.
// The required signature is 'Dictionary.set_Item(key: int, value: int * int * int) : unit'.
ys.[0] <- 2, 3, 4

// OK.
ys.Item 0 <- (2, 3, 4)
// error FS0501: The member or object constructor 'Item' takes 2 argument(s) but is here given 4.
// The required signature is 'Dictionary.set_Item(key: int, value: int * int * int) : unit'.
ys.Item 0 <- 2, 3, 4

// OK.
(ys).Item 0 <- (2, 3, 4)
// error FS0501: The member or object constructor 'Item' takes 2 argument(s) but is here given 4.
// The required signature is 'Dictionary.set_Item(key: int, value: int * int * int) : unit'.
(ys).Item 0 <- 2, 3, 4

Expected behavior

The syntactic precedence of , and <- should not change due to type information or presence of a . in the indexing syntax.

Actual behavior

The syntactic precedence of , and <- appear to change depending on type information and presence of a . in the indexing syntax.

Known workarounds

For the parentheses analyzer: always assume parentheses are required, or check the typed tree to see if the compiled translation involves a method call like set_Item(key: …, value: …*…).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions