@@ -3,25 +3,22 @@ use rustc_ast::InlineAsmTemplatePiece;
33use rustc_data_structures:: fx:: FxIndexSet ;
44use rustc_hir:: def_id:: DefId ;
55use rustc_hir:: { self as hir, LangItem } ;
6- use rustc_infer:: infer:: InferCtxt ;
76use rustc_middle:: bug;
8- use rustc_middle:: ty:: {
9- self , Article , FloatTy , IntTy , Ty , TyCtxt , TypeVisitableExt , TypeckResults , UintTy ,
10- } ;
7+ use rustc_middle:: ty:: { self , Article , FloatTy , IntTy , Ty , TyCtxt , TypeVisitableExt , UintTy } ;
118use rustc_session:: lint;
129use rustc_span:: def_id:: LocalDefId ;
13- use rustc_span:: { Symbol , sym} ;
10+ use rustc_span:: { Span , Symbol , sym} ;
1411use rustc_target:: asm:: {
1512InlineAsmReg , InlineAsmRegClass , InlineAsmRegOrRegClass , InlineAsmType , ModifierInfo ,
1613} ;
14+ use rustc_trait_selection:: infer:: InferCtxtExt ;
1715
16+ use crate :: FnCtxt ;
1817use crate :: errors:: RegisterTypeUnstable ;
1918
20- pub struct InlineAsmCtxt < ' a , ' tcx > {
21- typing_env : ty:: TypingEnv < ' tcx > ,
19+ pub ( crate ) struct InlineAsmCtxt < ' a , ' tcx > {
2220target_features : & ' tcx FxIndexSet < Symbol > ,
23- infcx : & ' a InferCtxt < ' tcx > ,
24- typeck_results : & ' a TypeckResults < ' tcx > ,
21+ fcx : & ' a FnCtxt < ' a , ' tcx > ,
2522}
2623
2724enum NonAsmTypeReason < ' tcx > {
@@ -33,27 +30,17 @@ enum NonAsmTypeReason<'tcx> {
3330}
3431
3532impl < ' a , ' tcx > InlineAsmCtxt < ' a , ' tcx > {
36- pub fn new (
37- def_id : LocalDefId ,
38- infcx : & ' a InferCtxt < ' tcx > ,
39- typing_env : ty:: TypingEnv < ' tcx > ,
40- typeck_results : & ' a TypeckResults < ' tcx > ,
41- ) ->Self {
42- InlineAsmCtxt {
43- typing_env,
44- target_features : infcx. tcx . asm_target_features ( def_id) ,
45- infcx,
46- typeck_results,
47- }
33+ pub ( crate ) fn new ( fcx : & ' a FnCtxt < ' a , ' tcx > , def_id : LocalDefId ) ->Self {
34+ InlineAsmCtxt { target_features : fcx. tcx . asm_target_features ( def_id) , fcx}
4835}
4936
5037fn tcx ( & self ) ->TyCtxt < ' tcx > {
51- self . infcx . tcx
38+ self . fcx . tcx
5239}
5340
5441fn expr_ty ( & self , expr : & hir:: Expr < ' tcx > ) ->Ty < ' tcx > {
55- let ty =self . typeck_results . expr_ty_adjusted ( expr) ;
56- let ty =self . infcx . resolve_vars_if_possible ( ty) ;
42+ let ty =self . fcx . typeck_results . borrow ( ) . expr_ty_adjusted ( expr) ;
43+ let ty =self . fcx . try_structurally_resolve_type ( expr . span , ty) ;
5744if ty. has_non_region_infer ( ) {
5845Ty :: new_misc_error ( self . tcx ( ) )
5946} else {
@@ -62,19 +49,23 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
6249}
6350
6451// FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
65- fn is_thin_ptr_ty ( & self , ty : Ty < ' tcx > ) ->bool {
52+ fn is_thin_ptr_ty ( & self , span : Span , ty : Ty < ' tcx > ) ->bool {
6653// Type still may have region variables, but `Sized` does not depend
6754// on those, so just erase them before querying.
68- if ty . is_sized ( self . tcx ( ) , self . typing_env ) {
55+ if self . fcx . type_is_sized_modulo_regions ( self . fcx . param_env , ty ) {
6956return true ;
7057}
71- if let ty:: Foreign ( ..) =ty . kind ( ) {
58+ if let ty:: Foreign ( ..) =self . fcx . try_structurally_resolve_type ( span , ty ) . kind ( ) {
7259return true ;
7360}
7461false
7562}
7663
77- fn get_asm_ty ( & self , ty : Ty < ' tcx > ) ->Result < InlineAsmType , NonAsmTypeReason < ' tcx > > {
64+ fn get_asm_ty (
65+ & self ,
66+ span : Span ,
67+ ty : Ty < ' tcx > ,
68+ ) ->Result < InlineAsmType , NonAsmTypeReason < ' tcx > > {
7869let asm_ty_isize =match self . tcx ( ) . sess . target . pointer_width {
797016 =>InlineAsmType :: I16 ,
807132 =>InlineAsmType :: I32 ,
@@ -95,7 +86,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
9586 ty:: Float ( FloatTy :: F128 ) =>Ok ( InlineAsmType :: F128 ) ,
9687 ty:: FnPtr ( ..) =>Ok ( asm_ty_isize) ,
9788 ty:: RawPtr ( elem_ty, _) =>{
98- if self . is_thin_ptr_ty ( elem_ty) {
89+ if self . is_thin_ptr_ty ( span , elem_ty) {
9990Ok ( asm_ty_isize)
10091} else {
10192Err ( NonAsmTypeReason :: NotSizedPtr ( ty) )
@@ -109,11 +100,20 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
109100let field =& fields[ FieldIdx :: ZERO ] ;
110101let elem_ty = field. ty ( self . tcx ( ) , args) ;
111102
112- let ( size, ty) =match elem_ty. kind ( ) {
103+ let ( size, ty) =match * elem_ty. kind ( ) {
113104 ty:: Array ( ty, len) =>{
114- let len =self . tcx ( ) . normalize_erasing_regions ( self . typing_env , * len) ;
105+ // FIXME: `try_structurally_resolve_const` doesn't eval consts
106+ // in the old solver.
107+ let len =if self . fcx . next_trait_solver ( ) {
108+ self . fcx . try_structurally_resolve_const ( span, len)
109+ } else {
110+ self . fcx . tcx . normalize_erasing_regions (
111+ self . fcx . typing_env ( self . fcx . param_env ) ,
112+ len,
113+ )
114+ } ;
115115if let Some ( len) = len. try_to_target_usize ( self . tcx ( ) ) {
116- ( len, * ty)
116+ ( len, ty)
117117} else {
118118return Err ( NonAsmTypeReason :: UnevaluatedSIMDArrayLength (
119119 field. did , len,
@@ -183,17 +183,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
183183) ;
184184let fields =& ty. non_enum_variant ( ) . fields ;
185185let ty = fields[ FieldIdx :: ZERO ] . ty ( self . tcx ( ) , args) ;
186- self . get_asm_ty ( ty)
186+ self . get_asm_ty ( expr . span , ty)
187187}
188- _ =>self . get_asm_ty ( ty) ,
188+ _ =>self . get_asm_ty ( expr . span , ty) ,
189189} ;
190190let asm_ty =match asm_ty{
191191Ok ( asm_ty) => asm_ty,
192192Err ( reason) =>{
193193match reason{
194194NonAsmTypeReason :: UnevaluatedSIMDArrayLength ( did, len) =>{
195195let msg =format ! ( "cannot evaluate SIMD vector length `{len}`" ) ;
196- self . infcx
196+ self . fcx
197197. dcx ( )
198198. struct_span_err ( self . tcx ( ) . def_span ( did) , msg)
199199. with_span_note (
@@ -204,7 +204,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
204204}
205205NonAsmTypeReason :: Invalid ( ty) =>{
206206let msg =format ! ( "cannot use value of type `{ty}` for inline assembly" ) ;
207- self . infcx . dcx ( ) . struct_span_err ( expr. span , msg) . with_note (
207+ self . fcx . dcx ( ) . struct_span_err ( expr. span , msg) . with_note (
208208"only integers, floats, SIMD vectors, pointers and function pointers\
209209 can be used as arguments for inline assembly",
210210) . emit ( ) ;
@@ -213,7 +213,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
213213let msg =format ! (
214214"cannot use value of unsized pointer type `{ty}` for inline assembly"
215215) ;
216- self . infcx
216+ self . fcx
217217. dcx ( )
218218. struct_span_err ( expr. span , msg)
219219. with_note ( "only sized pointers can be used in inline assembly" )
@@ -223,7 +223,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
223223let msg =format ! (
224224"cannot use SIMD vector with element type `{ty}` for inline assembly"
225225) ;
226- self . infcx . dcx ( )
226+ self . fcx . dcx ( )
227227. struct_span_err ( self . tcx ( ) . def_span ( did) , msg) . with_span_note (
228228 expr. span ,
229229"only integers, floats, SIMD vectors, pointers and function pointers\
@@ -232,7 +232,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
232232}
233233NonAsmTypeReason :: EmptySIMDArray ( ty) =>{
234234let msg =format ! ( "use of empty SIMD vector `{ty}`" ) ;
235- self . infcx . dcx ( ) . struct_span_err ( expr. span , msg) . emit ( ) ;
235+ self . fcx . dcx ( ) . struct_span_err ( expr. span , msg) . emit ( ) ;
236236}
237237}
238238return None ;
@@ -241,9 +241,9 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
241241
242242// Check that the type implements Copy. The only case where this can
243243// possibly fail is for SIMD types which don't #[derive(Copy)].
244- if !self . tcx ( ) . type_is_copy_modulo_regions ( self . typing_env , ty) {
244+ if !self . fcx . type_is_copy_modulo_regions ( self . fcx . param_env , ty) {
245245let msg ="arguments for inline assembly must be copyable" ;
246- self . infcx
246+ self . fcx
247247. dcx ( )
248248. struct_span_err ( expr. span , msg)
249249. with_note ( format ! ( "`{ty}` does not implement the Copy trait" ) )
@@ -263,7 +263,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
263263if in_asm_ty != asm_ty{
264264let msg ="incompatible types for asm inout argument" ;
265265let in_expr_ty =self . expr_ty ( in_expr) ;
266- self . infcx
266+ self . fcx
267267. dcx ( )
268268. struct_span_err ( vec ! [ in_expr. span, expr. span] , msg)
269269. with_span_label ( in_expr. span , format ! ( "type `{in_expr_ty}`" ) )
@@ -296,7 +296,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
296296)
297297} else {
298298let msg =format ! ( "type `{ty}` cannot be used with this register class" ) ;
299- let mut err =self . infcx . dcx ( ) . struct_span_err ( expr. span , msg) ;
299+ let mut err =self . fcx . dcx ( ) . struct_span_err ( expr. span , msg) ;
300300let supported_tys: Vec < _ > =
301301 supported_tys. iter ( ) . map ( |( t, _) | t. to_string ( ) ) . collect ( ) ;
302302 err. note ( format ! (
@@ -326,7 +326,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
326326if let Some ( feature) = feature{
327327if !self . target_features . contains ( feature) {
328328let msg =format ! ( "`{feature}` target feature is not enabled" ) ;
329- self . infcx
329+ self . fcx
330330. dcx ( )
331331. struct_span_err ( expr. span , msg)
332332. with_note ( format ! (
@@ -384,9 +384,9 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
384384Some ( asm_ty)
385385}
386386
387- pub fn check_asm ( & self , asm : & hir:: InlineAsm < ' tcx > ) {
387+ pub ( crate ) fn check_asm ( & self , asm : & hir:: InlineAsm < ' tcx > ) {
388388let Some ( asm_arch) =self . tcx ( ) . sess . asm_arch else {
389- self . infcx . dcx ( ) . delayed_bug ( "target architecture does not support asm" ) ;
389+ self . fcx . dcx ( ) . delayed_bug ( "target architecture does not support asm" ) ;
390390return ;
391391} ;
392392let allow_experimental_reg =self . tcx ( ) . features ( ) . asm_experimental_reg ( ) ;
@@ -418,7 +418,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
418418 op. is_clobber ( ) ,
419419) {
420420let msg =format ! ( "cannot use register `{}`: {}" , reg. name( ) , msg) ;
421- self . infcx . dcx ( ) . span_err ( op_sp, msg) ;
421+ self . fcx . dcx ( ) . span_err ( op_sp, msg) ;
422422continue ;
423423}
424424}
@@ -458,7 +458,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
458458 reg_class. name( ) ,
459459 feature
460460) ;
461- self . infcx . dcx ( ) . span_err ( op_sp, msg) ;
461+ self . fcx . dcx ( ) . span_err ( op_sp, msg) ;
462462// register isn't enabled, don't do more checks
463463continue ;
464464}
@@ -472,7 +472,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
472472. intersperse( ", " )
473473. collect:: <String >( ) ,
474474) ;
475- self . infcx . dcx ( ) . span_err ( op_sp, msg) ;
475+ self . fcx . dcx ( ) . span_err ( op_sp, msg) ;
476476// register isn't enabled, don't do more checks
477477continue ;
478478}
@@ -512,7 +512,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
512512 ty:: Error ( _) =>{ }
513513 _if ty. is_integral ( ) =>{ }
514514 _ =>{
515- self . infcx
515+ self . fcx
516516. dcx ( )
517517. struct_span_err ( op_sp, "invalid type for `const` operand" )
518518. with_span_label (
@@ -531,7 +531,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
531531 ty:: FnDef ( ..) =>{ }
532532 ty:: Error ( _) =>{ }
533533 _ =>{
534- self . infcx
534+ self . fcx
535535. dcx ( )
536536. struct_span_err ( op_sp, "invalid `sym` operand" )
537537. with_span_label (