about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-13 18:15:06 +0000
committerbors <bors@rust-lang.org>2022-09-13 18:15:06 +0000
commitc84083b08e2db69fcf270c4045837fa02663a3bf (patch)
tree277677faf6f1f7a00a808329f8d15cfd05f77829 /compiler
parent1ce51982b8550c782ded466c1abff0d2b2e21c4e (diff)
parentce9daa2f914dd48adef4e9e661391f6cb40e9893 (diff)
downloadrust-c84083b08e2db69fcf270c4045837fa02663a3bf.tar.gz
rust-c84083b08e2db69fcf270c4045837fa02663a3bf.zip
Auto merge of #101086 - cjgillot:thir-param, r=oli-obk
Compute information about function parameters on THIR

This avoids some manipulation of typeck results while building MIR.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/move_errors.rs2
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs14
-rw-r--r--compiler/rustc_borrowck/src/nll.rs5
-rw-r--r--compiler/rustc_hir/src/hir.rs2
-rw-r--r--compiler/rustc_middle/src/mir/mod.rs18
-rw-r--r--compiler/rustc_middle/src/thir.rs27
-rw-r--r--compiler/rustc_mir_build/src/build/matches/mod.rs2
-rw-r--r--compiler/rustc_mir_build/src/build/mod.rs597
-rw-r--r--compiler/rustc_mir_build/src/thir/cx/mod.rs114
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/mod.rs24
-rw-r--r--compiler/rustc_smir/src/mir.rs12
11 files changed, 390 insertions, 427 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs
index 16c2f9ccc6a..8d4c38d3a8e 100644
--- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs
@@ -360,7 +360,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
 
                 diag.span_label(upvar_span, "captured outer variable");
                 diag.span_label(
-                    self.body.span,
+                    self.infcx.tcx.def_span(def_id),
                     format!("captured by this `{closure_kind}` closure"),
                 );
 
diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
index 2c6c461267a..6b5014fa909 100644
--- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
@@ -9,10 +9,7 @@ use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
 use rustc_middle::ty::{self, Ty, TyCtxt};
 use rustc_middle::{
     hir::place::PlaceBase,
-    mir::{
-        self, BindingForm, ClearCrossCrate, ImplicitSelfKind, Local, LocalDecl, LocalInfo,
-        LocalKind, Location,
-    },
+    mir::{self, BindingForm, ClearCrossCrate, Local, LocalDecl, LocalInfo, LocalKind, Location},
 };
 use rustc_span::source_map::DesugaringKind;
 use rustc_span::symbol::{kw, Symbol};
@@ -312,7 +309,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
                     && !matches!(
                         decl.local_info,
                         Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
-                            ImplicitSelfKind::MutRef
+                            hir::ImplicitSelfKind::MutRef
                         ))))
                     )
                 {
@@ -973,6 +970,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
 
         let hir = self.infcx.tcx.hir();
         let closure_id = self.mir_hir_id();
+        let closure_span = self.infcx.tcx.def_span(self.mir_def_id());
         let fn_call_id = hir.get_parent_node(closure_id);
         let node = hir.get(fn_call_id);
         let def_id = hir.enclosing_body_owner(fn_call_id);
@@ -1024,7 +1022,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
                 if let Some(span) = arg {
                     err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
                     err.span_label(func.span, "expects `Fn` instead of `FnMut`");
-                    err.span_label(self.body.span, "in this closure");
+                    err.span_label(closure_span, "in this closure");
                     look_at_return = false;
                 }
             }
@@ -1050,7 +1048,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
                         sig.decl.output.span(),
                         "change this to return `FnMut` instead of `Fn`",
                     );
-                    err.span_label(self.body.span, "in this closure");
+                    err.span_label(closure_span, "in this closure");
                 }
                 _ => {}
             }
@@ -1074,7 +1072,7 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
             //
             // Deliberately fall into this case for all implicit self types,
             // so that we don't fall in to the next case with them.
-            *kind == mir::ImplicitSelfKind::MutRef
+            *kind == hir::ImplicitSelfKind::MutRef
         }
         _ if Some(kw::SelfLower) == local_name => {
             // Otherwise, check if the name is the `self` keyword - in which case
diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs
index 0961203d76d..12b2481cc79 100644
--- a/compiler/rustc_borrowck/src/nll.rs
+++ b/compiler/rustc_borrowck/src/nll.rs
@@ -389,8 +389,9 @@ pub(super) fn dump_annotation<'a, 'tcx>(
     // viewing the intraprocedural state, the -Zdump-mir output is
     // better.
 
+    let def_span = tcx.def_span(body.source.def_id());
     let mut err = if let Some(closure_region_requirements) = closure_region_requirements {
-        let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "external requirements");
+        let mut err = tcx.sess.diagnostic().span_note_diag(def_span, "external requirements");
 
         regioncx.annotate(tcx, &mut err);
 
@@ -409,7 +410,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
 
         err
     } else {
-        let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "no external requirements");
+        let mut err = tcx.sess.diagnostic().span_note_diag(def_span, "no external requirements");
         regioncx.annotate(tcx, &mut err);
 
         err
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs
index 1d62caef9b7..037cbc1be9a 100644
--- a/compiler/rustc_hir/src/hir.rs
+++ b/compiler/rustc_hir/src/hir.rs
@@ -2661,7 +2661,7 @@ pub struct FnDecl<'hir> {
 }
 
 /// Represents what type of implicit self a function has, if any.
-#[derive(Copy, Clone, Encodable, Decodable, Debug, HashStable_Generic)]
+#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
 pub enum ImplicitSelfKind {
     /// Represents a `fn x(self);`.
     Imm,
diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs
index dd768c5358d..9a6d34c8ddd 100644
--- a/compiler/rustc_middle/src/mir/mod.rs
+++ b/compiler/rustc_middle/src/mir/mod.rs
@@ -18,7 +18,7 @@ use rustc_data_structures::captures::Captures;
 use rustc_errors::ErrorGuaranteed;
 use rustc_hir::def::{CtorKind, Namespace};
 use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
-use rustc_hir::{self, GeneratorKind};
+use rustc_hir::{self, GeneratorKind, ImplicitSelfKind};
 use rustc_hir::{self as hir, HirId};
 use rustc_session::Session;
 use rustc_target::abi::{Size, VariantIdx};
@@ -653,22 +653,6 @@ pub enum BindingForm<'tcx> {
     RefForGuard,
 }
 
