@@ -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,13 +167,14 @@ 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:: Coroutine ( ..)
175
176
| ty:: CoroutineWitness ( ..)
177
+ | ty:: Alias ( ty:: Weak , _)
176
178
| ty:: Bound ( ..)
177
179
| ty:: Placeholder ( _)
178
180
| ty:: Infer ( _) => {
@@ -183,3 +185,30 @@ impl<'tcx> InherentCollect<'tcx> {
183
185
}
184
186
}
185
187
}
188
+
189
+ /// Peel off all weak alias types in this type until there are none left.
190
+ ///
191
+ /// <div class="warning">
192
+ ///
193
+ /// This assumes that `ty` gets normalized later and that any overflows occurring
194
+ /// during said normalization get reported.
195
+ ///
196
+ /// </div>
197
+ fn peel_off_weak_aliases < ' tcx > ( tcx : TyCtxt < ' tcx > , mut ty : Ty < ' tcx > ) -> Ty < ' tcx > {
198
+ let ty:: Alias ( ty:: Weak , _) = ty. kind ( ) else { return ty } ;
199
+
200
+ let limit = tcx. recursion_limit ( ) ;
201
+ let mut depth = 0 ;
202
+
203
+ while let ty:: Alias ( ty:: Weak , alias) = ty. kind ( ) {
204
+ if !limit. value_within_limit ( depth) {
205
+ let guar = tcx. dcx ( ) . delayed_bug ( "overflow expanding weak alias type" ) ;
206
+ return Ty :: new_error ( tcx, guar) ;
207
+ }
208
+
209
+ ty = tcx. type_of ( alias. def_id ) . instantiate ( tcx, alias. args ) ;
210
+ depth += 1 ;
211
+ }
212
+
213
+ ty
214
+ }
0 commit comments