Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007758OCamllexing and parsingpublic2018-03-25 19:182018-04-11 13:37
Reporterantron 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusnewResolutionopen 
PlatformOSOS Version
Product Version4.06.1 
Target VersionFixed in Version 
Summary0007758: Pattern type constraint in let expression parsed inconsistently
DescriptionThe expression

  (1) let foo : int = 42 in ...

seems to get parsed as

     let foo : int = 42 : int in ...

while the expression

  (2) let (foo : int) = 42 in ...

which differs only in having parentheses, seems to get parsed as

      let foo : int = 42 in ...

I would have expected the second parse for both cases. The first parse can be problematic when using PPX, because the variable and expression may need to have different types. In particular, in

      let%lwt foo : int = bar in ...

foo should have type int, and bar should have type int Lwt.t. See https://github.com/ocsigen/lwt/issues/564. [^]
Steps To Reproduce1. source.ml:

let () =
  let foo : int = 42 in
  let (foo : int) = 42 in
  ignore foo


2. ocamlopt -dparsetree -c source.ml


3. Result is:

[
  structure_item (source.ml[1,0+0]..[4,59+12])
    Pstr_value Nonrec
    [
      <def>
        pattern (source.ml[1,0+4]..[1,0+6])
          Ppat_construct "()" (source.ml[1,0+4]..[1,0+6])
          None
        expression (source.ml[2,9+2]..[4,59+12])
          Pexp_let Nonrec
          [
            <def>
              pattern (source.ml[2,9+6]..[2,9+20]) ghost
                Ppat_constraint
                pattern (source.ml[2,9+6]..[2,9+9])
                  Ppat_var "foo" (source.ml[2,9+6]..[2,9+9])
                core_type (source.ml[2,9+6]..[2,9+20]) ghost
                  Ptyp_poly
                  core_type (source.ml[2,9+12]..[2,9+15])
                    Ptyp_constr "int" (source.ml[2,9+12]..[2,9+15])
                    []
              expression (source.ml[2,9+6]..[2,9+20]) ghost
                Pexp_constraint <----------------------------- !!!!
                expression (source.ml[2,9+18]..[2,9+20])
                  Pexp_constant PConst_int (42,None)
                core_type (source.ml[2,9+12]..[2,9+15])
                  Ptyp_constr "int" (source.ml[2,9+12]..[2,9+15])
                  []
          ]
          expression (source.ml[3,33+2]..[4,59+12])
            Pexp_let Nonrec
            [
              <def>
                pattern (source.ml[3,33+6]..[3,33+17])
                  Ppat_constraint
                  pattern (source.ml[3,33+7]..[3,33+10])
                    Ppat_var "foo" (source.ml[3,33+7]..[3,33+10])
                  core_type (source.ml[3,33+13]..[3,33+16])
                    Ptyp_constr "int" (source.ml[3,33+13]..[3,33+16])
                    []
                expression (source.ml[3,33+20]..[3,33+22])
                  Pexp_constant PConst_int (42,None)
            ]
            expression (source.ml[4,59+2]..[4,59+12])
              Pexp_apply
              expression (source.ml[4,59+2]..[4,59+8])
                Pexp_ident "ignore" (source.ml[4,59+2]..[4,59+8])
              [
                <arg>
                Nolabel
                  expression (source.ml[4,59+9]..[4,59+12])
                    Pexp_ident "foo" (source.ml[4,59+9]..[4,59+12])
              ]
    ]
]
Additional InformationFurther complicating this in practice is that in 4.05.0, (1) is parsed as

      let foo = 42 : int in

i.e. the constraint is removed from the pattern and appears on the expression only.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0019009)
drup (reporter)
2018-04-11 13:37

It would be nice to sort this out before the next release, as it messes up with ppx in a way that is difficult to take into account, even with ocaml-migrate-parsetree.

If someone has a concrete proposition on how to make this cleaner, I'm ready to implement it.

- Issue History
Date Modified Username Field Change
2018-03-25 19:18 antron New Issue
2018-04-11 13:37 drup Note Added: 0019009


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker