Skip to content

Commit afd8a4e

Browse files
committed
Improve LocalDecl creation.
This commit adds some new `LocalDecl` methods: - `with_source_info`, a most general constructor. - `new`, a variant of `with_source_info` which represents the most common use case. - `internal` a modifying method (like the already present `immutable`). It removes some old `LocalDecl` methods: - `new_internal` and `new_local`, because they're subsumed by the new methods. - `new_return_place`, because it was identical to `new_temp`. Finally, it cleans up all the use sites.
1 parent a17234c commit afd8a4e

File tree

13 files changed

+70
-137
lines changed

13 files changed

+70
-137
lines changed

src/librustc_middle/mir/mod.rs

+24-38
Original file line numberDiff line numberDiff line change
@@ -918,10 +918,31 @@ impl<'tcx> LocalDecl<'tcx> {
918918
self.source_info.span.desugaring_kind().is_some()
919919
}
920920

921-
/// Creates a new `LocalDecl` for a temporary.
921+
/// Creates a new `LocalDecl` for a temporary: mutable, non-internal.
922922
#[inline]
923-
pub fn new_temp(ty: Ty<'tcx>, span: Span) -> Self {
924-
Self::new_local(ty, Mutability::Mut, false, span)
923+
pub fn new(ty: Ty<'tcx>, span: Span) -> Self {
924+
Self::with_source_info(ty, SourceInfo::outermost(span))
925+
}
926+
927+
/// Like `LocalDecl::new`, but takes a `SourceInfo` instead of a `Span`.
928+
#[inline]
929+
pub fn with_source_info(ty: Ty<'tcx>, source_info: SourceInfo) -> Self {
930+
LocalDecl {
931+
mutability: Mutability::Mut,
932+
local_info: LocalInfo::Other,
933+
internal: false,
934+
is_block_tail: None,
935+
ty,
936+
user_ty: UserTypeProjections::none(),
937+
source_info,
938+
}
939+
}
940+
941+
/// Converts `self` into same `LocalDecl` except tagged as internal.
942+
#[inline]
943+
pub fn internal(mut self) -> Self {
944+
self.internal = true;
945+
self
925946
}
926947

927948
/// Converts `self` into same `LocalDecl` except tagged as immutable.
@@ -938,41 +959,6 @@ impl<'tcx> LocalDecl<'tcx> {
938959
self.is_block_tail = Some(info);
939960
self
940961
}
941-
942-
/// Creates a new `LocalDecl` for a internal temporary.
943-
#[inline]
944-
pub fn new_internal(ty: Ty<'tcx>, span: Span) -> Self {
945-
Self::new_local(ty, Mutability::Mut, true, span)
946-
}
947-
948-
#[inline]
949-
fn new_local(ty: Ty<'tcx>, mutability: Mutability, internal: bool, span: Span) -> Self {
950-
LocalDecl {
951-
mutability,
952-
ty,
953-
user_ty: UserTypeProjections::none(),
954-
source_info: SourceInfo::outermost(span),
955-
internal,
956-
local_info: LocalInfo::Other,
957-
is_block_tail: None,
958-
}
959-
}
960-
961-
/// Builds a `LocalDecl` for the return place.
962-
///
963-
/// This must be inserted into the `local_decls` list as the first local.
964-
#[inline]
965-
pub fn new_return_place(return_ty: Ty<'_>, span: Span) -> LocalDecl<'_> {
966-
LocalDecl {
967-
mutability: Mutability::Mut,
968-
ty: return_ty,
969-
user_ty: UserTypeProjections::none(),
970-
source_info: SourceInfo::outermost(span),
971-
internal: false,
972-
is_block_tail: None,
973-
local_info: LocalInfo::Other,
974-
}
975-
}
976962
}
977963

978964
/// Debug information pertaining to a user variable.

src/librustc_mir/shim.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,12 @@ enum CallKind {
145145
Direct(DefId),
146146
}
147147

148-
fn temp_decl(mutability: Mutability, ty: Ty<'_>, span: Span) -> LocalDecl<'_> {
149-
LocalDecl {
150-
mutability,
151-
ty,
152-
user_ty: UserTypeProjections::none(),
153-
source_info: SourceInfo::outermost(span),
154-
internal: false,
155-
local_info: LocalInfo::Other,
156-
is_block_tail: None,
157-
}
158-
}
159-
160148
fn local_decls_for_sig<'tcx>(
161149
sig: &ty::FnSig<'tcx>,
162150
span: Span,
163151
) -> IndexVec<Local, LocalDecl<'tcx>> {
164-
iter::once(temp_decl(Mutability::Mut, sig.output(), span))
165-
.chain(sig.inputs().iter().map(|ity| temp_decl(Mutability::Not, ity, span)))
152+
iter::once(LocalDecl::new(sig.output(), span))
153+
.chain(sig.inputs().iter().map(|ity| LocalDecl::new(ity, span).immutable()))
166154
.collect()
167155
}
168156

