Skip to content

Commit 32da230

Browse files
committed
Auto merge of #105531 - matthiaskrgr:rollup-7y7zbgl, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #104460 (Migrate parts of `rustc_expand` to session diagnostics) - #105192 (Point at LHS on binop type err if relevant) - #105234 (Remove unneeded field from `SwitchTargets`) - #105239 (Avoid heap allocation when truncating thread names) - #105410 (Consider `parent_count` for const param defaults) - #105482 (Fix invalid codegen during debuginfo lowering) Failed merges: - #105411 (Introduce `with_forced_trimmed_paths`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a161a7b + ab50529 commit 32da230

File tree

172 files changed

+1704
-853
lines changed

Some content is hidden

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

172 files changed

+1704
-853
lines changed

compiler/rustc_borrowck/src/invalidation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
106106
self.check_activations(location);
107107

108108
match &terminator.kind {
109-
TerminatorKind::SwitchInt { discr, switch_ty: _, targets: _ } => {
109+
TerminatorKind::SwitchInt { discr, targets: _ } => {
110110
self.consume_operand(location, discr);
111111
}
112112
TerminatorKind::Drop { place: drop_place, target: _, unwind: _ } => {

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
644644
self.check_activations(loc, span, flow_state);
645645

646646
match &term.kind {
647-
TerminatorKind::SwitchInt { discr, switch_ty: _, targets: _ } => {
647+
TerminatorKind::SwitchInt { discr, targets: _ } => {
648648
self.consume_operand(loc, (discr, span), flow_state);
649649
}
650650
TerminatorKind::Drop { place, target: _, unwind: _ } => {

compiler/rustc_borrowck/src/type_check/mod.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -1360,25 +1360,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13601360
);
13611361
}
13621362
}
1363-
TerminatorKind::SwitchInt { discr, switch_ty, .. } => {
1363+
TerminatorKind::SwitchInt { discr, .. } => {
13641364
self.check_operand(discr, term_location);
13651365

1366-
let discr_ty = discr.ty(body, tcx);
1367-
if let Err(terr) = self.sub_types(
1368-
discr_ty,
1369-
*switch_ty,
1370-
term_location.to_locations(),
1371-
ConstraintCategory::Assignment,
1372-
) {
1373-
span_mirbug!(
1374-
self,
1375-
term,
1376-
"bad SwitchInt ({:?} on {:?}): {:?}",
1377-
switch_ty,
1378-
discr_ty,
1379-
terr
1380-
);
1381-
}
1366+
let switch_ty = discr.ty(body, tcx);
13821367
if !switch_ty.is_integral() && !switch_ty.is_char() && !switch_ty.is_bool() {
13831368
span_mirbug!(self, term, "bad SwitchInt discr ty {:?}", switch_ty);
13841369
}

compiler/rustc_builtin_macros/src/concat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn expand_concat(
1111
sp: rustc_span::Span,
1212
tts: TokenStream,
1313
) -> Box<dyn base::MacResult + 'static> {
14-
let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
14+
let Some(es) = base::get_exprs_from_tts(cx, tts) else {
1515
return DummyResult::any(sp);
1616
};
1717
let mut accumulator = String::new();

compiler/rustc_builtin_macros/src/concat_bytes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn expand_concat_bytes(
137137
sp: rustc_span::Span,
138138
tts: TokenStream,
139139
) -> Box<dyn base::MacResult + 'static> {
140-
let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
140+
let Some(es) = base::get_exprs_from_tts(cx, tts) else {
141141
return DummyResult::any(sp);
142142
};
143143
let mut accumulator = Vec::new();

compiler/rustc_builtin_macros/src/env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn expand_env<'cx>(
5252
sp: Span,
5353
tts: TokenStream,
5454
) -> Box<dyn base::MacResult + 'cx> {
55-
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
55+
let mut exprs = match get_exprs_from_tts(cx, tts) {
5656
Some(exprs) if exprs.is_empty() => {
5757
cx.span_err(sp, "env! takes 1 or 2 arguments");
5858
return DummyResult::any(sp);

compiler/rustc_codegen_cranelift/src/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
372372
}
373373
}
374374

375-
TerminatorKind::SwitchInt { discr, switch_ty, targets } => {
376-
let discr = codegen_operand(fx, discr).load_scalar(fx);
375+
TerminatorKind::SwitchInt { discr, targets } => {
376+
let discr = codegen_operand(fx, discr);
377+
let switch_ty = discr.layout().ty;
378+
let discr = discr.load_scalar(fx);
377379

378380
let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind()
379381
|| (targets.iter().count() == 1 && targets.iter().next().unwrap().0 == 0);

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
307307
helper: TerminatorCodegenHelper<'tcx>,
308308
bx: &mut Bx,
309309
discr: &mir::Operand<'tcx>,
310-
switch_ty: Ty<'tcx>,
311310
targets: &SwitchTargets,
312311
) {
313312
let discr = self.codegen_operand(bx, &discr);
314-
// `switch_ty` is redundant, sanity-check that.
315-
assert_eq!(discr.layout.ty, switch_ty);
313+
let switch_ty = discr.layout.ty;
316314
let mut target_iter = targets.iter();
317315
if target_iter.len() == 1 {
318316
// If there are two targets (one conditional, one fallback), emit `br` instead of
@@ -1293,8 +1291,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12931291
helper.funclet_br(self, bx, target, mergeable_succ())
12941292
}
12951293

1296-
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => {
1297-
self.codegen_switchint_terminator(helper, bx, discr, switch_ty, targets);
1294+
mir::TerminatorKind::SwitchInt { ref discr, ref targets } => {
1295+
self.codegen_switchint_terminator(helper, bx, discr, targets);
12981296
MergingSucc::False
12991297
}
13001298

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+107-29
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use rustc_index::vec::IndexVec;
33
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
44
use rustc_middle::mir;
55
use rustc_middle::ty;
6+
use rustc_middle::ty::layout::TyAndLayout;
67
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
78
use rustc_session::config::DebugInfo;
89
use rustc_span::symbol::{kw, Symbol};
910
use rustc_span::{BytePos, Span};
10-
use rustc_target::abi::Abi;
11-
use rustc_target::abi::Size;
11+
use rustc_target::abi::{Abi, Size, VariantIdx};
1212

1313
use super::operand::{OperandRef, OperandValue};
1414
use super::place::PlaceRef;
@@ -76,6 +76,106 @@ impl<'tcx, S: Copy, L: Copy> DebugScope<S, L> {
7676
}
7777
}
7878

79+
trait DebugInfoOffsetLocation<'tcx, Bx> {
80+
fn deref(&self, bx: &mut Bx) -> Self;
81+
fn layout(&self) -> TyAndLayout<'tcx>;
82+
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self;
83+
fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self;
84+
}
85+
86+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
87+
for PlaceRef<'tcx, Bx::Value>
88+
{
89+
fn deref(&self, bx: &mut Bx) -> Self {
90+
bx.load_operand(*self).deref(bx.cx())
91+
}
92+
93+
fn layout(&self) -> TyAndLayout<'tcx> {
94+
self.layout
95+
}
96+
97+
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self {
98+
PlaceRef::project_field(*self, bx, field.index())
99+
}
100+
101+
fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
102+
self.project_downcast(bx, variant)
103+
}
104+
}
105+
106+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
107+
for TyAndLayout<'tcx>
108+
{
109+
fn deref(&self, bx: &mut Bx) -> Self {
110+
bx.cx().layout_of(
111+
self.ty.builtin_deref(true).unwrap_or_else(|| bug!("cannot deref `{}`", self.ty)).ty,
112+
)
113+
}
114+
115+
fn layout(&self) -> TyAndLayout<'tcx> {
116+
*self
117+
}
118+
119+
fn project_field(&self, bx: &mut Bx, field: mir::Field) -> Self {
120+
self.field(bx.cx(), field.index())
121+
}
122+
123+
fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
124+
self.for_variant(bx.cx(), variant)
125+
}
126+
}
127+
128+
struct DebugInfoOffset<T> {
129+
/// Offset from the `base` used to calculate the debuginfo offset.
130+
direct_offset: Size,
131+
/// Each offset in this vector indicates one level of indirection from the base or previous
132+
/// indirect offset plus a dereference.
133+
indirect_offsets: Vec<Size>,
134+
/// The final location debuginfo should point to.
135+
result: T,
136+
}
137+
138+
fn calculate_debuginfo_offset<
139+
'a,
140+
'tcx,
141+
Bx: BuilderMethods<'a, 'tcx>,
142+
L: DebugInfoOffsetLocation<'tcx, Bx>,
143+
>(
144+
bx: &mut Bx,
145+
local: mir::Local,
146+
var: &PerLocalVarDebugInfo<'tcx, Bx::DIVariable>,
147+
base: L,
148+
) -> DebugInfoOffset<L> {
149+
let mut direct_offset = Size::ZERO;
150+
// FIXME(eddyb) use smallvec here.
151+
let mut indirect_offsets = vec![];
152+
let mut place = base;
153+
154+
for elem in &var.projection[..] {
155+
match *elem {
156+
mir::ProjectionElem::Deref => {
157+
indirect_offsets.push(Size::ZERO);
158+
place = place.deref(bx);
159+
}
160+
mir::ProjectionElem::Field(field, _) => {
161+
let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
162+
*offset += place.layout().fields.offset(field.index());
163+
place = place.project_field(bx, field);
164+
}
165+
mir::ProjectionElem::Downcast(_, variant) => {
166+
place = place.downcast(bx, variant);
167+
}
168+
_ => span_bug!(
169+
var.source_info.span,
170+
"unsupported var debuginfo place `{:?}`",
171+
mir::Place { local, projection: var.projection },
172+
),
173+
}
174+
}
175+
176+
DebugInfoOffset { direct_offset, indirect_offsets, result: place }
177+
}
178+
79179
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
80180
pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
81181
bx.set_span(source_info.span);
@@ -262,33 +362,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
262362
let Some(dbg_var) = var.dbg_var else { continue };
263363
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { continue };
264364

