Skip to content

rustc_resolve: Further reduce mutability in resolver - #160090

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
petrochenkov:cmonst
Jul 30, 2026
Merged

rustc_resolve: Further reduce mutability in resolver#160090
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
petrochenkov:cmonst

Conversation

@petrochenkov

@petrochenkov petrochenkov commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

resolver.cm() now returns a conditionally mutable reference (RefOrMut) that cannot be mutated, and resolver.cm_mut() returns a conditionally mutable reference that can be mutated.

The "can be mutated" property is checked dynamically, so this can potentially panic in some obscure error reporting cases not covered by the test suite, but they should be easily fixable and will be caught sooner or later, but it's more useful to see which code path actually use resolver in mutable way rather than just take RefOrMut.

The second commit converts RefOrMut to an enum instead of using raw pointers.

r? @mu001999
cc @LorrensP-2158466

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 28, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor Author

I'll try changing RefOrMut to an enum to avoid unsafe code.
(I remember why it wasn't made an enum originally, but it should no longer be relevant.)
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 29, 2026
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mu001999

Copy link
Copy Markdown
Member

I'm not sure how difficult this would be or whether it's feasible, but could we use different types for Ref and Mut instead of combining them in RefOrMut?

@petrochenkov

petrochenkov commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

I'm not sure how difficult this would be or whether it's feasible, but could we use different types for Ref and Mut instead of combining them in RefOrMut?

Not feasible, functions taking RefOrMut do that because they must work with both mutable and immutable versions, dynamically.

Or rather infeasible without duplicating a large number of resolver functions.
Perhaps the "duplication" could also be done by turning Finalize into a const generic argument, although I'm not sure and it would also be a large change.

@rustbot

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
rustc_resolve: Further reduce mutability in resolver
@rust-bors

rust-bors Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: df44b1a (df44b1aa081b3f74f4d68fe6d1de048820cbb3ca)
Base parent: ce69831 (ce6983167791bf9418726264f8e5cc7abf73d69b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (df44b1a): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 3.7%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.7% [1.4%, 5.2%] 3
Regressions ❌
(secondary)
5.3% [5.3%, 5.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.6% [-4.6%, -4.6%] 1
All ❌✅ (primary) 3.7% [1.4%, 5.2%] 3

Cycles

Results (secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.3%, 2.5%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 490.002s -> 491.705s (0.35%)
Artifact size: 390.14 MiB -> 390.21 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 29, 2026
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@petrochenkov

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 29, 2026

@mu001999 mu001999 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread compiler/rustc_resolve/src/lib.rs
Comment thread compiler/rustc_resolve/src/lib.rs
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 29, 2026
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 29, 2026
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 29, 2026
@mu001999

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c9f7b96 has been approved by mu001999

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 29, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 29, 2026
rustc_resolve: Further reduce mutability in resolver

`resolver.cm()` now returns a conditionally mutable reference (`RefOrMut`) that cannot be mutated, and `resolver.cm_mut()` returns a conditionally mutable reference that can be mutated.

The "can be mutated" property is checked dynamically, so this can potentially panic in some obscure error reporting cases not covered by the test suite, but they should be easily fixable and will be caught sooner or later, but it's more useful to see which code path *actually* use resolver in mutable way rather than just take `RefOrMut`.

The second commit uses standard `NonNull` in the implementation of `RefOrMut`.

r? @mu001999
cc @LorrensP-2158466
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 17 pull requests

Successful merges:

 - #159014 ([rustdoc] Do not take `doc(cfg())` into account when filtering doctests)
 - #159130 (a bit optimize four-digit chunks in integer formatting)
 - #159592 (core: implement bounded random sampling)
 - #159898 (Add intrinsic-test alias and set  sample rate)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 29, 2026
rustc_resolve: Further reduce mutability in resolver

`resolver.cm()` now returns a conditionally mutable reference (`RefOrMut`) that cannot be mutated, and `resolver.cm_mut()` returns a conditionally mutable reference that can be mutated.

The "can be mutated" property is checked dynamically, so this can potentially panic in some obscure error reporting cases not covered by the test suite, but they should be easily fixable and will be caught sooner or later, but it's more useful to see which code path *actually* use resolver in mutable way rather than just take `RefOrMut`.

The second commit uses standard `NonNull` in the implementation of `RefOrMut`.

r? @mu001999
cc @LorrensP-2158466
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
Rollup of 18 pull requests

Successful merges:

 - #159130 (a bit optimize four-digit chunks in integer formatting)
 - #159592 (core: implement bounded random sampling)
 - #159898 (Add intrinsic-test alias and set  sample rate)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160139 (iter: specialize Take::count using advance_by)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
 - #160166 (Use correct feature gates for `f16`/`f128` `From` impls)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 29, 2026
rustc_resolve: Further reduce mutability in resolver

`resolver.cm()` now returns a conditionally mutable reference (`RefOrMut`) that cannot be mutated, and `resolver.cm_mut()` returns a conditionally mutable reference that can be mutated.

The "can be mutated" property is checked dynamically, so this can potentially panic in some obscure error reporting cases not covered by the test suite, but they should be easily fixable and will be caught sooner or later, but it's more useful to see which code path *actually* use resolver in mutable way rather than just take `RefOrMut`.

The second commit uses standard `NonNull` in the implementation of `RefOrMut`.

r? @mu001999
cc @LorrensP-2158466
rust-bors Bot pushed a commit that referenced this pull request Jul 29, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #159898 (Add intrinsic-test alias and set  sample rate)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #158693 (Add type-check to offload intrinisc calls)
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160139 (iter: specialize Take::count using advance_by)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
 - #160166 (Use correct feature gates for `f16`/`f128` `From` impls)
rust-bors Bot pushed a commit that referenced this pull request Jul 30, 2026
Rollup of 20 pull requests

Successful merges:

 - #157669 (cfi: add diag mode support)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #158693 (Add type-check to offload intrinisc calls)
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160101 (Add missing `needs-unwind` annotation to `add-spawn-hook-reentrancy-159923` test)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160139 (iter: specialize Take::count using advance_by)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
 - #160166 (Use correct feature gates for `f16`/`f128` `From` impls)
 - #160178 (Remove unused `va_start` intrinsic)
rust-bors Bot pushed a commit that referenced this pull request Jul 30, 2026
Rollup of 20 pull requests

Successful merges:

 - #157669 (cfi: add diag mode support)
 - #158247 (hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`)
 - #158649 (Hermit: fix `readdir()` )
 - #158693 (Add type-check to offload intrinisc calls)
 - #159049 (Avoid ICE in From/TryFrom cast suggestion when encountering HRTBs)
 - #159411 ([rustdoc] Correctly handle output options with --show-coverage)
 - #160053 (test: add test suite for the 85681 issue)
 - #160087 (Add regression test for nested associated-type projection ICE)
 - #160090 (rustc_resolve: Further reduce mutability in resolver)
 - #160099 (Resolver: split module resolutions into local and external resolutions)
 - #160101 (Add missing `needs-unwind` annotation to `add-spawn-hook-reentrancy-159923` test)
 - #160106 (Add suggestions for `must_implement_one_of`)
 - #160117 (Remove unnecessary format usage)
 - #160134 (Work around Wine bug 60084 by calling WSAStartup at most once)
 - #160139 (iter: specialize Take::count using advance_by)
 - #160142 (bootstrap: remove use-lld config alias)
 - #160148 (Rename `errors.rs` file to `diagnostics.rs` (15/N))
 - #160151 (Mark a doctest as requiring unwinding)
 - #160166 (Use correct feature gates for `f16`/`f128` `From` impls)
 - #160178 (Remove unused `va_start` intrinsic)
@rust-bors
rust-bors Bot merged commit f0fea75 into rust-lang:main Jul 30, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 30, 2026
rust-timer added a commit that referenced this pull request Jul 30, 2026
Rollup merge of #160090 - petrochenkov:cmonst, r=mu001999

rustc_resolve: Further reduce mutability in resolver

`resolver.cm()` now returns a conditionally mutable reference (`RefOrMut`) that cannot be mutated, and `resolver.cm_mut()` returns a conditionally mutable reference that can be mutated.

The "can be mutated" property is checked dynamically, so this can potentially panic in some obscure error reporting cases not covered by the test suite, but they should be easily fixable and will be caught sooner or later, but it's more useful to see which code path *actually* use resolver in mutable way rather than just take `RefOrMut`.

The second commit converts `RefOrMut` to an enum instead of using raw pointers.

r? @mu001999
cc @LorrensP-2158466
@jhpratt

jhpratt commented Jul 30, 2026

Copy link
Copy Markdown
Member

@rust-timer build 3bb5bfc

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (3bb5bfc): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.2%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -1.2%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.3% [3.3%, 3.3%] 1
Improvements ✅
(primary)
-1.2% [-1.2%, -1.2%] 1
Improvements ✅
(secondary)
-0.5% [-0.6%, -0.4%] 6
All ❌✅ (primary) -1.2% [-1.2%, -1.2%] 1

Cycles

Results (primary 0.2%, secondary -3.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.7% [0.6%, 1.1%] 6
Regressions ❌
(secondary)
1.4% [0.6%, 2.9%] 3
Improvements ✅
(primary)
-1.5% [-2.1%, -1.0%] 2
Improvements ✅
(secondary)
-4.4% [-16.8%, -0.4%] 13
All ❌✅ (primary) 0.2% [-2.1%, 1.1%] 8

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 491.471s -> 489.765s (-0.35%)
Artifact size: 390.58 MiB -> 390.47 MiB (-0.03%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants