Skip to content

fix: Do not infer signatures, instead infer anon consts in them#22198

Merged
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
ChayimFriedman2:anon-consts
May 7, 2026
Merged

fix: Do not infer signatures, instead infer anon consts in them#22198
ChayimFriedman2 merged 2 commits into
rust-lang:masterfrom
ChayimFriedman2:anon-consts

Conversation

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 commented Apr 27, 2026

See the Zulip discussion for details, but in short:

  • This allows us to easily set the expected type.
  • They can have proper MIR an eval, preventing the need for hacks like fixme_resolve_all_clone().

This also fixes a bunch of old inference failures caused by us unable to evaluate constants.

In order to make them visible to the IDE layer, each query that can create anon consts now keeps a list of the anon consts it created. The expression store also gains an ability to find the root expression of something. Then, in Semantics, when wanting to find inference info for something, we find its root expr, then search it in the list of anon consts defined by the body.

This should reduce our diagnostics on self by at least two (they're caused by failure to evaluate consts on to_le_bytes(), we even have a test for that).

Blocked on #22194.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 27, 2026
@ChayimFriedman2 ChayimFriedman2 marked this pull request as draft April 27, 2026 23:32
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 27, 2026
223..227 'iter': IntoIter<u8>
230..231 'a': Vec<u8>
230..243 'a.into_iter()': IntoIter<u8>
322..323 '1': usize
Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 ChayimFriedman2 Apr 27, 2026

Choose a reason for hiding this comment

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

Previously we inferred all expressions in the signature. Now we skip literals and paths (we don't create an anon const for them) to save time and space. The net effect is that they aren't shown in tests and to IDE, although it's possibly to support them for IDE in little effort.

View changes since the review

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.

How big are the savings here? Imo we should definitely make these work for IDE analysis

);
// note: this may break later if we add more consteval. it just needs to be something that our
// consteval engine doesn't understand
check_assist_not_applicable(
Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 ChayimFriedman2 Apr 27, 2026

Choose a reason for hiding this comment

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

It's not easy to support this now, since the type does not contain an error type anymore: it just has an anon const that fails to evaluate, which causes it to fail to normalize. But I've realized this test is no longer needed, since _ in arrays is supported by now.

View changes since the review

@ChayimFriedman2 ChayimFriedman2 force-pushed the anon-consts branch 2 times, most recently from 047f10d to 372b99f Compare April 28, 2026 17:41
@ChayimFriedman2 ChayimFriedman2 marked this pull request as ready for review April 28, 2026 17:41
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 28, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@Veykril Veykril self-requested a review May 3, 2026 06:17
@Veykril Veykril self-assigned this May 3, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 5, 2026

This PR was rebased onto a different master 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.

Comment on lines +302 to +312
let konst = create_anon_const(
self.interner,
self.def,
self.store,
const_ref.expr,
self.resolver,
const_type,
&|| self.generics(),
None,
self.forbid_params_after,
);
Copy link
Copy Markdown
Contributor

@nicolas-guichard nicolas-guichard May 6, 2026

Choose a reason for hiding this comment

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

In the Expr::Underscore case, because this doesn't provide infcx here, create_anon_const returns an Err which will reach the unwrap_or case below. I believe we want to create a new const var instead?
(I tripped on this when rebasing my related PR on top of this.)

View changes since the review

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.

We don't have access to the InferCtxt here, only after #22237 we will.

Comment thread crates/hir-def/src/expr_store/lower.rs Outdated
Comment on lines +2005 to +2013
// The repeat is an anon const.
&hir_def::hir::Expr::Array(hir_def::hir::Array::Repeat {
initializer,
repeat: _,
}) => append_coroutines_in_expr(db, owner, store, initializer, result),
_ => store.walk_child_exprs(expr_id, |expr_id| {
append_coroutines_in_expr(db, owner, store, expr_id, result)
}),
}
Copy link
Copy Markdown
Member

@Veykril Veykril May 7, 2026

Choose a reason for hiding this comment

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

This sounds like we should have a walk_child_exprs version that exhaustively matches and skips anon const places. Otherwise it is easy for us to miss this if a new case arises

View changes since the review

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.

I want to change the visitors to be based on traits anyway (have a branch for that), that'll be a good place to do this.

223..227 'iter': IntoIter<u8>
230..231 'a': Vec<u8>
230..243 'a.into_iter()': IntoIter<u8>
322..323 '1': usize
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.

How big are the savings here? Imo we should definitely make these work for IDE analysis

Comment thread crates/hir-def/src/expr_store.rs
Comment thread crates/hir-ty/src/db.rs Outdated
Comment on lines +413 to +414
/// An anonymous const expression that appears in a type position (e.g., array lengths,
/// const generic arguments like `{ N + 1 }`). Unlike named constants, these don't have
Copy link
Copy Markdown
Member

@Veykril Veykril May 7, 2026

Choose a reason for hiding this comment

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

Aren't const param defaults also anon consts as well as field defaults?

View changes since the review

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.

Const param defaults, yes. Field defaults, I don't think we lower those yet.

Copy link
Copy Markdown
Member

@Veykril Veykril May 8, 2026

Choose a reason for hiding this comment

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

We are given the IDE layer now supports it

VariantFields {
def: VariantId,
store: &'db ExpressionStore,
source_map: &'db ExpressionStoreSourceMap,
infer: Option<&'db InferenceResult>,
},

See [the Zulip discussion](https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/Anon.20consts.20for.20everything/with/587684630) for details, but in short:
 - This allows us to easily set the expected type.
 - They can have proper MIR an eval, preventing the need for hacks like `fixme_resolve_all_clone()`.

This also fixes a bunch of old inference failures caused by us unable to evaluate constants.

In order to make them visible to the IDE layer, each query that can create anon consts now keeps a list of the anon consts it created. The expression store also gains an ability to find the root expression of something. Then, in `Semantics`, when wanting to find inference info for something, we find its root expr, then search it in the list of anon consts defined by the body.
@ChayimFriedman2
Copy link
Copy Markdown
Contributor Author

How big are the savings here? Imo we should definitely make these work for IDE analysis

They are big, 117mb on self. But as I said we can still support them in IDE, I'll work on that in a follow-up PR.

@ChayimFriedman2 ChayimFriedman2 enabled auto-merge May 7, 2026 12:41
@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue May 7, 2026
Merged via the queue into rust-lang:master with commit 5458851 May 7, 2026
18 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the anon-consts branch May 7, 2026 12:53
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants