@@ -146,11 +146,14 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
146
146
// and #84660 where it would otherwise allow unsoundness.
147
147
if trait_ref. has_opaque_types ( ) {
148
148
trace ! ( "{:#?}" , item) ;
149
+ // First we find the opaque type in question.
149
150
for ty in trait_ref. substs {
150
151
for ty in ty. walk ( ) {
151
152
let ty:: subst:: GenericArgKind :: Type ( ty) = ty. unpack ( ) else { continue } ;
152
153
let ty:: Opaque ( def_id, _) = * ty. kind ( ) else { continue } ;
153
154
trace ! ( ?def_id) ;
155
+
156
+ // Then we search for mentions of the opaque type's type alias in the HIR
154
157
struct SpanFinder < ' tcx > {
155
158
sp : Span ,
156
159
def_id : DefId ,
@@ -159,11 +162,14 @@ fn orphan_check_impl(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
159
162
impl < ' v , ' tcx > hir:: intravisit:: Visitor < ' v > for SpanFinder < ' tcx > {
160
163
#[ instrument( level = "trace" , skip( self , _id) ) ]
161
164
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
162
166
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
163
168
for arg in self . tcx . type_of ( def_id) . walk ( ) {
164
169
if let GenericArgKind :: Type ( ty) = arg. unpack ( ) {
165
170
if let ty:: Opaque ( def_id, _) = * ty. kind ( ) {
166
171
if def_id == self . def_id {
172
+ // Finally we update the span to the mention of the type alias
167
173
self . sp = path. span ;
168
174
return ;
169
175
}
0 commit comments