Skip to content

Commit a83bb4f

Browse files
committed
Intern ItemRef
1 parent e8c5b73 commit a83bb4f

File tree

9 files changed

+145
-52
lines changed

9 files changed

+145
-52
lines changed

engine/lib/concrete_ident/concrete_ident.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ module MakeRenderAPI (NP : NAME_POLICY) : RENDER_API = struct
485485
let*? _no_generics = List.is_empty impl_infos.generics.params in
486486
match impl_infos.trait_ref with
487487
| None -> Some ty
488-
| Some { def_id = trait; generic_args = [ _self ]; _ } ->
488+
| Some { value = { def_id = trait; generic_args = [ _self ]; _ }; _ } ->
489489
let* trait = Explicit_def_id.of_def_id trait in
490490
let trait = View.of_def_id trait in
491491
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: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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 = unboxed_item_ref
14+
type item_ref = node_for__item_ref_contents
1515
type trait_ref = item_ref
1616
end
1717

@@ -514,8 +514,14 @@ end) : EXPR = struct
514514
let f = c_expr fun' in
515515
match fun'.contents with
516516
| GlobalName
517-
{ item = { def_id = id; generic_args; impl_exprs; in_trait }; _ }
518-
->
517+
{
518+
item =
519+
{
520+
value = { def_id = id; generic_args; impl_exprs; in_trait };
521+
_;
522+
};
523+
_;
524+
} ->
519525
let f = { f with e = GlobalVar (def_id ~value:true id) } in
520526
let bounds_impls = List.map ~f:(c_impl_expr e.span) impl_exprs in
521527
let generic_args =
@@ -637,7 +643,8 @@ end) : EXPR = struct
637643
trait = None (* TODO: see issue #328 *);
638644
bounds_impls = [];
639645
}
640-
| GlobalName { item = { def_id = id; _ }; constructor = _ } ->
646+
| GlobalName { item = { value = { def_id = id; _ }; _ }; constructor = _ }
647+
->
641648
GlobalVar (def_id ~value:true id)
642649
| UpvarRef { var_hir_id = id; _ } -> LocalVar (local_ident Expr id)
643650
| Borrow { arg; borrow_kind = kind } ->
@@ -726,7 +733,11 @@ end) : EXPR = struct
726733
})
727734
l))
728735
| NamedConst
729-
{ item = { def_id = id; generic_args; in_trait = impl; _ }; _ } ->
736+
{
737+
item =
738+
{ value = { def_id = id; generic_args; in_trait = impl; _ }; _ };
739+
_;
740+
} ->
730741
let f = GlobalVar (def_id ~value:true id) in
731742
let args = List.map ~f:(c_generic_value e.span) generic_args in
732743
let const_args =
@@ -1025,7 +1036,7 @@ end) : EXPR = struct
10251036
else List.map ~f:(c_ty span) inputs
10261037
in
10271038
TArrow (inputs, c_ty span output)
1028-
| Adt { def_id = id; generic_args; _ } ->
1039+
| Adt { value = { def_id = id; generic_args; _ }; _ } ->
10291040
let ident = def_id ~value:false id in
10301041
let args = List.map ~f:(c_generic_value span) generic_args in
10311042
TApp { ident; args }
@@ -1110,15 +1121,15 @@ end) : EXPR = struct
11101121
let goal = c_trait_ref span ie.trait.value in
11111122
let impl = { kind = c_impl_expr_atom span ie.impl; goal } in
11121123
match ie.impl with
1113-
| Concrete { impl_exprs = []; _ } -> impl
1114-
| Concrete { impl_exprs; _ } ->
1124+
| Concrete { value = { impl_exprs = []; _ }; _ } -> impl
1125+
| Concrete { value = { impl_exprs; _ }; _ } ->
11151126
let args = List.map ~f:(c_impl_expr span) impl_exprs in
11161127
{ kind = ImplApp { impl; args }; goal }
11171128
| _ -> impl
11181129

11191130
and c_trait_ref span (tr : Thir.trait_ref) : trait_goal =
1120-
let trait = Concrete_ident.of_def_id ~value:false tr.def_id in
1121-
let args = List.map ~f:(c_generic_value span) tr.generic_args in
1131+
let trait = Concrete_ident.of_def_id ~value:false tr.value.def_id in
1132+
let args = List.map ~f:(c_generic_value span) tr.value.generic_args in
11221133
{ trait; args }
11231134

