Skip to content

Commit 90fd16e

Browse files
committed
Auto merge of rust-lang#140053 - ChrisDenton:rollup-tt00skl, r=ChrisDenton
Rollup of 7 pull requests Successful merges: - rust-lang#139042 (Do not remove trivial `SwitchInt` in analysis MIR) - rust-lang#139533 (add next_index to Enumerate) - rust-lang#139843 (Setup editor file associations for non-rs extensions) - rust-lang#140000 (skip llvm-config in autodiff check builds, when its unavailable) - rust-lang#140008 (Improve `clean_maybe_renamed_item` function code a bit) - rust-lang#140024 (Remove early exits from JumpThreading.) - rust-lang#140039 (Add option for stable backport poll) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 077cedc + 4c36aed commit 90fd16e

29 files changed

+363
-149
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,8 @@ fn test_unstable_options_tracking_hash() {
818818
tracked!(min_function_alignment, Some(Align::EIGHT));
819819
tracked!(mir_emit_retag, true);
820820
tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]);
821-
tracked!(mir_keep_place_mention, true);
822821
tracked!(mir_opt_level, Some(4));
822+
tracked!(mir_preserve_ub, true);
823823
tracked!(move_size_limit, Some(4096));
824824
tracked!(mutable_noalias, false);
825825
tracked!(next_solver, NextSolverConfig { coherence: true, globally: true });

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
223223
// Since this optimization adds new basic blocks and invalidates others,
224224
// clean up the cfg to make it nicer for other passes
225225
if should_cleanup {
226-
simplify_cfg(body);
226+
simplify_cfg(tcx, body);
227227
}
228228
}
229229

compiler/rustc_mir_transform/src/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> crate::MirPass<'tcx> for Inline {
6363
let _guard = span.enter();
6464
if inline::<NormalInliner<'tcx>>(tcx, body) {
6565
debug!("running simplify cfg on {:?}", body.source);
66-
simplify_cfg(body);
66+
simplify_cfg(tcx, body);
6767
deref_finder(tcx, body);
6868
}
6969
}
@@ -99,7 +99,7 @@ impl<'tcx> crate::MirPass<'tcx> for ForceInline {
9999
let _guard = span.enter();
100100
if inline::<ForceInliner<'tcx>>(tcx, body) {
101101
debug!("running simplify cfg on {:?}", body.source);
102-
simplify_cfg(body);
102+
simplify_cfg(tcx, body);
103103
deref_finder(tcx, body);
104104
}
105105
}

compiler/rustc_mir_transform/src/jump_threading.rs

+64-71
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/match_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
4343
}
4444

4545
if should_cleanup {
46-
simplify_cfg(body);
46+
simplify_cfg(tcx, body);
4747
}
4848
}
4949

compiler/rustc_mir_transform/src/remove_place_mention.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(super) struct RemovePlaceMention;
88

99
impl<'tcx> crate::MirPass<'tcx> for RemovePlaceMention {
1010
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
11-
!sess.opts.unstable_opts.mir_keep_place_mention
11+
!sess.opts.unstable_opts.mir_preserve_ub
1212
}
1313

1414
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/remove_unneeded_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
3535
// if we applied optimizations, we potentially have some cfg to cleanup to
3636
// make it easier for further passes
3737
if should_simplify {
38-
simplify_cfg(body);
38+
simplify_cfg(tcx, body);
3939
}
4040
}
4141

compiler/rustc_mir_transform/src/simplify.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
//! Here the block (`{ return; }`) has the return type `char`, rather than `()`, but the MIR we
2727
//! naively generate still contains the `_a = ()` write in the unreachable block "after" the
2828
//! return.
29+
//!
30+
//! **WARNING**: This is one of the few optimizations that runs on built and analysis MIR, and
31+
//! so its effects may affect the type-checking, borrow-checking, and other analysis of MIR.
32+
//! We must be extremely careful to only apply optimizations that preserve UB and all
33+
//! non-determinism, since changes here can affect which programs compile in an insta-stable way.
34+
//! The normal logic that a program with UB can be changed to do anything does not apply to
35+
//! pre-"runtime" MIR!
2936
3037
use rustc_index::{Idx, IndexSlice, IndexVec};
3138
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
@@ -66,8 +73,8 @@ impl SimplifyCfg {
6673
}
6774
}
6875