-/// Represents what type of implicit self a function has, if any.
-#[derive(Clone, Copy, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
-pub enum ImplicitSelfKind {
-    /// Represents a `fn x(self);`.
-    Imm,
-    /// Represents a `fn x(mut self);`.
-    Mut,
-    /// Represents a `fn x(&self);`.
-    ImmRef,
-    /// Represents a `fn x(&mut self);`.
-    MutRef,
-    /// Represents when a function does not have a self argument or
-    /// when a function has a `self: X` argument.
-    None,
-}
-
 TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx>, }
 
 mod binding_form_impl {
diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs
index c50f8b0eebe..165b9103968 100644
--- a/compiler/rustc_middle/src/thir.rs
+++ b/compiler/rustc_middle/src/thir.rs
@@ -73,11 +73,29 @@ macro_rules! thir_with_elements {
     }
 }
 
+pub const UPVAR_ENV_PARAM: ParamId = ParamId::from_u32(0);
+
 thir_with_elements! {
     arms: ArmId => Arm<'tcx> => "a{}",
     blocks: BlockId => Block => "b{}",
     exprs: ExprId => Expr<'tcx> => "e{}",
     stmts: StmtId => Stmt<'tcx> => "s{}",
+    params: ParamId => Param<'tcx> => "p{}",
+}
+
+/// Description of a type-checked function parameter.
+#[derive(Clone, Debug, HashStable)]
+pub struct Param<'tcx> {
+    /// The pattern that appears in the parameter list, or None for implicit parameters.
+    pub pat: Option<Box<Pat<'tcx>>>,
+    /// The possibly inferred type.
+    pub ty: Ty<'tcx>,
+    /// Span of the explicitly provided type, or None if inferred for closures.
+    pub ty_span: Option<Span>,
+    /// Whether this param is `self`, and how it is bound.
+    pub self_kind: Option<hir::ImplicitSelfKind>,
+    /// HirId for lints.
+    pub hir_id: Option<hir::HirId>,
 }
 
 #[derive(Copy, Clone, Debug, HashStable)]
@@ -548,6 +566,15 @@ impl<'tcx> Pat<'tcx> {
     pub fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
         Pat { ty, span: DUMMY_SP, kind: PatKind::Wild }
     }
+
+    pub fn simple_ident(&self) -> Option<Symbol> {
+        match self.kind {
+            PatKind::Binding { name, mode: BindingMode::ByValue, subpattern: None, .. } => {
+                Some(name)
+            }
+            _ => None,
+        }
+    }
 }
 
 #[derive(Clone, Debug, HashStable)]
diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs
index 505273033a4..b1cb9b9f084 100644
--- a/compiler/rustc_mir_build/src/build/matches/mod.rs
+++ b/compiler/rustc_mir_build/src/build/matches/mod.rs
@@ -568,7 +568,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
 
             _ => {
                 let place_builder = unpack!(block = self.as_place_builder(block, initializer));
-                self.place_into_pattern(block, irrefutable_pat, place_builder, true)
+                self.place_into_pattern(block, &irrefutable_pat, place_builder, true)
             }
         }
     }
diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs
index 7d41969a314..25c4e51cb92 100644
--- a/compiler/rustc_mir_build/src/build/mod.rs
+++ b/compiler/rustc_mir_build/src/build/mod.rs
@@ -1,16 +1,14 @@
-use crate::build;
 pub(crate) use crate::build::expr::as_constant::lit_to_mir_constant;
 use crate::build::expr::as_place::PlaceBuilder;
 use crate::build::scope::DropKind;
-use crate::thir::pattern::pat_from_hir;
 use rustc_apfloat::ieee::{Double, Single};
 use rustc_apfloat::Float;
 use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::sorted_map::SortedIndexMultiMap;
 use rustc_errors::ErrorGuaranteed;
 use rustc_hir as hir;
+use rustc_hir::def::DefKind;
 use rustc_hir::def_id::{DefId, LocalDefId};
-use rustc_hir::lang_items::LangItem;
 use rustc_hir::{GeneratorKind, Node};
 use rustc_index::vec::{Idx, IndexVec};
 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -19,8 +17,9 @@ use rustc_middle::middle::region;
 use rustc_middle::mir::interpret::ConstValue;
 use rustc_middle::mir::interpret::Scalar;
 use rustc_middle::mir::*;
-use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, LocalVarId, PatKind, Thir};
-use rustc_middle::ty::subst::Subst;
+use rustc_middle::thir::{
+    self, BindingMode, Expr, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir,
+};
 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable, TypeckResults};
 use rustc_span::symbol::sym;
 use rustc_span::Span;
@@ -48,9 +47,7 @@ pub(crate) fn mir_built<'tcx>(
 
 /// Construct the MIR for a given `DefId`.
 fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
-    let id = tcx.hir().local_def_id_to_hir_id(def.did);
     let body_owner_kind = tcx.hir().body_owner_kind(def.did);
-    let typeck_results = tcx.typeck_opt_const_arg(def);
 
     // Ensure unsafeck and abstract const building is ran before we steal the THIR.
     // We can't use `ensure()` for `thir_abstract_const` as it doesn't compute the query
@@ -67,246 +64,42 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
         }
     }
 