11241135
and c_impl_expr_atom (span : Thir.span) (ie : Thir.impl_expr_atom) :
@@ -1132,7 +1143,7 @@ end) : EXPR = struct
11321143
let ident =
11331144
{ goal = c_trait_ref span trait_ref; name = predicate_id }
11341145
in
1135-
let item = Concrete_ident.of_def_id ~value:false item.def_id in
1146+
let item = Concrete_ident.of_def_id ~value:false item.value.def_id in
11361147
let trait_ref = c_trait_ref span trait_ref in
11371148
Projection
11381149
{ impl = { kind = item_kind; goal = trait_ref }; ident; item }
@@ -1145,7 +1156,7 @@ end) : EXPR = struct
11451156
Parent { impl = { kind = item_kind; goal = trait_ref }; ident }
11461157
in
11471158
match ie with
1148-
| Concrete { def_id; generic_args; _ } ->
1159+
| Concrete { value = { def_id; generic_args; _ }; _ } ->
11491160
let trait = Concrete_ident.of_def_id ~value:false def_id in
11501161
let args = List.map ~f:(c_generic_value span) generic_args in
11511162
Concrete { trait; args }
@@ -1242,8 +1253,12 @@ end) : EXPR = struct
12421253
generic_constraint option =
12431254
match kind with
12441255
| Trait { is_positive = true; trait_ref } ->
1245-
let args = List.map ~f:(c_generic_value span) trait_ref.generic_args in
1246-
let trait = Concrete_ident.of_def_id ~value:false trait_ref.def_id in
1256+
let args =
1257+
List.map ~f:(c_generic_value span) trait_ref.value.generic_args
1258+
in
1259+
let trait =
1260+
Concrete_ident.of_def_id ~value:false trait_ref.value.def_id
1261+
in
12471262
Some (GCType { goal = { trait; args }; name = id })
12481263
| Projection { impl_expr; assoc_item; ty } ->
12491264
let impl = c_impl_expr span impl_expr in
@@ -1770,9 +1785,10 @@ and c_item_unwrapped ~ident ~type_only (item : Thir.item) : item list =
17701785
generics = c_generics generics;
17711786
self_ty = c_ty item.span self_ty;
17721787
of_trait =
1773-
( Concrete_ident.of_def_id ~value:false of_trait.def_id,
1774-
List.map ~f:(c_generic_value item.span) of_trait.generic_args
1775-
);
1788+
( Concrete_ident.of_def_id ~value:false of_trait.value.def_id,
1789+
List.map
1790+
~f:(c_generic_value item.span)
1791+
of_trait.value.generic_args );
17761792
items;
17771793
parent_bounds =
17781794
List.filter_map

engine/lib/import_thir.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
val import_ty : Types.span -> Types.node_for__ty_kind -> Ast.Rust.ty
22

33
val import_trait_ref :
4-
Types.span -> Types.unboxed_item_ref -> Ast.Rust.trait_goal
4+
Types.span -> Types.node_for__item_ref_contents -> Ast.Rust.trait_goal
55

