Skip to content

Commit 93a3cfb

Browse files
committed
Explain the span search logic
1 parent 08ef70b commit 93a3cfb

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

compiler/rustc_typeck/src/coherence/orphan.rs

+6
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
146146
// and #84660 where it would otherwise allow unsoundness.
147147
if trait_ref.has_opaque_types() {
148148
trace!("{:#?}", item);
149+
// First we find the opaque type in question.
149150
for ty in trait_ref.substs {
150151
for ty in ty.walk() {
151152
let ty::subst::GenericArgKind::Type(ty) = ty.unpack() else { continue };
152153
let ty::Opaque(def_id, _) = *ty.kind() else { continue };
153154
trace!(?def_id);
155+
156+
// Then we search for mentions of the opaque type's type alias in the HIR
154157
struct SpanFinder<'tcx> {
155158
sp: Span,
156159
def_id: DefId,
@@ -159,11 +162,14 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
159162
impl<'v, 'tcx> hir::intravisit::Visitor<'v> for SpanFinder<'tcx> {
160163
#[instrument(level = "trace", skip(self, _id))]
161164
fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
165+
// You can't mention an opaque type directly, so we look for type aliases
162166
if let hir::def::Res::Def(hir::def::DefKind::TyAlias, def_id) = path.res {
167+
// And check if that type alias's type contains the opaque type we're looking for
163168
for arg in self.tcx.type_of(def_id).walk() {
164169
if let GenericArgKind::Type(ty) = arg.unpack() {
165170
if let ty::Opaque(def_id, _) = *ty.kind() {
166171
if def_id == self.def_id {
172+
// Finally we update the span to the mention of the type alias
167173
self.sp = path.span;
168174
return;
169175
}

0 commit comments

Comments
 (0)