-    // Figure out what primary body this item has.
-    let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
-        Node::Expr(hir::Expr {
-            kind: hir::ExprKind::Closure(hir::Closure { fn_decl, body, .. }),
-            ..
-        }) => (*body, fn_decl.output.span(), None),
-        Node::Item(hir::Item {
-            kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id),
-            span,
-            ..
-        })
-        | Node::ImplItem(hir::ImplItem {
-            kind: hir::ImplItemKind::Fn(hir::FnSig { decl, .. }, body_id),
-            span,
-            ..
-        })
-        | Node::TraitItem(hir::TraitItem {
-            kind: hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)),
-            span,
-            ..
-        }) => {
-            // Use the `Span` of the `Item/ImplItem/TraitItem` as the body span,
-            // since the def span of a function does not include the body
-            (*body_id, decl.output.span(), Some(*span))
-        }
-        Node::Item(hir::Item {
-            kind: hir::ItemKind::Static(ty, _, body_id) | hir::ItemKind::Const(ty, body_id),
-            ..
-        })
-        | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, body_id), .. })
-        | Node::TraitItem(hir::TraitItem {
-            kind: hir::TraitItemKind::Const(ty, Some(body_id)),
-            ..
-        }) => (*body_id, ty.span, None),
-        Node::AnonConst(hir::AnonConst { body, hir_id, .. }) => {
-            (*body, tcx.hir().span(*hir_id), None)
-        }
-
-        _ => span_bug!(tcx.hir().span(id), "can't build MIR for {:?}", def.did),
-    };
-
-    // If we don't have a specialized span for the body, just use the
-    // normal def span.
-    let span_with_body = span_with_body.unwrap_or_else(|| tcx.hir().span(id));
-
-    tcx.infer_ctxt().enter(|infcx| {
-        let body = if let Some(error_reported) = typeck_results.tainted_by_errors {
-            build::construct_error(&infcx, def, id, body_id, body_owner_kind, error_reported)
-        } else if body_owner_kind.is_fn_or_closure() {
-            // fetch the fully liberated fn signature (that is, all bound
-            // types/lifetimes replaced)
-            let fn_sig = typeck_results.liberated_fn_sigs()[id];
-            let fn_def_id = tcx.hir().local_def_id(id);
-
-            let safety = match fn_sig.unsafety {
-                hir::Unsafety::Normal => Safety::Safe,
-                hir::Unsafety::Unsafe => Safety::FnUnsafe,
-            };
-
-            let body = tcx.hir().body(body_id);
-            let (thir, expr) = tcx
-                .thir_body(def)
-                .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0)));
+    let body = match tcx.thir_body(def) {
+        Err(error_reported) => construct_error(tcx, def.did, body_owner_kind, error_reported),
+        Ok((thir, expr)) => {
             // We ran all queries that depended on THIR at the beginning
             // of `mir_build`, so now we can steal it
             let thir = thir.steal();
-            let ty = tcx.type_of(fn_def_id);
-            let mut abi = fn_sig.abi;
-            let implicit_argument = match ty.kind() {
-                ty::Closure(..) => {
-                    // HACK(eddyb) Avoid having RustCall on closures,
-                    // as it adds unnecessary (and wrong) auto-tupling.
-                    abi = Abi::Rust;
-                    vec![ArgInfo(liberated_closure_env_ty(tcx, id, body_id), None, None, None)]
-                }
-                ty::Generator(..) => {
-                    let gen_ty = tcx.typeck_body(body_id).node_type(id);
-
-                    // The resume argument may be missing, in that case we need to provide it here.
-                    // It will always be `()` in this case.
-                    if body.params.is_empty() {
-                        vec![
-                            ArgInfo(gen_ty, None, None, None),
-                            ArgInfo(tcx.mk_unit(), None, None, None),
-                        ]
-                    } else {
-                        vec![ArgInfo(gen_ty, None, None, None)]
-                    }
-                }
-                _ => vec![],
-            };
 
-            let explicit_arguments = body.params.iter().enumerate().map(|(index, arg)| {
-                let owner_id = tcx.hir().body_owner(body_id);
-                let opt_ty_info;
-                let self_arg;
-                if let Some(ref fn_decl) = tcx.hir().fn_decl_by_hir_id(owner_id) {
-                    opt_ty_info = fn_decl
-                        .inputs
-                        .get(index)
-                        // Make sure that inferred closure args have no type span
-                        .and_then(|ty| if arg.pat.span != ty.span { Some(ty.span) } else { None });
-                    self_arg = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
-                        match fn_decl.implicit_self {
-                            hir::ImplicitSelfKind::Imm => Some(ImplicitSelfKind::Imm),
-                            hir::ImplicitSelfKind::Mut => Some(ImplicitSelfKind::Mut),
-                            hir::ImplicitSelfKind::ImmRef => Some(ImplicitSelfKind::ImmRef),
-                            hir::ImplicitSelfKind::MutRef => Some(ImplicitSelfKind::MutRef),
-                            _ => None,
-                        }
-                    } else {
-                        None
-                    };
-                } else {
-                    opt_ty_info = None;
-                    self_arg = None;
-                }
-
-                // C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
-                // (as it's created inside the body itself, not passed in from outside).
-                let ty = if fn_sig.c_variadic && index == fn_sig.inputs().len() {
-                    let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(arg.span));
-
-                    tcx.bound_type_of(va_list_did).subst(tcx, &[tcx.lifetimes.re_erased.into()])
-                } else {
-                    fn_sig.inputs()[index]
-                };
-
-                ArgInfo(ty, opt_ty_info, Some(&arg), self_arg)
-            });
-
-            let arguments = implicit_argument.into_iter().chain(explicit_arguments);
-
-            let (yield_ty, return_ty) = if body.generator_kind.is_some() {
-                let gen_ty = tcx.typeck_body(body_id).node_type(id);
-                let gen_sig = match gen_ty.kind() {
-                    ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(),
-                    _ => span_bug!(tcx.hir().span(id), "generator w/o generator type: {:?}", ty),
-                };
-                (Some(gen_sig.yield_ty), gen_sig.return_ty)
+            if body_owner_kind.is_fn_or_closure() {
+                construct_fn(tcx, def, &thir, expr)
             } else {
-                (None, fn_sig.output())
-            };
-
-            let mut mir = build::construct_fn(
-                &thir,
-                &infcx,
-                def,
-                id,
-                arguments,
-                safety,
-                abi,
-                return_ty,
-                return_ty_span,
-                body,
-                expr,
-                span_with_body,
-            );
-            if yield_ty.is_some() {
-                mir.generator.as_mut().unwrap().yield_ty = yield_ty;
+                construct_const(tcx, def, &thir, expr)
             }
-            mir
-        } else {
-            // Get the revealed type of this const. This is *not* the adjusted
-            // type of its body, which may be a subtype of this type. For
-            // example:
-            //
-            // fn foo(_: &()) {}
-            // static X: fn(&'static ()) = foo;
-            //
-            // The adjusted type of the body of X is `for<'a> fn(&'a ())` which
-            // is not the same as the type of X. We need the type of the return
-            // place to be the type of the constant because NLL typeck will
-            // equate them.
-
-            let return_ty = typeck_results.node_type(id);
-
-            let (thir, expr) = tcx
-                .thir_body(def)
-                .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0)));
-            // We ran all queries that depended on THIR at the beginning
-            // of `mir_build`, so now we can steal it
-            let thir = thir.steal();
-
-            let span_with_body = span_with_body.to(tcx.hir().span(body_id.hir_id));
-
-            build::construct_const(
-                &thir,
-                &infcx,
-                expr,
-                def,
-                id,
-                return_ty,
-                return_ty_span,
-                span_with_body,
-            )
-        };
+        }
+    };
 
