Skip to content

Commit fe98231

Browse files
committed
Auto merge of #79200 - Dylan-DPC:rollup-su689pq, r=Dylan-DPC
Rollup of 14 pull requests Successful merges: - #78961 (Make bad "rust-call" arguments no longer ICE) - #79082 (Improve the diagnostic for when an `fn` contains qualifiers inside an `extern` block.) - #79090 (libary: Forward compiler-builtins "asm" and "mangled-names" feature) - #79094 (Add //ignore-macos to pretty-std-collections.rs) - #79101 (Don't special case constant operands when lowering intrinsics) - #79102 (Add two regression tests) - #79110 (Remove redundant notes in E0275) - #79116 (compiletest: Fix a warning in debuginfo tests on windows-gnu) - #79117 (add optimization fuel checks to some mir passes) - #79147 (Highlight MIR as Rust on GitHub) - #79149 (Move capture lowering from THIR to MIR) - #79155 (fix handling the default config for profiler and sanitizers) - #79156 (Allow using `download-ci-llvm` from directories other than the root) - #79164 (Permit standalone generic parameters as const generic arguments in macros) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3d3c8c5 + b5fffdc commit fe98231

Some content is hidden

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

52 files changed

+632
-729
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*.h rust
66
*.rs rust diff=rust
77
*.fixed linguist-language=Rust
8+
*.mir linguist-language=Rust
89
src/etc/installer/gfx/* binary
910
*.woff binary
1011
src/vendor/** -text

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl<'a> AstValidator<'a> {
522522
self.err_handler()
523523
.struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers")
524524
.span_label(self.current_extern_span(), "in this `extern` block")
525-
.span_suggestion(
525+
.span_suggestion_verbose(
526526
span.until(ident.span.shrink_to_lo()),
527527
"remove the qualifiers",
528528
"fn ".to_string(),

compiler/rustc_mir/src/transform/const_prop.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
800800
}
801801
}
802802

803-
trace!("attepting to replace {:?} with {:?}", rval, value);
803+
trace!("attempting to replace {:?} with {:?}", rval, value);
804804
if let Err(e) = self.ecx.const_validate_operand(
805805
value,
806806
vec![],
@@ -890,6 +890,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
890890
return false;
891891
}
892892

893+
if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
894+
return false;
895+
}
896+
893897
match *op {
894898
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
895899
s.is_bits()

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
4646
let should_cleanup = !opts_to_apply.is_empty();
4747

4848
for opt_to_apply in opts_to_apply {
49+
if !tcx.consider_optimizing(|| format!("EarlyOtherwiseBranch {:?}", &opt_to_apply)) {
50+
break;
51+
}
52+
4953
trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);
5054

5155
let statements_before =

compiler/rustc_mir/src/transform/instcombine.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
3939
tcx: TyCtxt<'tcx>,
4040
}
4141

42+
impl<'tcx> InstCombineVisitor<'tcx> {
43+
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
44+
self.tcx.consider_optimizing(|| {
45+
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
46+
})
47+
}
48+
}
49+
4250
impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
4351
fn tcx(&self) -> TyCtxt<'tcx> {
4452
self.tcx
4553
}
4654

4755
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
48-
if self.optimizations.and_stars.remove(&location) {
56+
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
4957
debug!("replacing `&*`: {:?}", rvalue);
5058
let new_place = match rvalue {
5159
Rvalue::Ref(_, _, place) => {
@@ -67,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
6775
}
6876

6977
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
70-
debug!("replacing `Len([_; N])`: {:?}", rvalue);
71-
*rvalue = Rvalue::Use(Operand::Constant(box constant));
78+
if self.should_combine(rvalue, location) {
79+
debug!("replacing `Len([_; N])`: {:?}", rvalue);
80+
*rvalue = Rvalue::Use(Operand::Constant(box constant));
81+
}
7282
}
7383

7484
if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
75-
debug!("replacing {:?} with {:?}", rvalue, operand);
76-
*rvalue = Rvalue::Use(operand);
85+
if self.should_combine(rvalue, location) {
86+
debug!("replacing {:?} with {:?}", rvalue, operand);
87+
*rvalue = Rvalue::Use(operand);
88+
}
7789
}
7890

7991
if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
80-
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
81-
*rvalue = Rvalue::Use(Operand::Copy(place));
92+
if self.should_combine(rvalue, location) {
93+
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
94+
*rvalue = Rvalue::Use(Operand::Copy(place));
95+
}
8296
}
8397

8498
self.super_rvalue(rvalue, location)

compiler/rustc_mir/src/transform/lower_intrinsics.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ pub struct LowerIntrinsics;
1111

1212
impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
1313
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14-
for block in body.basic_blocks_mut() {
14+
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
15+
for block in basic_blocks {
1516
let terminator = block.terminator.as_mut().unwrap();
16-
if let TerminatorKind::Call {
17-
func: Operand::Constant(box Constant { literal: ty::Const { ty: func_ty, .. }, .. }),
18-
args,
19-
destination,
20-
..
21-
} = &mut terminator.kind
22-
{
17+
if let TerminatorKind::Call { func, args, destination, .. } = &mut terminator.kind {
18+
let func_ty = func.ty(local_decls, tcx);
2319
let (intrinsic_name, substs) = match resolve_rust_intrinsic(tcx, func_ty) {
2420
None => continue,
2521
Some(it) => it,

compiler/rustc_mir/src/transform/match_branches.rs

+5
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4343
}
4444

4545
let param_env = tcx.param_env(body.source.def_id());
46+
let def_id = body.source.def_id();
4647
let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
4748
'outer: for bb_idx in bbs.indices() {
49+
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
50+
continue;
51+
}
52+
4853
let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
4954
TerminatorKind::SwitchInt {
5055
discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)),

compiler/rustc_mir/src/transform/multiple_return_terminators.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
1616

1717
// find basic blocks with no statement and a return terminator
1818
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
19+
let def_id = body.source.def_id();
1920
let bbs = body.basic_blocks_mut();
2021
for idx in bbs.indices() {
2122
if bbs[idx].statements.is_empty()
@@ -26,6 +27,10 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
2627
}
2728

2829
for bb in bbs {
30+
if !tcx.consider_optimizing(|| format!("MultipleReturnTerminators {:?} ", def_id)) {
31+
break;
32+
}
33+
2934
if let TerminatorKind::Goto { target } = bb.terminator().kind {
3035
if bbs_simple_returns.contains(target) {
3136
bb.terminator_mut().kind = TerminatorKind::Return;

compiler/rustc_mir/src/transform/nrvo.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
3838
return;
3939
}
4040

41+
let def_id = body.source.def_id();
4142
let returned_local = match local_eligible_for_nrvo(body) {
4243
Some(l) => l,
4344
None => {
44-
debug!("`{:?}` was ineligible for NRVO", body.source.def_id());
45+
debug!("`{:?}` was ineligible for NRVO", def_id);
4546
return;
4647
}
4748
};
4849

50+
if !tcx.consider_optimizing(|| format!("RenameReturnPlace {:?}", def_id)) {
51+
return;
52+
}
53+
4954
debug!(
5055
"`{:?}` was eligible for NRVO, making {:?} the return place",
51-
body.source.def_id(),
52-
returned_local
56+
def_id, returned_local
5357
);
5458

5559
RenameToReturnPlace { tcx, to_rename: returned_local }.visit_body(body);

compiler/rustc_mir/src/transform/remove_unneeded_drops.rs

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
2121
opt_finder.visit_body(body);
2222
let should_simplify = !opt_finder.optimizations.is_empty();
2323
for (loc, target) in opt_finder.optimizations {
24+
if !tcx
25+
.consider_optimizing(|| format!("RemoveUnneededDrops {:?} ", body.source.def_id()))
26+
{
27+
break;
28+
}
29+
2430
let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
2531
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
2632
terminator.kind = TerminatorKind::Goto { target };

compiler/rustc_mir/src/transform/unreachable_prop.rs

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ impl MirPass<'_> for UnreachablePropagation {
5050

5151
let replaced = !replacements.is_empty();
5252
for (bb, terminator_kind) in replacements {
53+
if !tcx.consider_optimizing(|| {
54+
format!("UnreachablePropagation {:?} ", body.source.def_id())
55+
}) {
56+
break;
57+
}
58+
5359
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
5460
}
5561

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

+79-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
160160
expr_span,
161161
source_info,
162162
),
163-
ExprKind::SelfRef => block.and(PlaceBuilder::from(Local::new(1))),
163+
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
164+
let capture = this
165+
.hir
166+
.typeck_results
167+
.closure_captures
168+
.get(&closure_def_id)
169+
.and_then(|captures| captures.get_full(&var_hir_id));
170+
171+
if capture.is_none() {
172+
if !this.hir.tcx().features().capture_disjoint_fields {
173+
bug!(
174+
"No associated capture found for {:?} even though \
175+
capture_disjoint_fields isn't enabled",
176+
expr.kind
177+
)
178+
}
179+
// FIXME(project-rfc-2229#24): Handle this case properly
180+
}
181+
182+
// Unwrap until the FIXME has been resolved
183+
let (capture_index, _, upvar_id) = capture.unwrap();
184+
this.lower_closure_capture(block, capture_index, *upvar_id)
185+
}
186+
164187
ExprKind::VarRef { id } => {
165188
let place_builder = if this.is_bound_var_in_guard(id) {
166189
let index = this.var_local_id(id, RefWithinGuard);
@@ -270,6 +293,61 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
270293
}
271294
}
272295

296+
/// Lower a closure/generator capture by representing it as a field
297+
/// access within the desugared closure/generator.
298+
///
299+
/// `capture_index` is the index of the capture within the desugared
300+
/// closure/generator.
301+
fn lower_closure_capture(
302+
&mut self,
303+
block: BasicBlock,
304+
capture_index: usize,
305+
upvar_id: ty::UpvarId,
306+
) -> BlockAnd<PlaceBuilder<'tcx>> {
307+
let closure_ty = self
308+
.hir
309+
.typeck_results()
310+
.node_type(self.hir.tcx().hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
311+
312+
// Captures are represented using fields inside a structure.
313+
// This represents accessing self in the closure structure
314+
let mut place_builder = PlaceBuilder::from(Local::new(1));
315+
316+
// In case of Fn/FnMut closures we must deref to access the fields
317+
// Generators are considered FnOnce, so we ignore this step for them.
318+
if let ty::Closure(_, closure_substs) = closure_ty.kind() {
319+
match self.hir.infcx().closure_kind(closure_substs).unwrap() {
320+
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => {
321+
place_builder = place_builder.deref();
322+
}
323+
ty::ClosureKind::FnOnce => {}
324+
}
325+
}
326+
327+
let substs = match closure_ty.kind() {
328+
ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
329+
ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
330+
_ => bug!("Lowering capture for non-closure type {:?}", closure_ty)
331+
};
332+
333+
// Access the capture by accessing the field within the Closure struct.
334+
//
335+
// We must have inferred the capture types since we are building MIR, therefore
336+
// it's safe to call `upvar_tys` and we can unwrap here because
337+
// we know that the capture exists and is the `capture_index`-th capture.
338+
let var_ty = substs.upvar_tys().nth(capture_index).unwrap();
339+
place_builder = place_builder.field(Field::new(capture_index), var_ty);
340+
341+
// If the variable is captured via ByRef(Immutable/Mutable) Borrow,
342+
// we need to deref it
343+
match self.hir.typeck_results.upvar_capture(upvar_id) {
344+
ty::UpvarCapture::ByRef(_) => {
345+
block.and(place_builder.deref())
346+
}
347+
ty::UpvarCapture::ByValue(_) => block.and(place_builder),
348+
}
349+
}
350+
273351
/// Lower an index expression
274352
///
275353
/// This has two complications;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
250250
| ExprKind::Deref { .. }
251251
| ExprKind::Index { .. }
252252
| ExprKind::VarRef { .. }
253-
| ExprKind::SelfRef
253+
| ExprKind::UpvarRef { .. }
254254
| ExprKind::Break { .. }
255255
| ExprKind::Continue { .. }
256256
| ExprKind::Return { .. }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Category {
3838
ExprKind::Field { .. }
3939
| ExprKind::Deref { .. }
4040
| ExprKind::Index { .. }
41-
| ExprKind::SelfRef
41+
| ExprKind::UpvarRef { .. }
4242
| ExprKind::VarRef { .. }
4343
| ExprKind::PlaceTypeAscription { .. }
4444
| ExprKind::ValueTypeAscription { .. } => Some(Category::Place),

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

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

401401
// Avoid creating a temporary
402402
ExprKind::VarRef { .. }
403-
| ExprKind::SelfRef
403+
| ExprKind::UpvarRef { .. }
404404
| ExprKind::PlaceTypeAscription { .. }
405405
| ExprKind::ValueTypeAscription { .. } => {
406406
debug_assert!(Category::of(&expr.kind) == Some(Category::Place));

0 commit comments

Comments
 (0)