Skip to content

Commit 5a5a8cf

Browse files
committed
Change Instance::resolve* to take InferCtxt instead of TyCtxt. This will
allow param env to contain inference variables.
1 parent 95a3dd1 commit 5a5a8cf

File tree

12 files changed

+91
-98
lines changed

12 files changed

+91
-98
lines changed

src/librustc/ty/instance.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use crate::infer::InferCtxt;
12
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
23
use crate::middle::lang_items::DropInPlaceFnLangItem;
34
use crate::traits;
45
use crate::ty::print::{FmtPrinter, Printer};
5-
use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeFoldable};
6+
use crate::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt, TypeFoldable};
67
use rustc_hir::def::Namespace;
78
use rustc_hir::def_id::DefId;
89
use rustc_macros::HashStable;
@@ -206,17 +207,18 @@ impl<'tcx> Instance<'tcx> {
206207
/// Presuming that coherence and type-check have succeeded, if this method is invoked
207208
/// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
208209
/// `Some`.
209-
pub fn resolve(
210-
tcx: TyCtxt<'tcx>,
210+
pub fn resolve<'infcx>(
211+
infcx: &'infcx InferCtxt<'infcx, 'tcx>,
211212
param_env: ty::ParamEnv<'tcx>,
212213
def_id: DefId,
213214
substs: SubstsRef<'tcx>,
214215
) -> Option<Instance<'tcx>> {
216+
let tcx = infcx.tcx;
215217
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
216218
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
217219
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
218220
let item = tcx.associated_item(def_id);
219-
resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
221+
resolve_associated_item(infcx, &item, param_env, trait_def_id, substs)
220222
} else {
221223
let ty = tcx.type_of(def_id);
222224
let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, &ty);
@@ -253,16 +255,26 @@ impl<'tcx> Instance<'tcx> {
253255
result
254256
}
255257