@@ -413,7 +401,11 @@ impl CloneShimBuilder<'tcx> {
413401

414402
fn make_place(&mut self, mutability: Mutability, ty: Ty<'tcx>) -> Place<'tcx> {
415403
let span = self.span;
416-
Place::from(self.local_decls.push(temp_decl(mutability, ty, span)))
404+
let mut local = LocalDecl::new(ty, span);
405+
if mutability == Mutability::Not {
406+
local = local.immutable();
407+
}
408+
Place::from(self.local_decls.push(local))
417409
}
418410

419411
fn make_clone_call(
@@ -497,7 +489,7 @@ impl CloneShimBuilder<'tcx> {
497489
let tcx = self.tcx;
498490
let span = self.span;
499491

500-
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
492+
let beg = self.local_decls.push(LocalDecl::new(tcx.types.usize, span));
501493
let end = self.make_place(Mutability::Not, tcx.types.usize);
502494

503495
// BB #0
@@ -552,7 +544,7 @@ impl CloneShimBuilder<'tcx> {
552544
// `let mut beg = 0;`
553545
// goto #6;
554546
let end = beg;
555-
let beg = self.local_decls.push(temp_decl(Mutability::Mut, tcx.types.usize, span));
547+
let beg = self.local_decls.push(LocalDecl::new(tcx.types.usize, span));
556548
let init = self.make_statement(StatementKind::Assign(box (
557549
Place::from(beg),
558550
Rvalue::Use(Operand::Constant(self.make_usize(0))),
@@ -700,14 +692,16 @@ fn build_call_shim<'tcx>(
700692
Adjustment::DerefMove => Operand::Move(tcx.mk_place_deref(rcvr_place())),
701693
Adjustment::RefMut => {
702694
// let rcvr = &mut rcvr;
703-
let ref_rcvr = local_decls.push(temp_decl(
704-
Mutability::Not,
705-
tcx.mk_ref(
706-
tcx.lifetimes.re_erased,
707-
ty::TypeAndMut { ty: sig.inputs()[0], mutbl: hir::Mutability::Mut },
708-
),
709-
span,
710-
));
695+
let ref_rcvr = local_decls.push(
696+
LocalDecl::new(
697+
tcx.mk_ref(
698+
tcx.lifetimes.re_erased,
699+
ty::TypeAndMut { ty: sig.inputs()[0], mutbl: hir::Mutability::Mut },
700+
),
701+
span,
702+
)
703+
.immutable(),
704+
);
711705
let borrow_kind = BorrowKind::Mut { allow_two_phase_borrow: false };
712706
statements.push(Statement {
713707
source_info,

src/librustc_mir/transform/generator.rs

+6-28
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl TransformVisitor<'tcx> {
255255

256256
// Create a statement which reads the discriminant into a temporary
257257
fn get_discr(&self, body: &mut Body<'tcx>) -> (Statement<'tcx>, Place<'tcx>) {
258-
let temp_decl = LocalDecl::new_internal(self.tcx.types.isize, body.span);
258+
let temp_decl = LocalDecl::new(self.tcx.types.isize, body.span).internal();
259259
let local_decls_len = body.local_decls.push(temp_decl);
260260
let temp = Place::from(local_decls_len);
261261

@@ -395,16 +395,7 @@ fn replace_local<'tcx>(
395395
body: &mut Body<'tcx>,
396396
tcx: TyCtxt<'tcx>,
397397
) -> Local {
398-
let source_info = SourceInfo::outermost(body.span);
399-
let new_decl = LocalDecl {
400-
mutability: Mutability::Mut,
401-
ty,
402-
user_ty: UserTypeProjections::none(),
403-
source_info,
404-
internal: false,
405-
is_block_tail: None,
406-
local_info: LocalInfo::Other,
407-
};
398+
let new_decl = LocalDecl::new(ty, body.span);
408399
let new_local = body.local_decls.push(new_decl);
409400
body.local_decls.swap(local, new_local);
410401

@@ -877,28 +868,15 @@ fn create_generator_drop_shim<'tcx>(
877868
}
878869

879870
// Replace the return variable
880-
body.local_decls[RETURN_PLACE] = LocalDecl {
881-
mutability: Mutability::Mut,
882-
ty: tcx.mk_unit(),
883-
user_ty: UserTypeProjections::none(),
884-
source_info,
885-
internal: false,
886-
is_block_tail: None,
887-
local_info: LocalInfo::Other,
888-
};
871+
body.local_decls[RETURN_PLACE] = LocalDecl::with_source_info(tcx.mk_unit(), source_info);
889872

890873
make_generator_state_argument_indirect(tcx, &mut body);
891874

892875
// Change the generator argument from &mut to *mut
893-
body.local_decls[SELF_ARG] = LocalDecl {
894-
mutability: Mutability::Mut,
895-
ty: tcx.mk_ptr(ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut }),
896-
user_ty: UserTypeProjections::none(),
876+
body.local_decls[SELF_ARG] = LocalDecl::with_source_info(
877+
tcx.mk_ptr(ty::TypeAndMut { ty: gen_ty, mutbl: hir::Mutability::Mut }),
897878
source_info,
898-
internal: false,
899-
is_block_tail: None,
900-
local_info: LocalInfo::Other,
901-
};
879+
);
902880
if tcx.sess.opts.debugging_opts.mir_emit_retag {
903881
// Alias tracking must know we changed the type
904882
body.basic_blocks_mut()[START_BLOCK].statements.insert(

src/librustc_mir/transform/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl Inliner<'tcx> {
480480

481481
let ty = dest.ty(caller_body, self.tcx);
482482

483-
let temp = LocalDecl::new_temp(ty, callsite.location.span);
483+
let temp = LocalDecl::new(ty, callsite.location.span);
484484

485485
let tmp = caller_body.local_decls.push(temp);
486486
let tmp = Place::from(tmp);
@@ -631,7 +631,7 @@ impl Inliner<'tcx> {
631631

632632
let ty = arg.ty(caller_body, self.tcx);
633633

634-
let arg_tmp = LocalDecl::new_temp(ty, callsite.location.span);
634+
let arg_tmp = LocalDecl::new(ty, callsite.location.span);
635635
let arg_tmp = caller_body.local_decls.push(arg_tmp);
636636

637637
let stmt = Statement {

src/librustc_mir/transform/promote_consts.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
815815
}
816816

817817
let num_stmts = self.source[loc.block].statements.len();
818-
let new_temp = self.promoted.local_decls.push(LocalDecl::new_temp(
818+
let new_temp = self.promoted.local_decls.push(LocalDecl::new(
819819
self.source.local_decls[temp].ty,
820820
self.source.local_decls[temp].source_info.span,
821821
));
@@ -915,7 +915,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
915915
let tcx = self.tcx;
916916
let mut promoted_operand = |ty, span| {
917917
promoted.span = span;
918-
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
918+
promoted.local_decls[RETURN_PLACE] = LocalDecl::new(ty, span);
919919

920920
Operand::Constant(Box::new(Constant {
921921
span,
@@ -963,7 +963,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
963963
// Create a temp to hold the promoted reference.
964964
// This is because `*r` requires `r` to be a local,
965965
// otherwise we would use the `promoted` directly.
966-
let mut promoted_ref = LocalDecl::new_temp(ref_ty, span);
966+
let mut promoted_ref = LocalDecl::new(ref_ty, span);
967967
promoted_ref.source_info = statement.source_info;
968968
let promoted_ref = local_decls.push(promoted_ref);
969969
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
@@ -1081,8 +1081,7 @@ pub fn promote_candidates<'tcx>(
10811081
}
10821082

10831083
// Declare return place local so that `mir::Body::new` doesn't complain.
1084-
let initial_locals =
1085-
iter::once(LocalDecl::new_return_place(tcx.types.never, body.span)).collect();
1084+
let initial_locals = iter::once(LocalDecl::new(tcx.types.never, body.span)).collect();
10861085

10871086
let mut promoted = Body::new(
10881087
IndexVec::new(),

src/librustc_mir/util/patch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ impl<'tcx> MirPatch<'tcx> {
8383
pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
8484
let index = self.next_local;
8585
self.next_local += 1;
86-
self.new_locals.push(LocalDecl::new_temp(ty, span));
86+
self.new_locals.push(LocalDecl::new(ty, span));
8787
Local::new(index as usize)
8888
}
8989

9090
pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
9191
let index = self.next_local;
9292
self.next_local += 1;
93-
self.new_locals.push(LocalDecl::new_internal(ty, span));
93+
self.new_locals.push(LocalDecl::new(ty, span).internal());
9494
Local::new(index as usize)
9595
}
9696

src/librustc_mir_build/build/expr/as_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
383383
let fake_borrow_ty =
384384
tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
385385
let fake_borrow_temp =
386-
self.local_decls.push(LocalDecl::new_temp(fake_borrow_ty, expr_span));
386+
self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
387387
let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
388388
self.cfg.push_assign(
389389
block,

src/librustc_mir_build/build/expr/as_rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9797
// The `Box<T>` temporary created here is not a part of the HIR,
9898
// and therefore is not considered during generator OIBIT
9999
// determination. See the comment about `box` at `yield_in_scope`.
100-
let result = this.local_decls.push(LocalDecl::new_internal(expr.ty, expr_span));
100+
let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
101101
this.cfg.push(
102102
block,
103103
Statement { source_info, kind: StatementKind::StorageLive(result) },
@@ -377,7 +377,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
377377
let this = self;
378378

379379
let source_info = this.source_info(upvar_span);
380-
let temp = this.local_decls.push(LocalDecl::new_temp(upvar_ty, upvar_span));
380+
let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
381381

382382
this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
383383

src/librustc_mir_build/build/expr/as_temp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4747

4848
let expr_ty = expr.ty;
4949
let temp = {
50-
let mut local_decl = LocalDecl::new_temp(expr_ty, expr_span);
50+
let mut local_decl = LocalDecl::new(expr_ty, expr_span);
5151
if mutability == Mutability::Not {
5252
local_decl = local_decl.immutable();
5353
}

src/librustc_mir_build/build/expr/into.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
187187
let ptr_ty = ptr.ty;
188188
// Create an *internal* temp for the pointer, so that unsafety
189189
// checking won't complain about the raw pointer assignment.
190-
let ptr_temp = this.local_decls.push(LocalDecl {
191-
mutability: Mutability::Mut,
192-
ty: ptr_ty,
193-
user_ty: UserTypeProjections::none(),
190+
let ptr_temp = this.local_decls.push(LocalDecl::with_source_info(
191+
ptr_ty,
194192
source_info,
195-
internal: true,
196-
local_info: LocalInfo::Other,
197-
is_block_tail: None,
198-
});
193+
).internal());
199194
let ptr_temp = Place::from(ptr_temp);
200195
let block = unpack!(this.into(ptr_temp, block, ptr));
201196
this.into(this.hir.tcx().mk_place_deref(ptr_temp), block, val)
@@ -348,7 +343,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
348343
// value is Sized. Usually, this is caught in type checking, but
349344
// in the case of box expr there is no such check.
350345
if !destination.projection.is_empty() {
351-
this.local_decls.push(LocalDecl::new_temp(expr.ty, expr.span));
346+
this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
352347
}
353348

354349
debug_assert!(Category::of(&expr.kind) == Some(Category::Place));

src/librustc_mir_build/build/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15391539
let fake_borrow_deref_ty = matched_place.ty(&self.local_decls, tcx).ty;
15401540
let fake_borrow_ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
15411541
let fake_borrow_temp =
1542-
self.local_decls.push(LocalDecl::new_temp(fake_borrow_ty, temp_span));
1542+
self.local_decls.push(LocalDecl::new(fake_borrow_ty, temp_span));
15431543

15441544
(matched_place, fake_borrow_temp)
15451545
})

src/librustc_mir_build/build/misc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1515
/// N.B., **No cleanup is scheduled for this temporary.** You should
1616
/// call `schedule_drop` once the temporary is initialized.
1717
crate fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
18-
let temp = self.local_decls.push(LocalDecl::new_temp(ty, span));
18+
let temp = self.local_decls.push(LocalDecl::new(ty, span));
1919
let place = Place::from(temp);
2020
debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
2121
place

0 commit comments

Comments
 (0)