use crate::build;
use crate::build::expr::as_place::PlaceBuilder;
use crate::build::scope::DropKind;
-use crate::thir::{build_thir, Arena, BindingMode, Expr, LintLevel, Pat, PatKind};
+use crate::thir::pattern::pat_from_hir;
use rustc_attr::{self as attr, UnwindAttr};
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
use rustc_middle::middle::region;
use rustc_middle::mir::*;
+use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, PatKind, Thir};
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeckResults};
use rustc_span::symbol::{kw, sym};
let body_owner_kind = tcx.hir().body_owner_kind(id);
let typeck_results = tcx.typeck_opt_const_arg(def);
+ // Ensure unsafeck is ran before we steal the THIR.
+ match def {
+ ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
+ tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did))
+ }
+ ty::WithOptConstParam { did, const_param_did: None } => {
+ tcx.ensure().thir_check_unsafety(did)
+ }
+ }
+
// 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(_, decl, body_id, _, _), .. }) => {
// 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));
- let arena = Arena::default();
tcx.infer_ctxt().enter(|infcx| {
let body = if let Some(ErrorReported) = typeck_results.tainted_by_errors {
};
let body = tcx.hir().body(body_id);
- let thir = build_thir(tcx, def, &arena, &body.value);
+ let (thir, expr) = tcx.thir_body(def);
+ // 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() {
};
let mut mir = build::construct_fn(
+ &thir,
&infcx,
def,
id,
return_ty,
return_ty_span,
body,
- thir,
+ expr,
span_with_body,
);
if yield_ty.is_some() {
let return_ty = typeck_results.node_type(id);
- let ast_expr = &tcx.hir().body(body_id).value;
- let thir = build_thir(tcx, def, &arena, ast_expr);
+ let (thir, expr) = tcx.thir_body(def);
+ // We ran all queries that depended on THIR at the beginning
+ // of `mir_build`, so now we can steal it
+ let thir = thir.steal();
- build::construct_const(&infcx, thir, def, id, return_ty, return_ty_span)
+ build::construct_const(&thir, &infcx, expr, def, id, return_ty, return_ty_span)
};
lints::check(tcx, &body);
region_scope_tree: &'tcx region::ScopeTree,
param_env: ty::ParamEnv<'tcx>,
+ thir: &'a Thir<'tcx>,
cfg: CFG<'tcx>,
def_id: DefId,
);
fn construct_fn<'tcx, A>(
+ thir: &Thir<'tcx>,
infcx: &InferCtxt<'_, 'tcx>,
fn_def: ty::WithOptConstParam<LocalDefId>,
fn_id: hir::HirId,
return_ty: Ty<'tcx>,
return_ty_span: Span,
body: &'tcx hir::Body<'tcx>,
- expr: &Expr<'_, 'tcx>,
+ expr: ExprId,
span_with_body: Span,
) -> Body<'tcx>
where
let span = tcx.hir().span(fn_id);
let mut builder = Builder::new(
+ thir,
infcx,
fn_def,
fn_id,
fn_def.did.to_def_id(),
&arguments,
arg_scope,
- expr,
+ &thir[expr],
)
}))
}));
}
fn construct_const<'a, 'tcx>(
+ thir: &'a Thir<'tcx>,
infcx: &'a InferCtxt<'a, 'tcx>,
- expr: &Expr<'_, 'tcx>,
+ expr: ExprId,
def: ty::WithOptConstParam<LocalDefId>,
hir_id: hir::HirId,
const_ty: Ty<'tcx>,
) -> Body<'tcx> {
let tcx = infcx.tcx;
let span = tcx.hir().span(hir_id);
- let mut builder =
- Builder::new(infcx, def, hir_id, span, 0, Safety::Safe, const_ty, const_ty_span, None);
+ 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, &expr));
+ 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);
hir::BodyOwnerKind::Const => 0,
hir::BodyOwnerKind::Static(_) => 0,
};
- let mut builder =
- Builder::new(infcx, def, hir_id, span, num_params, Safety::Safe, ty, span, generator_kind);
- let source_info = builder.source_info(span);
+ let mut cfg = CFG { basic_blocks: IndexVec::new() };
+ let mut source_scopes = IndexVec::new();
+ let mut local_decls = IndexVec::from_elem_n(LocalDecl::new(ty, span), 1);
+
+ cfg.start_new_block();
+ source_scopes.push(SourceScopeData {
+ span,
+ parent_scope: None,
+ inlined: None,
+ inlined_parent_scope: None,
+ local_data: ClearCrossCrate::Set(SourceScopeLocalData {
+ lint_root: hir_id,
+ safety: Safety::Safe,
+ }),
+ });
+ let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
+
// Some MIR passes will expect the number of parameters to match the
// function declaration.
for _ in 0..num_params {
- builder.local_decls.push(LocalDecl::with_source_info(ty, source_info));
+ local_decls.push(LocalDecl::with_source_info(ty, source_info));
}
- builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
- let mut body = builder.finish();
+ cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
+
+ let mut body = Body::new(
+ MirSource::item(def.did.to_def_id()),
+ cfg.basic_blocks,
+ source_scopes,
+ local_decls,
+ IndexVec::new(),
+ num_params,
+ vec![],
+ span,
+ generator_kind,
+ );
body.generator.as_mut().map(|gen| gen.yield_ty = Some(ty));
body
}
impl<'a, 'tcx> Builder<'a, 'tcx> {
fn new(
+ thir: &'a Thir<'tcx>,
infcx: &'a InferCtxt<'a, 'tcx>,
def: ty::WithOptConstParam<LocalDefId>,
hir_id: hir::HirId,
let lint_level = LintLevel::Explicit(hir_id);
let mut builder = Builder {
+ thir,
tcx,
infcx,
typeck_results: tcx.typeck_opt_const_arg(def),
fn_def_id: DefId,
arguments: &[ArgInfo<'tcx>],
argument_scope: region::Scope,
- expr: &Expr<'_, 'tcx>,
+ expr: &Expr<'tcx>,
) -> BlockAnd<()> {
// Allocate locals for the function arguments
for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
// the given closure and use the necessary information to create upvar
// debuginfo and to fill `self.upvar_mutbls`.
if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() {
- let closure_env_arg = Local::new(1);
let mut closure_env_projs = vec![];
- let mut closure_ty = self.local_decls[closure_env_arg].ty;
+ let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
if let ty::Ref(_, ty, _) = closure_ty.kind() {
closure_env_projs.push(ProjectionElem::Deref);
closure_ty = ty;
name,
source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
value: VarDebugInfoContents::Place(Place {
- local: closure_env_arg,
+ local: ty::CAPTURE_STRUCT_LOCAL,
projection: tcx.intern_place_elems(&projs),
}),
});
Node::Pat(pat) | Node::Binding(pat) => pat,
node => bug!("pattern became {:?}", node),
};
- let pattern = Pat::from_hir(tcx, self.param_env, self.typeck_results, pat);
+ 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);