Skip to content

Commit 9d7b194

Browse files
oli-obkcompiler-errors
authored andcommitted
Avoid fetching the opaque type origin when only "is this in the defining scope" is actually needed
1 parent 1b499c8 commit 9d7b194

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
719719
for ty in ret_ty.walk() {
720720
if let ty::GenericArgKind::Type(ty) = ty.unpack()
721721
&& let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *ty.kind()
722-
&& let Some(def_id) = def_id.as_local()
723-
&& self.opaque_type_origin(def_id).is_some()
722+
&& self.can_define_opaque_ty(def_id)
724723
{
725724
return None;
726725
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub struct InferCtxt<'tcx> {
235235
pub tcx: TyCtxt<'tcx>,
236236

237237
/// The `DefIds` of the opaque types that may have their hidden types constrained.
238-
pub defining_opaque_types: &'tcx ty::List<LocalDefId>,
238+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
239239

240240
/// Whether this inference context should care about region obligations in
241241
/// the root universe. Most notably, this is used during hir typeck as region
@@ -1219,6 +1219,12 @@ impl<'tcx> InferCtxt<'tcx> {
12191219
self.inner.borrow().opaque_type_storage.opaque_types.clone()
12201220
}
12211221

1222+
#[inline(always)]
1223+
pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
1224+
let Some(id) = id.into().as_local() else { return false };
1225+
self.defining_opaque_types.contains(&id)
1226+
}
1227+
12221228
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
12231229
self.resolve_vars_if_possible(t).to_string()
12241230
}

compiler/rustc_infer/src/infer/opaque_types/mod.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::errors::OpaqueHiddenTypeDiag;
44
use crate::infer::{InferCtxt, InferOk};
55
use crate::traits::{self, PredicateObligation};
66
use hir::def_id::{DefId, LocalDefId};
7-
use hir::OpaqueTyOrigin;
87
use rustc_data_structures::fx::FxIndexMap;
98
use rustc_data_structures::sync::Lrc;
109
use rustc_hir as hir;
@@ -54,16 +53,13 @@ impl<'tcx> InferCtxt<'tcx> {
5453
}
5554

5655
let mut obligations = vec![];
57-
let replace_opaque_type = |def_id: DefId| {
58-
def_id.as_local().is_some_and(|def_id| self.opaque_type_origin(def_id).is_some())
59-
};
6056
let value = value.fold_with(&mut BottomUpFolder {
6157
tcx: self.tcx,
6258
lt_op: |lt| lt,
6359
ct_op: |ct| ct,
6460
ty_op: |ty| match *ty.kind() {
6561
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })
66-
if replace_opaque_type(def_id) && !ty.has_escaping_bound_vars() =>
62+
if self.can_define_opaque_ty(def_id) && !ty.has_escaping_bound_vars() =>
6763
{
6864
let def_span = self.tcx.def_span(def_id);
6965
let span = if span.contains(def_span) { def_span } else { span };
@@ -143,7 +139,7 @@ impl<'tcx> InferCtxt<'tcx> {
143139
// let x = || foo(); // returns the Opaque assoc with `foo`
144140
// }
145141
// ```
146-
if self.opaque_type_origin(def_id).is_none() {
142+
if !self.can_define_opaque_ty(def_id) {
147143
return None;
148144
}
149145

@@ -153,8 +149,8 @@ impl<'tcx> InferCtxt<'tcx> {
153149
// no one encounters it in practice.
154150
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
155151
// where it is of no concern, so we only check for TAITs.
156-
if let Some(OpaqueTyOrigin::TyAlias { .. }) =
157-
b_def_id.as_local().and_then(|b_def_id| self.opaque_type_origin(b_def_id))
152+
if self.can_define_opaque_ty(b_def_id)
153+
&& self.tcx.is_type_alias_impl_trait(b_def_id)
158154
{
159155
self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag {
160156
span: cause.span,
@@ -366,15 +362,6 @@ impl<'tcx> InferCtxt<'tcx> {
366362
op: |r| self.member_constraint(opaque_type_key, span, concrete_ty, r, &choice_regions),
367363
});
368364
}
369-
370-
/// Returns the origin of the opaque type `def_id` if we're currently
371-
/// in its defining scope.
372-
#[instrument(skip(self), level = "trace", ret)]
373-
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
374-
let origin = self.tcx.opaque_type_origin(def_id);
375-
376-
self.defining_opaque_types.contains(&def_id).then_some(origin)
377-
}
378365
}
379366

380367
/// Visitor that requires that (almost) all regions in the type visited outlive

compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_data_structures::stack::ensure_sufficient_stack;
2-
use rustc_hir::def_id::{DefId, LocalDefId};
2+
use rustc_hir::def_id::DefId;
33
use rustc_infer::infer::at::ToTrace;
44
use rustc_infer::infer::canonical::CanonicalVarValues;
55
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -883,8 +883,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
883883
}
884884
}
885885

886-
pub(super) fn can_define_opaque_ty(&self, def_id: LocalDefId) -> bool {
887-
self.infcx.opaque_type_origin(def_id).is_some()
886+
pub(super) fn can_define_opaque_ty(&self, def_id: impl Into<DefId>) -> bool {
887+
self.infcx.can_define_opaque_ty(def_id)
888888
}
889889

890890
pub(super) fn insert_hidden_type(

compiler/rustc_trait_selection/src/solve/trait_goals.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
149149
if let ty::Alias(ty::Opaque, opaque_ty) = goal.predicate.self_ty().kind() {
150150
if matches!(goal.param_env.reveal(), Reveal::All)
151151
|| matches!(ecx.solver_mode(), SolverMode::Coherence)
152-
|| opaque_ty
153-
.def_id
154-
.as_local()
155-
.is_some_and(|def_id| ecx.can_define_opaque_ty(def_id))
152+
|| ecx.can_define_opaque_ty(opaque_ty.def_id)
156153
{
157154
return Err(NoSolution);
158155
}

0 commit comments

Comments
 (0)