256-
pub fn resolve_for_fn_ptr(
258+
pub fn resolve_mono(
257259
tcx: TyCtxt<'tcx>,
260+
def_id: DefId,
261+
substs: SubstsRef<'tcx>,
262+
) -> Instance<'tcx> {
263+
tcx.infer_ctxt().enter(|ref infcx| {
264+
Instance::resolve(infcx, ParamEnv::reveal_all(), def_id, substs).unwrap()
265+
})
266+
}
267+
268+
pub fn resolve_for_fn_ptr<'infcx>(
269+
infcx: &'infcx InferCtxt<'infcx, 'tcx>,
258270
param_env: ty::ParamEnv<'tcx>,
259271
def_id: DefId,
260272
substs: SubstsRef<'tcx>,
261273
) -> Option<Instance<'tcx>> {
262274
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
263-
Instance::resolve(tcx, param_env, def_id, substs).map(|mut resolved| {
275+
Instance::resolve(infcx, param_env, def_id, substs).map(|mut resolved| {
264276
match resolved.def {
265-
InstanceDef::Item(def_id) if resolved.def.requires_caller_location(tcx) => {
277+
InstanceDef::Item(def_id) if resolved.def.requires_caller_location(infcx.tcx) => {
266278
debug!(" => fn pointer created for function with #[track_caller]");
267279
resolved.def = InstanceDef::ReifyShim(def_id);
268280
}
@@ -277,13 +289,24 @@ impl<'tcx> Instance<'tcx> {
277289
})
278290
}
279291

280-
pub fn resolve_for_vtable(
292+
pub fn resolve_for_fn_ptr_mono(
281293
tcx: TyCtxt<'tcx>,
294+
def_id: DefId,
295+
substs: SubstsRef<'tcx>,
296+
) -> Instance<'tcx> {
297+
tcx.infer_ctxt().enter(|ref infcx| {
298+
Instance::resolve_for_fn_ptr(infcx, ParamEnv::reveal_all(), def_id, substs).unwrap()
299+
})
300+
}
301+
302+
pub fn resolve_for_vtable<'infcx>(
303+
infcx: &'infcx InferCtxt<'infcx, 'tcx>,
282304
param_env: ty::ParamEnv<'tcx>,
283305
def_id: DefId,
284306
substs: SubstsRef<'tcx>,
285307
) -> Option<Instance<'tcx>> {
286308
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
309+
let tcx = infcx.tcx;
287310
let fn_sig = tcx.fn_sig(def_id);
288311
let is_vtable_shim = fn_sig.inputs().skip_binder().len() > 0
289312
&& fn_sig.input(0).skip_binder().is_param(0)
@@ -292,10 +315,20 @@ impl<'tcx> Instance<'tcx> {
292315
debug!(" => associated item with unsizeable self: Self");
293316
Some(Instance { def: InstanceDef::VtableShim(def_id), substs })
294317
} else {
295-
Instance::resolve(tcx, param_env, def_id, substs)
318+
Instance::resolve(infcx, param_env, def_id, substs)
296319
}
297320
}
298321

322+
pub fn resolve_for_vtable_mono(
323+
tcx: TyCtxt<'tcx>,
324+
def_id: DefId,
325+
substs: SubstsRef<'tcx>,
326+
) -> Instance<'tcx> {
327+
tcx.infer_ctxt().enter(|ref infcx| {
328+
Instance::resolve_for_vtable(infcx, ParamEnv::reveal_all(), def_id, substs).unwrap()
329+
})
330+
}
331+
299332
pub fn resolve_closure(
300333
tcx: TyCtxt<'tcx>,
301334
def_id: DefId,
@@ -313,7 +346,7 @@ impl<'tcx> Instance<'tcx> {
313346
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
314347
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem, None);
315348
let substs = tcx.intern_substs(&[ty.into()]);
316-
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
349+
Instance::resolve_mono(tcx, def_id, substs)
317350
}
318351

319352
pub fn fn_once_adapter_instance(
@@ -346,13 +379,14 @@ impl<'tcx> Instance<'tcx> {
346379
}
347380
}
348381

349-
fn resolve_associated_item<'tcx>(
350-
tcx: TyCtxt<'tcx>,
382+
fn resolve_associated_item<'infcx, 'tcx>(
383+
infcx: &'infcx InferCtxt<'infcx, 'tcx>,
351384
trait_item: &ty::AssocItem,
352385
param_env: ty::ParamEnv<'tcx>,
353386
trait_id: DefId,
354387
rcvr_substs: SubstsRef<'tcx>,
355388
) -> Option<Instance<'tcx>> {
389+
let tcx = infcx.tcx;
356390
let def_id = trait_item.def_id;
357391
debug!(
358392
"resolve_associated_item(trait_item={:?}, \

src/librustc_codegen_llvm/context.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,9 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
363363
}
364364
let tcx = self.tcx;
365365
let llfn = match tcx.lang_items().eh_personality() {
366-
Some(def_id) if !wants_msvc_seh(self.sess()) => self.get_fn_addr(
367-
ty::Instance::resolve(
368-
tcx,
369-
ty::ParamEnv::reveal_all(),
370-
def_id,
371-
tcx.intern_substs(&[]),
372-
)
373-
.unwrap(),
374-
),
366+
Some(def_id) if !wants_msvc_seh(self.sess()) => {
367+
self.get_fn_addr(ty::Instance::resolve_mono(tcx, def_id, tcx.intern_substs(&[])))
368+
}
375369
_ => {
376370
let name = if wants_msvc_seh(self.sess()) {
377371
"__CxxFrameHandler3"
@@ -398,15 +392,8 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
398392
let tcx = self.tcx;
399393
assert!(self.sess().target.target.options.custom_unwind_resume);
400394
if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
401-
let llfn = self.get_fn_addr(
402-
ty::Instance::resolve(
403-
tcx,
404-
ty::ParamEnv::reveal_all(),
405-
def_id,
406-
tcx.intern_substs(&[]),
407-
)
408-
.unwrap(),
409-
);
395+
let llfn =
396+
self.get_fn_addr(ty::Instance::resolve_mono(tcx, def_id, tcx.intern_substs(&[])));
410397
unwresume.set(Some(llfn));
411398
return llfn;
412399
}

src/librustc_codegen_ssa/base.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,15 +459,11 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'
459459

460460
let (start_fn, args) = if use_start_lang_item {
461461
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem, None);
462-
let start_fn = cx.get_fn_addr(
463-
ty::Instance::resolve(
464-
cx.tcx(),
465-
ty::ParamEnv::reveal_all(),
466-
start_def_id,
467-
cx.tcx().intern_substs(&[main_ret_ty.into()]),
468-
)
469-
.unwrap(),
470-
);
462+
let start_fn = cx.get_fn_addr(ty::Instance::resolve_mono(
463+
cx.tcx(),
464+
start_def_id,
465+
cx.tcx().intern_substs(&[main_ret_ty.into()]),
466+
));
471467
(
472468
start_fn,
473469
vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())), arg_argc, arg_argv],

src/librustc_codegen_ssa/meth.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
8787

8888
let methods = methods.cloned().map(|opt_mth| {
8989
opt_mth.map_or(nullptr, |(def_id, substs)| {
90-
cx.get_fn_addr(
91-
ty::Instance::resolve_for_vtable(
92-
cx.tcx(),
93-
ty::ParamEnv::reveal_all(),
94-
def_id,
95-
substs,
96-
)
97-
.unwrap(),
98-
)
90+
cx.get_fn_addr(ty::Instance::resolve_for_vtable_mono(cx.tcx(), def_id, substs))
9991
})
10092
});
10193

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
449449
let callee = self.codegen_operand(&mut bx, func);
450450

451451
let (instance, mut llfn) = match callee.layout.ty.kind {
452-
ty::FnDef(def_id, substs) => (
453-
Some(
454-
ty::Instance::resolve(bx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs)
455-
.unwrap(),
456-
),
457-
None,
458-
),
452+
ty::FnDef(def_id, substs) => {
453+
(Some(ty::Instance::resolve_mono(bx.tcx(), def_id, substs)), None)
454+
}
459455
ty::FnPtr(_) => (None, Some(callee.immediate())),
460456
_ => bug!("{} is not callable", callee.layout.ty),
461457
};

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
188188
if bx.cx().tcx().has_attr(def_id, sym::rustc_args_required_const) {
189189
bug!("reifying a fn ptr that requires const arguments");
190190
}
191-
OperandValue::Immediate(
192-
bx.get_fn_addr(
193-
ty::Instance::resolve_for_fn_ptr(
194-
bx.tcx(),
195-
ty::ParamEnv::reveal_all(),
196-
def_id,
197-
substs,
198-
)
199-
.unwrap(),
200-
),
201-
)
191+
OperandValue::Immediate(bx.get_fn_addr(
192+
ty::Instance::resolve_for_fn_ptr_mono(bx.tcx(), def_id, substs),
193+
))
202194
}
203195
_ => bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
204196
}

