1- use std:: assert_matches:: debug_assert_matches;
2-
31use rustc_abi:: FieldIdx ;
42use rustc_ast:: InlineAsmTemplatePiece ;
53use rustc_data_structures:: fx:: FxIndexSet ;
@@ -21,6 +19,7 @@ pub struct InlineAsmCtxt<'a, 'tcx: 'a> {
2119 typing_env : ty:: TypingEnv < ' tcx > ,
2220 target_features : & ' tcx FxIndexSet < Symbol > ,
2321 expr_ty : Box < dyn Fn ( & hir:: Expr < ' tcx > ) -> Ty < ' tcx > + ' a > ,
22+ node_ty : Box < dyn Fn ( hir:: HirId ) -> Ty < ' tcx > + ' a > ,
2423}
2524
2625enum NonAsmTypeReason < ' tcx > {
@@ -35,20 +34,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
3534 tcx : TyCtxt < ' tcx > ,
3635 def_id : LocalDefId ,
3736 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 ,
3939 ) -> Self {
4040 InlineAsmCtxt {
4141 tcx,
4242 typing_env,
4343 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) ,
4546 }
4647 }
4748
4849 fn expr_ty ( & self , expr : & hir:: Expr < ' tcx > ) -> Ty < ' tcx > {
4950 ( self . expr_ty ) ( expr)
5051 }
5152
53+ fn node_ty ( & self , hir_id : hir:: HirId ) -> Ty < ' tcx > {
54+ ( self . node_ty ) ( hir_id)
55+ }
56+
5257 // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
5358 fn is_thin_ptr_ty ( & self , ty : Ty < ' tcx > ) -> bool {
5459 // Type still may have region variables, but `Sized` does not depend
@@ -487,12 +492,23 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
487492 ) ;
488493 }
489494 }
490- // Typeck has checked that Const operands are integers.
491495 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+ }
496512 }
497513 // Typeck has checked that SymFn refers to a function.
498514 hir:: InlineAsmOperand :: SymFn { expr } => {
0 commit comments