Skip to content

Commit 64afc6b

Browse files
committed
Differentiate between closure and function bodies
1 parent b5f5a27 commit 64afc6b

File tree

12 files changed

+38
-17
lines changed

12 files changed

+38
-17
lines changed

src/librustc/hir/map/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,20 @@ impl<'hir> Map<'hir> {
455455
Node::AnonConst(_) => {
456456
BodyOwnerKind::Const
457457
}
458+
Node::Variant(&Spanned { node: VariantKind { data: VariantData::Tuple(..), .. }, .. }) |
459+
Node::StructCtor(..) |
460+
Node::Item(&Item { node: ItemKind::Fn(..), .. }) |
461+
Node::TraitItem(&TraitItem { node: TraitItemKind::Method(..), .. }) |
462+
Node::ImplItem(&ImplItem { node: ImplItemKind::Method(..), .. }) => {
463+
BodyOwnerKind::Fn
464+
}
458465
Node::Item(&Item { node: ItemKind::Static(_, m, _), .. }) => {
459466
BodyOwnerKind::Static(m)
460467
}
461-
// Default to function if it's not a constant or static.
462-
_ => BodyOwnerKind::Fn
468+
Node::Expr(&Expr { node: ExprKind::Closure(..), .. }) => {
469+
BodyOwnerKind::Closure
470+
}
471+
node => bug!("{:#?} is not a body node", node),
463472
}
464473
}
465474

src/librustc/hir/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1270,13 +1270,25 @@ pub enum BodyOwnerKind {
12701270
/// Functions and methods.
12711271
Fn,
12721272

1273+
/// Closures
1274+
Closure,
1275+
12731276
/// Constants and associated constants.
12741277
Const,
12751278

12761279
/// Initializer of a `static` item.
12771280
Static(Mutability),
12781281
}
12791282

1283+
impl BodyOwnerKind {
1284+
pub fn is_fn_or_closure(self) -> bool {
1285+
match self {
1286+
BodyOwnerKind::Fn | BodyOwnerKind::Closure => true,
1287+
BodyOwnerKind::Const | BodyOwnerKind::Static(_) => false,
1288+
}
1289+
}
1290+
}
1291+
12801292
/// A constant (expression) that's not an item or associated item,
12811293
/// but needs its own `DefId` for type-checking, const-eval, etc.
12821294
/// These are usually found nested inside types (e.g., array lengths)

