1
- use std:: assert_matches:: debug_assert_matches;
2
-
3
1
use rustc_abi:: FieldIdx ;
4
2
use rustc_ast:: InlineAsmTemplatePiece ;
5
3
use rustc_data_structures:: fx:: FxIndexSet ;
@@ -21,6 +19,7 @@ pub struct InlineAsmCtxt<'a, 'tcx: 'a> {
21
19
typing_env : ty:: TypingEnv < ' tcx > ,
22
20
target_features : & ' tcx FxIndexSet < Symbol > ,
23
21
expr_ty : Box < dyn Fn ( & hir:: Expr < ' tcx > ) -> Ty < ' tcx > + ' a > ,
22
+ node_ty : Box < dyn Fn ( hir:: HirId ) -> Ty < ' tcx > + ' a > ,
24
23
}
25
24
26
25
enum NonAsmTypeReason < ' tcx > {
@@ -35,20 +34,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
35
34
tcx : TyCtxt < ' tcx > ,
36
35
def_id : LocalDefId ,
37
36
typing_env : ty:: TypingEnv < ' tcx > ,
38
- get_operand_ty : impl Fn ( & hir:: Expr < ' tcx > ) -> Ty < ' tcx > + ' a ,
37
+ expr_ty : impl Fn ( & hir:: Expr < ' tcx > ) -> Ty < ' tcx > + ' a ,
38
+ node_ty : impl Fn ( hir:: HirId ) -> Ty < ' tcx > + ' a ,
39
39
) -> Self {
40
40
InlineAsmCtxt {
41
41
tcx,
42
42
typing_env,
43
43
target_features : tcx. asm_target_features ( def_id) ,
44
- expr_ty : Box :: new ( get_operand_ty) ,
44
+ expr_ty : Box :: new ( expr_ty) ,
45
+ node_ty : Box :: new ( node_ty) ,
45
46
}
46
47
}
47
48
48
49
fn expr_ty ( & self , expr : & hir:: Expr < ' tcx > ) -> Ty < ' tcx > {
49
50
( self . expr_ty ) ( expr)
50
51
}
51
52
53
+ fn node_ty ( & self , hir_id : hir:: HirId ) -> Ty < ' tcx > {
54
+ ( self . node_ty ) ( hir_id)
55
+ }
56
+
52
57
// FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
53
58
fn is_thin_ptr_ty ( & self , ty : Ty < ' tcx > ) -> bool {
54
59
// Type still may have region variables, but `Sized` does not depend
@@ -487,12 +492,23 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
487
492
) ;
488
493
}
489
494
}
490
- // Typeck has checked that Const operands are integers.
491
495
hir:: InlineAsmOperand :: Const { anon_const } => {
492
- debug_assert_matches ! (
493
- self . tcx. type_of( anon_const. def_id) . instantiate_identity( ) . kind( ) ,
494
- ty:: Error ( _) | ty:: Int ( _) | ty:: Uint ( _)
495
- ) ;
496
+ let ty = self . node_ty ( anon_const. hir_id ) ;
497
+ match ty. kind ( ) {
498
+ ty:: Error ( _) => { }
499
+ _ if ty. is_integral ( ) => { }
500
+ _ => {
501
+ self . tcx
502
+ . dcx ( )
503
+ . struct_span_err ( op_sp, "invalid type for `const` operand" )
504
+ . with_span_label (
505
+ self . tcx . def_span ( anon_const. def_id ) ,
506
+ format ! ( "is {} `{}`" , ty. kind( ) . article( ) , ty) ,
507
+ )
508
+ . with_help ( "`const` operands must be of an integer type" )
509
+ . emit ( ) ;
510
+ }
511
+ }
496
512
}
497
513
// Typeck has checked that SymFn refers to a function.
498
514
hir:: InlineAsmOperand :: SymFn { expr } => {
0 commit comments