Skip to content

Commit bbb664a

Browse files
committed
Auto merge of #66642 - ecstatic-morse:promotion-in-const, r=eddyb
Create promoted MIR fragments for `const` and `static`s Resolves #65732. The previous strategy of removing `Drop` and `StorageDead` for promoted locals only worked for rvalue lifetime extension and only if no `loop`s were present. This PR applies the approach currently used for `fn` and `const fn`s to `const` and `statics`. This may have some performance impacts. r? @eddyb
2 parents f453d11 + f9ed219 commit bbb664a

File tree

5 files changed

+48
-129
lines changed

5 files changed

+48
-129
lines changed

src/librustc_mir/transform/promote_consts.rs

+4-100
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use syntax::symbol::sym;
2424
use syntax_pos::{Span, DUMMY_SP};
2525

2626
use rustc_index::vec::{IndexVec, Idx};
27-
use rustc_index::bit_set::HybridBitSet;
2827
use rustc_target::spec::abi::Abi;
2928

3029
use std::cell::Cell;
@@ -35,10 +34,8 @@ use crate::transform::check_consts::{qualifs, Item, ConstKind, is_lang_panic_fn}
3534

3635
/// A `MirPass` for promotion.
3736
///
38-
/// In this case, "promotion" entails the following:
39-
/// - Extract promotable temps in `fn` and `const fn` into their own MIR bodies.
40-
/// - Extend lifetimes in `const` and `static` by removing `Drop` and `StorageDead`.
41-
/// - Emit errors if the requirements of `#[rustc_args_required_const]` are not met.
37+
/// Promotion is the extraction of promotable temps into separate MIR bodies. This pass also emits
38+
/// errors when promotion of `#[rustc_args_required_const]` arguments fails.
4239
///
4340
/// After this pass is run, `promoted_fragments` will hold the MIR body corresponding to each
4441
/// newly created `StaticKind::Promoted`.
@@ -63,26 +60,13 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
6360

6461
let def_id = src.def_id();
6562

66-
let item = Item::new(tcx, def_id, body);
6763
let mut rpo = traversal::reverse_postorder(body);
6864
let (temps, all_candidates) = collect_temps_and_candidates(tcx, body, &mut rpo);
6965

7066
let promotable_candidates = validate_candidates(tcx, body, def_id, &temps, &all_candidates);
7167

72-
// For now, lifetime extension is done in `const` and `static`s without creating promoted
73-
// MIR fragments by removing `Drop` and `StorageDead` for each referent. However, this will
74-
// not work inside loops when they are allowed in `const`s.
75-
//
76-
// FIXME: use promoted MIR fragments everywhere?
77-
let promoted_fragments = if should_create_promoted_mir_fragments(item.const_kind) {
78-
promote_candidates(def_id, body, tcx, temps, promotable_candidates)
79-
} else {
80-
// FIXME: promote const array initializers in consts.
81-
remove_drop_and_storage_dead_on_promoted_locals(tcx, body, &promotable_candidates);
82-
IndexVec::new()
83-
};
84-
85-
self.promoted_fragments.set(promoted_fragments);
68+
let promoted = promote_candidates(def_id, body, tcx, temps, promotable_candidates);
69+
self.promoted_fragments.set(promoted);
8670
}
8771
}
8872

