Skip to content

Simplify TypeFreshener methods. - #150239

Merged
bors merged 6 commits into
rust-lang:mainfrom
nnethercote:simplify-TypeFreshener-methods
Dec 30, 2025
Merged

Simplify TypeFreshener methods.#150239
bors merged 6 commits into
rust-lang:mainfrom
nnethercote:simplify-TypeFreshener-methods

Conversation

@nnethercote

Copy link
Copy Markdown
Contributor

freshen_{ty,const} take a Result and just do a fold if the input is Ok. It's simpler to do those folds at the call site, and only call freshen_{ty,const} in the Err case. That way we can also avoid useless fold operations on the results of new_{int,uint,float}.

Also, make some bug! calls more concise.

r? @lcnr

`freshen_{ty,const}` take a `Result` and just do a fold if the input is
`Ok`. It's simpler to do those folds at the call site, and only call
`freshen_{ty,const}` in the `Err` case. That way we can also avoid
useless fold operations on the results of `new_{int,uint,float}`.

Also, make some `bug!` calls more concise.
@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 Dec 22, 2025
I have always found this confusingly named, because it creates a new
freshener rather than returning an existing one. We can remove it and
just use `TypeFreshener::new()` at the two call sites, avoiding this
confusion.
Comment thread compiler/rustc_infer/src/infer/freshen.rs Outdated

@lcnr lcnr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me, but would love for u to look into a related refactor

View changes since this review

Sometimes we freshen using a new `TypeFreshener`, and sometimes we
freshen with an existing `TypeFreshener`. For the former we have the
method `InferCtxt::freshen`. For the latter we just call `fold_with`.
This asymmetry has been confusing to me.

This commit removes `InferCtxt::freshen` so that all the freshening
sites consistently use `fold_with` and it's obvious if each one is using
a new or existing `TypeFreshener`.
Because `fresh_trait_pred` is the name of the field/argument. The `_ref`
suffix appears to be a typo, or left over from earlier versions of the
code.
@nnethercote
nnethercote force-pushed the simplify-TypeFreshener-methods branch from 0d4649d to 2d10f47 Compare December 22, 2025 12:47
#[inline(never)]
fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
match v {
fn fold_infer_ty(&mut self, ty: ty::InferTy) -> Option<Ty<'tcx>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

now always returns Some?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes! I have added a commit to remove the Option.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions, this has ended up much nicer than the first version.

@lcnr

lcnr commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

@bors try @rust-timer queue

freshening should be at least somewhat hot?

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Dec 23, 2025
…=<try>

Simplify `TypeFreshener` methods.
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2025
@nnethercote

Copy link
Copy Markdown
Contributor Author

Freshening is definitely hot, but freshening of inference variables is much less so. Local measurements indicated that the perf effects were negligible, but it doesn't hurt to double check.

@rust-bors

rust-bors Bot commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

💥 Test timed out after 21600s

@lcnr

lcnr commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Dec 23, 2025
…=<try>

Simplify `TypeFreshener` methods.
@rust-bors

rust-bors Bot commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: d1d7c44 (d1d7c443a2cb57b25b43cf0031489c133be86cff, parent: 99ff3fbb86658b427f5dd7daaae8db5626a63c26)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d1d7c44): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (primary 2.1%, secondary 2.1%)

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

mean range count
Regressions ❌
(primary)
2.1% [1.7%, 2.6%] 4
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.1% [1.7%, 2.6%] 4

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 482.582s -> 480.862s (-0.36%)
Artifact size: 390.35 MiB -> 390.37 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 23, 2025
@lcnr

lcnr commented Dec 30, 2025

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@bors

bors commented Dec 30, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit ec5e96e has been approved by lcnr

It is now in the queue for this repository.

@bors bors 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 Dec 30, 2025
bors added a commit that referenced this pull request Dec 30, 2025
…uwer

Rollup of 2 pull requests

Successful merges:

 - #150239 (Simplify `TypeFreshener` methods.)
 - #150344 (Cleanup linked list)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit be715d4 into rust-lang:main Dec 30, 2025
12 checks passed
@rustbot rustbot added this to the 1.94.0 milestone Dec 30, 2025
rust-timer added a commit that referenced this pull request Dec 30, 2025
Rollup merge of #150239 - nnethercote:simplify-TypeFreshener-methods, r=lcnr

Simplify `TypeFreshener` methods.

`freshen_{ty,const}` take a `Result` and just do a fold if the input is `Ok`. It's simpler to do those folds at the call site, and only call `freshen_{ty,const}` in the `Err` case. That way we can also avoid useless fold operations on the results of `new_{int,uint,float}`.

Also, make some `bug!` calls more concise.

r? `@lcnr`
@nnethercote
nnethercote deleted the simplify-TypeFreshener-methods branch January 1, 2026 21:41
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