-        lints::check(tcx, &body);
-
-        // The borrow checker will replace all the regions here with its own
-        // inference variables. There's no point having non-erased regions here.
-        // The exception is `body.user_type_annotations`, which is used unmodified
-        // by borrow checking.
-        debug_assert!(
-            !(body.local_decls.has_free_regions()
-                || body.basic_blocks.has_free_regions()
-                || body.var_debug_info.has_free_regions()
-                || body.yield_ty().has_free_regions()),
-            "Unexpected free regions in MIR: {:?}",
-            body,
-        );
+    lints::check(tcx, &body);
+
+    // The borrow checker will replace all the regions here with its own
+    // inference variables. There's no point having non-erased regions here.
+    // The exception is `body.user_type_annotations`, which is used unmodified
+    // by borrow checking.
+    debug_assert!(
+        !(body.local_decls.has_free_regions()
+            || body.basic_blocks.has_free_regions()
+            || body.var_debug_info.has_free_regions()
+            || body.yield_ty().has_free_regions()),
+        "Unexpected free regions in MIR: {:?}",
+        body,
+    );
 
-        body
-    })
+    body
 }
 
 ///////////////////////////////////////////////////////////////////////////
 // BuildMir -- walks a crate, looking for fn items and methods to build MIR from
 
-fn liberated_closure_env_ty(
-    tcx: TyCtxt<'_>,
-    closure_expr_id: hir::HirId,
-    body_id: hir::BodyId,
-) -> Ty<'_> {
-    let closure_ty = tcx.typeck_body(body_id).node_type(closure_expr_id);
-
-    let ty::Closure(closure_def_id, closure_substs) = *closure_ty.kind() else {
-        bug!("closure expr does not have closure type: {:?}", closure_ty);
-    };
-
-    let bound_vars =
-        tcx.mk_bound_variable_kinds(std::iter::once(ty::BoundVariableKind::Region(ty::BrEnv)));
-    let br =
-        ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind: ty::BrEnv };
-    let env_region = ty::ReLateBound(ty::INNERMOST, br);
-    let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs, env_region).unwrap();
-    tcx.erase_late_bound_regions(ty::Binder::bind_with_vars(closure_env_ty, bound_vars))
-}
-
 #[derive(Debug, PartialEq, Eq)]
 enum BlockFrame {
     /// Evaluation is currently within a statement.
@@ -364,7 +157,7 @@ struct BlockContext(Vec<BlockFrame>);
 
 struct Builder<'a, 'tcx> {
     tcx: TyCtxt<'tcx>,
-    infcx: &'a InferCtxt<'a, 'tcx>,
+    infcx: InferCtxt<'a, 'tcx>,
     typeck_results: &'tcx TypeckResults<'tcx>,
     region_scope_tree: &'tcx region::ScopeTree,
     param_env: ty::ParamEnv<'tcx>,
@@ -636,148 +429,212 @@ macro_rules! unpack {
 ///////////////////////////////////////////////////////////////////////////
 /// the main entry point for building MIR for a function
 
-struct ArgInfo<'tcx>(
-    Ty<'tcx>,
-    Option<Span>,
-    Option<&'tcx hir::Param<'tcx>>,
-    Option<ImplicitSelfKind>,
-);
-
-fn construct_fn<'tcx, A>(
-    thir: &Thir<'tcx>,
-    infcx: &InferCtxt<'_, 'tcx>,
+fn construct_fn<'tcx>(
+    tcx: TyCtxt<'tcx>,
     fn_def: ty::WithOptConstParam<LocalDefId>,
-    fn_id: hir::HirId,
-    arguments: A,
-    safety: Safety,
-    abi: Abi,
-    return_ty: Ty<'tcx>,
-    return_ty_span: Span,
-    body: &'tcx hir::Body<'tcx>,
+    thir: &Thir<'tcx>,
     expr: ExprId,
-    span_with_body: Span,
-) -> Body<'tcx>
-where
-    A: Iterator<Item = ArgInfo<'tcx>>,
-{
-    let arguments: Vec<_> = arguments.collect();
-
-    let tcx = infcx.tcx;
-    let span = tcx.hir().span(fn_id);
-
-    let mut builder = Builder::new(
-        thir,
-        infcx,
-        fn_def,
-        fn_id,
-        span_with_body,
-        arguments.len(),
-        safety,
-        return_ty,
-        return_ty_span,
-        body.generator_kind,
-    );
+) -> Body<'tcx> {
+    let span = tcx.def_span(fn_def.did);
+    let fn_id = tcx.hir().local_def_id_to_hir_id(fn_def.did);
+    let generator_kind = tcx.generator_kind(fn_def.did);
 
-    let call_site_scope =
-        region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite };
-    let arg_scope =
-        region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments };
-    let source_info = builder.source_info(span);
-    let call_site_s = (call_site_scope, source_info);
-    unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
-        let arg_scope_s = (arg_scope, source_info);
-        // Attribute epilogue to function's closing brace
-        let fn_end = span_with_body.shrink_to_hi();
-        let return_block =
-            unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
-                Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
-                    builder.args_and_body(
-                        START_BLOCK,
-                        fn_def.did,
-                        &arguments,
-                        arg_scope,
-                        &thir[expr],
-                    )
-                }))
-            }));
-        let source_info = builder.source_info(fn_end);
-        builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
-        builder.build_drop_trees();
-        return_block.unit()
-    }));
+    // Figure out what primary body this item has.
+    let body_id = tcx.hir().body_owned_by(fn_def.did);
+    let span_with_body = tcx.hir().span_with_body(fn_id);
+    let return_ty_span = tcx
+        .hir()
+        .fn_decl_by_hir_id(fn_id)
+        .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def.did))
+        .output
+        .span();
+
+    // fetch the fully liberated fn signature (that is, all bound
+    // types/lifetimes replaced)
+    let typeck_results = tcx.typeck_opt_const_arg(fn_def);
+    let fn_sig = typeck_results.liberated_fn_sigs()[fn_id];
+
+    let safety = match fn_sig.unsafety {
+        hir::Unsafety::Normal => Safety::Safe,
+        hir::Unsafety::Unsafe => Safety::FnUnsafe,
+    };
+
+    let mut abi = fn_sig.abi;
+    if let DefKind::Closure = tcx.def_kind(fn_def.did) {
+        // HACK(eddyb) Avoid having RustCall on closures,
+        // as it adds unnecessary (and wrong) auto-tupling.
+        abi = Abi::Rust;
+    }
+
+    let arguments = &thir.params;
+
+    let (yield_ty, return_ty) = if generator_kind.is_some() {
+        let gen_ty = arguments[thir::UPVAR_ENV_PARAM].ty;
+        let gen_sig = match gen_ty.kind() {
+            ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(),
+            _ => {
+                span_bug!(span, "generator w/o generator type: {:?}", gen_ty)
+            }
+        };
+        (Some(gen_sig.yield_ty), gen_sig.return_ty)
+    } else {
+        (None, fn_sig.output())
+    };
 
