2020-03-29 10:19:48 -05:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2020-08-18 05:47:27 -05:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
2022-05-08 00:17:58 -05:00
|
|
|
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
|
2020-03-31 11:16:47 -05:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2019-09-26 00:38:33 -05:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2020-02-08 18:39:14 -06:00
|
|
|
use rustc_span::Span;
|
2018-04-25 11:30:39 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
use std::fmt;
|
2017-02-07 15:46:21 -06:00
|
|
|
use std::iter;
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
use crate::util::expand_aggregate;
|
2020-12-31 18:53:25 -06:00
|
|
|
use crate::{
|
2021-12-02 16:20:03 -06:00
|
|
|
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, marker, pass_manager as pm,
|
|
|
|
remove_noop_landing_pads, simplify,
|
2020-12-31 18:53:25 -06:00
|
|
|
};
|
2021-01-05 12:53:07 -06:00
|
|
|
use rustc_middle::mir::patch::MirPatch;
|
|
|
|
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
|
2017-03-09 12:36:01 -06:00
|
|
|
|
2020-07-05 15:00:14 -05:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2017-02-08 11:31:03 -06:00
|
|
|
providers.mir_shims = make_shim;
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:26:20 -05:00
|
|
|
fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'tcx> {
|
2017-03-06 04:58:51 -06:00
|
|
|
debug!("make_shim({:?})", instance);
|
2017-03-08 10:33:21 -06:00
|
|
|
|
2017-03-09 12:36:01 -06:00
|
|
|
let mut result = match instance {
|
2017-02-08 11:31:03 -06:00
|
|
|
ty::InstanceDef::Item(..) => bug!("item {:?} passed to make_shim", instance),
|
2022-07-19 18:57:44 -05:00
|
|
|
ty::InstanceDef::VTableShim(def_id) => {
|
2020-08-09 14:08:45 -05:00
|
|
|
build_call_shim(tcx, instance, Some(Adjustment::Deref), CallKind::Direct(def_id))
|
2020-06-14 16:01:32 -05:00
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
ty::InstanceDef::FnPtrShim(def_id, ty) => {
|
|
|
|
let trait_ = tcx.trait_of_item(def_id).unwrap();
|
2020-02-11 16:21:21 -06:00
|
|
|
let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) {
|
2017-03-08 10:33:21 -06:00
|
|
|
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
|
2020-04-16 19:38:52 -05:00
|
|
|
Some(ty::ClosureKind::FnMut | ty::ClosureKind::Fn) => Adjustment::Deref,
|
2017-03-08 10:33:21 -06:00
|
|
|
None => bug!("fn pointer {:?} is not an fn", ty),
|
|
|
|
};
|
2020-08-09 14:08:45 -05:00
|
|
|
|
|
|
|
build_call_shim(tcx, instance, Some(adjustment), CallKind::Indirect(ty))
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
2019-10-09 23:02:54 -05:00
|
|
|
// We are generating a call back to our def-id, which the
|
2019-10-29 13:56:21 -05:00
|
|
|
// codegen backend knows to turn to an actual call, be it
|
|
|
|
// a virtual call, or a direct call to a function for which
|
|
|
|
// indirect calls must be codegen'd differently than direct ones
|
|
|
|
// (such as `#[track_caller]`).
|
2019-10-09 23:02:54 -05:00
|
|
|
ty::InstanceDef::ReifyShim(def_id) => {
|
2020-08-09 14:08:45 -05:00
|
|
|
build_call_shim(tcx, instance, None, CallKind::Direct(def_id))
|
2017-03-06 04:58:51 -06:00
|
|
|
}
|
Support `#[track_caller]` on closures and generators
This PR allows applying a `#[track_caller]` attribute to a
closure/generator expression. The attribute as interpreted as applying
to the compiler-generated implementation of the corresponding trait
method (`FnOnce::call_once`, `FnMut::call_mut`, `Fn::call`, or
`Generator::resume`).
This feature does not have its own feature gate - however, it requires
`#![feature(stmt_expr_attributes)]` in order to actually apply
an attribute to a closure or generator.
This is implemented in the same way as for functions - an extra
location argument is appended to the end of the ABI. For closures,
this argument is *not* part of the 'tupled' argument storing the
parameters - the final closure argument for `#[track_caller]` closures
is no longer a tuple.
For direct (monomorphized) calls, the necessary support was already
implemented - we just needeed to adjust some assertions around checking
the ABI and argument count to take closures into account.
For calls through a trait object, more work was needed.
When creating a `ReifyShim`, we need to create a shim
for the trait method (e.g. `FnOnce::call_mut`) - unlike normal
functions, closures are never invoked directly, and always go through a
trait method.
Additional handling was needed for `InstanceDef::ClosureOnceShim`. In
order to pass location information throgh a direct (monomorphized) call
to `FnOnce::call_once` on an `FnMut` closure, we need to make
`ClosureOnceShim` aware of `#[tracked_caller]`. A new field
`track_caller` is added to `ClosureOnceShim` - this is used by
`InstanceDef::requires_caller` location, allowing codegen to
pass through the extra location argument.
Since `ClosureOnceShim.track_caller` is only used by codegen,
we end up generating two identical MIR shims - one for
`track_caller == true`, and one for `track_caller == false`. However,
these two shims are used by the entire crate (i.e. it's two shims total,
not two shims per unique closure), so this shouldn't a big deal.
2021-06-27 14:01:11 -05:00
|
|
|
ty::InstanceDef::ClosureOnceShim { call_once: _, track_caller: _ } => {
|
2020-08-18 05:47:27 -05:00
|
|
|
let fn_mut = tcx.require_lang_item(LangItem::FnMut, None);
|
2019-09-25 14:36:14 -05:00
|
|
|
let call_mut = tcx
|
2017-03-08 15:19:09 -06:00
|
|
|
.associated_items(fn_mut)
|
2020-02-17 15:09:01 -06:00
|
|
|
.in_definition_order()
|
2020-03-31 21:09:50 -05:00
|
|
|
.find(|it| it.kind == ty::AssocKind::Fn)
|
2017-03-08 15:19:09 -06:00
|
|
|
.unwrap()
|
|
|
|
.def_id;
|
|
|
|
|
2020-08-09 14:08:45 -05:00
|
|
|
build_call_shim(tcx, instance, Some(Adjustment::RefMut), CallKind::Direct(call_mut))
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
2021-12-05 18:21:23 -06:00
|
|
|
|
|
|
|
ty::InstanceDef::DropGlue(def_id, ty) => {
|
|
|
|
// FIXME(#91576): Drop shims for generators aren't subject to the MIR passes at the end
|
|
|
|
// of this function. Is this intentional?
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
if let Some(ty::Generator(gen_def_id, substs, _)) = ty.map(Ty::kind) {
|
2021-12-05 18:21:23 -06:00
|
|
|
let body = tcx.optimized_mir(*gen_def_id).generator_drop().unwrap();
|
2022-05-08 00:17:58 -05:00
|
|
|
let body = EarlyBinder(body.clone()).subst(tcx, substs);
|
2021-12-05 18:21:23 -06:00
|
|
|
debug!("make_shim({:?}) = {:?}", instance, body);
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
build_drop_shim(tcx, def_id, ty)
|
|
|
|
}
|
2020-06-22 07:45:05 -05:00
|
|
|
ty::InstanceDef::CloneShim(def_id, ty) => build_clone_shim(tcx, def_id, ty),
|
2019-10-29 13:56:21 -05:00
|
|
|
ty::InstanceDef::Virtual(..) => {
|
|
|
|
bug!("InstanceDef::Virtual ({:?}) is for direct calls only", instance)
|
|
|
|
}
|
2017-03-08 15:19:09 -06:00
|
|
|
ty::InstanceDef::Intrinsic(_) => {
|
|
|
|
bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
|
|
|
|
}
|
2017-03-06 04:58:51 -06:00
|
|
|
};
|
2019-11-05 23:04:53 -06:00
|
|
|
debug!("make_shim({:?}) = untransformed {:?}", instance, result);
|
2018-11-22 10:17:45 -06:00
|
|
|
|
2021-12-02 16:20:03 -06:00
|
|
|
pm::run_passes(
|
2019-08-15 05:39:31 -05:00
|
|
|
tcx,
|
|
|
|
&mut result,
|
2021-12-02 16:20:03 -06:00
|
|
|
&[
|
2018-11-22 10:17:45 -06:00
|
|
|
&add_moves_for_packed_drops::AddMovesForPackedDrops,
|
|
|
|
&remove_noop_landing_pads::RemoveNoopLandingPads,
|
|
|
|
&simplify::SimplifyCfg::new("make_shim"),
|
|
|
|
&add_call_guards::CriticalCallEdges,
|
2021-06-08 13:23:58 -05:00
|
|
|
&abort_unwinding_calls::AbortUnwindingCalls,
|
2021-12-02 16:20:03 -06:00
|
|
|
&marker::PhaseChange(MirPhase::Const),
|
|
|
|
],
|
2018-11-22 10:17:45 -06:00
|
|
|
);
|
|
|
|
|
2019-11-05 23:04:53 -06:00
|
|
|
debug!("make_shim({:?}) = {:?}", instance, result);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2020-03-27 14:26:20 -05:00
|
|
|
result
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
enum Adjustment {
|
2020-06-14 16:44:08 -05:00
|
|
|
/// Pass the receiver as-is.
|
2017-03-08 10:33:21 -06:00
|
|
|
Identity,
|
2020-06-14 16:44:08 -05:00
|
|
|
|
|
|
|
/// We get passed `&[mut] self` and call the target with `*self`.
|
|
|
|
///
|
|
|
|
/// This either copies `self` (if `Self: Copy`, eg. for function items), or moves out of it
|
2022-07-19 18:57:44 -05:00
|
|
|
/// (for `VTableShim`, which effectively is passed `&own Self`).
|
2017-03-08 10:33:21 -06:00
|
|
|
Deref,
|
2020-06-14 16:44:08 -05:00
|
|
|
|
|
|
|
/// We get passed `self: Self` and call the target with `&mut self`.
|
|
|
|
///
|
|
|
|
/// In this case we need to ensure that the `Self` is dropped after the call, as the callee
|
|
|
|
/// won't do it for us.
|
2017-03-08 15:19:09 -06:00
|
|
|
RefMut,
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2020-06-14 16:46:06 -05:00
|
|
|
enum CallKind<'tcx> {
|
2020-06-14 16:44:08 -05:00
|
|
|
/// Call the `FnPtr` that was passed as the receiver.
|
2020-06-14 16:46:06 -05:00
|
|
|
Indirect(Ty<'tcx>),
|
2020-06-14 16:44:08 -05:00
|
|
|
|
|
|
|
/// Call a known `FnDef`.
|
2017-03-08 10:33:21 -06:00
|
|
|
Direct(DefId),
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
fn local_decls_for_sig<'tcx>(
|
|
|
|
sig: &ty::FnSig<'tcx>,
|
|
|
|
span: Span,
|
2017-02-07 15:46:21 -06:00
|
|
|
) -> IndexVec<Local, LocalDecl<'tcx>> {
|
2020-05-05 19:17:38 -05:00
|
|
|
iter::once(LocalDecl::new(sig.output(), span))
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
.chain(sig.inputs().iter().map(|ity| LocalDecl::new(*ity, span).immutable()))
|
2017-03-08 10:33:21 -06:00
|
|
|
.collect()
|
2017-02-07 15:46:21 -06:00
|
|
|
}
|
|
|
|
|
2020-04-12 12:31:00 -05:00
|
|
|
fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>) -> Body<'tcx> {
|
2017-03-13 18:08:21 -05:00
|
|
|
debug!("build_drop_shim(def_id={:?}, ty={:?})", def_id, ty);
|
|
|
|
|
2021-12-05 18:21:23 -06:00
|
|
|
assert!(!matches!(ty, Some(ty) if ty.is_generator()));
|
2016-12-26 07:34:03 -06:00
|
|
|
|
2017-03-13 18:08:21 -05:00
|
|
|
let substs = if let Some(ty) = ty {
|
2018-05-16 02:43:24 -05:00
|
|
|
tcx.intern_substs(&[ty.into()])
|
2017-03-13 18:08:21 -05:00
|
|
|
} else {
|
2019-02-25 19:30:34 -06:00
|
|
|
InternalSubsts::identity_for_item(tcx, def_id)
|
2017-03-13 18:08:21 -05:00
|
|
|
};
|
2022-05-08 14:43:18 -05:00
|
|
|
let sig = tcx.bound_fn_sig(def_id).subst(tcx, substs);
|
2020-10-23 19:21:18 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(sig);
|
2017-03-13 18:08:21 -05:00
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
|
2020-05-05 19:30:11 -05:00
|
|
|
let source_info = SourceInfo::outermost(span);
|
2017-03-13 18:08:21 -05:00
|
|
|
|
|
|
|
let return_block = BasicBlock::new(1);
|
2018-11-07 08:38:06 -06:00
|
|
|
let mut blocks = IndexVec::with_capacity(2);
|
2017-03-13 18:08:21 -05:00
|
|
|
let block = |blocks: &mut IndexVec<_, _>, kind| {
|
|
|
|
blocks.push(BasicBlockData {
|
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
|
|
|
is_cleanup: false,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
block(&mut blocks, TerminatorKind::Goto { target: return_block });
|
|
|
|
block(&mut blocks, TerminatorKind::Return);
|
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
let source = MirSource::from_instance(ty::InstanceDef::DropGlue(def_id, ty));
|
|
|
|
let mut body =
|
2022-01-11 21:19:52 -06:00
|
|
|
new_body(source, blocks, local_decls_for_sig(&sig, span), sig.inputs().len(), span);
|
2019-10-25 09:01:08 -05:00
|
|
|
|
2021-01-17 18:06:12 -06:00
|
|
|
if ty.is_some() {
|
2018-11-07 08:36:36 -06:00
|
|
|
// The first argument (index 0), but add 1 for the return value.
|
2019-06-24 10:46:09 -05:00
|
|
|
let dropee_ptr = Place::from(Local::new(1 + 0));
|
2022-07-06 07:44:47 -05:00
|
|
|
if tcx.sess.opts.unstable_opts.mir_emit_retag {
|
2018-12-11 12:54:38 -06:00
|
|
|
// Function arguments should be retagged, and we make this one raw.
|
2019-11-06 11:00:46 -06:00
|
|
|
body.basic_blocks_mut()[START_BLOCK].statements.insert(
|
|
|
|
0,
|
|
|
|
Statement {
|
2018-11-22 06:43:05 -06:00
|
|
|
source_info,
|
2021-08-04 22:36:38 -05:00
|
|
|
kind: StatementKind::Retag(RetagKind::Raw, Box::new(dropee_ptr)),
|
2018-11-22 06:43:05 -06:00
|
|
|
},
|
|
|
|
);
|
2018-11-07 05:21:28 -06:00
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
let patch = {
|
2020-04-10 23:50:02 -05:00
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(def_id);
|
2017-03-13 18:08:21 -05:00
|
|
|
let mut elaborator =
|
2019-11-06 11:00:46 -06:00
|
|
|
DropShimElaborator { body: &body, patch: MirPatch::new(&body), tcx, param_env };
|
2019-10-20 20:04:59 -05:00
|
|
|
let dropee = tcx.mk_place_deref(dropee_ptr);
|
2017-03-13 18:08:21 -05:00
|
|
|
let resume_block = elaborator.patch.resume_block();
|
|
|
|
elaborate_drops::elaborate_drop(
|
|
|
|
&mut elaborator,
|
|
|
|
source_info,
|
2020-03-30 18:25:43 -05:00
|
|
|
dropee,
|
2017-03-13 18:08:21 -05:00
|
|
|
(),
|
|
|
|
return_block,
|
2017-05-12 07:00:36 -05:00
|
|
|
elaborate_drops::Unwind::To(resume_block),
|
2017-03-13 18:08:21 -05:00
|
|
|
START_BLOCK,
|
|
|
|
);
|
|
|
|
elaborator.patch
|
|
|
|
};
|
2019-11-06 11:00:46 -06:00
|
|
|
patch.apply(&mut body);
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
body
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-11-26 04:46:49 -06:00
|
|
|
fn new_body<'tcx>(
|
2020-10-04 13:01:38 -05:00
|
|
|
source: MirSource<'tcx>,
|
2019-11-26 04:46:49 -06:00
|
|
|
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
|
|
|
arg_count: usize,
|
|
|
|
span: Span,
|
2019-11-25 19:31:27 -06:00
|
|
|
) -> Body<'tcx> {
|
|
|
|
Body::new(
|
2020-10-04 13:01:38 -05:00
|
|
|
source,
|
2019-11-25 19:31:27 -06:00
|
|
|
basic_blocks,
|
2019-11-26 11:55:03 -06:00
|
|
|
IndexVec::from_elem_n(
|
2020-02-08 13:31:09 -06:00
|
|
|
SourceScopeData {
|
|
|
|
span,
|
|
|
|
parent_scope: None,
|
|
|
|
inlined: None,
|
2020-09-20 22:52:37 -05:00
|
|
|
inlined_parent_scope: None,
|
2020-02-08 13:31:09 -06:00
|
|
|
local_data: ClearCrossCrate::Clear,
|
|
|
|
},
|
2019-11-26 14:17:35 -06:00
|
|
|
1,
|
2019-11-26 11:55:03 -06:00
|
|
|
),
|
2019-11-25 19:31:27 -06:00
|
|
|
local_decls,
|
|
|
|
IndexVec::new(),
|
|
|
|
arg_count,
|
|
|
|
vec![],
|
|
|
|
span,
|
|
|
|
None,
|
2022-02-08 00:00:15 -06:00
|
|
|
// FIXME(compiler-errors): is this correct?
|
|
|
|
None,
|
2019-11-25 19:31:27 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
pub struct DropShimElaborator<'a, 'tcx> {
|
2019-06-03 17:26:48 -05:00
|
|
|
pub body: &'a Body<'tcx>,
|
2016-12-26 07:34:03 -06:00
|
|
|
pub patch: MirPatch<'tcx>,
|
2019-06-13 16:48:52 -05:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2016-12-26 07:34:03 -06:00
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl fmt::Debug for DropShimElaborator<'_, '_> {
|
2019-02-07 15:28:15 -06:00
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
2017-03-13 18:08:21 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
|
|
|
|
type Path = ();
|
|
|
|
|
|
|
|
fn patch(&mut self) -> &mut MirPatch<'tcx> {
|
|
|
|
&mut self.patch
|
|
|
|
}
|
2019-06-03 17:26:48 -05:00
|
|
|
fn body(&self) -> &'a Body<'tcx> {
|
|
|
|
self.body
|
|
|
|
}
|
2019-06-13 16:48:52 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2019-06-11 16:11:55 -05:00
|
|
|
self.tcx
|
|
|
|
}
|
2017-05-15 16:57:30 -05:00
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
|
|
|
self.param_env
|
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
|
|
|
|
fn drop_style(&self, _path: Self::Path, mode: DropFlagMode) -> DropStyle {
|
2020-05-16 15:10:05 -05:00
|
|
|
match mode {
|
|
|
|
DropFlagMode::Shallow => {
|
|
|
|
// Drops for the contained fields are "shallow" and "static" - they will simply call
|
|
|
|
// the field's own drop glue.
|
|
|
|
DropStyle::Static
|
|
|
|
}
|
|
|
|
DropFlagMode::Deep => {
|
|
|
|
// The top-level drop is "deep" and "open" - it will be elaborated to a drop ladder
|
|
|
|
// dropping each field contained in the value.
|
|
|
|
DropStyle::Open
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_drop_flag(&mut self, _location: Location, _path: Self::Path, _mode: DropFlagMode) {}
|
|
|
|
|
|
|
|
fn field_subpath(&self, _path: Self::Path, _field: Field) -> Option<Self::Path> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
fn deref_subpath(&self, _path: Self::Path) -> Option<Self::Path> {
|
|
|
|
None
|
|
|
|
}
|
2018-11-01 13:03:38 -05:00
|
|
|
fn downcast_subpath(&self, _path: Self::Path, _variant: VariantIdx) -> Option<Self::Path> {
|
2017-03-13 18:08:21 -05:00
|
|
|
Some(())
|
|
|
|
}
|
2020-08-23 07:54:58 -05:00
|
|
|
fn array_subpath(&self, _path: Self::Path, _index: u64, _size: u64) -> Option<Self::Path> {
|
2017-11-28 06:48:23 -06:00
|
|
|
None
|
|
|
|
}
|
2017-03-13 18:08:21 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Builds a `Clone::clone` shim for `self_ty`. Here, `def_id` is `Clone::clone`.
|
2020-04-12 12:31:00 -05:00
|
|
|
fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Body<'tcx> {
|
2017-08-04 07:44:12 -05:00
|
|
|
debug!("build_clone_shim(def_id={:?})", def_id);
|
|
|
|
|
2019-03-25 18:13:09 -05:00
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
|
2018-01-30 03:17:25 -06:00
|
|
|
let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty);
|
2020-06-21 04:20:48 -05:00
|
|
|
let is_copy = self_ty.is_copy_modulo_regions(tcx.at(builder.span), param_env);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
2019-07-29 17:07:28 -05:00
|
|
|
let dest = Place::return_place();
|
2019-10-20 20:04:59 -05:00
|
|
|
let src = tcx.mk_place_deref(Place::from(Local::new(1 + 0)));
|
2018-01-30 06:06:19 -06:00
|
|
|
|
2020-08-02 17:49:11 -05:00
|
|
|
match self_ty.kind() {
|
2017-08-09 06:55:27 -05:00
|
|
|
_ if is_copy => builder.copy_shim(),
|
2020-03-12 20:23:38 -05:00
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
builder.tuple_like_shim(dest, src, substs.as_closure().upvar_tys())
|
2017-09-13 15:40:48 -05:00
|
|
|
}
|
2019-08-07 02:01:53 -05:00
|
|
|
ty::Tuple(..) => builder.tuple_like_shim(dest, src, self_ty.tuple_fields()),
|
2017-09-13 15:40:48 -05:00
|
|
|
_ => bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty),
|
2017-08-09 06:55:27 -05:00
|
|
|
};
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2020-04-12 12:31:00 -05:00
|
|
|
builder.into_mir()
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2017-08-08 10:13:12 -05:00
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct CloneShimBuilder<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-08-09 06:55:27 -05:00
|
|
|
def_id: DefId,
|
|
|
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
|
|
|
blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
|
|
|
|
span: Span,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> CloneShimBuilder<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
fn new(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -> Self {
|
2018-01-30 03:17:25 -06:00
|
|
|
// we must subst the self_ty because it's
|
|
|
|
// otherwise going to be TySelf and we can't index
|
|
|
|
// or access fields of a Place of type TySelf.
|
|
|
|
let substs = tcx.mk_substs_trait(self_ty, &[]);
|
2022-05-08 14:43:18 -05:00
|
|
|
let sig = tcx.bound_fn_sig(def_id).subst(tcx, substs);
|
2020-10-23 19:21:18 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(sig);
|
2017-08-09 06:55:27 -05:00
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
|
|
|
|
CloneShimBuilder {
|
|
|
|
tcx,
|
|
|
|
def_id,
|
|
|
|
local_decls: local_decls_for_sig(&sig, span),
|
|
|
|
blocks: IndexVec::new(),
|
|
|
|
span,
|
|
|
|
sig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 16:55:04 -05:00
|
|
|
fn into_mir(self) -> Body<'tcx> {
|
2020-10-04 13:01:38 -05:00
|
|
|
let source = MirSource::from_instance(ty::InstanceDef::CloneShim(
|
|
|
|
self.def_id,
|
|
|
|
self.sig.inputs_and_output[0],
|
|
|
|
));
|
2022-01-11 21:19:52 -06:00
|
|
|
new_body(source, self.blocks, self.local_decls, self.sig.inputs().len(), self.span)
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn source_info(&self) -> SourceInfo {
|
2020-05-05 19:30:11 -05:00
|
|
|
SourceInfo::outermost(self.span)
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn block(
|
|
|
|
&mut self,
|
|
|
|
statements: Vec<Statement<'tcx>>,
|
|
|
|
kind: TerminatorKind<'tcx>,
|
|
|
|
is_cleanup: bool,
|
|
|
|
) -> BasicBlock {
|
|
|
|
let source_info = self.source_info();
|
|
|
|
self.blocks.push(BasicBlockData {
|
2017-08-08 10:13:12 -05:00
|
|
|
statements,
|
2017-08-04 07:44:12 -05:00
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
2017-08-08 10:13:12 -05:00
|
|
|
is_cleanup,
|
2017-08-04 07:44:12 -05:00
|
|
|
})
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
/// Gives the index of an upcoming BasicBlock, with an offset.
|
|
|
|
/// offset=0 will give you the index of the next BasicBlock,
|
|
|
|
/// offset=1 will give the index of the next-to-next block,
|
|
|
|
/// offset=-1 will give you the index of the last-created block
|
|
|
|
fn block_index_offset(&mut self, offset: usize) -> BasicBlock {
|
|
|
|
BasicBlock::new(self.blocks.len() + offset)
|
|
|
|
}
|
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
fn make_statement(&self, kind: StatementKind<'tcx>) -> Statement<'tcx> {
|
|
|
|
Statement { source_info: self.source_info(), kind }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_shim(&mut self) {
|
2019-10-20 20:04:59 -05:00
|
|
|
let rcvr = self.tcx.mk_place_deref(Place::from(Local::new(1 + 0)));
|
2021-08-04 22:36:38 -05:00
|
|
|
let ret_statement = self.make_statement(StatementKind::Assign(Box::new((
|
2019-09-11 14:05:45 -05:00
|
|
|
Place::return_place(),
|
|
|
|
Rvalue::Use(Operand::Copy(rcvr)),
|
2021-08-04 22:36:38 -05:00
|
|
|
))));
|
2017-08-09 06:55:27 -05:00
|
|
|
self.block(vec![ret_statement], TerminatorKind::Return, false);
|
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
fn make_place(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Place<'tcx> {
|
2017-08-09 06:55:27 -05:00
|
|
|
let span = self.span;
|
2020-05-05 19:17:38 -05:00
|
|
|
let mut local = LocalDecl::new(ty, span);
|
|
|
|
if mutability == Mutability::Not {
|
|
|
|
local = local.immutable();
|
|
|
|
}
|
|
|
|
Place::from(self.local_decls.push(local))
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_clone_call(
|
|
|
|
&mut self,
|
2018-01-30 05:54:56 -06:00
|
|
|
dest: Place<'tcx>,
|
|
|
|
src: Place<'tcx>,
|
2017-09-14 20:44:23 -05:00
|
|
|
ty: Ty<'tcx>,
|
2017-08-09 06:55:27 -05:00
|
|
|
next: BasicBlock,
|
2018-01-30 05:54:56 -06:00
|
|
|
cleanup: BasicBlock,
|
2018-01-29 23:08:54 -06:00
|
|
|
) {
|
2017-08-09 06:55:27 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
|
2019-02-19 19:19:13 -06:00
|
|
|
let substs = tcx.mk_substs_trait(ty, &[]);
|
2017-08-04 07:44:12 -05:00
|
|
|
|
|
|
|
// `func == Clone::clone(&ty) -> ty`
|
2017-08-04 03:25:13 -05:00
|
|
|
let func_ty = tcx.mk_fn_def(self.def_id, substs);
|
2021-08-04 22:36:38 -05:00
|
|
|
let func = Operand::Constant(Box::new(Constant {
|
2017-08-09 06:55:27 -05:00
|
|
|
span: self.span,
|
2018-08-09 05:18:00 -05:00
|
|
|
user_ty: None,
|
2022-02-16 03:56:01 -06:00
|
|
|
literal: ConstantKind::zero_sized(func_ty),
|
2021-08-04 22:36:38 -05:00
|
|
|
}));
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
let ref_loc = self.make_place(
|
2017-08-08 10:13:12 -05:00
|
|
|
Mutability::Not,
|
2019-04-25 16:05:04 -05:00
|
|
|
tcx.mk_ref(tcx.lifetimes.re_erased, ty::TypeAndMut { ty, mutbl: hir::Mutability::Not }),
|
2017-08-04 07:44:12 -05:00
|
|
|
);
|
|
|
|
|
2018-01-30 05:54:56 -06:00
|
|
|
// `let ref_loc: &ty = &src;`
|
2021-08-04 22:36:38 -05:00
|
|
|
let statement = self.make_statement(StatementKind::Assign(Box::new((
|
2020-01-22 09:30:15 -06:00
|
|
|
ref_loc,
|
2019-09-11 14:05:45 -05:00
|
|
|
Rvalue::Ref(tcx.lifetimes.re_erased, BorrowKind::Shared, src),
|
2021-08-04 22:36:38 -05:00
|
|
|
))));
|
2017-08-04 07:44:12 -05:00
|
|
|
|
|
|
|
// `let loc = Clone::clone(ref_loc);`
|
2017-08-09 06:55:27 -05:00
|
|
|
self.block(
|
|
|
|
vec![statement],
|
|
|
|
TerminatorKind::Call {
|
2017-08-04 07:44:12 -05:00
|
|
|
func,
|
2017-11-17 09:19:57 -06:00
|
|
|
args: vec![Operand::Move(ref_loc)],
|
2022-04-16 08:27:54 -05:00
|
|
|
destination: dest,
|
|
|
|
target: Some(next),
|
2017-08-08 10:13:12 -05:00
|
|
|
cleanup: Some(cleanup),
|
2018-09-29 04:34:12 -05:00
|
|
|
from_hir_call: true,
|
2020-06-09 14:34:23 -05:00
|
|
|
fn_span: self.span,
|
2017-08-08 10:13:12 -05:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>, src: Place<'tcx>, tys: I)
|
2019-04-26 07:26:49 -05:00
|
|
|
where
|
2022-02-07 09:06:31 -06:00
|
|
|
I: IntoIterator<Item = Ty<'tcx>>,
|
2019-04-26 07:26:49 -05:00
|
|
|
{
|
2018-01-30 06:06:19 -06:00
|
|
|
let mut previous_field = None;
|
2022-02-07 09:06:31 -06:00
|
|
|
for (i, ity) in tys.into_iter().enumerate() {
|
2018-01-29 23:21:48 -06:00
|
|
|
let field = Field::new(i);
|
2020-04-15 17:00:22 -05:00
|
|
|
let src_field = self.tcx.mk_place_field(src, field, ity);
|
2018-01-30 06:06:19 -06:00
|
|
|
|
2020-04-15 17:00:22 -05:00
|
|
|
let dest_field = self.tcx.mk_place_field(dest, field, ity);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
// #(2i + 1) is the cleanup block for the previous clone operation
|
|
|
|
let cleanup_block = self.block_index_offset(1);
|
|
|
|
// #(2i + 2) is the next cloning block
|
|
|
|
// (or the Return terminator if this is the last block)
|
|
|
|
let next_block = self.block_index_offset(2);
|
2018-01-29 23:08:54 -06:00
|
|
|
|
2017-08-09 06:55:27 -05:00
|
|
|
// BB #(2i)
|
2018-01-30 06:06:19 -06:00
|
|
|
// `dest.i = Clone::clone(&src.i);`
|
2017-08-09 06:55:27 -05:00
|
|
|
// Goto #(2i + 2) if ok, #(2i + 1) if unwinding happens.
|
2020-04-15 17:00:22 -05:00
|
|
|
self.make_clone_call(dest_field, src_field, ity, next_block, cleanup_block);
|
2017-08-09 06:55:27 -05:00
|
|
|
|
|
|
|
// BB #(2i + 1) (cleanup)
|
2018-01-30 06:06:19 -06:00
|
|
|
if let Some((previous_field, previous_cleanup)) = previous_field.take() {
|
2017-08-09 06:55:27 -05:00
|
|
|
// Drop previous field and goto previous cleanup block.
|
|
|
|
self.block(
|
|
|
|
vec![],
|
|
|
|
TerminatorKind::Drop {
|
2020-06-10 02:56:54 -05:00
|
|
|
place: previous_field,
|
2018-01-30 06:06:19 -06:00
|
|
|
target: previous_cleanup,
|
2017-08-09 06:55:27 -05:00
|
|
|
unwind: None,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
2018-01-29 23:21:48 -06:00
|
|
|
} else {
|
|
|
|
// Nothing to drop, just resume.
|
|
|
|
self.block(vec![], TerminatorKind::Resume, true);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2018-01-29 23:21:48 -06:00
|
|
|
|
2018-01-30 06:06:19 -06:00
|
|
|
previous_field = Some((dest_field, cleanup_block));
|
2017-08-04 07:44:12 -05:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:21:48 -06:00
|
|
|
self.block(vec![], TerminatorKind::Return, false);
|
2017-08-09 06:55:27 -05:00
|
|
|
}
|
2017-08-04 07:44:12 -05:00
|
|
|
}
|
|
|
|
|
2020-08-09 14:08:45 -05:00
|
|
|
/// Builds a "call" shim for `instance`. The shim calls the function specified by `call_kind`,
|
|
|
|
/// first adjusting its first argument according to `rcvr_adjustment`.
|
2019-06-11 16:11:55 -05:00
|
|
|
fn build_call_shim<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-12-03 04:54:47 -06:00
|
|
|
instance: ty::InstanceDef<'tcx>,
|
2020-01-19 13:21:48 -06:00
|
|
|
rcvr_adjustment: Option<Adjustment>,
|
2020-06-14 16:46:06 -05:00
|
|
|
call_kind: CallKind<'tcx>,
|
2020-04-12 12:31:00 -05:00
|
|
|
) -> Body<'tcx> {
|
2019-12-03 04:54:47 -06:00
|
|
|
debug!(
|
2020-08-09 14:08:45 -05:00
|
|
|
"build_call_shim(instance={:?}, rcvr_adjustment={:?}, call_kind={:?})",
|
|
|
|
instance, rcvr_adjustment, call_kind
|
2019-12-03 04:54:47 -06:00
|
|
|
);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2020-08-09 14:08:45 -05:00
|
|
|
// `FnPtrShim` contains the fn pointer type that a call shim is being built for - this is used
|
|
|
|
// to substitute into the signature of the shim. It is not necessary for users of this
|
|
|
|
// MIR body to perform further substitutions (see `InstanceDef::has_polymorphic_mir_body`).
|
|
|
|
let (sig_substs, untuple_args) = if let ty::InstanceDef::FnPtrShim(_, ty) = instance {
|
2020-10-23 19:21:18 -05:00
|
|
|
let sig = tcx.erase_late_bound_regions(ty.fn_sig(tcx));
|
2020-08-09 14:08:45 -05:00
|
|
|
|
|
|
|
let untuple_args = sig.inputs();
|
|
|
|
|
|
|
|
// Create substitutions for the `Self` and `Args` generic parameters of the shim body.
|
|
|
|
let arg_tup = tcx.mk_tup(untuple_args.iter());
|
|
|
|
let sig_substs = tcx.mk_substs_trait(ty, &[ty::subst::GenericArg::from(arg_tup)]);
|
|
|
|
|
|
|
|
(Some(sig_substs), Some(untuple_args))
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
|
|
|
|
2019-12-03 04:54:47 -06:00
|
|
|
let def_id = instance.def_id();
|
2022-07-07 20:43:24 -05:00
|
|
|
let sig = tcx.bound_fn_sig(def_id);
|
|
|
|
let sig = sig.map_bound(|sig| tcx.erase_late_bound_regions(sig));
|
2019-12-03 04:54:47 -06:00
|
|
|
|
2020-08-09 14:08:45 -05:00
|
|
|
assert_eq!(sig_substs.is_some(), !instance.has_polymorphic_mir_body());
|
2022-07-07 20:43:24 -05:00
|
|
|
let mut sig =
|
|
|
|
if let Some(sig_substs) = sig_substs { sig.subst(tcx, sig_substs) } else { sig.0 };
|
2020-08-09 14:08:45 -05:00
|
|
|
|
2020-06-14 16:46:06 -05:00
|
|
|
if let CallKind::Indirect(fnty) = call_kind {
|
|
|
|
// `sig` determines our local decls, and thus the callee type in the `Call` terminator. This
|
|
|
|
// can only be an `FnDef` or `FnPtr`, but currently will be `Self` since the types come from
|
|
|
|
// the implemented `FnX` trait.
|
|
|
|
|
|
|
|
// Apply the opposite adjustment to the MIR input.
|
|
|
|
let mut inputs_and_output = sig.inputs_and_output.to_vec();
|
|
|
|
|
|
|
|
// Initial signature is `fn(&? Self, Args) -> Self::Output` where `Args` is a tuple of the
|
|
|
|
// fn arguments. `Self` may be passed via (im)mutable reference or by-value.
|
|
|
|
assert_eq!(inputs_and_output.len(), 3);
|
|
|
|
|
|
|
|
// `Self` is always the original fn type `ty`. The MIR call terminator is only defined for
|
|
|
|
// `FnDef` and `FnPtr` callees, not the `Self` type param.
|
|
|
|
let self_arg = &mut inputs_and_output[0];
|
|
|
|
*self_arg = match rcvr_adjustment.unwrap() {
|
|
|
|
Adjustment::Identity => fnty,
|
|
|
|
Adjustment::Deref => tcx.mk_imm_ptr(fnty),
|
|
|
|
Adjustment::RefMut => tcx.mk_mut_ptr(fnty),
|
|
|
|
};
|
|
|
|
sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
|
|
|
|
}
|
|
|
|
|
2019-12-03 04:54:47 -06:00
|
|
|
// FIXME(eddyb) avoid having this snippet both here and in
|
|
|
|
// `Instance::fn_sig` (introduce `InstanceDef::fn_sig`?).
|
2022-07-19 18:57:44 -05:00
|
|
|
if let ty::InstanceDef::VTableShim(..) = instance {
|
2019-12-03 04:54:47 -06:00
|
|
|
// Modify fn(self, ...) to fn(self: *mut Self, ...)
|
|
|
|
let mut inputs_and_output = sig.inputs_and_output.to_vec();
|
|
|
|
let self_arg = &mut inputs_and_output[0];
|
|
|
|
debug_assert!(tcx.generics_of(def_id).has_self && *self_arg == tcx.types.self_param);
|
|
|
|
*self_arg = tcx.mk_mut_ptr(*self_arg);
|
|
|
|
sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
|
|
|
|
}
|
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
let span = tcx.def_span(def_id);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
debug!("build_call_shim: sig={:?}", sig);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
let mut local_decls = local_decls_for_sig(&sig, span);
|
2020-05-05 19:30:11 -05:00
|
|
|
let source_info = SourceInfo::outermost(span);
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2020-01-19 13:21:48 -06:00
|
|
|
let rcvr_place = || {
|
|
|
|
assert!(rcvr_adjustment.is_some());
|
|
|
|
Place::from(Local::new(1 + 0))
|
|
|
|
};
|
2017-03-08 15:19:09 -06:00
|
|
|
let mut statements = vec![];
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2020-01-19 13:21:48 -06:00
|
|
|
let rcvr = rcvr_adjustment.map(|rcvr_adjustment| match rcvr_adjustment {
|
|
|
|
Adjustment::Identity => Operand::Move(rcvr_place()),
|
2020-06-14 16:01:32 -05:00
|
|
|
Adjustment::Deref => Operand::Move(tcx.mk_place_deref(rcvr_place())),
|
2017-03-08 15:19:09 -06:00
|
|
|
Adjustment::RefMut => {
|
|
|
|
// let rcvr = &mut rcvr;
|
2020-05-05 19:17:38 -05:00
|
|
|
let ref_rcvr = local_decls.push(
|
|
|
|
LocalDecl::new(
|
|
|
|
tcx.mk_ref(
|
|
|
|
tcx.lifetimes.re_erased,
|
|
|
|
ty::TypeAndMut { ty: sig.inputs()[0], mutbl: hir::Mutability::Mut },
|
|
|
|
),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
.immutable(),
|
|
|
|
);
|
2018-01-15 05:47:26 -06:00
|
|
|
let borrow_kind = BorrowKind::Mut { allow_two_phase_borrow: false };
|
2017-03-08 15:19:09 -06:00
|
|
|
statements.push(Statement {
|
2017-08-07 00:54:09 -05:00
|
|
|
source_info,
|
2021-08-04 22:36:38 -05:00
|
|
|
kind: StatementKind::Assign(Box::new((
|
2019-09-11 14:05:45 -05:00
|
|
|
Place::from(ref_rcvr),
|
2020-01-19 13:21:48 -06:00
|
|
|
Rvalue::Ref(tcx.lifetimes.re_erased, borrow_kind, rcvr_place()),
|
2021-08-04 22:36:38 -05:00
|
|
|
))),
|
2017-03-08 15:19:09 -06:00
|
|
|
});
|
2019-06-24 10:46:09 -05:00
|
|
|
Operand::Move(Place::from(ref_rcvr))
|
2017-03-08 15:19:09 -06:00
|
|
|
}
|
2020-01-19 13:21:48 -06:00
|
|
|
});
|
2017-03-08 10:33:21 -06:00
|
|
|
|
|
|
|
let (callee, mut args) = match call_kind {
|
2020-06-14 16:44:08 -05:00
|
|
|
// `FnPtr` call has no receiver. Args are untupled below.
|
2020-06-14 16:46:06 -05:00
|
|
|
CallKind::Indirect(_) => (rcvr.unwrap(), vec![]),
|
2020-06-14 16:44:08 -05:00
|
|
|
|
|
|
|
// `FnDef` call with optional receiver.
|
2017-08-04 03:25:13 -05:00
|
|
|
CallKind::Direct(def_id) => {
|
|
|
|
let ty = tcx.type_of(def_id);
|
|
|
|
(
|
2021-08-04 22:36:38 -05:00
|
|
|
Operand::Constant(Box::new(Constant {
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2018-08-09 05:18:00 -05:00
|
|
|
user_ty: None,
|
2022-02-16 03:56:01 -06:00
|
|
|
literal: ConstantKind::zero_sized(ty),
|
2021-08-04 22:36:38 -05:00
|
|
|
})),
|
2020-01-19 13:21:48 -06:00
|
|
|
rcvr.into_iter().collect::<Vec<_>>(),
|
2017-08-04 03:25:13 -05:00
|
|
|
)
|
|
|
|
}
|
2017-03-08 10:33:21 -06:00
|
|
|
};
|
|
|
|
|
2020-01-19 13:21:48 -06:00
|
|
|
let mut arg_range = 0..sig.inputs().len();
|
|
|
|
|
|
|
|
// Take the `self` ("receiver") argument out of the range (it's adjusted above).
|
|
|
|
if rcvr_adjustment.is_some() {
|
|
|
|
arg_range.start += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take the last argument, if we need to untuple it (handled below).
|
|
|
|
if untuple_args.is_some() {
|
|
|
|
arg_range.end -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass all of the non-special arguments directly.
|
|
|
|
args.extend(arg_range.map(|i| Operand::Move(Place::from(Local::new(1 + i)))));
|
|
|
|
|
|
|
|
// Untuple the last argument, if we have to.
|
2017-03-08 10:33:21 -06:00
|
|
|
if let Some(untuple_args) = untuple_args {
|
2020-01-19 13:21:48 -06:00
|
|
|
let tuple_arg = Local::new(1 + (sig.inputs().len() - 1));
|
2017-03-08 10:33:21 -06:00
|
|
|
args.extend(untuple_args.iter().enumerate().map(|(i, ity)| {
|
2020-01-19 13:21:48 -06:00
|
|
|
Operand::Move(tcx.mk_place_field(Place::from(tuple_arg), Field::new(i), *ity))
|
2017-03-08 10:33:21 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-01-19 13:21:48 -06:00
|
|
|
let n_blocks = if let Some(Adjustment::RefMut) = rcvr_adjustment { 5 } else { 2 };
|
2018-11-07 08:38:06 -06:00
|
|
|
let mut blocks = IndexVec::with_capacity(n_blocks);
|
2017-03-08 15:19:09 -06:00
|
|
|
let block = |blocks: &mut IndexVec<_, _>, statements, kind, is_cleanup| {
|
|
|
|
blocks.push(BasicBlockData {
|
|
|
|
statements,
|
|
|
|
terminator: Some(Terminator { source_info, kind }),
|
|
|
|
is_cleanup,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// BB #0
|
|
|
|
block(
|
|
|
|
&mut blocks,
|
|
|
|
statements,
|
|
|
|
TerminatorKind::Call {
|
|
|
|
func: callee,
|
2017-08-07 00:54:09 -05:00
|
|
|
args,
|
2022-04-16 08:27:54 -05:00
|
|
|
destination: Place::return_place(),
|
|
|
|
target: Some(BasicBlock::new(1)),
|
2020-01-19 13:21:48 -06:00
|
|
|
cleanup: if let Some(Adjustment::RefMut) = rcvr_adjustment {
|
2017-03-08 15:19:09 -06:00
|
|
|
Some(BasicBlock::new(3))
|
|
|
|
} else {
|
|
|
|
None
|
2019-12-22 16:42:04 -06:00
|
|
|
},
|
2018-09-29 04:34:12 -05:00
|
|
|
from_hir_call: true,
|
2020-06-09 14:34:23 -05:00
|
|
|
fn_span: span,
|
2018-09-29 04:34:12 -05:00
|
|
|
},
|
2017-03-08 15:19:09 -06:00
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2020-01-19 13:21:48 -06:00
|
|
|
if let Some(Adjustment::RefMut) = rcvr_adjustment {
|
2017-03-08 15:19:09 -06:00
|
|
|
// BB #1 - drop for Self
|
|
|
|
block(
|
|
|
|
&mut blocks,
|
|
|
|
vec![],
|
2020-06-10 02:56:54 -05:00
|
|
|
TerminatorKind::Drop { place: rcvr_place(), target: BasicBlock::new(2), unwind: None },
|
2017-03-08 15:19:09 -06:00
|
|
|
false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// BB #1/#2 - return
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Return, false);
|
2020-01-19 13:21:48 -06:00
|
|
|
if let Some(Adjustment::RefMut) = rcvr_adjustment {
|
2017-03-08 15:19:09 -06:00
|
|
|
// BB #3 - drop if closure panics
|
|
|
|
block(
|
|
|
|
&mut blocks,
|
|
|
|
vec![],
|
2020-06-10 02:56:54 -05:00
|
|
|
TerminatorKind::Drop { place: rcvr_place(), target: BasicBlock::new(4), unwind: None },
|
2017-03-08 15:19:09 -06:00
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
// BB #4 - resume
|
|
|
|
block(&mut blocks, vec![], TerminatorKind::Resume, true);
|
|
|
|
}
|
2017-03-06 04:58:51 -06:00
|
|
|
|
2022-01-11 21:19:52 -06:00
|
|
|
let mut body =
|
|
|
|
new_body(MirSource::from_instance(instance), blocks, local_decls, sig.inputs().len(), span);
|
2019-11-25 19:31:27 -06:00
|
|
|
|
2017-03-08 10:33:21 -06:00
|
|
|
if let Abi::RustCall = sig.abi {
|
2019-06-03 17:26:48 -05:00
|
|
|
body.spread_arg = Some(Local::new(sig.inputs().len()));
|
2017-03-08 10:33:21 -06:00
|
|
|
}
|
2020-04-12 12:31:00 -05:00
|
|
|
|
|
|
|
body
|
2017-03-06 04:58:51 -06:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:26:20 -05:00
|
|
|
pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
|
2019-05-26 03:55:50 -05:00
|
|
|
debug_assert!(tcx.is_constructor(ctor_id));
|
|
|
|
|
|
|
|
let param_env = tcx.param_env(ctor_id);
|
2017-11-20 12:08:52 -06:00
|
|
|
|
2018-03-03 07:23:28 -06:00
|
|
|
// Normalize the sig.
|
2018-10-24 15:30:34 -05:00
|
|
|
let sig = tcx.fn_sig(ctor_id).no_bound_vars().expect("LBR in ADT constructor signature");
|
2019-05-26 03:55:50 -05:00
|
|
|
let sig = tcx.normalize_erasing_regions(param_env, sig);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2022-02-18 17:48:49 -06:00
|
|
|
let ty::Adt(adt_def, substs) = sig.output().kind() else {
|
|
|
|
bug!("unexpected type for ADT ctor {:?}", sig.output());
|
2017-02-07 15:46:21 -06:00
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2022-03-29 16:50:01 -05:00
|
|
|
let span = tcx.def_span(ctor_id);
|
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
let local_decls = local_decls_for_sig(&sig, span);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2020-05-05 19:30:11 -05:00
|
|
|
let source_info = SourceInfo::outermost(span);
|
2017-02-07 15:46:21 -06:00
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
let variant_index = if adt_def.is_enum() {
|
|
|
|
adt_def.variant_index_with_ctor_id(ctor_id)
|
2017-02-07 15:46:21 -06:00
|
|
|
} else {
|
2018-11-01 13:03:38 -05:00
|
|
|
VariantIdx::new(0)
|
2017-02-07 15:46:21 -06:00
|
|
|
};
|
|
|
|
|
2019-05-26 03:55:50 -05:00
|
|
|
// Generate the following MIR:
|
|
|
|
//
|
|
|
|
// (return as Variant).field0 = arg0;
|
|
|
|
// (return as Variant).field1 = arg1;
|
|
|
|
//
|
|
|
|
// return;
|
|
|
|
debug!("build_ctor: variant_index={:?}", variant_index);
|
|
|
|
|
|
|
|
let statements = expand_aggregate(
|
2019-07-29 17:07:28 -05:00
|
|
|
Place::return_place(),
|
2022-03-04 14:28:41 -06:00
|
|
|
adt_def.variant(variant_index).fields.iter().enumerate().map(|(idx, field_def)| {
|
2019-06-24 10:46:09 -05:00
|
|
|
(Operand::Move(Place::from(Local::new(idx + 1))), field_def.ty(tcx, substs))
|
2019-05-26 03:55:50 -05:00
|
|
|
}),
|
2022-03-04 14:28:41 -06:00
|
|
|
AggregateKind::Adt(adt_def.did(), variant_index, substs, None, None),
|
2019-05-26 03:55:50 -05:00
|
|
|
source_info,
|
2019-10-20 15:11:04 -05:00
|
|
|
tcx,
|
2019-05-26 03:55:50 -05:00
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
|
2017-02-07 15:46:21 -06:00
|
|
|
let start_block = BasicBlockData {
|
2019-05-26 03:55:50 -05:00
|
|
|
statements,
|
2017-02-07 15:46:21 -06:00
|
|
|
terminator: Some(Terminator { source_info, kind: TerminatorKind::Return }),
|
|
|
|
is_cleanup: false,
|
|
|
|
};
|
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
let source = MirSource::item(ctor_id);
|
|
|
|
let body = new_body(
|
|
|
|
source,
|
|
|
|
IndexVec::from_elem_n(start_block, 1),
|
|
|
|
local_decls,
|
|
|
|
sig.inputs().len(),
|
|
|
|
span,
|
2019-05-26 03:55:50 -05:00
|
|
|
);
|
|
|
|
|
2021-01-05 12:53:07 -06:00
|
|
|
rustc_middle::mir::dump_mir(tcx, None, "mir_map", &0, &body, |_, _| Ok(()));
|
2020-10-04 13:01:38 -05:00
|
|
|
|
2020-03-27 14:26:20 -05:00
|
|
|
body
|
2017-02-07 15:46:21 -06:00
|
|
|
}
|