src/librustc/middle/region.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionResolutionVisitor<'a, 'tcx> {
12681268

12691269
// The body of the every fn is a root scope.
12701270
self.cx.parent = self.cx.var_parent;
1271-
if let hir::BodyOwnerKind::Fn = self.tcx.hir().body_owner_kind(owner_id) {
1272-
self.visit_expr(&body.value);
1271+
if self.tcx.hir().body_owner_kind(owner_id).is_fn_or_closure() {
1272+
self.visit_expr(&body.value)
12731273
} else {
12741274
// Only functions have an outer terminating (drop) scope, while
12751275
// temporaries in constant initializers may be 'static, but only

src/librustc_mir/borrow_check/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
163163
|bd, i| DebugFormatted::new(&bd.move_data().move_paths[i]),
164164
));
165165

166-
let locals_are_invalidated_at_exit = match tcx.hir().body_owner_kind(id) {
167-
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => false,
168-
hir::BodyOwnerKind::Fn => true,
169-
};
166+
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
170167
let borrow_set = Rc::new(BorrowSet::build(
171168
tcx, mir, locals_are_invalidated_at_exit, &mdpe.move_data));
172169

src/librustc_mir/borrow_check/nll/universal_regions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ impl<'cx, 'gcx, 'tcx> UniversalRegionsBuilder<'cx, 'gcx, 'tcx> {
476476
let closure_base_def_id = tcx.closure_base_def_id(self.mir_def_id);
477477

478478
match tcx.hir().body_owner_kind(self.mir_node_id) {
479+
BodyOwnerKind::Closure |
479480
BodyOwnerKind::Fn => {
480481
let defining_ty = if self.mir_def_id == closure_base_def_id {
481482
tcx.type_of(closure_base_def_id)

src/librustc_mir/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
7575
let cx = Cx::new(&infcx, id);
7676
let mut mir = if cx.tables().tainted_by_errors {
7777
build::construct_error(cx, body_id)
78-
} else if let hir::BodyOwnerKind::Fn = cx.body_owner_kind {
78+
} else if cx.body_owner_kind.is_fn_or_closure() {
7979
// fetch the fully liberated fn signature (that is, all bound
8080
// types/lifetimes replaced)
8181
let fn_hir_id = tcx.hir().node_to_hir_id(id);

src/librustc_mir/build/scope.rs

+1
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
613613
hir::BodyOwnerKind::Static(_) =>
614614
// No need to free storage in this context.
615615
None,
616+
hir::BodyOwnerKind::Closure |
616617
hir::BodyOwnerKind::Fn =>
617618
Some(self.topmost_scope()),
618619
}

src/librustc_mir/hair/cx/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
6161
let constness = match body_owner_kind {
6262
hir::BodyOwnerKind::Const |
6363
hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
64+
hir::BodyOwnerKind::Closure |
6465
hir::BodyOwnerKind::Fn => hir::Constness::NotConst,
6566
};
6667

src/librustc_mir/transform/inline.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Inlining pass for MIR functions
22
3-
use rustc::hir;
43
use rustc::hir::CodegenFnAttrFlags;
54
use rustc::hir::def_id::DefId;
65

@@ -74,15 +73,12 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
7473

7574
// Only do inlining into fn bodies.
7675
let id = self.tcx.hir().as_local_node_id(self.source.def_id).unwrap();
77-
let body_owner_kind = self.tcx.hir().body_owner_kind(id);
78-
79-
if let (hir::BodyOwnerKind::Fn, None) = (body_owner_kind, self.source.promoted) {
80-
76+
if self.tcx.hir().body_owner_kind(id).is_fn_or_closure() && self.source.promoted.is_none() {
8177
for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated() {
8278
if let Some(callsite) = self.get_valid_function_call(bb,
83-
bb_data,
84-
caller_mir,
85-
param_env) {
79+
bb_data,
80+
caller_mir,
81+
param_env) {
8682
callsites.push_back(callsite);
8783
}
8884
}

src/librustc_mir/transform/qualify_consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ impl MirPass for QualifyAndPromoteConstants {
11521152
let id = tcx.hir().as_local_node_id(def_id).unwrap();
11531153
let mut const_promoted_temps = None;
11541154
let mode = match tcx.hir().body_owner_kind(id) {
1155+
hir::BodyOwnerKind::Closure => Mode::Fn,
11551156
hir::BodyOwnerKind::Fn => {
11561157
if tcx.is_const_fn(def_id) {
11571158
Mode::ConstFn

src/librustc_mir/util/pretty.rs

+2
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut dyn Write) -> i
573573
let body_owner_kind = tcx.hir().body_owner_kind(id);
574574
match (body_owner_kind, src.promoted) {
575575
(_, Some(i)) => write!(w, "{:?} in", i)?,
576+
(hir::BodyOwnerKind::Closure, _) |
576577
(hir::BodyOwnerKind::Fn, _) => write!(w, "fn")?,
577578
(hir::BodyOwnerKind::Const, _) => write!(w, "const")?,
578579
(hir::BodyOwnerKind::Static(hir::MutImmutable), _) => write!(w, "static")?,
@@ -585,6 +586,7 @@ fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut dyn Write) -> i
585586
})?;
586587

587588
match (body_owner_kind, src.promoted) {
589+
(hir::BodyOwnerKind::Closure, None) |
588590
(hir::BodyOwnerKind::Fn, None) => {
589591
write!(w, "(")?;
590592

src/librustc_passes/rvalue_promotion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
191191
self.in_static = false;
192192

193193
match self.tcx.hir().body_owner_kind(item_id) {
194+
hir::BodyOwnerKind::Closure |
194195
hir::BodyOwnerKind::Fn => self.in_fn = true,
195196
hir::BodyOwnerKind::Static(_) => self.in_static = true,
196197
_ => {}

0 commit comments

Comments
 (0)