@@ -1178,83 +1162,3 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
11781162
should_promote={:?} feature_flag={:?}", mir_def_id, should_promote, feature_flag);
11791163
should_promote && !feature_flag
11801164
}
1181-
1182-
fn should_create_promoted_mir_fragments(const_kind: Option<ConstKind>) -> bool {
1183-
match const_kind {
1184-
Some(ConstKind::ConstFn) | None => true,
1185-
Some(ConstKind::Const) | Some(ConstKind::Static) | Some(ConstKind::StaticMut) => false,
1186-
}
1187-
}
1188-
1189-
/// In `const` and `static` everything without `StorageDead`
1190-
/// is `'static`, we don't have to create promoted MIR fragments,
1191-
/// just remove `Drop` and `StorageDead` on "promoted" locals.
1192-
fn remove_drop_and_storage_dead_on_promoted_locals(
1193-
tcx: TyCtxt<'tcx>,
1194-
body: &mut Body<'tcx>,
1195-
promotable_candidates: &[Candidate],
1196-
) {
1197-
debug!("run_pass: promotable_candidates={:?}", promotable_candidates);
1198-
1199-
// Removing `StorageDead` will cause errors for temps declared inside a loop body. For now we
1200-
// simply skip promotion if a loop exists, since loops are not yet allowed in a `const`.
1201-
//
1202-
// FIXME: Just create MIR fragments for `const`s instead of using this hackish approach?
1203-
if body.is_cfg_cyclic() {
1204-
tcx.sess.delay_span_bug(body.span, "Control-flow cycle detected in `const`");
1205-
return;
1206-
}
1207-
1208-
// The underlying local for promotion contexts like `&temp` and `&(temp.proj)`.
1209-
let mut requires_lifetime_extension = HybridBitSet::new_empty(body.local_decls.len());
1210-
1211-
promotable_candidates
1212-
.iter()
1213-
.filter_map(|c| {
1214-
match c {
1215-
Candidate::Ref(loc) => Some(loc),
1216-
Candidate::Repeat(_) | Candidate::Argument { .. } => None,
1217-
}
1218-
})
1219-
.map(|&Location { block, statement_index }| {
1220-
// FIXME: store the `Local` for each `Candidate` when it is created.
1221-
let place = match &body[block].statements[statement_index].kind {
1222-
StatementKind::Assign(box ( _, Rvalue::Ref(_, _, place))) => place,
1223-
_ => bug!("`Candidate::Ref` without corresponding assignment"),
1224-
};
1225-
1226-
match place.base {
1227-
PlaceBase::Local(local) => local,
1228-
PlaceBase::Static(_) => bug!("`Candidate::Ref` for a non-local"),
1229-
}
1230-
})
1231-
.for_each(|local| {
1232-
requires_lifetime_extension.insert(local);
1233-
});
1234-
1235-
// Remove `Drop` terminators and `StorageDead` statements for all promotable temps that require
1236-
// lifetime extension.
1237-
for block in body.basic_blocks_mut() {
1238-
block.statements.retain(|statement| {
1239-
match statement.kind {
1240-
StatementKind::StorageDead(index) => !requires_lifetime_extension.contains(index),
1241-
_ => true
1242-
}
1243-
});
1244-
let terminator = block.terminator_mut();
1245-
match &terminator.kind {
1246-
TerminatorKind::Drop {
1247-
location,
1248-
target,
1249-
..
1250-
} => {
1251-
if let Some(index) = location.as_local() {
1252-
if requires_lifetime_extension.contains(index) {
1253-
terminator.kind = TerminatorKind::Goto { target: *target };
1254-
}
1255-
}
1256-
}
1257-
_ => {}
1258-
}
1259-
}
1260-
}

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/const-fns.rs

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-pass
2+
3+
#![allow(unused)]
4+
#![feature(const_in_array_repeat_expressions)]
5+
6+
// Some type that is not copyable.
7+
struct Bar;
8+
9+
const fn type_no_copy() -> Option<Bar> {
10+
None
11+
}
12+
13+
const fn type_copy() -> u32 {
14+
3
15+
}
16+
17+
const _: [u32; 2] = [type_copy(); 2];
18+
19+
// This is allowed because all promotion contexts use the explicit rules for promotability when
20+
// inside an explicit const context.
21+
const _: [Option<Bar>; 2] = [type_no_copy(); 2];
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(const_in_array_repeat_expressions)]
2+
3+
// Some type that is not copyable.
4+
struct Bar;
5+
6+
const fn no_copy() -> Option<Bar> {
7+
None
8+
}
9+
10+
const fn copy() -> u32 {
11+
3
12+
}
13+
14+
fn main() {
15+
let _: [u32; 2] = [copy(); 2];
16+
let _: [Option<Bar>; 2] = [no_copy(); 2];
17+
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
18+
}

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/const-fns.stderr src/test/ui/consts/rfc-2203-const-array-repeat-exprs/fn-call-in-non-const.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
2-
--> $DIR/const-fns.rs:18:35
2+
--> $DIR/fn-call-in-non-const.rs:16:31
33
|
4-
LL | const ARR: [Option<Bar>; 2] = [type_no_copy(); 2];
5-
| ^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
4+
LL | let _: [Option<Bar>; 2] = [no_copy(); 2];
5+
| ^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
66
|
77
= help: the following implementations were found:
88
<std::option::Option<T> as std::marker::Copy>

0 commit comments

Comments
 (0)