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
::arena
::Arena
;
6 use crate::thir
::util
::UserAnnotatedTyHelpers
;
11 use rustc_hir
::def_id
::{DefId, LocalDefId}
;
13 use rustc_middle
::middle
::region
;
14 use rustc_middle
::mir
::interpret
::{LitToConstError, LitToConstInput}
;
15 use rustc_middle
::ty
::{self, Ty, TyCtxt}
;
17 pub fn build_thir
<'thir
, 'tcx
>(
19 owner_def
: ty
::WithOptConstParam
<LocalDefId
>,
20 arena
: &'thir Arena
<'thir
, 'tcx
>,
21 expr
: &'tcx hir
::Expr
<'tcx
>,
22 ) -> &'thir Expr
<'thir
, 'tcx
> {
23 Cx
::new(tcx
, owner_def
, &arena
).mirror_expr(expr
)
26 struct Cx
<'thir
, 'tcx
> {
28 arena
: &'thir Arena
<'thir
, 'tcx
>,
30 crate param_env
: ty
::ParamEnv
<'tcx
>,
32 crate region_scope_tree
: &'tcx region
::ScopeTree
,
33 crate typeck_results
: &'tcx ty
::TypeckResults
<'tcx
>,
35 /// The `DefId` of the owner of this body.
39 impl<'thir
, 'tcx
> Cx
<'thir
, 'tcx
> {
42 def
: ty
::WithOptConstParam
<LocalDefId
>,
43 arena
: &'thir Arena
<'thir
, 'tcx
>,
44 ) -> Cx
<'thir
, 'tcx
> {
45 let typeck_results
= tcx
.typeck_opt_const_arg(def
);
49 param_env
: tcx
.param_env(def
.did
),
50 region_scope_tree
: tcx
.region_scope_tree(def
.did
),
52 body_owner
: def
.did
.to_def_id(),
56 crate fn const_eval_literal(
58 lit
: &'tcx ast
::LitKind
,
62 ) -> &'tcx ty
::Const
<'tcx
> {
63 trace
!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit
, ty
, sp
, neg
);
65 match self.tcx
.at(sp
).lit_to_const(LitToConstInput { lit, ty, neg }
) {
67 Err(LitToConstError
::UnparseableFloat
) => {
68 // FIXME(#31407) this is only necessary because float parsing is buggy
69 self.tcx
.sess
.span_err(sp
, "could not evaluate float literal (see issue #31407)");
70 // create a dummy value and continue compiling
71 self.tcx
.const_error(ty
)
73 Err(LitToConstError
::Reported
) => {
74 // create a dummy value and continue compiling
75 self.tcx
.const_error(ty
)
77 Err(LitToConstError
::TypeError
) => bug
!("const_eval_literal: had type error"),
81 crate fn pattern_from_hir(&mut self, p
: &hir
::Pat
<'_
>) -> Pat
<'tcx
> {
82 let p
= match self.tcx
.hir().get(p
.hir_id
) {
83 Node
::Pat(p
) | Node
::Binding(p
) => p
,
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
> {