265-
let mut direct_offset = Size::ZERO;
266-
// FIXME(eddyb) use smallvec here.
267-
let mut indirect_offsets = vec![];
268-
let mut place = base;
269-
270-
for elem in &var.projection[..] {
271-
match *elem {
272-
mir::ProjectionElem::Deref => {
273-
indirect_offsets.push(Size::ZERO);
274-
place = bx.load_operand(place).deref(bx.cx());
275-
}
276-
mir::ProjectionElem::Field(field, _) => {
277-
let i = field.index();
278-
let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
279-
*offset += place.layout.fields.offset(i);
280-
place = place.project_field(bx, i);
281-
}
282-
mir::ProjectionElem::Downcast(_, variant) => {
283-
place = place.project_downcast(bx, variant);
284-
}
285-
_ => span_bug!(
286-
var.source_info.span,
287-
"unsupported var debuginfo place `{:?}`",
288-
mir::Place { local, projection: var.projection },
289-
),
290-
}
291-
}
365+
let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
366+
calculate_debuginfo_offset(bx, local, &var, base.layout);
292367

293368
// When targeting MSVC, create extra allocas for arguments instead of pointing multiple
294369
// dbg_var_addr() calls into the same alloca with offsets. MSVC uses CodeView records
@@ -306,6 +381,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
306381
|| !matches!(&indirect_offsets[..], [Size::ZERO] | []));
307382

