1
Fork 0

Instance::resolve -> Instance::try_resolve, and other nits

This commit is contained in:
Michael Goulet 2024-07-02 15:55:17 -04:00
parent 3273ccea4b
commit b1059ccda2
22 changed files with 44 additions and 27 deletions

View file

@ -3733,7 +3733,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
if tcx.is_diagnostic_item(sym::deref_method, method_did) { if tcx.is_diagnostic_item(sym::deref_method, method_did) {
let deref_target = let deref_target =
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| { tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
Instance::resolve(tcx, self.param_env, deref_target, method_args) Instance::try_resolve(tcx, self.param_env, deref_target, method_args)
.transpose() .transpose()
}); });
if let Some(Ok(instance)) = deref_target { if let Some(Ok(instance)) = deref_target {

View file

@ -948,7 +948,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
return; return;
} }
if let Ok(Some(instance)) = ty::Instance::resolve( if let Ok(Some(instance)) = ty::Instance::try_resolve(
tcx, tcx,
self.param_env, self.param_env,
*fn_did, *fn_did,

View file

@ -768,7 +768,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
is_trait = true; is_trait = true;
if let Ok(Some(instance)) = if let Ok(Some(instance)) =
Instance::resolve(tcx, param_env, callee, fn_args) Instance::try_resolve(tcx, param_env, callee, fn_args)
&& let InstanceKind::Item(def) = instance.def && let InstanceKind::Item(def) = instance.def
{ {
// Resolve a trait method call to its concrete implementation, which may be in a // Resolve a trait method call to its concrete implementation, which may be in a

View file

@ -618,7 +618,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
trace!("resolve: {:?}, {:#?}", def, args); trace!("resolve: {:?}, {:#?}", def, args);
trace!("param_env: {:#?}", self.param_env); trace!("param_env: {:#?}", self.param_env);
trace!("args: {:#?}", args); trace!("args: {:#?}", args);
match ty::Instance::resolve(*self.tcx, self.param_env, def, args) { match ty::Instance::try_resolve(*self.tcx, self.param_env, def, args) {
Ok(Some(instance)) => Ok(instance), Ok(Some(instance)) => Ok(instance),
Ok(None) => throw_inval!(TooGeneric), Ok(None) => throw_inval!(TooGeneric),

View file

@ -534,7 +534,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let tcx = self.tcx(); let tcx = self.tcx();
// Find the method being called. // Find the method being called.
let Ok(Some(instance)) = ty::Instance::resolve( let Ok(Some(instance)) = ty::Instance::try_resolve(
tcx, tcx,
ctxt.param_env, ctxt.param_env,
ctxt.assoc_item.def_id, ctxt.assoc_item.def_id,

View file

@ -88,7 +88,7 @@ declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
impl LateLintPass<'_> for QueryStability { impl LateLintPass<'_> for QueryStability {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return }; let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, args) { if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, args) {
let def_id = instance.def_id(); let def_id = instance.def_id();
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) { if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
cx.emit_span_lint( cx.emit_span_lint(
@ -393,7 +393,7 @@ impl LateLintPass<'_> for Diagnostics {
}; };
// Is the callee marked with `#[rustc_lint_diagnostics]`? // Is the callee marked with `#[rustc_lint_diagnostics]`?
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, fn_gen_args) let has_attr = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
.ok() .ok()
.flatten() .flatten()
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics)); .is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));

View file

@ -96,7 +96,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
.tcx .tcx
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_args(expr.hir_id)); .normalize_erasing_regions(cx.param_env, cx.typeck_results().node_args(expr.hir_id));
// Resolve the trait method instance. // Resolve the trait method instance.
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, cx.param_env, did, args) else { return }; let Ok(Some(i)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, did, args) else {
return;
};
// (Re)check that it implements the noop diagnostic. // (Re)check that it implements the noop diagnostic.
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return }; let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
if !matches!( if !matches!(

View file

@ -73,7 +73,7 @@ impl<'tcx> TyCtxt<'tcx> {
bug!("did not expect inference variables here"); bug!("did not expect inference variables here");
} }
match ty::Instance::resolve( match ty::Instance::try_resolve(
self, param_env, self, param_env,
// FIXME: maybe have a separate version for resolving mir::UnevaluatedConst? // FIXME: maybe have a separate version for resolving mir::UnevaluatedConst?
ct.def, ct.args, ct.def, ct.args,
@ -106,7 +106,7 @@ impl<'tcx> TyCtxt<'tcx> {
bug!("did not expect inference variables here"); bug!("did not expect inference variables here");
} }
match ty::Instance::resolve(self, param_env, ct.def, ct.args) { match ty::Instance::try_resolve(self, param_env, ct.def, ct.args) {
Ok(Some(instance)) => { Ok(Some(instance)) => {
let cid = GlobalId { instance, promoted: None }; let cid = GlobalId { instance, promoted: None };
self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| { self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| {

View file

@ -516,7 +516,7 @@ impl<'tcx> Instance<'tcx> {
/// from `Ok(None)` to avoid misleading diagnostics when an error /// from `Ok(None)` to avoid misleading diagnostics when an error
/// has already been/will be emitted, for the original cause /// has already been/will be emitted, for the original cause
#[instrument(level = "debug", skip(tcx), ret)] #[instrument(level = "debug", skip(tcx), ret)]
pub fn resolve( pub fn try_resolve(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
def_id: DefId, def_id: DefId,
@ -555,7 +555,7 @@ impl<'tcx> Instance<'tcx> {
let span_or_local_def_span = let span_or_local_def_span =
|| if span.is_dummy() && def_id.is_local() { tcx.def_span(def_id) } else { span }; || if span.is_dummy() && def_id.is_local() { tcx.def_span(def_id) } else { span };
match ty::Instance::resolve(tcx, param_env, def_id, args) { match ty::Instance::try_resolve(tcx, param_env, def_id, args) {
Ok(Some(instance)) => instance, Ok(Some(instance)) => instance,
Ok(None) => { Ok(None) => {
let type_length = type_length(args); let type_length = type_length(args);
@ -605,7 +605,7 @@ impl<'tcx> Instance<'tcx> {
// Use either `resolve_closure` or `resolve_for_vtable` // Use either `resolve_closure` or `resolve_for_vtable`
assert!(!tcx.is_closure_like(def_id), "Called `resolve_for_fn_ptr` on closure: {def_id:?}"); assert!(!tcx.is_closure_like(def_id), "Called `resolve_for_fn_ptr` on closure: {def_id:?}");
let reason = tcx.sess.is_sanitizer_kcfi_enabled().then_some(ReifyReason::FnPtr); let reason = tcx.sess.is_sanitizer_kcfi_enabled().then_some(ReifyReason::FnPtr);
Instance::resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| { Instance::try_resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| {
match resolved.def { match resolved.def {
InstanceKind::Item(def) if resolved.def.requires_caller_location(tcx) => { InstanceKind::Item(def) if resolved.def.requires_caller_location(tcx) => {
debug!(" => fn pointer created for function with #[track_caller]"); debug!(" => fn pointer created for function with #[track_caller]");
@ -738,13 +738,25 @@ impl<'tcx> Instance<'tcx> {
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
let def_id = tcx.require_lang_item(LangItem::DropInPlace, None); let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
let args = tcx.mk_args(&[ty.into()]); let args = tcx.mk_args(&[ty.into()]);
Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args, DUMMY_SP) Instance::expect_resolve(
tcx,
ty::ParamEnv::reveal_all(),
def_id,
args,
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
)
} }
pub fn resolve_async_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { pub fn resolve_async_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
let def_id = tcx.require_lang_item(LangItem::AsyncDropInPlace, None); let def_id = tcx.require_lang_item(LangItem::AsyncDropInPlace, None);
let args = tcx.mk_args(&[ty.into()]); let args = tcx.mk_args(&[ty.into()]);
Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args, DUMMY_SP) Instance::expect_resolve(
tcx,
ty::ParamEnv::reveal_all(),
def_id,
args,
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
)
} }
#[instrument(level = "debug", skip(tcx), ret)] #[instrument(level = "debug", skip(tcx), ret)]

View file

@ -98,7 +98,7 @@ pub fn call_kind<'tcx>(
Some(CallKind::Operator { self_arg, trait_id, self_ty: method_args.type_at(0) }) Some(CallKind::Operator { self_arg, trait_id, self_ty: method_args.type_at(0) })
} else if is_deref { } else if is_deref {
let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| { let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
Instance::resolve(tcx, param_env, deref_target, method_args).transpose() Instance::try_resolve(tcx, param_env, deref_target, method_args).transpose()
}); });
if let Some(Ok(instance)) = deref_target { if let Some(Ok(instance)) = deref_target {
let deref_target_ty = instance.ty(tcx, param_env); let deref_target_ty = instance.ty(tcx, param_env);

View file

@ -141,7 +141,7 @@ impl<'tcx> TerminatorClassifier<'tcx> for CallRecursion<'tcx> {
return false; return false;
}; };
let (callee, call_args) = if let Ok(Some(instance)) = let (callee, call_args) = if let Ok(Some(instance)) =
Instance::resolve(tcx, param_env, callee, normalized_args) Instance::try_resolve(tcx, param_env, callee, normalized_args)
{ {
(instance.def_id(), instance.args) (instance.def_id(), instance.args)
} else { } else {

View file

@ -558,7 +558,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let args = self let args = self
.tcx .tcx
.normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_args(id)); .normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_args(id));
let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, args) { let instance = match ty::Instance::try_resolve(self.tcx, param_env_reveal_all, def_id, args)
{
Ok(Some(i)) => i, Ok(Some(i)) => i,
Ok(None) => { Ok(None) => {
// It should be assoc consts if there's no error but we cannot resolve it. // It should be assoc consts if there's no error but we cannot resolve it.

View file

@ -389,7 +389,7 @@ impl<'tcx> Inliner<'tcx> {
// To resolve an instance its args have to be fully normalized. // To resolve an instance its args have to be fully normalized.
let args = self.tcx.try_normalize_erasing_regions(self.param_env, args).ok()?; let args = self.tcx.try_normalize_erasing_regions(self.param_env, args).ok()?;
let callee = let callee =
Instance::resolve(self.tcx, self.param_env, def_id, args).ok().flatten()?; Instance::try_resolve(self.tcx, self.param_env, def_id, args).ok().flatten()?;
if let InstanceKind::Virtual(..) | InstanceKind::Intrinsic(_) = callee.def { if let InstanceKind::Virtual(..) | InstanceKind::Intrinsic(_) = callee.def {
return None; return None;

View file

@ -53,7 +53,7 @@ pub(crate) fn mir_callgraph_reachable<'tcx>(
trace!(?caller, ?param_env, ?args, "cannot normalize, skipping"); trace!(?caller, ?param_env, ?args, "cannot normalize, skipping");
continue; continue;
}; };
let Ok(Some(callee)) = ty::Instance::resolve(tcx, param_env, callee, args) else { let Ok(Some(callee)) = ty::Instance::try_resolve(tcx, param_env, callee, args) else {
trace!(?callee, "cannot resolve, skipping"); trace!(?callee, "cannot resolve, skipping");
continue; continue;
}; };

View file

@ -61,7 +61,7 @@ fn unwrap_fn_abi<'tcx>(
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) { fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id); let param_env = tcx.param_env(item_def_id);
let args = GenericArgs::identity_for_item(tcx, item_def_id); let args = GenericArgs::identity_for_item(tcx, item_def_id);
let instance = match Instance::resolve(tcx, param_env, item_def_id.into(), args) { let instance = match Instance::try_resolve(tcx, param_env, item_def_id.into(), args) {
Ok(Some(instance)) => instance, Ok(Some(instance)) => instance,
Ok(None) => { Ok(None) => {
// Not sure what to do here, but `LayoutError::Unknown` seems reasonable? // Not sure what to do here, but `LayoutError::Unknown` seems reasonable?

View file

@ -629,7 +629,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let tcx = tables.tcx; let tcx = tables.tcx;
let def_id = def.0.internal(&mut *tables, tcx); let def_id = def.0.internal(&mut *tables, tcx);
let args_ref = args.internal(&mut *tables, tcx); let args_ref = args.internal(&mut *tables, tcx);
match Instance::resolve(tables.tcx, ParamEnv::reveal_all(), def_id, args_ref) { match Instance::try_resolve(tables.tcx, ParamEnv::reveal_all(), def_id, args_ref) {
Ok(Some(instance)) => Some(instance.stable(&mut *tables)), Ok(Some(instance)) => Some(instance.stable(&mut *tables)),
Ok(None) | Err(_) => None, Ok(None) | Err(_) => None,
} }

View file

@ -103,7 +103,7 @@ fn extract_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<
let args = cx.typeck_results().node_args(expr.hir_id); let args = cx.typeck_results().node_args(expr.hir_id);
// If we could not resolve the method, don't apply the lint // If we could not resolve the method, don't apply the lint
let Ok(Some(resolved_method)) = Instance::resolve(cx.tcx, cx.param_env, fn_def_id, args) else { let Ok(Some(resolved_method)) = Instance::try_resolve(cx.tcx, cx.param_env, fn_def_id, args) else {
return None; return None;
}; };
if is_trait_method(cx, expr, sym::Clone) && path.ident.name == sym::clone { if is_trait_method(cx, expr, sym::Clone) && path.ident.name == sym::clone {
@ -119,7 +119,7 @@ fn extract_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<
// If we could not resolve the method, don't apply the lint // If we could not resolve the method, don't apply the lint
let Ok(Some(resolved_method)) = (match kind { let Ok(Some(resolved_method)) = (match kind {
ty::FnDef(_, args) => Instance::resolve(cx.tcx, cx.param_env, fn_def_id, args), ty::FnDef(_, args) => Instance::try_resolve(cx.tcx, cx.param_env, fn_def_id, args),
_ => Ok(None), _ => Ok(None),
}) else { }) else {
return None; return None;

View file

@ -293,7 +293,7 @@ impl<'tcx> NonCopyConst<'tcx> {
ct: ty::UnevaluatedConst<'tcx>, ct: ty::UnevaluatedConst<'tcx>,
span: Span, span: Span,
) -> EvalToValTreeResult<'tcx> { ) -> EvalToValTreeResult<'tcx> {
match ty::Instance::resolve(tcx, param_env, ct.def, ct.args) { match ty::Instance::try_resolve(tcx, param_env, ct.def, ct.args) {
Ok(Some(instance)) => { Ok(Some(instance)) => {
let cid = GlobalId { let cid = GlobalId {
instance, instance,

View file

@ -375,7 +375,7 @@ pub fn create_ecx<'tcx>(
}); });
let main_ret_ty = tcx.fn_sig(entry_id).no_bound_vars().unwrap().output(); let main_ret_ty = tcx.fn_sig(entry_id).no_bound_vars().unwrap().output();
let main_ret_ty = main_ret_ty.no_bound_vars().unwrap(); let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
let start_instance = ty::Instance::resolve( let start_instance = ty::Instance::try_resolve(
tcx, tcx,
ty::ParamEnv::reveal_all(), ty::ParamEnv::reveal_all(),
start_id, start_id,

View file

@ -1,4 +1,5 @@
//@ build-fail //@ build-fail
//@ error-pattern: reached the type-length limit while instantiating
#![recursion_limit = "32"] #![recursion_limit = "32"]

View file

@ -1,4 +1,4 @@
error: reached the type-length limit while instantiating `<Filter<Filter<std::array::IntoIter<i32, 11>, {closure@$DIR/overflow-during-mono.rs:12:41: 12:44}>, ...> as Iterator>::try_fold::<..., ..., ...>` error: reached the type-length limit while instantiating `<Filter<Filter<std::array::IntoIter<i32, 11>, {closure@$DIR/overflow-during-mono.rs:13:41: 13:44}>, ...> as Iterator>::try_fold::<..., ..., ...>`
--> $SRC_DIR/core/src/iter/adapters/filter.rs:LL:COL --> $SRC_DIR/core/src/iter/adapters/filter.rs:LL:COL
| |
= help: consider adding a `#![type_length_limit="20156994"]` attribute to your crate = help: consider adding a `#![type_length_limit="20156994"]` attribute to your crate

View file

@ -1,4 +1,5 @@
//@ build-fail //@ build-fail
//@ error-pattern: reached the type-length limit while instantiating
//! This snippet causes the type length to blowup exponentially, //! This snippet causes the type length to blowup exponentially,
//! so check that we don't accidentally exceed the type length limit. //! so check that we don't accidentally exceed the type length limit.