src/librustc_mir/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M>
4646
}
4747

4848
let instance = ty::Instance::resolve_for_fn_ptr(
49-
*self.tcx,
49+
self.infcx,
5050
self.param_env,
5151
def_id,
5252
substs,

src/librustc_mir/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M>
376376
trace!("resolve: {:?}, {:#?}", def_id, substs);
377377
trace!("param_env: {:#?}", self.param_env);
378378
trace!("substs: {:#?}", substs);
379-
ty::Instance::resolve(*self.tcx, self.param_env, def_id, substs)
379+
ty::Instance::resolve(self.infcx, self.param_env, def_id, substs)
380380
.ok_or_else(|| err_inval!(TooGeneric).into())
381381
}
382382

src/librustc_mir/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M>
7878
if let Some((def_id, substs)) = *method {
7979
// resolve for vtable: insert shims where needed
8080
let instance =
81-
ty::Instance::resolve_for_vtable(*tcx, self.param_env, def_id, substs)
81+
ty::Instance::resolve_for_vtable(self.infcx, self.param_env, def_id, substs)
8282
.ok_or_else(|| err_inval!(TooGeneric))?;
8383
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
8484
// We cannot use `vtable_allic` as we are creating fn ptrs in this loop.

src/librustc_mir/lints.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,18 @@ fn check_fn_for_unconditional_recursion(
8383
let func_ty = func.ty(body, tcx);
8484

8585
if let ty::FnDef(fn_def_id, substs) = func_ty.kind {
86-
let (call_fn_id, call_substs) = if let Some(instance) =
87-
Instance::resolve(tcx, param_env, fn_def_id, substs)
88-
{
89-
(instance.def_id(), instance.substs)
90-
} else {
91-
(fn_def_id, substs)
92-
};
93-
94-
let is_self_call = call_fn_id == def_id
95-
&& &call_substs[..caller_substs.len()] == caller_substs;
86+
let is_self_call = tcx.infer_ctxt().enter(|ref infcx| {
87+
let (call_fn_id, call_substs) = if let Some(instance) =
88+
Instance::resolve(infcx, param_env, fn_def_id, substs)
89+
{
90+
(instance.def_id(), instance.substs)
91+
} else {
92+
(fn_def_id, substs)
93+
};
94+
95+
call_fn_id == def_id
96+
&& &call_substs[..caller_substs.len()] == caller_substs
97+
});
9698

9799
if is_self_call {
98100
self_call_locations.push(terminator.source_info);

0 commit comments

Comments
 (0)