Skip to content

Commit 9dee4e4

Browse files
committed
Auto merge of rust-lang#107267 - cjgillot:keep-aggregate, r=oli-obk
Do not deaggregate MIR This turns out to simplify a lot of things. I haven't checked the consequences for miri yet. cc `@JakobDegen` r? `@oli-obk`
2 parents 4aa6afa + 5c39ba2 commit 9dee4e4

File tree

114 files changed

+658
-1039
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+658
-1039
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
26452645
operands,
26462646
) = rvalue
26472647
{
2648+
let def_id = def_id.expect_local();
26482649
for operand in operands {
26492650
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand else {
26502651
continue;
@@ -2667,7 +2668,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
26672668
// into a place then we should annotate the closure in
26682669
// case it ends up being assigned into the return place.
26692670
annotated_closure =
2670-
self.annotate_fn_sig(*def_id, substs.as_closure().sig());
2671+
self.annotate_fn_sig(def_id, substs.as_closure().sig());
26712672
debug!(
26722673
"annotate_argument_and_return_for_borrow: \
26732674
annotated_closure={:?} assigned_from_local={:?} \

compiler/rustc_borrowck/src/diagnostics/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
817817
&& let AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) = **kind
818818
{
819819
debug!("move_spans: def_id={:?} places={:?}", def_id, places);
820+
let def_id = def_id.expect_local();
820821
if let Some((args_span, generator_kind, capture_kind_span, path_span)) =
821822
self.closure_span(def_id, moved_place, places)
822823
{
@@ -945,6 +946,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
945946
box AggregateKind::Generator(def_id, _, _) => (def_id, true),
946947
_ => continue,
947948
};
949+
let def_id = def_id.expect_local();
948950

949951
debug!(
950952
"borrow_spans: def_id={:?} is_generator={:?} places={:?}",

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12781278
// in order to populate our used_mut set.
12791279
match **aggregate_kind {
12801280
AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
1281+
let def_id = def_id.expect_local();
12811282
let BorrowCheckResult { used_mut_upvars, .. } =
12821283
self.infcx.tcx.mir_borrowck(def_id);
12831284
debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25362536
// clauses on the struct.
25372537
AggregateKind::Closure(def_id, substs)
25382538
| AggregateKind::Generator(def_id, substs, _) => {
2539-
(def_id.to_def_id(), self.prove_closure_bounds(tcx, def_id, substs, location))
2539+
(def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), substs, location))
25402540
}
25412541

25422542
AggregateKind::Array(_) | AggregateKind::Tuple => {

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
1313
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1414
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
1515
use rustc_span::source_map::{Span, DUMMY_SP};
16+
use rustc_target::abi::VariantIdx;
1617

1718
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1819
#[instrument(level = "trace", skip(self, bx))]
@@ -106,31 +107,31 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
106107
}
107108

108109
mir::Rvalue::Aggregate(ref kind, ref operands) => {
109-
let (dest, active_field_index) = match **kind {
110-
mir::AggregateKind::Adt(adt_did, variant_index, _, _, active_field_index) => {
111-
dest.codegen_set_discr(bx, variant_index);
112-
if bx.tcx().adt_def(adt_did).is_enum() {
113-
(dest.project_downcast(bx, variant_index), active_field_index)
114-
} else {
115-
(dest, active_field_index)
116-
}
110+
let (variant_index, variant_dest, active_field_index) = match **kind {
111+
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
112+
let variant_dest = dest.project_downcast(bx, variant_index);
113+
(variant_index, variant_dest, active_field_index)
117114
}
118-
_ => (dest, None),
115+
_ => (VariantIdx::from_u32(0), dest, None),
119116
};
117+
if active_field_index.is_some() {
118+
assert_eq!(operands.len(), 1);
119+
}
120120
for (i, operand) in operands.iter().enumerate() {
121121
let op = self.codegen_operand(bx, operand);
122122
// Do not generate stores and GEPis for zero-sized fields.
123123
if !op.layout.is_zst() {
124124
let field_index = active_field_index.unwrap_or(i);
125125
let field = if let mir::AggregateKind::Array(_) = **kind {
126126
let llindex = bx.cx().const_usize(field_index as u64);
127-
dest.project_index(bx, llindex)
127+
variant_dest.project_index(bx, llindex)
128128
} else {
129-
dest.project_field(bx, field_index)
129+
variant_dest.project_field(bx, field_index)
130130
};
131131
op.val.store(bx, field);
132132
}
133133
}
134+
dest.codegen_set_discr(bx, variant_index);
134135
}
135136

136137
_ => {

compiler/rustc_const_eval/src/interpret/place.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -774,15 +774,6 @@ where
774774
variant_index: VariantIdx,
775775
dest: &PlaceTy<'tcx, M::Provenance>,
776776
) -> InterpResult<'tcx> {
777-
// This must be an enum or generator.
778-
match dest.layout.ty.kind() {
779-
ty::Adt(adt, _) => assert!(adt.is_enum()),
780-
ty::Generator(..) => {}
781-
_ => span_bug!(
782-
self.cur_span(),
783-
"write_discriminant called on non-variant-type (neither enum nor generator)"
784-
),
785-
}
786777
// Layout computation excludes uninhabited variants from consideration
787778
// therefore there's no way to represent those variants in the given layout.
788779
// Essentially, uninhabited variants do not have a tag that corresponds to their
@@ -855,6 +846,34 @@ where
855846
Ok(())
856847
}
857848

849+
/// Writes the discriminant of the given variant.
850+
#[instrument(skip(self), level = "debug")]
851+
pub fn write_aggregate(
852+
&mut self,
853+
kind: &mir::AggregateKind<'tcx>,
854+
operands: &[mir::Operand<'tcx>],
855+
dest: &PlaceTy<'tcx, M::Provenance>,
856+
) -> InterpResult<'tcx> {
857+
self.write_uninit(&dest)?;
858+
let (variant_index, variant_dest, active_field_index) = match *kind {
859+
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
860+
let variant_dest = self.place_downcast(&dest, variant_index)?;
861+
(variant_index, variant_dest, active_field_index)
862+
}
863+
_ => (VariantIdx::from_u32(0), dest.clone(), None),
864+
};
865+
if active_field_index.is_some() {
866+
assert_eq!(operands.len(), 1);
867+
}
868+
for (field_index, operand) in operands.iter().enumerate() {
869+
let field_index = active_field_index.unwrap_or(field_index);
870+
let field_dest = self.place_field(&variant_dest, field_index)?;
871+
let op = self.eval_operand(operand, Some(field_dest.layout))?;
872+
self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?;
873+
}
874+
self.write_discriminant(variant_index, &dest)
875+
}
876+
858877
pub fn raw_const_to_mplace(
859878
&self,
860879
raw: ConstAlloc<'tcx>,

compiler/rustc_const_eval/src/interpret/step.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
199199
}
200200

201201
Aggregate(box ref kind, ref operands) => {
202-
assert!(matches!(kind, mir::AggregateKind::Array(..)));
203-
204-
for (field_index, operand) in operands.iter().enumerate() {
205-
let op = self.eval_operand(operand, None)?;
206-
let field_dest = self.place_field(&dest, field_index)?;
207-
self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?;
208-
}
202+
self.write_aggregate(kind, operands, &dest)?;
209203
}
210204

211205
Repeat(ref operand, _) => {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
453453

454454
Rvalue::Aggregate(kind, ..) => {
455455
if let AggregateKind::Generator(def_id, ..) = kind.as_ref()
456-
&& let Some(generator_kind @ hir::GeneratorKind::Async(..)) = self.tcx.generator_kind(def_id.to_def_id())
456+
&& let Some(generator_kind @ hir::GeneratorKind::Async(..)) = self.tcx.generator_kind(def_id)
457457
{
458458
self.check_op(ops::Generator(generator_kind));
459459
}

compiler/rustc_const_eval/src/transform/validate.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_middle::mir::interpret::Scalar;
88
use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
99
use rustc_middle::mir::visit::{PlaceContext, Visitor};
1010
use rustc_middle::mir::{
11-
traversal, AggregateKind, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping,
12-
Local, Location, MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef,
13-
ProjectionElem, RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind,
14-
Terminator, TerminatorKind, UnOp, START_BLOCK,
11+
traversal, BasicBlock, BinOp, Body, BorrowKind, CastKind, CopyNonOverlapping, Local, Location,
12+
MirPass, MirPhase, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, ProjectionElem,
13+
RetagKind, RuntimePhase, Rvalue, SourceScope, Statement, StatementKind, Terminator,
14+
TerminatorKind, UnOp, START_BLOCK,
1515
};
1616
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt};
1717
use rustc_mir_dataflow::impls::MaybeStorageLive;
@@ -423,19 +423,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
423423
};
424424
}
425425
match rvalue {
426-
Rvalue::Use(_) | Rvalue::CopyForDeref(_) => {}
427-
Rvalue::Aggregate(agg_kind, _) => {
428-
let disallowed = match **agg_kind {
429-
AggregateKind::Array(..) => false,
430-
_ => self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup),
431-
};
432-
if disallowed {
433-
self.fail(
434-
location,
435-
format!("{:?} have been lowered to field assignments", rvalue),
436-
)
437-
}
438-
}
426+
Rvalue::Use(_) | Rvalue::CopyForDeref(_) | Rvalue::Aggregate(..) => {}
439427
Rvalue::Ref(_, BorrowKind::Shallow, _) => {
440428
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
441429
self.fail(

compiler/rustc_const_eval/src/util/aggregate.rs

-77
This file was deleted.

compiler/rustc_const_eval/src/util/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod aggregate;
21
mod alignment;
32
mod call_kind;
43
pub mod collect_writes;
@@ -7,7 +6,6 @@ mod find_self_call;
76
mod might_permit_raw_init;
87
mod type_name;
98

10-
pub use self::aggregate::expand_aggregate;
119
pub use self::alignment::is_disaligned;
1210
pub use self::call_kind::{call_kind, CallDesugaringKind, CallKind};
1311
pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};

compiler/rustc_middle/src/mir/mod.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2098,10 +2098,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
20982098
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
20992099
let name = if tcx.sess.opts.unstable_opts.span_free_formats {
21002100
let substs = tcx.lift(substs).unwrap();
2101-
format!(
2102-
"[closure@{}]",
2103-
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
2104-
)
2101+
format!("[closure@{}]", tcx.def_path_str_with_substs(def_id, substs),)
21052102
} else {
21062103
let span = tcx.def_span(def_id);
21072104
format!(
@@ -2112,11 +2109,17 @@ impl<'tcx> Debug for Rvalue<'tcx> {
21122109
let mut struct_fmt = fmt.debug_struct(&name);
21132110

21142111
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2115-
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2112+
if let Some(def_id) = def_id.as_local()
2113+
&& let Some(upvars) = tcx.upvars_mentioned(def_id)
2114+
{
21162115
for (&var_id, place) in iter::zip(upvars.keys(), places) {
21172116
let var_name = tcx.hir().name(var_id);
21182117
struct_fmt.field(var_name.as_str(), place);
21192118
}
2119+
} else {
2120+
for (index, place) in places.iter().enumerate() {
2121+
struct_fmt.field(&format!("{index}"), place);
2122+
}
21202123
}
21212124

21222125
struct_fmt.finish()
@@ -2127,11 +2130,17 @@ impl<'tcx> Debug for Rvalue<'tcx> {
21272130
let mut struct_fmt = fmt.debug_struct(&name);
21282131

21292132
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
2130-
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
2133+
if let Some(def_id) = def_id.as_local()
2134+
&& let Some(upvars) = tcx.upvars_mentioned(def_id)
2135+
{
21312136
for (&var_id, place) in iter::zip(upvars.keys(), places) {
21322137
let var_name = tcx.hir().name(var_id);
21332138
struct_fmt.field(var_name.as_str(), place);
21342139
}
2140+
} else {
2141+
for (index, place) in places.iter().enumerate() {
2142+
struct_fmt.field(&format!("{index}"), place);
2143+
}
21352144
}
21362145

21372146
struct_fmt.finish()

compiler/rustc_middle/src/mir/syntax.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1203,10 +1203,8 @@ pub enum AggregateKind<'tcx> {
12031203
/// active field index would identity the field `c`
12041204
Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<usize>),
12051205

1206-
// Note: We can use LocalDefId since closures and generators a deaggregated
1207-
// before codegen.
1208-
Closure(LocalDefId, SubstsRef<'tcx>),
1209-
Generator(LocalDefId, SubstsRef<'tcx>, hir::Movability),
1206+
Closure(DefId, SubstsRef<'tcx>),
1207+
Generator(DefId, SubstsRef<'tcx>, hir::Movability),
12101208
}
12111209

12121210
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]

compiler/rustc_middle/src/mir/tcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ impl<'tcx> Rvalue<'tcx> {
205205
AggregateKind::Adt(did, _, substs, _, _) => {
206206
tcx.bound_type_of(did).subst(tcx, substs)
207207
}
208-
AggregateKind::Closure(did, substs) => tcx.mk_closure(did.to_def_id(), substs),
208+
AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
209209
AggregateKind::Generator(did, substs, movability) => {
210-
tcx.mk_generator(did.to_def_id(), substs, movability)
210+
tcx.mk_generator(did, substs, movability)
211211
}
212212
},
213213
Rvalue::ShallowInitBox(_, ty) => tcx.mk_box(ty),

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
439439
// We implicitly set the discriminant to 0. See
440440
// librustc_mir/transform/deaggregator.rs for details.
441441
let movability = movability.unwrap();
442-
Box::new(AggregateKind::Generator(closure_id, substs, movability))
442+
Box::new(AggregateKind::Generator(
443+
closure_id.to_def_id(),
444+
substs,
445+
movability,
446+
))
443447
}
444448
UpvarSubsts::Closure(substs) => {
445-
Box::new(AggregateKind::Closure(closure_id, substs))
449+
Box::new(AggregateKind::Closure(closure_id.to_def_id(), substs))
446450
}
447451
};
448452
block.and(Rvalue::Aggregate(result, operands))

0 commit comments

Comments
 (0)