Skip to content

Commit b1059cc

Browse files
Instance::resolve -> Instance::try_resolve, and other nits
1 parent 3273cce commit b1059cc

File tree

22 files changed

+44
-27
lines changed

22 files changed

+44
-27
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37333733
if tcx.is_diagnostic_item(sym::deref_method, method_did) {
37343734
let deref_target =
37353735
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
3736-
Instance::resolve(tcx, self.param_env, deref_target, method_args)
3736+
Instance::try_resolve(tcx, self.param_env, deref_target, method_args)
37373737
.transpose()
37383738
});
37393739
if let Some(Ok(instance)) = deref_target {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
948948
return;
949949
}
950950

951-
if let Ok(Some(instance)) = ty::Instance::resolve(
951+
if let Ok(Some(instance)) = ty::Instance::try_resolve(
952952
tcx,
953953
self.param_env,
954954
*fn_did,

compiler/rustc_const_eval/src/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
768768
is_trait = true;
769769

770770
if let Ok(Some(instance)) =
771-
Instance::resolve(tcx, param_env, callee, fn_args)
771+
Instance::try_resolve(tcx, param_env, callee, fn_args)
772772
&& let InstanceKind::Item(def) = instance.def
773773
{
774774
// Resolve a trait method call to its concrete implementation, which may be in a

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
618618
trace!("resolve: {:?}, {:#?}", def, args);
619619
trace!("param_env: {:#?}", self.param_env);
620620
trace!("args: {:#?}", args);
621-
match ty::Instance::resolve(*self.tcx, self.param_env, def, args) {
621+
match ty::Instance::try_resolve(*self.tcx, self.param_env, def, args) {
622622
Ok(Some(instance)) => Ok(instance),
623623
Ok(None) => throw_inval!(TooGeneric),
624624

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
534534
let tcx = self.tcx();
535535

536536
// Find the method being called.
537-
let Ok(Some(instance)) = ty::Instance::resolve(
537+
let Ok(Some(instance)) = ty::Instance::try_resolve(
538538
tcx,
539539
ctxt.param_env,
540540
ctxt.assoc_item.def_id,

compiler/rustc_lint/src/internal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare_lint_pass!(QueryStability => [POTENTIAL_QUERY_INSTABILITY]);
8888
impl LateLintPass<'_> for QueryStability {
8989
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
9090
let Some((span, def_id, args)) = typeck_results_of_method_fn(cx, expr) else { return };
91-
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, args) {
91+
if let Ok(Some(instance)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, args) {
9292
let def_id = instance.def_id();
9393
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
9494
cx.emit_span_lint(
@@ -393,7 +393,7 @@ impl LateLintPass<'_> for Diagnostics {
393393
};
394394

395395
// Is the callee marked with `#[rustc_lint_diagnostics]`?
396-
let has_attr = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
396+
let has_attr = ty::Instance::try_resolve(cx.tcx, cx.param_env, def_id, fn_gen_args)
397397
.ok()
398398
.flatten()
399399
.is_some_and(|inst| cx.tcx.has_attr(inst.def_id(), sym::rustc_lint_diagnostics));

compiler/rustc_lint/src/noop_method_call.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
9696
.tcx
9797
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_args(expr.hir_id));
9898
// Resolve the trait method instance.
99-
let Ok(Some(i)) = ty::Instance::resolve(cx.tcx, cx.param_env, did, args) else { return };
99+
let Ok(Some(i)) = ty::Instance::try_resolve(cx.tcx, cx.param_env, did, args) else {
100+
return;
101+
};
100102
// (Re)check that it implements the noop diagnostic.
101103
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
102104
if !matches!(

compiler/rustc_middle/src/mir/interpret/queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> TyCtxt<'tcx> {
7373
bug!("did not expect inference variables here");
7474
}
7575

76-
match ty::Instance::resolve(
76+
match ty::Instance::try_resolve(
7777
self, param_env,
7878
// FIXME: maybe have a separate version for resolving mir::UnevaluatedConst?
7979
ct.def, ct.args,
@@ -106,7 +106,7 @@ impl<'tcx> TyCtxt<'tcx> {
106106
bug!("did not expect inference variables here");
107107
}
108108

109-
match ty::Instance::resolve(self, param_env, ct.def, ct.args) {
109+
match ty::Instance::try_resolve(self, param_env, ct.def, ct.args) {
110110
Ok(Some(instance)) => {
111111
let cid = GlobalId { instance, promoted: None };
112112
self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| {

compiler/rustc_middle/src/ty/instance.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<'tcx> Instance<'tcx> {
516516
/// from `Ok(None)` to avoid misleading diagnostics when an error
517517
/// has already been/will be emitted, for the original cause
518518
#[instrument(level = "debug", skip(tcx), ret)]
519-
pub fn resolve(
519+
pub fn try_resolve(
520520
tcx: TyCtxt<'tcx>,
521521
param_env: ty::ParamEnv<'tcx>,
522522
def_id: DefId,
@@ -555,7 +555,7 @@ impl<'tcx> Instance<'tcx> {
555555
let span_or_local_def_span =
556556
|| if span.is_dummy() && def_id.is_local() { tcx.def_span(def_id) } else { span };
557557

558-
match ty::Instance::resolve(tcx, param_env, def_id, args) {
558+
match ty::Instance::try_resolve(tcx, param_env, def_id, args) {
559559
Ok(Some(instance)) => instance,
560560
Ok(None) => {
561561
let type_length = type_length(args);
@@ -605,7 +605,7 @@ impl<'tcx> Instance<'tcx> {
605605
// Use either `resolve_closure` or `resolve_for_vtable`
606606
assert!(!tcx.is_closure_like(def_id), "Called `resolve_for_fn_ptr` on closure: {def_id:?}");
607607
let reason = tcx.sess.is_sanitizer_kcfi_enabled().then_some(ReifyReason::FnPtr);
608-
Instance::resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| {
608+
Instance::try_resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| {
609609
match resolved.def {
610610
InstanceKind::Item(def) if resolved.def.requires_caller_location(tcx) => {
611611
debug!(" => fn pointer created for function with #[track_caller]");
@@ -738,13 +738,25 @@ impl<'tcx> Instance<'tcx> {
738738
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
739739
let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
740740
let args = tcx.mk_args(&[ty.into()]);
741-
Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args, DUMMY_SP)
741+
Instance::expect_resolve(
742+
tcx,
743+
ty::ParamEnv::reveal_all(),
744+
def_id,
745+
args,
746+
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
747+
)
742748
}
743749

744750
pub fn resolve_async_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
745751
let def_id = tcx.require_lang_item(LangItem::AsyncDropInPlace, None);
746752
let args = tcx.mk_args(&[ty.into()]);
747-
Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args, DUMMY_SP)
753+
Instance::expect_resolve(
754+
tcx,
755+
ty::ParamEnv::reveal_all(),
756+
def_id,
757+
args,
758+
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
759+
)
748760
}
749761

750762
#[instrument(level = "debug", skip(tcx), ret)]

compiler/rustc_middle/src/util/call_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn call_kind<'tcx>(
9898
Some(CallKind::Operator { self_arg, trait_id, self_ty: method_args.type_at(0) })
9999
} else if is_deref {
100100
let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
101-
Instance::resolve(tcx, param_env, deref_target, method_args).transpose()
101+
Instance::try_resolve(tcx, param_env, deref_target, method_args).transpose()
102102
});
103103
if let Some(Ok(instance)) = deref_target {
104104
let deref_target_ty = instance.ty(tcx, param_env);

compiler/rustc_mir_build/src/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'tcx> TerminatorClassifier<'tcx> for CallRecursion<'tcx> {
141141
return false;
142142
};
143143
let (callee, call_args) = if let Ok(Some(instance)) =
144-
Instance::resolve(tcx, param_env, callee, normalized_args)
144+
Instance::try_resolve(tcx, param_env, callee, normalized_args)
145145
{
146146
(instance.def_id(), instance.args)
147147
} else {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
558558
let args = self
559559
.tcx
560560
.normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_args(id));
561-
let instance = match ty::Instance::resolve(self.tcx, param_env_reveal_all, def_id, args) {
561+
let instance = match ty::Instance::try_resolve(self.tcx, param_env_reveal_all, def_id, args)
562+
{
562563
Ok(Some(i)) => i,
563564
Ok(None) => {
564565
// It should be assoc consts if there's no error but we cannot resolve it.

compiler/rustc_mir_transform/src/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'tcx> Inliner<'tcx> {
389389
// To resolve an instance its args have to be fully normalized.
390390
let args = self.tcx.try_normalize_erasing_regions(self.param_env, args).ok()?;
391391
let callee =
392-
Instance::resolve(self.tcx, self.param_env, def_id, args).ok().flatten()?;
392+
Instance::try_resolve(self.tcx, self.param_env, def_id, args).ok().flatten()?;
393393

394394
if let InstanceKind::Virtual(..) | InstanceKind::Intrinsic(_) = callee.def {
395395
return None;

compiler/rustc_mir_transform/src/inline/cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn mir_callgraph_reachable<'tcx>(
5353
trace!(?caller, ?param_env, ?args, "cannot normalize, skipping");
5454
continue;
5555
};
56-
let Ok(Some(callee)) = ty::Instance::resolve(tcx, param_env, callee, args) else {
56+
let Ok(Some(callee)) = ty::Instance::try_resolve(tcx, param_env, callee, args) else {
5757
trace!(?callee, "cannot resolve, skipping");
5858
continue;
5959
};

compiler/rustc_passes/src/abi_test.rs

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

compiler/rustc_smir/src/rustc_smir/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
629629
let tcx = tables.tcx;
630630
let def_id = def.0.internal(&mut *tables, tcx);
631631
let args_ref = args.internal(&mut *tables, tcx);
632-
match Instance::resolve(tables.tcx, ParamEnv::reveal_all(), def_id, args_ref) {
632+
match Instance::try_resolve(tables.tcx, ParamEnv::reveal_all(), def_id, args_ref) {
633633
Ok(Some(instance)) => Some(instance.stable(&mut *tables)),
634634
Ok(None) | Err(_) => None,
635635
}

src/tools/clippy/clippy_lints/src/assigning_clones.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn extract_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<
103103
let args = cx.typeck_results().node_args(expr.hir_id);
104104

105105
// If we could not resolve the method, don't apply the lint
106-
let Ok(Some(resolved_method)) = Instance::resolve(cx.tcx, cx.param_env, fn_def_id, args) else {
106+
let Ok(Some(resolved_method)) = Instance::try_resolve(cx.tcx, cx.param_env, fn_def_id, args) else {
107107
return None;
108108
};
109109
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<
119119

120120
// If we could not resolve the method, don't apply the lint
121121
let Ok(Some(resolved_method)) = (match kind {
122-
ty::FnDef(_, args) => Instance::resolve(cx.tcx, cx.param_env, fn_def_id, args),
122+
ty::FnDef(_, args) => Instance::try_resolve(cx.tcx, cx.param_env, fn_def_id, args),
123123
_ => Ok(None),
124124
}) else {
125125
return None;

src/tools/clippy/clippy_lints/src/non_copy_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'tcx> NonCopyConst<'tcx> {
293293
ct: ty::UnevaluatedConst<'tcx>,
294294
span: Span,
295295
) -> EvalToValTreeResult<'tcx> {
296-
match ty::Instance::resolve(tcx, param_env, ct.def, ct.args) {
296+
match ty::Instance::try_resolve(tcx, param_env, ct.def, ct.args) {
297297
Ok(Some(instance)) => {
298298
let cid = GlobalId {
299299
instance,

src/tools/miri/src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ pub fn create_ecx<'tcx>(
375375
});
376376
let main_ret_ty = tcx.fn_sig(entry_id).no_bound_vars().unwrap().output();
377377
let main_ret_ty = main_ret_ty.no_bound_vars().unwrap();
378-
let start_instance = ty::Instance::resolve(
378+
let start_instance = ty::Instance::try_resolve(
379379
tcx,
380380
ty::ParamEnv::reveal_all(),
381381
start_id,

tests/ui/codegen/overflow-during-mono.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ build-fail
2+
//@ error-pattern: reached the type-length limit while instantiating
23

34
#![recursion_limit = "32"]
45

tests/ui/codegen/overflow-during-mono.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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::<..., ..., ...>`
1+
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::<..., ..., ...>`
22
--> $SRC_DIR/core/src/iter/adapters/filter.rs:LL:COL
33
|
44
= help: consider adding a `#![type_length_limit="20156994"]` attribute to your crate

tests/ui/iterators/issue-58952-filter-type-length.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ build-fail
2+
//@ error-pattern: reached the type-length limit while instantiating
23

34
//! This snippet causes the type length to blowup exponentially,
45
//! so check that we don't accidentally exceed the type length limit.

0 commit comments

Comments
 (0)