Skip to content

Commit e84428a

Browse files
Merge branch 'main' into ocaml-spans-reviewable
2 parents d486758 + b82bd9c commit e84428a

File tree

17 files changed

+436
-404
lines changed

17 files changed

+436
-404
lines changed

engine/lib/concrete_ident/concrete_ident.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ module MakeRenderAPI (NP : NAME_POLICY) : RENDER_API = struct
503503
let*? _no_generics = List.is_empty impl_infos.generics.params in
504504
match impl_infos.trait_ref with
505505
| None -> Some ty
506-
| Some { def_id = trait; generic_args = [ _self ]; _ } ->
506+
| Some { value = { def_id = trait; generic_args = [ _self ]; _ }; _ } ->
507507
let* trait = Explicit_def_id.of_def_id trait in
508508
let trait = View.of_def_id trait in
509509
let*? _same_ns = [%eq: View.ModPath.t] namespace trait.mod_path in

engine/lib/concrete_ident/thir_simple_types.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ let to_string ~(namespace : View.ModPath.t) :
5858
| Float F32 -> Some "f32"
5959
| Float F64 -> Some "f64"
6060
| Tuple [] -> Some "unit"
61-
| Adt { def_id; generic_args = []; _ } -> Option.map ~f:escape (adt def_id)
61+
| Adt { value = { def_id; generic_args = []; _ }; _ } ->
62+
Option.map ~f:escape (adt def_id)
6263
| _ -> None
6364
in
6465
let apply left right = left ^ "_of_" ^ right in
6566
let rec arity1 (ty : Types.node_for__ty_kind) =
6667
match ty.value with
6768
| Slice sub -> arity1 sub |> Option.map ~f:(apply "slice")
6869
| Ref (_, sub, _) -> arity1 sub |> Option.map ~f:(apply "ref")
69-
| Adt { def_id; generic_args = [ Type arg ]; _ } ->
70+
| Adt { value = { def_id; generic_args = [ Type arg ]; _ }; _ } ->
7071
let* adt = adt def_id in
7172
let* arg = arity1 arg in
7273
Some (apply adt arg)

engine/lib/import_thir.ml

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module Thir = struct
1111
type generic_param_kind = generic_param_kind_for__decorated_for__expr_kind
1212
type trait_item = trait_item_for__decorated_for__expr_kind
1313
type ty = node_for__ty_kind
14+
type item_ref = node_for__item_ref_contents
15+
type trait_ref = item_ref
1416
end
1517

