Skip to content

Commit 1e62827

Browse files
committed
Regroup generic and trait arguments in a struct
1 parent c26cc9c commit 1e62827

File tree

12 files changed

+305
-368
lines changed

12 files changed

+305
-368
lines changed

engine/lib/import_thir.ml

Lines changed: 34 additions & 39 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 = unboxed_item_ref
15+
type trait_ref = item_ref
1416
end
1517

1618
open! Prelude
@@ -504,38 +506,29 @@ end) : EXPR = struct
504506
let then_ = c_expr then' in
505507
let else_ = Option.map ~f:c_expr else_opt in
506508
If { cond; else_; then_ }
507-
| Call
508-
{
509-
args;
510-
fn_span = _;
511-
trait;
512-
from_hir_call = _;
513-
fun';
514-
ty = _;
515-
generic_args;
516-
bounds_impls;
517-
} ->
518-
let args = List.map ~f:c_expr args in
519-
let bounds_impls = List.map ~f:(c_impl_expr e.span) bounds_impls in
520-
let c_generic_values = List.map ~f:(c_generic_value e.span) in
521-
let generic_args = c_generic_values generic_args in
522-
let f =
523-
let f = c_expr fun' in
524-
match (trait, fun'.contents) with
525-
| Some _, GlobalName { id; _ } ->
526-
{ f with e = GlobalVar (def_id ~value:true id) }
527-
| _ -> f
509+
| Call { args; fn_span = _; from_hir_call = _; fun'; ty = _ } -> (
510+
let args =
511+
if List.is_empty args then [ unit_expr span ]
512+
else List.map ~f:c_expr args
528513
in
529-
let args = if List.is_empty args then [ unit_expr span ] else args in
530-
App
531-
{
532-
f;
533-
args;
534-
generic_args;
535-
bounds_impls;
536-
trait =
537-
Option.map ~f:(c_impl_expr e.span *** c_generic_values) trait;
538-
}
514+
let f = c_expr fun' in
515+
match fun'.contents with
516+
| GlobalName
517+
{ item = { def_id = id; generic_args; impl_exprs; in_trait }; _ }
518+
->
519+
let f = { f with e = GlobalVar (def_id ~value:true id) } in
520+
let bounds_impls = List.map ~f:(c_impl_expr e.span) impl_exprs in
521+
let generic_args =
522+
List.map ~f:(c_generic_value e.span) generic_args
523+
in
524+
let in_trait = Option.map ~f:(c_impl_expr e.span) in_trait in
525+
let trait =
526+
Option.map ~f:(fun ie -> (ie, ie.goal.args)) in_trait
527+
in
528+
App { f; args; generic_args; bounds_impls; trait }
529+
| _ ->
530+
App
531+
{ f; args; generic_args = []; bounds_impls = []; trait = None })
539532
| Box { value } ->
540533
(U.call Rust_primitives__hax__box_new [ c_expr value ] span typ).e
541534
| Deref { arg } ->
@@ -644,7 +637,8 @@ end) : EXPR = struct
644637
trait = None (* TODO: see issue #328 *);
645638
bounds_impls = [];
646639
}
647-
| GlobalName { id; constructor = _ } -> GlobalVar (def_id ~value:true id)
640+
| GlobalName { item = { def_id = id; _ }; constructor = _ } ->
641+
GlobalVar (def_id ~value:true id)
648642
| UpvarRef { var_hir_id = id; _ } -> LocalVar (local_ident Expr id)
649643
| Borrow { arg; borrow_kind = kind } ->
650644
let e' = c_expr arg in
@@ -731,9 +725,10 @@ end) : EXPR = struct
731725
typ = TInt { size = S8; signedness = Unsigned };
732726
})
733727
l))
734-
| NamedConst { def_id = id; impl; args; _ } ->
728+
| NamedConst
729+
{ item = { def_id = id; generic_args; in_trait = impl; _ }; _ } ->
735730
let f = GlobalVar (def_id ~value:true id) in
736-
let args = List.map ~f:(c_generic_value e.span) args in
731+
let args = List.map ~f:(c_generic_value e.span) generic_args in
737732
let const_args =
738733
List.filter_map args ~f:(function GConst e -> Some e | _ -> None)
739734
in
@@ -853,7 +848,7 @@ end) : EXPR = struct
853848
Array { fields = List.map ~f:constant_expr_to_expr fields }
854849
| Tuple { fields } ->
855850
Tuple { fields = List.map ~f:constant_expr_to_expr fields }
856-
| GlobalName { id; _ } -> GlobalName { id; constructor = None }
851+
| GlobalName item -> GlobalName { item; constructor = None }
857852
| Borrow arg ->
858853
Borrow { arg = constant_expr_to_expr arg; borrow_kind = Thir.Shared }
859854
| ConstRef { id } -> ConstRef { id }
@@ -1152,9 +1147,9 @@ end) : EXPR = struct
11521147
Parent { impl = { kind = item_kind; goal = trait_ref }; ident }
11531148
in
11541149
match ie with
1155-
| Concrete { id; generics; _ } ->
1156-
let trait = Concrete_ident.of_def_id ~value:false id in
1157-
let args = List.map ~f:(c_generic_value span) generics in
1150+
| Concrete { def_id; generic_args; _ } ->
1151+
let trait = Concrete_ident.of_def_id ~value:false def_id in
1152+
let args = List.map ~f:(c_generic_value span) generic_args in
11581153
Concrete { trait; args }
11591154
| LocalBound { predicate_id; path; _ } ->
11601155
let init = LocalBound { id = predicate_id } in
@@ -1329,7 +1324,7 @@ include struct
13291324

13301325
let import_ty : Types.span -> Types.node_for__ty_kind -> Ast.Rust.ty = c_ty
13311326

1332-
let import_trait_ref : Types.span -> Types.trait_ref -> Ast.Rust.trait_goal =
1327+
let import_trait_ref : Types.span -> Thir.trait_ref -> Ast.Rust.trait_goal =
13331328
c_trait_ref
13341329

13351330
let import_clause :

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.unboxed_item_ref -> Ast.Rust.trait_goal
35

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

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 {

frontend/exporter/src/constant_utils/uneval.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,16 @@ pub fn translate_constant_reference<'tcx>(
110110
let ty = tcx
111111
.try_normalize_erasing_regions(typing_env, ty)
112112
.unwrap_or(ty);
113-
let kind = if let Some(assoc) = s.base().tcx.opt_associated_item(ucv.def) {
114-
if assoc.trait_item_def_id.is_some() || assoc.container == ty::AssocItemContainer::Trait {
115-
// This is an associated constant in a trait.
116-
let name = assoc.name().to_string();
117-
let impl_expr = self_clause_for_item(s, ucv.def, ucv.args).unwrap();
118-
ConstantExprKind::TraitConst { impl_expr, name }
119-
} else {
120-
// Constant appearing in an inherent impl block.
121-
let trait_refs = solve_item_required_traits(s, ucv.def, ucv.args);
122-
ConstantExprKind::GlobalName {
123-
id: ucv.def.sinto(s),
124-
generics: ucv.args.sinto(s),
125-
trait_refs,
126-
}
127-
}
113+
let kind = if let Some(assoc) = s.base().tcx.opt_associated_item(ucv.def)
114+
&& (assoc.trait_item_def_id.is_some() || assoc.container == ty::AssocItemContainer::Trait)
115+
{
116+
// This is an associated constant in a trait.
117+
let name = assoc.name().to_string();
118+
let impl_expr = self_clause_for_item(s, ucv.def, ucv.args).unwrap();
119+
ConstantExprKind::TraitConst { impl_expr, name }
128120
} else {
129-
// Top-level constant.
130-
ConstantExprKind::GlobalName {
131-
id: ucv.def.sinto(s),
132-
generics: ucv.args.sinto(s),
133-
trait_refs: vec![],
134-
}
121+
let item = translate_item_ref(s, ucv.def, ucv.args);
122+
ConstantExprKind::GlobalName(item)
135123
};
136124
let cv = kind.decorate(ty.sinto(s), span.sinto(s));
137125
Some(cv)
@@ -188,7 +176,7 @@ impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, ConstantExpr> for ty::Const<'tcx>
188176
ty::ConstKind::Expr(e) => fatal!(s[span], "ty::ConstKind::Expr {:#?}", e),
189177

190178
ty::ConstKind::Bound(i, bound) => {
191-
supposely_unreachable_fatal!(s[span], "ty::ConstKind::Bound"; {i, bound});
179+
supposely_unreachable_fatal!(s[span], "ty::ConstKind::Bound"; {i, bound})
192180
}
193181
_ => fatal!(s[span], "unexpected case"),
194182
}
@@ -302,11 +290,8 @@ fn op_to_const<'tcx, S: UnderOwnerState<'tcx>>(
302290
&& let (alloc_id, _, _) = ecx.ptr_get_alloc_id(ptr, 0)?
303291
&& let interpret::GlobalAlloc::Static(did) = tcx.global_alloc(alloc_id) =>
304292
{
305-
ConstantExprKind::GlobalName {
306-
id: did.sinto(s),
307-
generics: Vec::new(),
308-
trait_refs: Vec::new(),
309-
}
293+
let item = translate_item_ref(s, did, ty::GenericArgsRef::default());
294+
ConstantExprKind::GlobalName(item)
310295
}
311296
ty::Char | ty::Bool | ty::Uint(_) | ty::Int(_) | ty::Float(_) => {
312297
let scalar = ecx.read_scalar(&op)?;
@@ -380,14 +365,8 @@ fn op_to_const<'tcx, S: UnderOwnerState<'tcx>>(
380365
ConstantExprKind::Literal(ConstantLiteral::Str(str.to_owned()))
381366
}
382367
ty::FnDef(def_id, args) => {
383-
let (def_id, generics, generics_impls, method_impl) =
384-
get_function_from_def_id_and_generics(s, *def_id, args);
385-
ConstantExprKind::FnPtr {
386-
def_id,
387-
generics,
388-
generics_impls,
389-
method_impl,
390-
}
368+
let item = translate_item_ref(s, *def_id, args);
369+
ConstantExprKind::FnPtr(item)
391370
}
392371
ty::RawPtr(..) | ty::Ref(..) => {
393372
let op = ecx.deref_pointer(&op)?;

0 commit comments

Comments
 (0)