@@ -14,15 +14,17 @@ use rustc_middle::ty::layout::{
1414 FnAbiError , FnAbiOf , FnAbiOfHelpers , FnAbiRequest , HasParamEnv , HasTyCtxt , LayoutError ,
1515 LayoutOfHelpers , TyAndLayout ,
1616} ;
17- use rustc_middle:: ty:: { self , Instance as InstanceInternal , Ty , TyCtxt } ;
17+ use rustc_middle:: ty:: { self , Instance as InstanceInternal , Ty as TyInternal , TyCtxt } ;
1818use rustc_session:: config:: OutputType ;
1919use rustc_smir:: rustc_internal;
2020use rustc_span:: source_map:: respan;
2121use rustc_span:: Span ;
2222use rustc_target:: abi:: call:: FnAbi ;
2323use rustc_target:: abi:: { HasDataLayout , TargetDataLayout } ;
2424use stable_mir:: mir:: mono:: { Instance , InstanceKind , MonoItem } ;
25- use stable_mir:: ty:: { BoundVariableKind , Span as SpanStable } ;
25+ use stable_mir:: mir:: pretty:: pretty_ty;
26+ use stable_mir:: ty:: { BoundVariableKind , RigidTy , Span as SpanStable , Ty , TyKind } ;
27+ use stable_mir:: visitor:: { Visitable , Visitor as TypeVisitor } ;
2628use stable_mir:: { CrateDef , DefId } ;
2729use std:: fs:: File ;
2830use std:: io:: BufWriter ;
@@ -104,10 +106,9 @@ pub fn check_reachable_items(tcx: TyCtxt, queries: &QueryDb, items: &[MonoItem])
104106/// This is a temporary safety measure because contracts cannot yet reason
105107/// about the heap.
106108fn check_is_contract_safe ( tcx : TyCtxt , instance : Instance ) {
107- use ty:: TypeVisitor ;
108109 struct NoMutPtr < ' tcx > {
109110 tcx : TyCtxt < ' tcx > ,
110- is_prohibited : fn ( ty :: Ty < ' tcx > ) -> bool ,
111+ is_prohibited : fn ( Ty ) -> bool ,
111112 /// Where (top level) did the type we're analyzing come from. Used for
112113 /// composing error messages.
113114 r#where : & ' static str ,
@@ -116,14 +117,15 @@ fn check_is_contract_safe(tcx: TyCtxt, instance: Instance) {
116117 what : & ' static str ,
117118 }
118119
119- impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for NoMutPtr < ' tcx > {
120- fn visit_ty ( & mut self , t : ty :: Ty < ' tcx > ) -> std :: ops :: ControlFlow < Self :: BreakTy > {
121- use ty :: TypeSuperVisitable ;
122- if ( self . is_prohibited ) ( t ) {
120+ impl < ' tcx > TypeVisitor for NoMutPtr < ' tcx > {
121+ type Break = ( ) ;
122+ fn visit_ty ( & mut self , ty : & Ty ) -> std :: ops :: ControlFlow < Self :: Break > {
123+ if ( self . is_prohibited ) ( * ty ) {
123124 // TODO make this more user friendly
124125 self . tcx . sess . err ( format ! (
125- "{} contains a {}pointer ({t:?}). This is prohibited for functions with contracts, \
126- as they cannot yet reason about the pointer behavior.", self . r#where, self . what) ) ;
126+ "{} contains a {}pointer ({}). This is prohibited for functions with contracts, \
127+ as they cannot yet reason about the pointer behavior.", self . r#where, self . what,
128+ pretty_ty( ty. kind( ) ) ) ) ;
127129 }
128130
129131 // Rust's type visitor only recurses into type arguments, (e.g.
@@ -134,28 +136,28 @@ fn check_is_contract_safe(tcx: TyCtxt, instance: Instance) {
134136 // Since the field types also must contain in some form all the type
135137 // arguments the visitor will see them as it inspects the fields and
136138 // we don't need to call back to `super`.
137- if let ty :: TyKind :: Adt ( adt_def, generics) = t . kind ( ) {
139+ if let TyKind :: RigidTy ( RigidTy :: Adt ( adt_def, generics) ) = ty . kind ( ) {
138140 for variant in adt_def. variants ( ) {
139- for field in & variant. fields {
140- let ctrl = self . visit_ty ( field. ty ( self . tcx , generics) ) ;
141- if ctrl. is_break ( ) {
142- // Technically we can just ignore this because we
143- // know this case will never happen, but just to be
144- // safe.
145- return ctrl;
146- }
141+ for field in & variant. fields ( ) {
142+ self . visit_ty ( & field. ty_with_args ( & generics) ) ?;
147143 }
148144 }
149145 std:: ops:: ControlFlow :: Continue ( ( ) )
150146 } else {
151147 // For every other type.
152- t . super_visit_with ( self )
148+ ty . super_visit ( self )
153149 }
154150 }
155151 }
156152
157- fn is_raw_mutable_ptr ( t : ty:: Ty ) -> bool {
158- matches ! ( t. kind( ) , ty:: TyKind :: RawPtr ( tmut) if tmut. mutbl == rustc_ast:: Mutability :: Mut )
153+ fn is_raw_mutable_ptr ( ty : Ty ) -> bool {
154+ let kind = ty. kind ( ) ;
155+ kind. is_raw_ptr ( ) && kind. is_mutable_ptr ( )
156+ }
157+
158+ fn is_raw_ptr ( ty : Ty ) -> bool {
159+ let kind = ty. kind ( ) ;
160+ kind. is_raw_ptr ( )
159161 }
160162
161163 // TODO: Replace this with fn_abi.
@@ -173,15 +175,15 @@ fn check_is_contract_safe(tcx: TyCtxt, instance: Instance) {
173175
174176 let fn_typ = bound_fn_sig. skip_binder ( ) ;
175177
176- for ( typ , ( is_prohibited, r#where, what) ) in fn_typ
178+ for ( input_ty , ( is_prohibited, r#where, what) ) in fn_typ
177179 . inputs ( )
178180 . iter ( )
179181 . copied ( )
180182 . zip ( std:: iter:: repeat ( ( is_raw_mutable_ptr as fn ( _) -> _ , "This argument" , "mutable " ) ) )
181- . chain ( [ ( fn_typ. output ( ) , ( ty :: Ty :: is_unsafe_ptr as fn ( _) -> _ , "The return" , "" ) ) ] )
183+ . chain ( [ ( fn_typ. output ( ) , ( is_raw_ptr as fn ( _) -> _ , "The return" , "" ) ) ] )
182184 {
183185 let mut v = NoMutPtr { tcx, is_prohibited, r#where, what } ;
184- v. visit_ty ( rustc_internal :: internal ( typ ) ) ;
186+ v. visit_ty ( & input_ty ) ;
185187 }
186188}
187189
@@ -226,7 +228,7 @@ pub struct SourceLocation {
226228}
227229
228230impl SourceLocation {
229- pub fn new ( span : & SpanStable ) -> Self {
231+ pub fn new ( span : SpanStable ) -> Self {
230232 let loc = span. get_lines ( ) ;
231233 let filename = span. get_filename ( ) . to_string ( ) ;
232234 let start_line = loc. start_line ;
@@ -243,7 +245,7 @@ impl SourceLocation {
243245pub fn fn_abi < ' tcx > (
244246 tcx : TyCtxt < ' tcx > ,
245247 instance : InstanceInternal < ' tcx > ,
246- ) -> & ' tcx FnAbi < ' tcx , Ty < ' tcx > > {
248+ ) -> & ' tcx FnAbi < ' tcx , TyInternal < ' tcx > > {
247249 let helper = CompilerHelpers { tcx } ;
248250 helper. fn_abi_of_instance ( instance, ty:: List :: empty ( ) )
249251}
@@ -274,14 +276,14 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CompilerHelpers<'tcx> {
274276 type LayoutOfResult = TyAndLayout < ' tcx > ;
275277
276278 #[ inline]
277- fn handle_layout_err ( & self , err : LayoutError < ' tcx > , span : Span , ty : Ty < ' tcx > ) -> ! {
279+ fn handle_layout_err ( & self , err : LayoutError < ' tcx > , span : Span , ty : TyInternal < ' tcx > ) -> ! {
278280 span_bug ! ( span, "failed to get layout for `{}`: {}" , ty, err)
279281 }
280282}
281283
282284/// Implement error handling for extracting function ABI information.
283285impl < ' tcx > FnAbiOfHelpers < ' tcx > for CompilerHelpers < ' tcx > {
284- type FnAbiOfResult = & ' tcx FnAbi < ' tcx , Ty < ' tcx > > ;
286+ type FnAbiOfResult = & ' tcx FnAbi < ' tcx , TyInternal < ' tcx > > ;
285287
286288 #[ inline]
287289 fn handle_fn_abi_err (
0 commit comments