Skip to content

TailCall attribute does not lead to errors when recursive call is in a list comprehension #16649

Closed

Description

@Larocceau

When using the TailCall attribute, warning FS3569 is not shown when the recursive call is inside a list comprehension

Repro steps

  1. Create a new f# console project.
  2. Add the following code to Program.fs
[<TailCall>]
let rec reverse (input: list<'t>) =
    match input with
    | head :: tail -> [ yield! reverse tail; head ]
    | [] -> []
  1. run dotnet build

Example project with a case where the bug occurs , and a slightly different version where the bug does not occur

Expected behavior

The compiler displays warning FS3569

Actual behavior

The compiler does not show the warning

Known workarounds

Do not use list comprehensions when trying to use the tail call compiler attribute

Related information

  • Operating system: Windows 11 enterprise
  • .NET Core
  • Editing Tools: VSCode

Activity

  1. added this to the Backlog milestone on Feb 3, 2024
  2. Larocceau commented on Feb 3, 2024

    @Larocceau
    Author

    @dawedawe since you contributed to this feature this might be of interest to you

  3. dawedawe commented on Feb 3, 2024

    @dawedawe
    Contributor

    @dawedawe since you contributed to this feature this might be of interest to you

    Thanks for the finding, I'll work on it.

  4. vzarytovskii commented on Feb 5, 2024

    @vzarytovskii
    Member

    Example project with a case where the bug occurs , and a slightly different version where the bug does not occur

    // Nearly identical code that is flagged by the compiler;
    // only difference is using List.Append instead of a list comprehension

    Difference is that list expression will use list collector, and will result in something like

    let tailOrNull = input.TailOrNull
    let headOrDefault = input.HeadOrDefault
    let listCollector = ListCollector<t>()
    listCollector.AddMany(reverse(tailOrNull))
    listCollector.Add(headOrDefault)
    listCollector.Close()

    But when the analysis happens, it probably thinks it's atomic or something.

  5. vzarytovskii commented on Feb 5, 2024

    @vzarytovskii
    Member

    Oh, yeah

    [<TailCall>]
    let rec recurse (list : list<int>) =
        [ yield! recurse list; () ]

    Will result in something like (roughly):

    DebugPoint((6,8--6,35), App(toList, [Op(Coerce, App(seq, [App(delay, [Lambda([unitVar], App(append, [.., ..]))])]))]))

    Whereas

    [<TailCall>]
    let rec recurse (list : list<int>) =
        recurse list; ()

    into

    Sequential(DebugPoint((7,8--7,20), App(.., [..])), DebugPoint((7,22--7,24), ()))

    We analyze the latter correctly as sequential expression and produce the warning, and we end up analysing the former as application and decide that it's tail recursive, which we shouldn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions