Skip to content

Commit 95c3941

Browse files
authored
Merge pull request #1467 from Nadrieril/drops
Resolve Drop calls
2 parents 8882add + a10089a commit 95c3941

File tree

13 files changed

+360
-58
lines changed

13 files changed

+360
-58
lines changed

cli/driver/src/exporter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl From<ExtractionCallbacks> for hax_frontend_exporter_options::Options {
7171
fn from(opts: ExtractionCallbacks) -> hax_frontend_exporter_options::Options {
7272
hax_frontend_exporter_options::Options {
7373
inline_anon_consts: true,
74+
resolve_drop_bounds: false,
7475
}
7576
}
7677
}

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 { 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/import_thir.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ end) : EXPR = struct
11471147
| Dyn -> Dyn
11481148
| SelfImpl { path; _ } -> List.fold ~init:Self ~f:browse_path path
11491149
| Builtin { trait; _ } -> Builtin (c_trait_ref span trait.value)
1150+
| Drop _ -> failwith @@ "impl_expr_atom: Drop"
11501151
| Error str -> failwith @@ "impl_expr_atom: Error " ^ str
11511152

11521153
and c_generic_value (span : Thir.span) (ty : Thir.generic_arg) : generic_value

frontend/exporter/options/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,7 @@ pub struct Options {
101101
/// blocks or advanced constant expressions as in `[T; N+1]`), or refer to them as
102102
/// `GlobalName`s.
103103
pub inline_anon_consts: bool,
104+
/// Add `T: Drop` bounds to every type generic, so that we can build `ImplExpr`s to know what
105+
/// code is run on drop.
106+
pub resolve_drop_bounds: bool,
104107
}

frontend/exporter/src/traits.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ pub enum ImplExprAtom {
9393
/// `dyn Trait` implements `Trait` using a built-in implementation; this refers to that
9494
/// built-in implementation.
9595
Dyn,
96+
/// A virtual `Drop` implementation.
97+
/// `Drop` doesn't work like a real trait but we want to pretend it does. If a type has a
98+
/// user-defined `impl Drop for X` we just use the `Concrete` variant, but if it doesn't we use
99+
/// this variant to supply the data needed to know what code will run on drop.
100+
Drop(DropData),
96101
/// A built-in trait whose implementation is computed by the compiler, such as `FnMut`. This
97102
/// morally points to an invisible `impl` block; as such it contains the information we may
98103
/// need from one.
@@ -109,6 +114,30 @@ pub enum ImplExprAtom {
109114
Error(String),
110115
}
111116

117+
#[derive(AdtInto)]
118+
#[args(<'tcx, S: UnderOwnerState<'tcx> >, from: resolution::DropData<'tcx>, state: S as s)]
119+
#[derive_group(Serializers)]
120+
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
121+
pub enum DropData {
122+
/// A drop that does nothing, e.g. for scalars and pointers.
123+
Noop,
124+
/// An implicit `Drop` local clause, if the `resolve_drop_bounds` option is `false`. If that
125+
/// option is `true`, we'll add `Drop` bounds to every type param, and use that to resolve
126+
/// `Drop` impls of generics. If it's `false`, we use this variant to indicate that the drop
127+
/// clause comes from a generic or associated type.
128+
Implicit,
129+
/// The implicit `Drop` impl that exists for every type without an explicit `Drop` impl. The
130+
/// virtual impl is considered to have one `T: Drop` bound for each generic argument of the
131+
/// target type; it then simply drops each field in order.
132+
Glue {
133+
/// The type we're generating glue for.
134+
ty: Ty,
135+
/// The `ImplExpr`s for the `T: Drop` bounds of the virtual impl. There is one for each
136+
/// generic argument, in order.
137+
impl_exprs: Vec<ImplExpr>,
138+
},
139+
}
140+
112141
/// An `ImplExpr` describes the full data of a trait implementation. Because of generics, this may
113142
/// need to combine several concrete trait implementation items. For example, `((1u8, 2u8),
114143
/// "hello").clone()` combines the generic implementation of `Clone` for `(A, B)` with the
@@ -173,7 +202,13 @@ pub fn solve_trait<'tcx, S: BaseState<'tcx> + HasOwnerId>(
173202
let resolved = s.with_cache(|cache| {
174203
cache
175204
.predicate_searcher
176-
.get_or_insert_with(|| PredicateSearcher::new_for_owner(s.base().tcx, s.owner_id()))
205+
.get_or_insert_with(|| {
206+
PredicateSearcher::new_for_owner(
207+
s.base().tcx,
208+
s.owner_id(),
209+
s.base().options.resolve_drop_bounds,
210+
)
211+
})
177212
.resolve(&trait_ref, &warn)
178213
});
179214
let impl_expr = match resolved {
@@ -209,7 +244,7 @@ pub fn solve_item_required_traits<'tcx, S: UnderOwnerState<'tcx>>(
209244
}
210245
_ => {}
211246
}
212-
let predicates = required_predicates(tcx, def_id);
247+
let predicates = required_predicates(tcx, def_id, s.base().options.resolve_drop_bounds);
213248
impl_exprs.extend(solve_item_traits_inner(s, generics, predicates));
214249
}
215250
let mut impl_exprs = vec![];
@@ -226,7 +261,7 @@ pub fn solve_item_implied_traits<'tcx, S: UnderOwnerState<'tcx>>(
226261
def_id: RDefId,
227262
generics: ty::GenericArgsRef<'tcx>,
228263
) -> Vec<ImplExpr> {
229-
let predicates = implied_predicates(s.base().tcx, def_id);
264+
let predicates = implied_predicates(s.base().tcx, def_id, s.base().options.resolve_drop_bounds);
230265
solve_item_traits_inner(s, generics, predicates)
231266
}
232267

0 commit comments

Comments
 (0)