@@ -144,6 +144,7 @@ impl<'tcx> InherentCollect<'tcx> {
144
144
let id = id. owner_id . def_id ;
145
145
let item_span = self . tcx . def_span ( id) ;
146
146
let self_ty = self . tcx . type_of ( id) . instantiate_identity ( ) ;
147
+ let self_ty = peel_off_weak_aliases ( self . tcx , self_ty) ;
147
148
match * self_ty. kind ( ) {
148
149
ty:: Adt ( def, _) => self . check_def_id ( id, self_ty, def. did ( ) ) ,
149
150
ty:: Foreign ( did) => self . check_def_id ( id, self_ty, did) ,
@@ -166,14 +167,15 @@ impl<'tcx> InherentCollect<'tcx> {
166
167
| ty:: Never
167
168
| ty:: FnPtr ( _)
168
169
| ty:: Tuple ( ..) => self . check_primitive_impl ( id, self_ty) ,
169
- ty:: Alias ( .. ) | ty:: Param ( _) => {
170
+ ty:: Alias ( ty :: Projection | ty :: Inherent | ty :: Opaque , _ ) | ty:: Param ( _) => {
170
171
Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentNominal { span : item_span } ) )
171
172
}
172
173
ty:: FnDef ( ..)
173
174
| ty:: Closure ( ..)
174
175
| ty:: CoroutineClosure ( ..)
175
176
| ty:: Coroutine ( ..)
176
177
| ty:: CoroutineWitness ( ..)
178
+ | ty:: Alias ( ty:: Weak , _)
177
179
| ty:: Bound ( ..)
178
180
| ty:: Placeholder ( _)
179
181
| ty:: Infer ( _) => {
@@ -184,3 +186,30 @@ impl<'tcx> InherentCollect<'tcx> {
184
186
}
185
187
}
186
188
}
189
+
190
+ /// Peel off all weak alias types in this type until there are none left.
191
+ ///
192
+ /// <div class="warning">
193
+ ///
194
+ /// This assumes that `ty` gets normalized later and that any overflows occurring
195
+ /// during said normalization get reported.
196
+ ///
197
+ /// </div>
198
+ fn peel_off_weak_aliases < ' tcx > ( tcx : TyCtxt < ' tcx > , mut ty : Ty < ' tcx > ) -> Ty < ' tcx > {
199
+ let ty:: Alias ( ty:: Weak , _) = ty. kind ( ) else { return ty } ;
200
+
201
+ let limit = tcx. recursion_limit ( ) ;
202
+ let mut depth = 0 ;
203
+
204
+ while let ty:: Alias ( ty:: Weak , alias) = ty. kind ( ) {
205
+ if !limit. value_within_limit ( depth) {
206
+ let guar = tcx. dcx ( ) . delayed_bug ( "overflow expanding weak alias type" ) ;
207
+ return Ty :: new_error ( tcx, guar) ;
208
+ }
209
+
210
+ ty = tcx. type_of ( alias. def_id ) . instantiate ( tcx, alias. args ) ;
211
+ depth += 1 ;
212
+ }
213
+
214
+ ty
215
+ }
0 commit comments