Guest User

Untitled

a guest
Feb 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. Index: typing/typecore.ml
  2. ===================================================================
  3. --- typing/typecore.ml (revision 15965)
  4. +++ typing/typecore.ml (working copy)
  5. @@ -291,6 +291,10 @@
  6. (p0, p, {type_kind=Type_record (fields, _)}) -> (p0, p, fields)
  7. | _ -> raise Not_found
  8.  
  9. +type label_access =
  10. + | Record_access of Typedtree.expression * Types.label_description * bool
  11. + | Method of Longident.t * string
  12. +
  13. let extract_concrete_variant env ty =
  14. match extract_concrete_typedecl env ty with
  15. (p0, p, {type_kind=Type_variant cstrs}) -> (p0, p, cstrs)
  16. @@ -2148,30 +2152,46 @@
  17. exp_attributes = sexp.pexp_attributes;
  18. exp_env = env }
  19. | Pexp_field(srecord, lid) ->
  20. - let (record, label, _) = type_label_access env loc srecord lid in
  21. - let (_, ty_arg, ty_res) = instance_label false label in
  22. - unify_exp env record ty_res;
  23. - rue {
  24. - exp_desc = Texp_field(record, lid, label);
  25. - exp_loc = loc; exp_extra = [];
  26. - exp_type = ty_arg;
  27. - exp_attributes = sexp.pexp_attributes;
  28. - exp_env = env }
  29. + begin match type_label_access env loc srecord lid with
  30. + | Record_access (record, label, _) ->
  31. + let (_, ty_arg, ty_res) = instance_label false label in
  32. + unify_exp env record ty_res;
  33. + rue {
  34. + exp_desc = Texp_field(record, lid, label);
  35. + exp_loc = loc; exp_extra = [];
  36. + exp_type = ty_arg;
  37. + exp_attributes = sexp.pexp_attributes;
  38. + exp_env = env }
  39. + | Method (mlid, s) ->
  40. + let fun_lid = Longident.Ldot (mlid, s) in
  41. + type_expect ?in_function env
  42. + Ast_helper.(Exp.apply (Exp.ident (mknoloc fun_lid))
  43. + [Nolabel, srecord])
  44. + ty_expected
  45. + end
  46. | Pexp_setfield(srecord, lid, snewval) ->
  47. - let (record, label, opath) = type_label_access env loc srecord lid in
  48. - let ty_record = if opath = None then newvar () else record.exp_type in
  49. - let (label_loc, label, newval) =
  50. - type_label_exp false env loc ty_record (lid, label, snewval) in
  51. - unify_exp env record ty_record;
  52. - if label.lbl_mut = Immutable then
  53. - raise(Error(loc, env, Label_not_mutable lid.txt));
  54. - rue {
  55. - exp_desc = Texp_setfield(record, label_loc, label, newval);
  56. - exp_loc = loc; exp_extra = [];
  57. - exp_type = instance_def Predef.type_unit;
  58. - exp_attributes = sexp.pexp_attributes;
  59. - exp_env = env }
  60. - | Pexp_array(sargl) ->
  61. + begin match type_label_access env loc srecord lid with
  62. + | Record_access (record, label, has_opath) ->
  63. + let ty_record = if has_opath then record.exp_type else newvar () in
  64. + let (label_loc, label, newval) =
  65. + type_label_exp false env loc ty_record (lid, label, snewval) in
  66. + unify_exp env record ty_record;
  67. + if label.lbl_mut = Immutable then
  68. + raise(Error(loc, env, Label_not_mutable lid.txt));
  69. + rue {
  70. + exp_desc = Texp_setfield(record, label_loc, label, newval);
  71. + exp_loc = loc; exp_extra = [];
  72. + exp_type = instance_def Predef.type_unit;
  73. + exp_attributes = sexp.pexp_attributes;
  74. + exp_env = env }
  75. + | Method (mlid, s) ->
  76. + let fun_lid = Longident.Ldot (mlid, "set_" ^ s) in
  77. + type_expect ?in_function env
  78. + Ast_helper.(Exp.apply (Exp.ident (mknoloc fun_lid))
  79. + [Nolabel, srecord; Nolabel, snewval])
  80. + ty_expected
  81. + end
  82. + | Pexp_array(sargl) ->
  83. let ty = newgenvar() in
  84. let to_unify = Predef.type_array ty in
  85. unify_exp_types loc env to_unify ty_expected;
  86. @@ -2796,17 +2816,34 @@
  87. generalize_structure record.exp_type
  88. end;
  89. let ty_exp = record.exp_type in
  90. - let opath =
  91. - try
  92. - let (p0, p,_) = extract_concrete_record env ty_exp in
  93. - Some(p0, p, ty_exp.level = generic_level || not !Clflags.principal)
  94. + let ty_decl =
  95. + try Some (extract_concrete_typedecl env ty_exp)
  96. with Not_found -> None
  97. in
  98. - let labels = Typetexp.find_all_labels env lid.loc lid.txt in
  99. - let label =
  100. - wrap_disambiguate "This expression has" ty_exp
  101. - (Label.disambiguate lid env opath) labels in
  102. - (record, label, opath)
  103. + match ty_decl with
  104. + | Some (p0, p, {type_kind=Type_abstract}) ->
  105. + let rec lid_of_path = function
  106. + | Path.Pdot (p, s, _) -> Longident.Ldot (lid_of_path p, s)
  107. + | Path.Pident s -> Longident.Lident (Ident.name s)
  108. + | Path.Papply _ -> assert false (* XXX *)
  109. + in
  110. + begin match p, lid.txt with
  111. + | Path.Pdot (p, _, _), Longident.Lident s ->
  112. + Method (lid_of_path p, s)
  113. + | _ -> assert false
  114. + end
  115. + | _ ->
  116. + let opath =
  117. + match ty_decl with
  118. + | Some (p0, p, {type_kind=Type_record _}) ->
  119. + Some(p0, p, ty_exp.level = generic_level || not !Clflags.principal)
  120. + | _ -> None
  121. + in
  122. + let labels = Typetexp.find_all_labels env lid.loc lid.txt in
  123. + let label =
  124. + wrap_disambiguate "This expression has" ty_exp
  125. + (Label.disambiguate lid env opath) labels in
  126. + Record_access (record, label, opath <> None)
  127.  
  128. (* Typing format strings for printing or reading.
  129. These formats are used by functions in modules Printf, Format, and Scanf.
  130. Index: typing/ctype.ml
  131. ===================================================================
  132. --- typing/ctype.ml (revision 15965)
  133. +++ typing/ctype.ml (working copy)
  134. @@ -1537,10 +1537,14 @@
  135. let decl = Env.find_type p env in
  136. if decl.type_kind <> Type_abstract then (p, p, decl) else
  137. let ty =
  138. - try try_expand_once env ty with Cannot_expand -> raise Not_found
  139. + try Some (try_expand_once env ty) with Cannot_expand -> None
  140. in
  141. - let (_, p', decl) = extract_concrete_typedecl env ty in
  142. - (p, p', decl)
  143. + begin match ty with
  144. + | None -> (p, p, decl)
  145. + | Some ty ->
  146. + let (_, p', decl) = extract_concrete_typedecl env ty in
  147. + (p, p', decl)
  148. + end
  149. | _ -> raise Not_found
  150.  
  151. (* Implementing function [expand_head_opt], the compiler's own version of
Add Comment
Please, Sign In to add comment