66
val import_clause :
77
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
@@ -576,6 +576,7 @@ end
576576
let items = Object.entries(definitions)
577577
.map(([name, def]) => ['Node_for_TyKind' == name ? 'node_for_ty_kind_generated' : name, def])
578578
.map(([name, def]) => ['Node_for_DefIdContents' == name ? 'node_for_def_id_contents_generated' : name, def])
579+
.map(([name, def]) => ['Node_for_ItemRefContents' == name ? 'node_for_item_ref_contents_generated' : name, def])
579580
.map(
580581
([name, def]) => export_definition(name, def)
581582
).filter(x => x instanceof Object);
@@ -611,9 +612,10 @@ open ParseError
611612
impl += `
612613
and node_for__ty_kind = node_for_ty_kind_generated
613614
and node_for__def_id_contents = node_for_def_id_contents_generated
615+
and node_for__item_ref_contents = node_for_item_ref_contents_generated
614616
615617
616-
type map_types = ${"[`TyKind of ty_kind | `DefIdContents of def_id_contents]"}
618+
type map_types = ${"[`TyKind of ty_kind | `DefIdContents of def_id_contents | `ItemRefContents of item_ref_contents]"}
617619
let cache_map: (int64, ${"[ `Value of map_types | `JSON of Yojson.Safe.t ]"}) Base.Hashtbl.t = Base.Hashtbl.create (module Base.Int64)
618620
619621
module Exn = struct
@@ -667,6 +669,15 @@ and node_for__def_id_contents_of_yojson (o: Yojson.Safe.t): node_for__def_id_con
667669
o
668670
in
669671
{value; id = Base.Int64.zero}
672+
and node_for__item_ref_contents_of_yojson (o: Yojson.Safe.t): node_for__item_ref_contents =
673+
let (value, _id) =
674+
table_id_node_of_yojson "ItemRefContents"
675+
(fun value -> \`ItemRefContents value)
676+
(function | \`ItemRefContents value -> Some value | _ -> None)
677+
item_ref_contents_of_yojson
678+
o
679+
in
680+
{value; id = Base.Int64.zero}
670681
`;
671682
impl += ('');
672683
impl += ('let rec ' + items.map(({ name, type, parse, to_json }) =>
@@ -675,6 +686,7 @@ and node_for__def_id_contents_of_yojson (o: Yojson.Safe.t): node_for__def_id_con
675686
impl += `
676687
and yojson_of_node_for__ty_kind {value; id} = yojson_of_node_for_ty_kind_generated {value; id}
677688
and yojson_of_node_for__def_id_contents {value; id} = yojson_of_node_for_def_id_contents_generated {value; id}
689+
and yojson_of_node_for__item_ref_contents {value; id} = yojson_of_node_for_item_ref_contents_generated {value; id}
678690
end
679691
680692
open struct

frontend/exporter/src/id_table.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl Session {
4848
pub enum Value {
4949
Ty(Arc<TyKind>),
5050
DefId(Arc<DefIdContents>),
51+
ItemRef(Arc<ItemRefContents>),
5152
}
5253

5354
impl SupportedType<Value> for TyKind {
@@ -74,6 +75,18 @@ impl SupportedType<Value> for DefIdContents {
7475
}
7576
}
7677

78+
impl SupportedType<Value> for ItemRefContents {
79+
fn to_types(value: Arc<Self>) -> Value {
80+
Value::ItemRef(value)
81+
}
82+
fn from_types(t: &Value) -> Option<Arc<Self>> {
83+
match t {
84+
Value::ItemRef(value) => Some(value.clone()),
85+
_ => None,
86+
}
87+
}
88+
}
89+
7790
/// A node is a bundle of an ID with a value.
7891
#[derive(Deserialize, Serialize, Debug, JsonSchema, PartialEq, Eq, PartialOrd, Ord)]
7992
#[serde(into = "serde_repr::NodeRepr<T>")]

frontend/exporter/src/traits.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ImplExprPathChunk> for resolution:
5757
index,
5858
..
5959
} => ImplExprPathChunk::AssocItem {
60-
item: UnboxedItemRef::new(
60+
item: ItemRef::new(
61+
s,
6162
item.def_id.sinto(s),
6263
generic_args.sinto(s),
6364
impl_exprs.sinto(s),
65+
None,
6466
),
6567
assoc_item: item.sinto(s),
6668
predicate: predicate.sinto(s),
@@ -87,10 +89,12 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ImplExprPathChunk> for resolution:
8789
pub enum ImplExprAtom {
8890
/// A concrete `impl Trait for Type {}` item.
8991
#[custom_arm(FROM_TYPE::Concrete { def_id, generics, impl_exprs } => TO_TYPE::Concrete(
90-
UnboxedItemRef::new(
92+
ItemRef::new(
93+
s,
9194
def_id.sinto(s),
9295
generics.sinto(s),
9396
impl_exprs.sinto(s),
97+
None,
9498
)
9599
),)]
96100
Concrete(ItemRef),
@@ -285,12 +289,13 @@ pub fn translate_item_ref<'tcx, S: UnderOwnerState<'tcx>>(
285289
impl_exprs.drain(0..num_trait_trait_clauses);
286290
}
287291

288-
Box::new(UnboxedItemRef {
292+
(ItemRefContents {
289293
def_id: def_id.sinto(s),
290294
generic_args,
291295
impl_exprs,
292296
in_trait: trait_info,
293297
})
298+
.intern(s)
294299
}
295300

296301
/// Solve the trait obligations for a specific item use (for example, a method call, an ADT, etc.)

frontend/exporter/src/types/mir.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,10 @@ fn translate_mir_const<'tcx, S: UnderOwnerState<'tcx>>(
259259
match ucv.promoted {
260260
Some(promoted) => {
261261
let mut item = translate_item_ref(s, ucv.def, ucv.args);
262-
// Construct a def_id for the promoted constant.
263-
item.def_id = item.def_id.make_promoted_child(s, promoted.sinto(s));
262+
item.mutate(s, |item| {
263+
// Construct a def_id for the promoted constant.
264+
item.def_id = item.def_id.make_promoted_child(s, promoted.sinto(s));
265+
});
264266
Promoted(item)
265267
}
266268
None => Value(match translate_constant_reference(s, span, ucv.shrink()) {

0 commit comments

Comments
 (0)