@@ -3,25 +3,22 @@ use rustc_ast::InlineAsmTemplatePiece;
3
3
use rustc_data_structures:: fx:: FxIndexSet ;
4
4
use rustc_hir:: def_id:: DefId ;
5
5
use rustc_hir:: { self as hir, LangItem } ;
6
- use rustc_infer:: infer:: InferCtxt ;
7
6
use 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 } ;
11
8
use rustc_session:: lint;
12
9
use rustc_span:: def_id:: LocalDefId ;
13
- use rustc_span:: { Symbol , sym} ;
10
+ use rustc_span:: { Span , Symbol , sym} ;
14
11
use rustc_target:: asm:: {
15
12
InlineAsmReg , InlineAsmRegClass , InlineAsmRegOrRegClass , InlineAsmType , ModifierInfo ,
16
13
} ;
14
+ use rustc_trait_selection:: infer:: InferCtxtExt ;
17
15
16
+ use crate :: FnCtxt ;
18
17
use crate :: errors:: RegisterTypeUnstable ;
19
18
20
- pub struct InlineAsmCtxt < ' a , ' tcx > {
21
- typing_env : ty:: TypingEnv < ' tcx > ,
19
+ pub ( crate ) struct InlineAsmCtxt < ' a , ' tcx > {
22
20
target_features : & ' tcx FxIndexSet < Symbol > ,
23
- infcx : & ' a InferCtxt < ' tcx > ,
24
- typeck_results : & ' a TypeckResults < ' tcx > ,
21
+ fcx : & ' a FnCtxt < ' a , ' tcx > ,
25
22
}
26
23
27
24
enum NonAsmTypeReason < ' tcx > {
@@ -33,27 +30,17 @@ enum NonAsmTypeReason<'tcx> {
33
30
}
34
31
35
32
impl < ' 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 }
48
35
}
49
36
50
37
fn tcx ( & self ) -> TyCtxt < ' tcx > {
51
- self . infcx . tcx
38
+ self . fcx . tcx
52
39
}
53
40
54
41
fn 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) ;
57
44
if ty. has_non_region_infer ( ) {
58
45
Ty :: new_misc_error ( self . tcx ( ) )
59
46
} else {
@@ -62,19 +49,23 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
62
49
}
63
50
64
51
// 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 {
66
53
// Type still may have region variables, but `Sized` does not depend
67
54
// 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 ) {
69
56
return true ;
70
57
}
71
- if let ty:: Foreign ( ..) = ty . kind ( ) {
58
+ if let ty:: Foreign ( ..) = self . fcx . try_structurally_resolve_type ( span , ty ) . kind ( ) {
72
59
return true ;
73
60
}
74
61
false
75
62
}
76
63
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 > > {
78
69
let asm_ty_isize = match self . tcx ( ) . sess . target . pointer_width {
79
70
16 => InlineAsmType :: I16 ,
80
71
32 => InlineAsmType :: I32 ,
@@ -95,7 +86,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
95
86
ty:: Float ( FloatTy :: F128 ) => Ok ( InlineAsmType :: F128 ) ,
96
87
ty:: FnPtr ( ..) => Ok ( asm_ty_isize) ,
97
88
ty:: RawPtr ( elem_ty, _) => {
98
- if self . is_thin_ptr_ty ( elem_ty) {
89
+ if self . is_thin_ptr_ty ( span , elem_ty) {
99
90
Ok ( asm_ty_isize)
100
91
} else {
101
92
Err ( NonAsmTypeReason :: NotSizedPtr ( ty) )
@@ -109,11 +100,20 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
109
100
let field = & fields[ FieldIdx :: ZERO ] ;
110
101
let elem_ty = field. ty ( self . tcx ( ) , args) ;
111
102
112
- let ( size, ty) = match elem_ty. kind ( ) {
103
+ let ( size, ty) = match * elem_ty. kind ( ) {
113
104
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
+ } ;
115
115
if let Some ( len) = len. try_to_target_usize ( self . tcx ( ) ) {
116
- ( len, * ty)
116
+ ( len, ty)
117
117
} else {
118
118
return Err ( NonAsmTypeReason :: UnevaluatedSIMDArrayLength (
119
119
field. did , len,
@@ -183,17 +183,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
183
183
) ;
184
184
let fields = & ty. non_enum_variant ( ) . fields ;
185
185
let ty = fields[ FieldIdx :: ZERO ] . ty ( self . tcx ( ) , args) ;
186
- self . get_asm_ty ( ty)
186
+ self . get_asm_ty ( expr . span , ty)
187
187
}
188
- _ => self . get_asm_ty ( ty) ,
188
+ _ => self . get_asm_ty ( expr . span , ty) ,
189
189
} ;
190
190
let asm_ty = match asm_ty {
191
191
Ok ( asm_ty) => asm_ty,
192
192
Err ( reason) => {
193
193
match reason {
194
194
NonAsmTypeReason :: UnevaluatedSIMDArrayLength ( did, len) => {
195
195
let msg = format ! ( "cannot evaluate SIMD vector length `{len}`" ) ;
196
- self . infcx
196
+ self . fcx
197
197
. dcx ( )
198
198
. struct_span_err ( self . tcx ( ) . def_span ( did) , msg)
199
199
. with_span_note (
@@ -204,7 +204,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
204
204
}
205
205
NonAsmTypeReason :: Invalid ( ty) => {
206
206
let 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 (
208
208
"only integers, floats, SIMD vectors, pointers and function pointers \
209
209
can be used as arguments for inline assembly",
210
210
) . emit ( ) ;
@@ -213,7 +213,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
213
213
let msg = format ! (
214
214
"cannot use value of unsized pointer type `{ty}` for inline assembly"
215
215
) ;
216
- self . infcx
216
+ self . fcx
217
217
. dcx ( )
218
218
. struct_span_err ( expr. span , msg)
219
219
. with_note ( "only sized pointers can be used in inline assembly" )
@@ -223,7 +223,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
223
223
let msg = format ! (
224
224
"cannot use SIMD vector with element type `{ty}` for inline assembly"
225
225
) ;
226
- self . infcx . dcx ( )
226
+ self . fcx . dcx ( )
227
227
. struct_span_err ( self . tcx ( ) . def_span ( did) , msg) . with_span_note (
228
228
expr. span ,
229
229
"only integers, floats, SIMD vectors, pointers and function pointers \
@@ -232,7 +232,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
232
232
}
233
233
NonAsmTypeReason :: EmptySIMDArray ( ty) => {
234
234
let 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 ( ) ;
236
236
}
237
237
}
238
238
return None ;
@@ -241,9 +241,9 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
241
241
242
242
// Check that the type implements Copy. The only case where this can
243
243
// 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) {
245
245
let msg = "arguments for inline assembly must be copyable" ;
246
- self . infcx
246
+ self . fcx
247
247
. dcx ( )
248
248
. struct_span_err ( expr. span , msg)
249
249
. with_note ( format ! ( "`{ty}` does not implement the Copy trait" ) )
@@ -263,7 +263,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
263
263
if in_asm_ty != asm_ty {
264
264
let msg = "incompatible types for asm inout argument" ;
265
265
let in_expr_ty = self . expr_ty ( in_expr) ;
266
- self . infcx
266
+ self . fcx
267
267
. dcx ( )
268
268
. struct_span_err ( vec ! [ in_expr. span, expr. span] , msg)
269
269
. with_span_label ( in_expr. span , format ! ( "type `{in_expr_ty}`" ) )
@@ -296,7 +296,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
296
296
)
297
297
} else {
298
298
let 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) ;
300
300
let supported_tys: Vec < _ > =
301
301
supported_tys. iter ( ) . map ( |( t, _) | t. to_string ( ) ) . collect ( ) ;
302
302
err. note ( format ! (
@@ -326,7 +326,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
326
326
if let Some ( feature) = feature {
327
327
if !self . target_features . contains ( feature) {
328
328
let msg = format ! ( "`{feature}` target feature is not enabled" ) ;
329
- self . infcx
329
+ self . fcx
330
330
. dcx ( )
331
331
. struct_span_err ( expr. span , msg)
332
332
. with_note ( format ! (
@@ -384,9 +384,9 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
384
384
Some ( asm_ty)
385
385
}
386
386
387
- pub fn check_asm ( & self , asm : & hir:: InlineAsm < ' tcx > ) {
387
+ pub ( crate ) fn check_asm ( & self , asm : & hir:: InlineAsm < ' tcx > ) {
388
388
let 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" ) ;
390
390
return ;
391
391
} ;
392
392
let allow_experimental_reg = self . tcx ( ) . features ( ) . asm_experimental_reg ( ) ;
@@ -418,7 +418,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
418
418
op. is_clobber ( ) ,
419
419
) {
420
420
let 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) ;
422
422
continue ;
423
423
}
424
424
}
@@ -458,7 +458,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
458
458
reg_class. name( ) ,
459
459
feature
460
460
) ;
461
- self . infcx . dcx ( ) . span_err ( op_sp, msg) ;
461
+ self . fcx . dcx ( ) . span_err ( op_sp, msg) ;
462
462
// register isn't enabled, don't do more checks
463
463
continue ;
464
464
}
@@ -472,7 +472,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
472
472
. intersperse( ", " )
473
473
. collect:: <String >( ) ,
474
474
) ;
475
- self . infcx . dcx ( ) . span_err ( op_sp, msg) ;
475
+ self . fcx . dcx ( ) . span_err ( op_sp, msg) ;
476
476
// register isn't enabled, don't do more checks
477
477
continue ;
478
478
}
@@ -512,7 +512,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
512
512
ty:: Error ( _) => { }
513
513
_ if ty. is_integral ( ) => { }
514
514
_ => {
515
- self . infcx
515
+ self . fcx
516
516
. dcx ( )
517
517
. struct_span_err ( op_sp, "invalid type for `const` operand" )
518
518
. with_span_label (
@@ -531,7 +531,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
531
531
ty:: FnDef ( ..) => { }
532
532
ty:: Error ( _) => { }
533
533
_ => {
534
- self . infcx
534
+ self . fcx
535
535
. dcx ( )
536
536
. struct_span_err ( op_sp, "invalid `sym` operand" )
537
537
. with_span_label (
0 commit comments