1618
open! Prelude
@@ -505,38 +507,35 @@ end) : EXPR = struct
505507
let then_ = c_expr then' in
506508
let else_ = Option.map ~f:c_expr else_opt in
507509
If { cond; else_; then_ }
508-
| Call
509-
{
510-
args;
511-
fn_span = _;
512-
trait;
513-
from_hir_call = _;
514-
fun';
515-
ty = _;
516-
generic_args;
517-
bounds_impls;
518-
} ->
519-
let args = List.map ~f:c_expr args in
520-
let bounds_impls = List.map ~f:(c_impl_expr e.span) bounds_impls in
521-
let c_generic_values = List.map ~f:(c_generic_value e.span) in
522-
let generic_args = c_generic_values generic_args in
523-
let f =
524-
let f = c_expr fun' in
525-
match (trait, fun'.contents) with
526-
| Some _, GlobalName { id; _ } ->
527-
{ f with e = GlobalVar (def_id ~value:true id) }
528-
| _ -> f
510+
| Call { args; fn_span = _; from_hir_call = _; fun'; ty = _ } -> (
511+
let args =
512+
if List.is_empty args then [ unit_expr span ]
513+
else List.map ~f:c_expr args
529514
in
530-
let args = if List.is_empty args then [ unit_expr span ] else args in
531-
App
532-
{
533-
f;
534-
args;
535-
generic_args;
536-
bounds_impls;
537-
trait =
538-
Option.map ~f:(c_impl_expr e.span *** c_generic_values) trait;
539-
}
515+
let f = c_expr fun' in
516+
match fun'.contents with
517+
| GlobalName
518+
{
519+
item =
520+
{
521+
value = { def_id = id; generic_args; impl_exprs; in_trait };
522+
_;
523+
};
524+
_;
525+
} ->
526+
let f = { f with e = GlobalVar (def_id ~value:true id) } in
527+
let bounds_impls = List.map ~f:(c_impl_expr e.span) impl_exprs in
528+
let generic_args =
529+
List.map ~f:(c_generic_value e.span) generic_args
530+
in
531+
let in_trait = Option.map ~f:(c_impl_expr e.span) in_trait in
532+
let trait =
533+
Option.map ~f:(fun ie -> (ie, ie.goal.args)) in_trait
534+
in
535+
App { f; args; generic_args; bounds_impls; trait }
536+
| _ ->
537+
App
538+
{ f; args; generic_args = []; bounds_impls = []; trait = None })
540539
| Box { value } ->
541540
(U.call Rust_primitives__hax__box_new [ c_expr value ] span typ).e
542541
| Deref { arg } ->
@@ -645,7 +644,9 @@ end) : EXPR = struct
645644
trait = None (* TODO: see issue #328 *);
646645
bounds_impls = [];
647646
}
648-
| GlobalName { id; constructor = _ } -> GlobalVar (def_id ~value:true id)
647+
| GlobalName { item = { value = { def_id = id; _ }; _ }; constructor = _ }
648+
->
649+
GlobalVar (def_id ~value:true id)
649650
| UpvarRef { var_hir_id = id; _ } -> LocalVar (local_ident Expr id)
650651
| Borrow { arg; borrow_kind = kind } ->
651652
let e' = c_expr arg in
@@ -732,9 +733,14 @@ end) : EXPR = struct
732733
typ = TInt { size = S8; signedness = Unsigned };
733734
})
734735
l))
735-
| NamedConst { def_id = id; impl; args; _ } ->
736+
| NamedConst
737+
{
738+
item =
739+
{ value = { def_id = id; generic_args; in_trait = impl; _ }; _ };
740+
_;
741+
} ->
736742
let f = GlobalVar (def_id ~value:true id) in
737-
let args = List.map ~f:(c_generic_value e.span) args in
743+
let args = List.map ~f:(c_generic_value e.span) generic_args in
738744
let const_args =
739745
List.filter_map args ~f:(function GConst e -> Some e | _ -> None)
740746
in
@@ -854,7 +860,7 @@ end) : EXPR = struct
854860
Array { fields = List.map ~f:constant_expr_to_expr fields }
855861
| Tuple { fields } ->
856862
Tuple { fields = List.map ~f:constant_expr_to_expr fields }
857-
| GlobalName { id; _ } -> GlobalName { id; constructor = None }
863+
| GlobalName item -> GlobalName { item; constructor = None }
858864
| Borrow arg ->
859865
Borrow { arg = constant_expr_to_expr arg; borrow_kind = Thir.Shared }
860866
| ConstRef { id } -> ConstRef { id }
@@ -1024,16 +1030,14 @@ end) : EXPR = struct
10241030
| Float k ->
10251031
TFloat
10261032
(match k with F16 -> F16 | F32 -> F32 | F64 -> F64 | F128 -> F128)
1027-
| Arrow signature
1028-
| Closure (_, { untupled_sig = signature; _ })
1029-
| FnDef { fn_sig = signature; _ } ->
1030-
let ({ inputs; output; _ } : Thir.ty_fn_sig) = signature.value in
1033+
| Arrow fn_sig | Closure { fn_sig; _ } | FnDef { fn_sig; _ } ->
1034+
let ({ inputs; output; _ } : Thir.ty_fn_sig) = fn_sig.value in
10311035
let inputs =
10321036
if List.is_empty inputs then [ U.unit_typ ]
10331037
else List.map ~f:(c_ty span) inputs
10341038
in
10351039
TArrow (inputs, c_ty span output)
1036-
| Adt { def_id = id; generic_args; _ } ->
1040+
| Adt { value = { def_id = id; generic_args; _ }; _ } ->
10371041
let ident = def_id ~value:false id in
10381042
let args = List.map ~f:(c_generic_value span) generic_args in
10391043
TApp { ident; args }
@@ -1118,15 +1122,15 @@ end) : EXPR = struct
11181122
let goal = c_trait_ref span ie.trait.value in
11191123
let impl = { kind = c_impl_expr_atom span ie.impl; goal } in
11201124
match ie.impl with
1121-
| Concrete { impl_exprs = []; _ } -> impl
1122-
| Concrete { impl_exprs; _ } ->
1125+
| Concrete { value = { impl_exprs = []; _ }; _ } -> impl
1126+
| Concrete { value = { impl_exprs; _ }; _ } ->
11231127
let args = List.map ~f:(c_impl_expr span) impl_exprs in
11241128
{ kind = ImplApp { impl; args }; goal }
11251129
| _ -> impl
11261130

