Skip to content

Translcore.push_defaults does not respect scoping #10338

@stedolan

Description

@stedolan

Evaluation of defaults of optional parameters is pushed under function binders in a way that is not always sound:

        OCaml version 4.12.0

# let s = 42;;
val s : int = 42
# let f ?(s="hello") = function x when (print_string s; true) -> x;;
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
All clauses in this pattern-matching are guarded.
val f : ?s:string -> 'a -> 'a = <fun>
# f ();;
Segmentation fault

f gets translated to something resembling:

let f opt = function
  | x when (print_string s; true) ->
    let s = match opt with Some s -> s | None -> "hello" in
    x

where the s in the guard is the wrong one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions