rust-lang / rust-lang/rust-analyzer

Support const generics

Open
#8,655 7 comments 38 reactions 1 assignee View on GitHub

@Veykril is already working on this.

Since Mar 18, 2026.

A-ty C-bug C-feature E-hard E-has-instructions S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

There's a bunch of stuff we need to implement to support const generics at least enough to get method resolution right in simple cases:

  • lower const parameters and array sizes as bodies (#7434)
  • represent const parameters in the type checker: this (like supporting lifetime parameters) will mean fixing a bunch of places in hir_ty that currently assume only type parameters exist. A good place to start is probably the Generics helper in hir_ty's util.rs
  • use Chalk's unification logic instead of ours that doesn't take consts into account (I'm working on this)
  • represent consts in types, at least simple numbers:
    • this means changing InternedConcreteConst in our Interner from () to an enum that can represent these numbers
    • then we need to implement the const_eq method on Interner
  • evaluate const bodies: there should be a query that takes a const body and tries to evaluate it, returning a Result<Binders<Const>, ConstEvalError> that can be either a concrete const, a BoundVar referring to a const param, or an error if the expression is too complicated. We'll want to handle errors differently depending on the situation (while inferring bodies, we could replace them by const variables, for example). We should stick to only evaluating simple integer literals for now. We'll need to call this query during type lowering.
    • this would basically also get us most of the way to #8497
    • this should also fix #8654 since our method resolution will then take the const values into account
    • to represent more complicated const generics like { N + 1 } (where N is a const param), we need support in Chalk for unevaluated consts that doesn't exist yet
  • expand the const evaluation logic: we shouldn't put too much work into this, but a good subset might be integer literals, consts and maybe the standard binary operations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.