]> git.lizzy.rs Git - rust.git/commitdiff
change `InlineAsmCtxt` to not talk about `FnCtxt`
authorlcnr <rust@lcnr.de>
Sat, 13 Aug 2022 17:57:22 +0000 (19:57 +0200)
committerlcnr <rust@lcnr.de>
Sat, 13 Aug 2022 17:58:39 +0000 (19:58 +0200)
compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
compiler/rustc_typeck/src/check/intrinsicck.rs

index 1d1f755947fdcde4a167d5776f08a80c8746af3c..8f0d9dd71a850673640cef6d75055152e2a9f339 100644 (file)
@@ -58,7 +58,17 @@ pub(in super::super) fn check_asms(&self) {
         debug!("FnCtxt::check_asm: {} deferred checks", deferred_asm_checks.len());
         for (asm, hir_id) in deferred_asm_checks.drain(..) {
             let enclosing_id = self.tcx.hir().enclosing_body_owner(hir_id);
-            InlineAsmCtxt::new_in_fn(self)
+            let get_operand_ty = |expr| {
+                let ty = self.typeck_results.borrow().expr_ty_adjusted(expr);
+                let ty = self.resolve_vars_if_possible(ty);
+                if ty.has_infer_types_or_consts() {
+                    assert!(self.is_tainted_by_errors());
+                    self.tcx.ty_error()
+                } else {
+                    self.tcx.erase_regions(ty)
+                }
+            };
+            InlineAsmCtxt::new_in_fn(self.tcx, self.param_env, get_operand_ty)
                 .check_asm(asm, self.tcx.hir().local_def_id_to_hir_id(enclosing_id));
         }
     }
index df94abbafb1bd7e9cb726c2935a074bd650c6a84..721ebba6514779066e486d8214ebe3ab2bd76aea 100644 (file)
@@ -9,7 +9,6 @@
 use rustc_span::{Span, Symbol, DUMMY_SP};
 use rustc_target::abi::{Pointer, VariantIdx};
 use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
-use rustc_trait_selection::infer::InferCtxtExt;
 
 use super::FnCtxt;
 
@@ -98,12 +97,36 @@ pub fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
         }
         err.emit();
     }
+}
+
+pub struct InlineAsmCtxt<'a, 'tcx> {
+    tcx: TyCtxt<'tcx>,
+    param_env: ty::ParamEnv<'tcx>,
+    get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
+}
+
+impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
+    pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
+        InlineAsmCtxt {
+            tcx,
+            param_env: ty::ParamEnv::empty(),
+            get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
+        }
+    }
+
+    pub fn new_in_fn(
+        tcx: TyCtxt<'tcx>,
+        param_env: ty::ParamEnv<'tcx>,
+        get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
+    ) -> Self {
+        InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
+    }
 
     // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
     fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
         // Type still may have region variables, but `Sized` does not depend
         // on those, so just erase them before querying.
-        if self.tcx.erase_regions(ty).is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
+        if ty.is_sized(self.tcx.at(DUMMY_SP), self.param_env) {
             return true;
         }
         if let ty::Foreign(..) = ty.kind() {
@@ -111,36 +134,21 @@ fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
         }
         false
     }
-}
-
-pub struct InlineAsmCtxt<'a, 'tcx> {
-    tcx: TyCtxt<'tcx>,
-    fcx: Option<&'a FnCtxt<'a, 'tcx>>,
-}
-
-impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
-    pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
-        InlineAsmCtxt { tcx, fcx: None }
-    }
-
-    pub fn new_in_fn(fcx: &'a FnCtxt<'a, 'tcx>) -> Self {
-        InlineAsmCtxt { tcx: fcx.tcx, fcx: Some(fcx) }
-    }
 
     fn check_asm_operand_type(
         &self,
         idx: usize,
         reg: InlineAsmRegOrRegClass,
-        expr: &hir::Expr<'tcx>,
+        expr: &'tcx hir::Expr<'tcx>,
         template: &[InlineAsmTemplatePiece],
         is_input: bool,
-        tied_input: Option<(&hir::Expr<'tcx>, Option<InlineAsmType>)>,
+        tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
         target_features: &FxHashSet<Symbol>,
     ) -> Option<InlineAsmType> {
-        let fcx = self.fcx.unwrap_or_else(|| span_bug!(expr.span, "asm operand for global asm"));
-        // Check the type against the allowed types for inline asm.
-        let ty = fcx.typeck_results.borrow().expr_ty_adjusted(expr);
-        let ty = fcx.resolve_vars_if_possible(ty);
+        let ty = (self.get_operand_ty)(expr);
+        if ty.has_infer_types_or_consts() {
+            bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
+        }
         let asm_ty_isize = match self.tcx.sess.target.pointer_width {
             16 => InlineAsmType::I16,
             32 => InlineAsmType::I32,
@@ -148,12 +156,6 @@ fn check_asm_operand_type(
             _ => unreachable!(),
         };
 
-        // Expect types to be fully resolved, no const or type variables.
-        if ty.has_infer_types_or_consts() {
-            assert!(fcx.is_tainted_by_errors());
-            return None;
-        }
-
         let asm_ty = match *ty.kind() {
             // `!` is allowed for input but not for output (issue #87802)
             ty::Never if is_input => return None,
@@ -167,7 +169,7 @@ fn check_asm_operand_type(
             ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
             ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
             ty::FnPtr(_) => Some(asm_ty_isize),
-            ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if fcx.is_thin_ptr_ty(ty) => {
+            ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if self.is_thin_ptr_ty(ty) => {
                 Some(asm_ty_isize)
             }
             ty::Adt(adt, substs) if adt.repr().simd() => {
@@ -219,7 +221,7 @@ fn check_asm_operand_type(
 
         // Check that the type implements Copy. The only case where this can
         // possibly fail is for SIMD types which don't #[derive(Copy)].
-        if !fcx.infcx.type_is_copy_modulo_regions(fcx.param_env, ty, DUMMY_SP) {
+        if !ty.is_copy_modulo_regions(self.tcx.at(expr.span), self.param_env) {
             let msg = "arguments for inline assembly must be copyable";
             let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
             err.note(&format!("`{ty}` does not implement the Copy trait"));
@@ -240,8 +242,7 @@ fn check_asm_operand_type(
                 let msg = "incompatible types for asm inout argument";
                 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
 
-                let in_expr_ty = fcx.typeck_results.borrow().expr_ty_adjusted(in_expr);
-                let in_expr_ty = fcx.resolve_vars_if_possible(in_expr_ty);
+                let in_expr_ty = (self.get_operand_ty)(in_expr);
                 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
                 err.span_label(expr.span, &format!("type `{ty}`"));
                 err.note(