-    let spread_arg = if abi == Abi::RustCall {
+    let mut body = tcx.infer_ctxt().enter(|infcx| {
+        let mut builder = Builder::new(
+            thir,
+            infcx,
+            fn_def,
+            fn_id,
+            span_with_body,
+            arguments.len(),
+            safety,
+            return_ty,
+            return_ty_span,
+            generator_kind,
+        );
+
+        let call_site_scope =
+            region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::CallSite };
+        let arg_scope =
+            region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::Arguments };
+        let source_info = builder.source_info(span);
+        let call_site_s = (call_site_scope, source_info);
+        unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
+            let arg_scope_s = (arg_scope, source_info);
+            // Attribute epilogue to function's closing brace
+            let fn_end = span_with_body.shrink_to_hi();
+            let return_block = unpack!(builder.in_breakable_scope(
+                None,
+                Place::return_place(),
+                fn_end,
+                |builder| {
+                    Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
+                        builder.args_and_body(
+                            START_BLOCK,
+                            fn_def.did,
+                            arguments,
+                            arg_scope,
+                            &thir[expr],
+                        )
+                    }))
+                }
+            ));
+            let source_info = builder.source_info(fn_end);
+            builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
+            builder.build_drop_trees();
+            return_block.unit()
+        }));
+
+        builder.finish()
+    });
+
+    body.spread_arg = if abi == Abi::RustCall {
         // RustCall pseudo-ABI untuples the last argument.
         Some(Local::new(arguments.len()))
     } else {
         None
     };
-
-    let mut body = builder.finish();
-    body.spread_arg = spread_arg;
+    if yield_ty.is_some() {
+        body.generator.as_mut().unwrap().yield_ty = yield_ty;
+    }
     body
 }
 
 fn construct_const<'a, 'tcx>(
+    tcx: TyCtxt<'tcx>,
+    def: ty::WithOptConstParam<LocalDefId>,
     thir: &'a Thir<'tcx>,
-    infcx: &'a InferCtxt<'a, 'tcx>,
     expr: ExprId,
-    def: ty::WithOptConstParam<LocalDefId>,
-    hir_id: hir::HirId,
-    const_ty: Ty<'tcx>,
-    const_ty_span: Span,
-    span: Span,
 ) -> Body<'tcx> {
-    let mut builder = Builder::new(
-        thir,
-        infcx,
-        def,
-        hir_id,
-        span,
-        0,
-        Safety::Safe,
-        const_ty,
-        const_ty_span,
-        None,
-    );
+    let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
 
-    let mut block = START_BLOCK;
-    unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr]));
+    // Figure out what primary body this item has.
+    let (span, const_ty_span) = match tcx.hir().get(hir_id) {
+        Node::Item(hir::Item {
+            kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _),
+            span,
+            ..
+        })
+        | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
+        | Node::TraitItem(hir::TraitItem {
+            kind: hir::TraitItemKind::Const(ty, Some(_)),
+            span,
+            ..
+        }) => (*span, ty.span),
+        Node::AnonConst(_) => {
+            let span = tcx.def_span(def.did);
+            (span, span)
+        }
+        _ => span_bug!(tcx.def_span(def.did), "can't build MIR for {:?}", def.did),
+    };
 
-    let source_info = builder.source_info(span);
-    builder.cfg.terminate(block, source_info, TerminatorKind::Return);
+    // Get the revealed type of this const. This is *not* the adjusted
+    // type of its body, which may be a subtype of this type. For
+    // example:
+    //
+    // fn foo(_: &()) {}
+    // static X: fn(&'static ()) = foo;
+    //
+    // The adjusted type of the body of X is `for<'a> fn(&'a ())` which
+    // is not the same as the type of X. We need the type of the return
+    // place to be the type of the constant because NLL typeck will
+    // equate them.
+    let typeck_results = tcx.typeck_opt_const_arg(def);
+    let const_ty = typeck_results.node_type(hir_id);
 