11271131
and c_trait_ref span (tr : Thir.trait_ref) : trait_goal =
1128-
let trait = Concrete_ident.of_def_id ~value:false tr.def_id in
1129-
let args = List.map ~f:(c_generic_value span) tr.generic_args in
1132+
let trait = Concrete_ident.of_def_id ~value:false tr.value.def_id in
1133+
let args = List.map ~f:(c_generic_value span) tr.value.generic_args in
11301134
{ trait; args }
11311135

11321136
and c_impl_expr_atom (span : Thir.span) (ie : Thir.impl_expr_atom) :
@@ -1140,7 +1144,7 @@ end) : EXPR = struct
11401144
let ident =
11411145
{ goal = c_trait_ref span trait_ref; name = predicate_id }
11421146
in
1143-
let item = Concrete_ident.of_def_id ~value:false item.def_id in
1147+
let item = Concrete_ident.of_def_id ~value:false item.value.def_id in
11441148
let trait_ref = c_trait_ref span trait_ref in
11451149
Projection
11461150
{ impl = { kind = item_kind; goal = trait_ref }; ident; item }
@@ -1153,9 +1157,9 @@ end) : EXPR = struct
11531157
Parent { impl = { kind = item_kind; goal = trait_ref }; ident }
11541158
in
11551159
match ie with
1156-
| Concrete { id; generics; _ } ->
1157-
let trait = Concrete_ident.of_def_id ~value:false id in
1158-
let args = List.map ~f:(c_generic_value span) generics in
1160+
| Concrete { value = { def_id; generic_args; _ }; _ } ->
1161+
let trait = Concrete_ident.of_def_id ~value:false def_id in
1162+
let args = List.map ~f:(c_generic_value span) generic_args in
11591163
Concrete { trait; args }
11601164
| LocalBound { predicate_id; path; _ } ->
11611165
let init = LocalBound { id = predicate_id } in
@@ -1250,8 +1254,12 @@ end) : EXPR = struct
12501254
generic_constraint option =
12511255
match kind with
12521256
| Trait { is_positive = true; trait_ref } ->
1253-
let args = List.map ~f:(c_generic_value span) trait_ref.generic_args in
1254-
let trait = Concrete_ident.of_def_id ~value:false trait_ref.def_id in
1257+
let args =
1258+
List.map ~f:(c_generic_value span) trait_ref.value.generic_args
1259+
in
1260+
let trait =
1261+
Concrete_ident.of_def_id ~value:false trait_ref.value.def_id
1262+
in
12551263
Some (GCType { goal = { trait; args }; name = id })
12561264
| Projection { impl_expr; assoc_item; ty } ->
12571265
let impl = c_impl_expr span impl_expr in
@@ -1330,7 +1338,7 @@ include struct
13301338

13311339
let import_ty : Types.span -> Types.node_for__ty_kind -> Ast.Rust.ty = c_ty
13321340

1333-
let import_trait_ref : Types.span -> Types.trait_ref -> Ast.Rust.trait_goal =
1341+
let import_trait_ref : Types.span -> Thir.trait_ref -> Ast.Rust.trait_goal =
13341342
c_trait_ref
13351343

13361344
let import_clause :
@@ -1778,9 +1786,10 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
17781786
generics = c_generics generics;
17791787
self_ty = c_ty item.span self_ty;
17801788
of_trait =
1781-
( Concrete_ident.of_def_id ~value:false of_trait.def_id,
1782-
List.map ~f:(c_generic_value item.span) of_trait.generic_args
1783-
);
1789+
( Concrete_ident.of_def_id ~value:false of_trait.value.def_id,
1790+
List.map
1791+
~f:(c_generic_value item.span)
1792+
of_trait.value.generic_args );
17841793
items;
17851794
parent_bounds =
17861795
List.filter_map

engine/lib/import_thir.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
val import_ty : Types.span -> Types.node_for__ty_kind -> Ast.Rust.ty
2-
val import_trait_ref : Types.span -> Types.trait_ref -> Ast.Rust.trait_goal
2+
3+
val import_trait_ref :
4+
Types.span -> Types.node_for__item_ref_contents -> Ast.Rust.trait_goal
35

46
val import_clause :
57
Types.span -> Types.clause -> Ast.Rust.generic_constraint option

