@@ -9,16 +9,17 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
9
9
use rustc_middle:: mir:: visit:: { MutatingUseContext , NonMutatingUseContext , PlaceContext , Visitor } ;
10
10
use rustc_middle:: mir:: * ;
11
11
use rustc_middle:: traits:: BuiltinImplSource ;
12
+ use rustc_middle:: ty:: GenericArgs ;
12
13
use rustc_middle:: ty:: { self , adjustment:: PointerCoercion , Instance , InstanceDef , Ty , TyCtxt } ;
13
- use rustc_middle:: ty:: { GenericArgKind , GenericArgs } ;
14
14
use rustc_middle:: ty:: { TraitRef , TypeVisitableExt } ;
15
15
use rustc_mir_dataflow:: { self , Analysis } ;
16
16
use rustc_span:: { sym, Span , Symbol } ;
17
17
use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt as _;
18
18
use rustc_trait_selection:: traits:: { self , ObligationCauseCode , ObligationCtxt , SelectionContext } ;
19
+ use rustc_type_ir:: visit:: { TypeSuperVisitable , TypeVisitor } ;
19
20
20
21
use std:: mem;
21
- use std:: ops:: Deref ;
22
+ use std:: ops:: { ControlFlow , Deref } ;
22
23
23
24
use super :: ops:: { self , NonConstOp , Status } ;
24
25
use super :: qualifs:: { self , CustomEq , HasMutInterior , NeedsDrop } ;
@@ -188,6 +189,24 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
188
189
}
189
190
}
190
191
192
+ struct LocalReturnTyVisitor < ' ck , ' mir , ' tcx > {
193
+ kind : LocalKind ,
194
+ checker : & ' ck mut Checker < ' mir , ' tcx > ,
195
+ }
196
+
197
+ impl < ' ck , ' mir , ' tcx > TypeVisitor < TyCtxt < ' tcx > > for LocalReturnTyVisitor < ' ck , ' mir , ' tcx > {
198
+ fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
199
+ match t. kind ( ) {
200
+ ty:: FnPtr ( _) => ControlFlow :: Continue ( ( ) ) ,
201
+ ty:: Ref ( _, _, hir:: Mutability :: Mut ) => {
202
+ self . checker . check_op ( ops:: ty:: MutRef ( self . kind ) ) ;
203
+ t. super_visit_with ( self )
204
+ }
205
+ _ => t. super_visit_with ( self ) ,
206
+ }
207
+ }
208
+ }
209
+
191
210
pub struct Checker < ' mir , ' tcx > {
192
211
ccx : & ' mir ConstCx < ' mir , ' tcx > ,
193
212
qualifs : Qualifs < ' mir , ' tcx > ,
@@ -346,20 +365,9 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
346
365
fn check_local_or_return_ty ( & mut self , ty : Ty < ' tcx > , local : Local ) {
347
366
let kind = self . body . local_kind ( local) ;
348
367
349
- for ty in ty. walk ( ) {
350
- let ty = match ty. unpack ( ) {
351
- GenericArgKind :: Type ( ty) => ty,
352
-
353
- // No constraints on lifetimes or constants, except potentially
354
- // constants' types, but `walk` will get to them as well.
355
- GenericArgKind :: Lifetime ( _) | GenericArgKind :: Const ( _) => continue ,
356
- } ;
368
+ let mut visitor = LocalReturnTyVisitor { kind, checker : self } ;
357
369
358
- match * ty. kind ( ) {
359
- ty:: Ref ( _, _, hir:: Mutability :: Mut ) => self . check_op ( ops:: ty:: MutRef ( kind) ) ,
360
- _ => { }
361
- }
362
- }
370
+ visitor. visit_ty ( ty) ;
363
371
}
364
372
365
373
fn check_mut_borrow ( & mut self , local : Local , kind : hir:: BorrowKind ) {
0 commit comments