1 //! This module contains the functionality to convert from the wacky tcx data
2 //! structures into the THIR. The `builder` is generally ignorant of the tcx,
3 //! etc., and instead goes through the `Cx` for most of its work.
5 use crate::thir
::pattern
::pat_from_hir
;
6 use crate::thir
::util
::UserAnnotatedTyHelpers
;
8 use rustc_data_structures
::steal
::Steal
;
9 use rustc_errors
::ErrorGuaranteed
;
11 use rustc_hir
::def_id
::{DefId, LocalDefId}
;
14 use rustc_middle
::middle
::region
;
15 use rustc_middle
::thir
::*;
16 use rustc_middle
::ty
::{self, RvalueScopes, TyCtxt}
;
19 pub(crate) fn thir_body
<'tcx
>(
21 owner_def
: ty
::WithOptConstParam
<LocalDefId
>,
22 ) -> Result
<(&'tcx Steal
<Thir
<'tcx
>>, ExprId
), ErrorGuaranteed
> {
24 let body
= hir
.body(hir
.body_owned_by(owner_def
.did
));
25 let mut cx
= Cx
::new(tcx
, owner_def
);
26 if let Some(reported
) = cx
.typeck_results
.tainted_by_errors
{
29 let expr
= cx
.mirror_expr(&body
.value
);
30 Ok((tcx
.alloc_steal_thir(cx
.thir
), expr
))
33 pub(crate) fn thir_tree
<'tcx
>(
35 owner_def
: ty
::WithOptConstParam
<LocalDefId
>,
37 match thir_body(tcx
, owner_def
) {
38 Ok((thir
, _
)) => format
!("{:#?}", thir
.steal()),
39 Err(_
) => "error".into(),
47 pub(crate) param_env
: ty
::ParamEnv
<'tcx
>,
49 pub(crate) region_scope_tree
: &'tcx region
::ScopeTree
,
50 pub(crate) typeck_results
: &'tcx ty
::TypeckResults
<'tcx
>,
51 pub(crate) rvalue_scopes
: &'tcx RvalueScopes
,
53 /// When applying adjustments to the expression
54 /// with the given `HirId`, use the given `Span`,
55 /// instead of the usual span. This is used to
56 /// assign the span of an overall method call
57 /// (e.g. `my_val.foo()`) to the adjustment expressions
59 adjustment_span
: Option
<(HirId
, Span
)>,
61 /// The `DefId` of the owner of this body.
66 fn new(tcx
: TyCtxt
<'tcx
>, def
: ty
::WithOptConstParam
<LocalDefId
>) -> Cx
<'tcx
> {
67 let typeck_results
= tcx
.typeck_opt_const_arg(def
);
71 param_env
: tcx
.param_env(def
.did
),
72 region_scope_tree
: tcx
.region_scope_tree(def
.did
),
74 rvalue_scopes
: &typeck_results
.rvalue_scopes
,
75 body_owner
: def
.did
.to_def_id(),
76 adjustment_span
: None
,
80 #[tracing::instrument(level = "debug", skip(self))]
81 pub(crate) fn pattern_from_hir(&mut self, p
: &hir
::Pat
<'_
>) -> Pat
<'tcx
> {
82 let p
= match self.tcx
.hir().get(p
.hir_id
) {
84 node
=> bug
!("pattern became {:?}", node
),
86 pat_from_hir(self.tcx
, self.param_env
, self.typeck_results(), p
)
90 impl<'tcx
> UserAnnotatedTyHelpers
<'tcx
> for Cx
<'tcx
> {
91 fn tcx(&self) -> TyCtxt
<'tcx
> {
95 fn typeck_results(&self) -> &ty
::TypeckResults
<'tcx
> {