69-
pub(super) fn simplify_cfg(body: &mut Body<'_>) {
70-
CfgSimplifier::new(body).simplify();
76+
pub(super) fn simplify_cfg<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
77+
CfgSimplifier::new(tcx, body).simplify();
7178
remove_dead_blocks(body);
7279

7380
// FIXME: Should probably be moved into some kind of pass manager
@@ -79,9 +86,9 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyCfg {
7986
self.name()
8087
}
8188

82-
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
89+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8390
debug!("SimplifyCfg({:?}) - simplifying {:?}", self.name(), body.source);
84-
simplify_cfg(body);
91+
simplify_cfg(tcx, body);
8592
}
8693

8794
fn is_required(&self) -> bool {
@@ -90,12 +97,13 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyCfg {
9097
}
9198

9299
struct CfgSimplifier<'a, 'tcx> {
100+
preserve_switch_reads: bool,
93101
basic_blocks: &'a mut IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
94102
pred_count: IndexVec<BasicBlock, u32>,
95103
}
96104

97105
impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
98-
fn new(body: &'a mut Body<'tcx>) -> Self {
106+
fn new(tcx: TyCtxt<'tcx>, body: &'a mut Body<'tcx>) -> Self {
99107
let mut pred_count = IndexVec::from_elem(0u32, &body.basic_blocks);
100108

101109
// we can't use mir.predecessors() here because that counts
@@ -110,9 +118,12 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
110118
}
111119
}
112120

121+
// Preserve `SwitchInt` reads on built and analysis MIR, or if `-Zmir-preserve-ub`.
122+
let preserve_switch_reads = matches!(body.phase, MirPhase::Built | MirPhase::Analysis(_))
123+
|| tcx.sess.opts.unstable_opts.mir_preserve_ub;
113124
let basic_blocks = body.basic_blocks_mut();
114125

115-
CfgSimplifier { basic_blocks, pred_count }
126+
CfgSimplifier { preserve_switch_reads, basic_blocks, pred_count }
116127
}
117128

118129
fn simplify(mut self) {
@@ -253,9 +264,15 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
253264

254265
// turn a branch with all successors identical to a goto
255266
fn simplify_branch(&mut self, terminator: &mut Terminator<'tcx>) -> bool {
256-
match terminator.kind {
257-
TerminatorKind::SwitchInt { .. } => {}
258-
_ => return false,
267+
// Removing a `SwitchInt` terminator may remove reads that result in UB,
268+
// so we must not apply this optimization before borrowck or when
269+
// `-Zmir-preserve-ub` is set.
270+
if self.preserve_switch_reads {
271+
return false;
272+
}
273+
274+
let TerminatorKind::SwitchInt { .. } = terminator.kind else {
275+
return false;
259276
};
260277

261278
let first_succ = {

compiler/rustc_session/src/options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2322,12 +2322,12 @@ options! {
23222322
mir_include_spans: MirIncludeSpans = (MirIncludeSpans::default(), parse_mir_include_spans, [UNTRACKED],
23232323
"include extra comments in mir pretty printing, like line numbers and statement indices, \
23242324
details about types, etc. (boolean for all passes, 'nll' to enable in NLL MIR only, default: 'nll')"),
2325-
mir_keep_place_mention: bool = (false, parse_bool, [TRACKED],
2326-
"keep place mention MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
2327-
(default: no)"),
23282325
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
23292326
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
23302327
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
2328+
mir_preserve_ub: bool = (false, parse_bool, [TRACKED],
2329+
"keep place mention statements and reads in trivial SwitchInt terminators, which are interpreted \
2330+
e.g., by miri; implies -Zmir-opt-level=0 (default: no)"),
23312331
mir_strip_debuginfo: MirStripDebugInfo = (MirStripDebugInfo::None, parse_mir_strip_debuginfo, [TRACKED],
23322332
"Whether to remove some of the MIR debug info from methods. Default: None"),
23332333
move_size_limit: Option<usize> = (None, parse_opt_number, [TRACKED],

library/core/src/iter/adapters/enumerate.rs

+33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ impl<I> Enumerate<I> {
2323
pub(in crate::iter) fn new(iter: I) -> Enumerate<I> {
2424
Enumerate { iter, count: 0 }
2525
}
26+
27+
/// Retrieve the current position of the iterator.
28+
///
29+
/// If the iterator has not advanced, the position returned will be 0.
30+
///
31+
/// The position may also exceed the bounds of the iterator to allow for calculating
32+
/// the displacement of the iterator from following calls to [`Iterator::next`].
33+
///
34+
/// # Examples
35+
///
36+
/// ```
37+
/// #![feature(next_index)]
38+
///
39+
/// let arr = ['a', 'b'];
40+
///
41+
/// let mut iter = arr.iter().enumerate();
42+
///
43+
/// assert_eq!(iter.next_index(), 0);
44+
/// assert_eq!(iter.next(), Some((0, &'a')));
45+
///
46+
/// assert_eq!(iter.next_index(), 1);
47+
/// assert_eq!(iter.next_index(), 1);
48+
/// assert_eq!(iter.next(), Some((1, &'b')));
49+
///
50+
/// assert_eq!(iter.next_index(), 2);
51+
/// assert_eq!(iter.next(), None);
52+
/// assert_eq!(iter.next_index(), 2);
53+
/// ```
54+
#[inline]
55+
#[unstable(feature = "next_index", issue = "130711")]
56+
pub fn next_index(&self) -> usize {
57+
self.count
58+
}
2659
}
2760

2861
#[stable(feature = "rust1", since = "1.0.0")]

library/coretests/tests/iter/adapters/enumerate.rs

+10
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,13 @@ fn test_double_ended_enumerate() {
120120
assert_eq!(it.next_back(), Some((2, 3)));
121121
assert_eq!(it.next(), None);
122122
}
123+
124+
#[test]
125+
fn test_empty_iterator_enumerate_next_index() {
126+
let mut it = empty::<i32>().enumerate();
127+
assert_eq!(it.next_index(), 0);
128+
assert_eq!(it.next_index(), 0);
129+
assert_eq!(it.next(), None);
130+
assert_eq!(it.next_index(), 0);
131+
assert_eq!(it.next_index(), 0);
132+
}

library/coretests/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#![feature(maybe_uninit_write_slice)]
6464
#![feature(min_specialization)]
6565
#![feature(never_type)]
66+
#![feature(next_index)]
6667
#![feature(numfmt)]
6768
#![feature(pattern)]
6869
#![feature(pointer_is_aligned_to)]

src/bootstrap/src/core/build_steps/compile.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,7 @@ pub fn rustc_cargo(
11941194
let enzyme_dir = builder.build.out.join(arch).join("enzyme").join("lib");
11951195
cargo.rustflag("-L").rustflag(enzyme_dir.to_str().expect("Invalid path"));
11961196

1197-
if !builder.config.dry_run() {
1198-
let llvm_config = builder.llvm_config(builder.config.build).unwrap();
1197+
if let Some(llvm_config) = builder.llvm_config(builder.config.build) {
11991198
let llvm_version_major = llvm::get_llvm_version_major(builder, &llvm_config);
12001199
cargo.rustflag("-l").rustflag(&format!("Enzyme-{llvm_version_major}"));
12011200
}

src/bootstrap/src/core/build_steps/setup.rs

+3
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ Select which editor you would like to set up [default: None]: ";
584584
"51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
585585
"d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
586586
"b5dd299b93dca3ceeb9b335f929293cb3d4bf4977866fbe7ceeac2a8a9f99088",
587+
"631c837b0e98ae35fd48b0e5f743b1ca60adadf2d0a2b23566ba25df372cf1a9",
587588
],
588589
EditorKind::Helix => &[
589590
"2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
@@ -602,10 +603,12 @@ Select which editor you would like to set up [default: None]: ";
602603
"4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
603604
"c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
604605
"e53e9129ca5ee5dcbd6ec8b68c2d87376474eb154992deba3c6d9ab1703e0717",
606+
"f954316090936c7e590c253ca9d524008375882fa13c5b41d7e2547a896ff893",
605607
],
606608
EditorKind::Zed => &[
607609
"bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c",
608610
"a5380cf5dd9328731aecc5dfb240d16dac46ed272126b9728006151ef42f5909",
611+
"2e96bf0d443852b12f016c8fc9840ab3d0a2b4fe0b0fb3a157e8d74d5e7e0e26",
609612
],
610613
}
611614
}

src/etc/rust_analyzer_eglot.el

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
"check"
99
"--json-output"])
1010
:linkedProjects ["Cargo.toml"
11-
"src/bootstrap/Cargo.toml"
12-
"src/tools/rust-analyzer/Cargo.toml"
1311
"compiler/rustc_codegen_cranelift/Cargo.toml"
14-
"compiler/rustc_codegen_gcc/Cargo.toml"]
12+
"compiler/rustc_codegen_gcc/Cargo.toml"
13+
"library/Cargo.toml"
14+
"src/bootstrap/Cargo.toml"
15+
"src/tools/rust-analyzer/Cargo.toml"]
1516
:rustfmt ( :overrideCommand ["build/host/rustfmt/bin/rustfmt"
1617
"--edition=2021"])
1718
:procMacro ( :server "build/host/stage0/libexec/rust-analyzer-proc-macro-srv"

src/etc/rust_analyzer_settings.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
],
1010
"rust-analyzer.linkedProjects": [
1111
"Cargo.toml",
12+
"compiler/rustc_codegen_cranelift/Cargo.toml",
13+
"compiler/rustc_codegen_gcc/Cargo.toml",
1214
"library/Cargo.toml",
1315
"src/bootstrap/Cargo.toml",
14-
"src/tools/rust-analyzer/Cargo.toml",
15-
"compiler/rustc_codegen_cranelift/Cargo.toml",
16-
"compiler/rustc_codegen_gcc/Cargo.toml"
16+
"src/tools/rust-analyzer/Cargo.toml"
1717
],
1818
"rust-analyzer.rustfmt.overrideCommand": [
1919
"${workspaceFolder}/build/host/rustfmt/bin/rustfmt",
@@ -36,5 +36,10 @@
3636
},
3737
"rust-analyzer.server.extraEnv": {
3838
"RUSTUP_TOOLCHAIN": "nightly"
39+
},
40+
"files.associations": {
41+
"*.fixed": "rust",
42+
"*.pp": "rust",
43+
"*.mir": "rust"
3944
}
4045
}

src/etc/rust_analyzer_zed.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
},
2222
"linkedProjects": [
2323
"Cargo.toml",
24+
"compiler/rustc_codegen_cranelift/Cargo.toml",
25+
"compiler/rustc_codegen_gcc/Cargo.toml",
2426
"library/Cargo.toml",
2527
"src/bootstrap/Cargo.toml",
26-
"src/tools/rust-analyzer/Cargo.toml",
27-
"compiler/rustc_codegen_cranelift/Cargo.toml",
28-
"compiler/rustc_codegen_gcc/Cargo.toml"
28+
"src/tools/rust-analyzer/Cargo.toml"
2929
],
3030
"procMacro": {
31-
"enable": true,
32-
"server": "${workspaceFolder}/build/host/stage0/libexec/rust-analyzer-proc-macro-srv"
31+
"enable": true,
32+
"server": "${workspaceFolder}/build/host/stage0/libexec/rust-analyzer-proc-macro-srv"
3333
},
3434
"rustc": {
3535
"source": "./Cargo.toml"
@@ -47,5 +47,8 @@
4747
}
4848
}
4949
}
50+
},
51+
"file_types": {
52+
"Rust": ["fixed", "pp", "mir"]
5053
}
5154
}

0 commit comments

Comments
 (0)