-    builder.build_drop_trees();
+    tcx.infer_ctxt().enter(|infcx| {
+        let mut builder = Builder::new(
+            thir,
+            infcx,
+            def,
+            hir_id,
+            span,
+            0,
+            Safety::Safe,
+            const_ty,
+            const_ty_span,
+            None,
+        );
+
+        let mut block = START_BLOCK;
+        unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr]));
+
+        let source_info = builder.source_info(span);
+        builder.cfg.terminate(block, source_info, TerminatorKind::Return);
+
+        builder.build_drop_trees();
 
-    builder.finish()
+        builder.finish()
+    })
 }
 
 /// Construct MIR for an item that has had errors in type checking.
 ///
 /// This is required because we may still want to run MIR passes on an item
 /// with type errors, but normal MIR construction can't handle that in general.
-fn construct_error<'a, 'tcx>(
-    infcx: &'a InferCtxt<'a, 'tcx>,
-    def: ty::WithOptConstParam<LocalDefId>,
-    hir_id: hir::HirId,
-    body_id: hir::BodyId,
+fn construct_error<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    def: LocalDefId,
     body_owner_kind: hir::BodyOwnerKind,
     err: ErrorGuaranteed,
 ) -> Body<'tcx> {
-    let tcx = infcx.tcx;
-    let span = tcx.hir().span(hir_id);
+    let span = tcx.def_span(def);
+    let hir_id = tcx.hir().local_def_id_to_hir_id(def);
+    let generator_kind = tcx.generator_kind(def);
+
     let ty = tcx.ty_error();