engine/utils/ocaml_of_json_schema/ocaml_of_json_schema.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ end
591591
let items = Object.entries(definitions)
592592
.map(([name, def]) => ['Node_for_TyKind' == name ? 'node_for_ty_kind_generated' : name, def])
593593
.map(([name, def]) => ['Node_for_DefIdContents' == name ? 'node_for_def_id_contents_generated' : name, def])
594+
.map(([name, def]) => ['Node_for_ItemRefContents' == name ? 'node_for_item_ref_contents_generated' : name, def])
594595
.map(
595596
([name, def]) => export_definition(name, def)
596597
).filter(x => x instanceof Object);
@@ -626,9 +627,10 @@ open ParseError
626627
impl += `
627628
and node_for__ty_kind = node_for_ty_kind_generated
628629
and node_for__def_id_contents = node_for_def_id_contents_generated
630+
and node_for__item_ref_contents = node_for_item_ref_contents_generated
629631
630632
631-
type map_types = ${"[`TyKind of ty_kind | `DefIdContents of def_id_contents]"}
633+
type map_types = ${"[`TyKind of ty_kind | `DefIdContents of def_id_contents | `ItemRefContents of item_ref_contents]"}
632634
let cache_map: (int64, ${"[ `Value of map_types | `JSON of Yojson.Safe.t ]"}) Base.Hashtbl.t = Base.Hashtbl.create (module Base.Int64)
633635
634636
module Exn = struct
@@ -682,6 +684,15 @@ and node_for__def_id_contents_of_yojson (o: Yojson.Safe.t): node_for__def_id_con
682684
o
683685
in
684686
{value; id = Base.Int64.zero}
687+
and node_for__item_ref_contents_of_yojson (o: Yojson.Safe.t): node_for__item_ref_contents =
688+
let (value, _id) =
689+
table_id_node_of_yojson "ItemRefContents"
690+
(fun value -> \`ItemRefContents value)
691+
(function | \`ItemRefContents value -> Some value | _ -> None)
692+
item_ref_contents_of_yojson
693+
o
694+
in
695+
{value; id = Base.Int64.zero}
685696
`;
686697
impl += ('');
687698
impl += ('let rec ' + items.map(({ name, type, parse, to_json }) =>
@@ -690,6 +701,7 @@ and node_for__def_id_contents_of_yojson (o: Yojson.Safe.t): node_for__def_id_con
690701
impl += `
691702
and yojson_of_node_for__ty_kind {value; id} = yojson_of_node_for_ty_kind_generated {value; id}
692703
and yojson_of_node_for__def_id_contents {value; id} = yojson_of_node_for_def_id_contents_generated {value; id}
704+
and yojson_of_node_for__item_ref_contents {value; id} = yojson_of_node_for_item_ref_contents_generated {value; id}
693705
end
694706
695707
open struct

frontend/exporter/src/constant_utils.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ pub enum ConstantExprKind {
6060
///
6161
/// If `options.inline_anon_consts` is `false`, this is also used for inline const blocks and
6262
/// advanced const generics expressions.
63-
GlobalName {
64-
id: GlobalIdent,
65-
generics: Vec<GenericArg>,
66-
trait_refs: Vec<ImplExpr>,
67-
},
63+
GlobalName(ItemRef),
6864
/// A trait constant
6965
///
7066
/// Ex.:
@@ -94,19 +90,7 @@ pub enum ConstantExprKind {
9490
ConstRef {
9591
id: ParamConst,
9692
},
97-
FnPtr {
98-
def_id: DefId,
99-
generics: Vec<GenericArg>,
100-
/// The implementation expressions for every generic bounds
101-
/// ```text
102-
/// fn foo<T : Bar>(...)
103-
/// ^^^
104-
/// ```
105-
generics_impls: Vec<ImplExpr>,
106-
/// If the function is a method of trait `Foo`, `method_impl`
107-
/// is an implementation of `Foo`
108-
method_impl: Option<ImplExpr>,
109-
},
93+
FnPtr(ItemRef),
11094
/// A blob of memory containing the byte representation of the value. This can occur when
11195
/// evaluating MIR constants. Interpreting this back to a structured value is left as an
11296
/// exercice to the consumer.
@@ -175,13 +159,8 @@ impl From<ConstantExpr> for Expr {
175159
base: AdtExprBase::None,
176160
user_ty: None,
177161
}),
178-
// TODO: propagate the generics and trait refs (see #636)
179-
GlobalName {
180-
id,
181-
generics: _,
182-
trait_refs: _,
183-
} => ExprKind::GlobalName {
184-
id,
162+
GlobalName(item) => ExprKind::GlobalName {
163+
item,
185164
constructor: None,
186165
},
187166
Borrow(e) => ExprKind::Borrow {

0 commit comments

Comments
 (0)