308383
if should_create_individual_allocas {
384+
let DebugInfoOffset { direct_offset: _, indirect_offsets: _, result: place } =
385+
calculate_debuginfo_offset(bx, local, &var, base);
386+
309387
// Create a variable which will be a pointer to the actual value
310388
let ptr_ty = bx.tcx().mk_ty(ty::RawPtr(ty::TypeAndMut {
311389
mutbl: mir::Mutability::Mut,

compiler/rustc_const_eval/src/interpret/terminator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2929

3030
Goto { target } => self.go_to_block(target),
3131

32-
SwitchInt { ref discr, ref targets, switch_ty } => {
32+
SwitchInt { ref discr, ref targets } => {
3333
let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
3434
trace!("SwitchInt({:?})", *discr);
35-
assert_eq!(discr.layout.ty, switch_ty);
3635

3736
// Branch to the `otherwise` case by default, if no match is found.
3837
let mut target_block = targets.otherwise();

compiler/rustc_const_eval/src/transform/validate.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -686,17 +686,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
686686
TerminatorKind::Goto { target } => {
687687
self.check_edge(location, *target, EdgeKind::Normal);
688688
}
689-
TerminatorKind::SwitchInt { targets, switch_ty, discr } => {
690-
let ty = discr.ty(&self.body.local_decls, self.tcx);
691-
if ty != *switch_ty {
692-
self.fail(
693-
location,
694-
format!(
695-
"encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
696-
ty, switch_ty,
697-
),
698-
);
699-
}
689+
TerminatorKind::SwitchInt { targets, discr } => {
690+
let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
700691

701692
let target_width = self.tcx.sess.target.pointer_width;
702693

0 commit comments

Comments
 (0)