-    let generator_kind = tcx.hir().body(body_id).generator_kind;
     let num_params = match body_owner_kind {
-        hir::BodyOwnerKind::Fn => tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len(),
+        hir::BodyOwnerKind::Fn => tcx.fn_sig(def).inputs().skip_binder().len(),
         hir::BodyOwnerKind::Closure => {
-            if generator_kind.is_some() {
-                // Generators have an implicit `self` parameter *and* a possibly
-                // implicit resume parameter.
-                2
-            } else {
-                // The implicit self parameter adds another local in MIR.
-                1 + tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len()
+            let ty = tcx.type_of(def);
+            match ty.kind() {
+                ty::Closure(_, substs) => {
+                    1 + substs.as_closure().sig().inputs().skip_binder().len()
+                }
+                ty::Generator(..) => 2,
+                _ => bug!("expected closure or generator, found {ty:?}"),
             }
         }
         hir::BodyOwnerKind::Const => 0,
@@ -808,7 +665,7 @@ fn construct_error<'a, 'tcx>(
     cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
 
     let mut body = Body::new(
-        MirSource::item(def.did.to_def_id()),
+        MirSource::item(def.to_def_id()),
         cfg.basic_blocks,
         source_scopes,
         local_decls,
@@ -826,7 +683,7 @@ fn construct_error<'a, 'tcx>(
 impl<'a, 'tcx> Builder<'a, 'tcx> {
     fn new(
         thir: &'a Thir<'tcx>,
-        infcx: &'a InferCtxt<'a, 'tcx>,
+        infcx: InferCtxt<'a, 'tcx>,
         def: ty::WithOptConstParam<LocalDefId>,
         hir_id: hir::HirId,
         span: Span,
@@ -916,20 +773,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
         &mut self,
         mut block: BasicBlock,
         fn_def_id: LocalDefId,
-        arguments: &[ArgInfo<'tcx>],
+        arguments: &IndexVec<ParamId, Param<'tcx>>,
         argument_scope: region::Scope,
         expr: &Expr<'tcx>,
     ) -> BlockAnd<()> {
         // Allocate locals for the function arguments
-        for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
+        for param in arguments.iter() {
             let source_info =
-                SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
-            let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
+                SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
+            let arg_local =
+                self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
 
             // If this is a simple binding pattern, give debuginfo a nice name.
-            if let Some(arg) = arg_opt && let Some(ident) = arg.pat.simple_ident() {
+            if let Some(ref pat) = param.pat && let Some(name) = pat.simple_ident() {
                 self.var_debug_info.push(VarDebugInfo {
-                    name: ident.name,
+                    name,
                     source_info,
                     value: VarDebugInfoContents::Place(arg_local.into()),
                 });
@@ -1002,32 +860,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
 
         let mut scope = None;
         // Bind the argument patterns
-        for (index, arg_info) in arguments.iter().enumerate() {
+        for (index, param) in arguments.iter().enumerate() {
             // Function arguments always get the first Local indices after the return place
             let local = Local::new(index + 1);
             let place = Place::from(local);
-            let &ArgInfo(_, opt_ty_info, arg_opt, ref self_binding) = arg_info;
 
             // Make sure we drop (parts of) the argument even when not matched on.
             self.schedule_drop(
-                arg_opt.as_ref().map_or(expr.span, |arg| arg.pat.span),
+                param.pat.as_ref().map_or(expr.span, |pat| pat.span),
                 argument_scope,
                 local,
                 DropKind::Value,
             );
 
-            let Some(arg) = arg_opt else {
+            let Some(ref pat) = param.pat else {
                 continue;
             };
-            let pat = match tcx.hir().get(arg.pat.hir_id) {
-                Node::Pat(pat) => pat,
-                node => bug!("pattern became {:?}", node),
-            };
-            let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
             let original_source_scope = self.source_scope;
-            let span = pattern.span;
-            self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
-            match pattern.kind {
+            let span = pat.span;
+            if let Some(arg_hir_id) = param.hir_id {
+                self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
+            }
+            match pat.kind {
                 // Don't introduce extra copies for simple bindings
                 PatKind::Binding {
                     mutability,
@@ -1038,16 +892,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                 } => {
                     self.local_decls[local].mutability = mutability;
                     self.local_decls[local].source_info.scope = self.source_scope;
-                    self.local_decls[local].local_info = if let Some(kind) = self_binding {
+                    self.local_decls[local].local_info = if let Some(kind) = param.self_kind {
                         Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
-                            BindingForm::ImplicitSelf(*kind),
+                            BindingForm::ImplicitSelf(kind),
                         ))))
                     } else {
                         let binding_mode = ty::BindingMode::BindByValue(mutability);
                         Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
                             VarBindingForm {
                                 binding_mode,
-                                opt_ty_info,
+                                opt_ty_info: param.ty_span,
                                 opt_match_place: Some((None, span)),
                                 pat_span: span,
                             },
@@ -1059,15 +913,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
                     scope = self.declare_bindings(
                         scope,
                         expr.span,
-                        &pattern,
+                        &pat,
                         matches::ArmHasGuard(false),
                         Some((Some(&place), span)),
                     );
                     let place_builder = PlaceBuilder::from(local);
-                    unpack!(
-                        block =
-                            self.place_into_pattern(block, pattern.as_ref(), place_builder, false)
-                    );
+                    unpack!(block = self.place_into_pattern(block, &pat, place_builder, false));
                 }
             }
             self.source_scope = original_source_scope;
diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs
index ae53df1f9b9..3ef1b263ffd 100644
--- a/compiler/rustc_mir_build/src/thir/cx/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs
@@ -8,12 +8,14 @@ use crate::thir::util::UserAnnotatedTyHelpers;
 use rustc_data_structures::steal::Steal;
 use rustc_errors::ErrorGuaranteed;
 use rustc_hir as hir;
+use rustc_hir::def::DefKind;
 use rustc_hir::def_id::{DefId, LocalDefId};
+use rustc_hir::lang_items::LangItem;
 use rustc_hir::HirId;
 use rustc_hir::Node;
 use rustc_middle::middle::region;
 use rustc_middle::thir::*;
-use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
+use rustc_middle::ty::{self, RvalueScopes, Subst, TyCtxt};
 use rustc_span::Span;
 
 pub(crate) fn thir_body<'tcx>(
@@ -27,6 +29,26 @@ pub(crate) fn thir_body<'tcx>(
         return Err(reported);
     }
     let expr = cx.mirror_expr(&body.value);
+
+    let owner_id = hir.local_def_id_to_hir_id(owner_def.did);
+    if let Some(ref fn_decl) = hir.fn_decl_by_hir_id(owner_id) {
+        let closure_env_param = cx.closure_env_param(owner_def.did, owner_id);
+        let explicit_params = cx.explicit_params(owner_id, fn_decl, body);
+        cx.thir.params = closure_env_param.into_iter().chain(explicit_params).collect();
+
+        // The resume argument may be missing, in that case we need to provide it here.
+        // It will always be `()` in this case.
+        if tcx.def_kind(owner_def.did) == DefKind::Generator && body.params.is_empty() {
+            cx.thir.params.push(Param {
+                ty: tcx.mk_unit(),
+                pat: None,
+                ty_span: None,
+                self_kind: None,
+                hir_id: None,
+            });
+        }
+    }
+
     Ok((tcx.alloc_steal_thir(cx.thir), expr))
 }
 
@@ -44,11 +66,11 @@ struct Cx<'tcx> {
     tcx: TyCtxt<'tcx>,
     thir: Thir<'tcx>,
 
-    pub(crate) param_env: ty::ParamEnv<'tcx>,
+    param_env: ty::ParamEnv<'tcx>,
 
-    pub(crate) region_scope_tree: &'tcx region::ScopeTree,
-    pub(crate) typeck_results: &'tcx ty::TypeckResults<'tcx>,
-    pub(crate) rvalue_scopes: &'tcx RvalueScopes,
+    region_scope_tree: &'tcx region::ScopeTree,
+    typeck_results: &'tcx ty::TypeckResults<'tcx>,
+    rvalue_scopes: &'tcx RvalueScopes,
 
     /// When applying adjustments to the expression
     /// with the given `HirId`, use the given `Span`,
@@ -78,13 +100,93 @@ impl<'tcx> Cx<'tcx> {
     }
 
     #[instrument(level = "debug", skip(self))]
-    pub(crate) fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Box<Pat<'tcx>> {
+    fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Box<Pat<'tcx>> {
         let p = match self.tcx.hir().get(p.hir_id) {
             Node::Pat(p) => p,
             node => bug!("pattern became {:?}", node),
         };
         pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
     }
+
+    fn closure_env_param(&self, owner_def: LocalDefId, owner_id: HirId) -> Option<Param<'tcx>> {
+        match self.tcx.def_kind(owner_def) {
+            DefKind::Closure => {
+                let closure_ty = self.typeck_results.node_type(owner_id);
+
+                let ty::Closure(closure_def_id, closure_substs) = *closure_ty.kind() else {
+                    bug!("closure expr does not have closure type: {:?}", closure_ty);
+                };
+
+                let bound_vars = self.tcx.mk_bound_variable_kinds(std::iter::once(
+                    ty::BoundVariableKind::Region(ty::BrEnv),
+                ));
+                let br = ty::BoundRegion {
+                    var: ty::BoundVar::from_usize(bound_vars.len() - 1),
+                    kind: ty::BrEnv,
+                };
+                let env_region = ty::ReLateBound(ty::INNERMOST, br);
+                let closure_env_ty =
+                    self.tcx.closure_env_ty(closure_def_id, closure_substs, env_region).unwrap();
+                let liberated_closure_env_ty = self.tcx.erase_late_bound_regions(
+                    ty::Binder::bind_with_vars(closure_env_ty, bound_vars),
+                );
+                let env_param = Param {
+                    ty: liberated_closure_env_ty,
+                    pat: None,
+                    ty_span: None,
+                    self_kind: None,
+                    hir_id: None,
+                };
+
+                Some(env_param)
+            }
+            DefKind::Generator => {
+                let gen_ty = self.typeck_results.node_type(owner_id);
+                let gen_param =
+                    Param { ty: gen_ty, pat: None, ty_span: None, self_kind: None, hir_id: None };
+                Some(gen_param)
+            }
+            _ => None,
+        }
+    }
+
+    fn explicit_params<'a>(
+        &'a mut self,
+        owner_id: HirId,
+        fn_decl: &'tcx hir::FnDecl<'tcx>,
+        body: &'tcx hir::Body<'tcx>,
+    ) -> impl Iterator<Item = Param<'tcx>> + 'a {
+        let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
+
+        body.params.iter().enumerate().map(move |(index, param)| {
+            let ty_span = fn_decl
+                .inputs
+                .get(index)
+                // Make sure that inferred closure args have no type span
+                .and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
+
+            let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
+                Some(fn_decl.implicit_self)
+            } else {
+                None
+            };
+
+            // C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
+            // (as it's created inside the body itself, not passed in from outside).
+            let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
+                let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));
+
+                self.tcx
+                    .bound_type_of(va_list_did)
+                    .subst(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
+            } else {
+                fn_sig.inputs()[index]
+            };
+
+            let pat = self.pattern_from_hir(param.pat);
+            Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
+        })
+    }
 }
 
 impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> {
diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
index 4968032b416..2c131a26d3a 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
@@ -29,22 +29,22 @@ use rustc_span::{Span, Symbol};
 use std::cmp::Ordering;
 
 #[derive(Clone, Debug)]
-pub(crate) enum PatternError {
+enum PatternError {
     AssocConstInPattern(Span),
     ConstParamInPattern(Span),
     StaticInPattern(Span),
     NonConstPath(Span),
 }
 
-pub(crate) struct PatCtxt<'a, 'tcx> {
-    pub(crate) tcx: TyCtxt<'tcx>,
-    pub(crate) param_env: ty::ParamEnv<'tcx>,
-    pub(crate) typeck_results: &'a ty::TypeckResults<'tcx>,
-    pub(crate) errors: Vec<PatternError>,
+struct PatCtxt<'a, 'tcx> {
+    tcx: TyCtxt<'tcx>,
+    param_env: ty::ParamEnv<'tcx>,
+    typeck_results: &'a ty::TypeckResults<'tcx>,
+    errors: Vec<PatternError>,
     include_lint_checks: bool,
 }
 
-pub(crate) fn pat_from_hir<'a, 'tcx>(
+pub(super) fn pat_from_hir<'a, 'tcx>(
     tcx: TyCtxt<'tcx>,
     param_env: ty::ParamEnv<'tcx>,
     typeck_results: &'a ty::TypeckResults<'tcx>,
@@ -61,7 +61,7 @@ pub(crate) fn pat_from_hir<'a, 'tcx>(
 }
 
 impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
-    pub(crate) fn new(
+    fn new(
         tcx: TyCtxt<'tcx>,
         param_env: ty::ParamEnv<'tcx>,
         typeck_results: &'a ty::TypeckResults<'tcx>,
@@ -69,12 +69,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
         PatCtxt { tcx, param_env, typeck_results, errors: vec![], include_lint_checks: false }
     }
 
-    pub(crate) fn include_lint_checks(&mut self) -> &mut Self {
+    fn include_lint_checks(&mut self) -> &mut Self {
         self.include_lint_checks = true;
         self
     }
 
-    pub(crate) fn lower_pattern(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
+    fn lower_pattern(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
         // When implicit dereferences have been inserted in this pattern, the unadjusted lowered
         // pattern has the type that results *after* dereferencing. For example, in this code:
         //
@@ -627,7 +627,7 @@ impl<'tcx> UserAnnotatedTyHelpers<'tcx> for PatCtxt<'_, 'tcx> {
     }
 }
 
-pub(crate) trait PatternFoldable<'tcx>: Sized {
+trait PatternFoldable<'tcx>: Sized {
     fn fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self {
         self.super_fold_with(folder)
     }
@@ -635,7 +635,7 @@ pub(crate) trait PatternFoldable<'tcx>: Sized {
     fn super_fold_with<F: PatternFolder<'tcx>>(&self, folder: &mut F) -> Self;
 }
 
-pub(crate) trait PatternFolder<'tcx>: Sized {
+trait PatternFolder<'tcx>: Sized {
     fn fold_pattern(&mut self, pattern: &Pat<'tcx>) -> Pat<'tcx> {
         pattern.super_fold_with(self)
     }
diff --git a/compiler/rustc_smir/src/mir.rs b/compiler/rustc_smir/src/mir.rs
index 855605b1a4f..887e6572930 100644
--- a/compiler/rustc_smir/src/mir.rs
+++ b/compiler/rustc_smir/src/mir.rs
@@ -1,10 +1,10 @@
+pub use crate::very_unstable::hir::ImplicitSelfKind;
 pub use crate::very_unstable::middle::mir::{
     visit::MutVisitor, AggregateKind, AssertKind, BasicBlock, BasicBlockData, BinOp, BindingForm,
     BlockTailInfo, Body, BorrowKind, CastKind, ClearCrossCrate, Constant, ConstantKind,
-    CopyNonOverlapping, Coverage, FakeReadCause, Field, GeneratorInfo, ImplicitSelfKind,
-    InlineAsmOperand, Local, LocalDecl, LocalInfo, LocalKind, Location, MirPhase, MirSource,
-    NullOp, Operand, Place, PlaceRef, ProjectionElem, ProjectionKind, Promoted, RetagKind, Rvalue,
-    Safety, SourceInfo, SourceScope, SourceScopeData, SourceScopeLocalData, Statement,
-    StatementKind, UnOp, UserTypeProjection, UserTypeProjections, VarBindingForm, VarDebugInfo,
-    VarDebugInfoContents,
+    CopyNonOverlapping, Coverage, FakeReadCause, Field, GeneratorInfo, InlineAsmOperand, Local,
+    LocalDecl, LocalInfo, LocalKind, Location, MirPhase, MirSource, NullOp, Operand, Place,
+    PlaceRef, ProjectionElem, ProjectionKind, Promoted, RetagKind, Rvalue, Safety, SourceInfo,
+    SourceScope, SourceScopeData, SourceScopeLocalData, Statement, StatementKind, UnOp,
+    UserTypeProjection, UserTypeProjections, VarBindingForm, VarDebugInfo, VarDebugInfoContents,
 };