Skip to content

Conversation

@philberty
Copy link
Member

@philberty philberty commented Nov 9, 2025

Impl items can have constants defined which could in turn be generic this was not supported by gccrs and missed. So for example:

  impl<T> Foo<T> {
    const MAGIC: usize = mem::size_of::<T>();
  }

This is a normal type parameter but in order to setup the generics we need to create a synthetic TyTy::FnType so we can bind the parent's impl generics to the type system and it just works like any other generic item at that point. Then for example we have:

  impl<const N: usize> Foo<N> {
    const VALUE: usize = N;
  }

Again we consistently bind the this const generic parameter the same way so the lazy evaluation of the generic can take place.

gcc/rust/ChangeLog:

* backend/rust-compile-item.cc (CompileItem::visit): support the synthetic function consts
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::Resolve): likewise
* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): create the synth
* typecheck/rust-tyty.h: new flag for synthetic constant

gcc/testsuite/ChangeLog:

* rust/execute/torture/const-generics-5.rs: New test.
* rust/execute/torture/const-generics-6.rs: New test.
* rust/execute/torture/const-generics-7.rs: New test.

Impl items can have constants defined which could in turn be generic this was
not supported by gccrs and missed. So for example:

  impl<T> Foo<T> {
    const MAGIC: usize = mem::size_of::<T>();
  }

This is a normal type parameter but in order to setup the generics we need to
create a synthetic TyTy::FnType so we can bind the parent's impl generics to
the type system and it just works like any other generic item at that point.
Then for example we have:

  impl<const N: usize> Foo<N> {
    const VALUE: usize = N;
  }

Again we consistently bind the this const generic parameter the same way so
the lazy evaluation of the generic can take place.

gcc/rust/ChangeLog:

	* backend/rust-compile-item.cc (CompileItem::visit): support the synthetic function consts
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::Resolve): likewise
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): create the synth
	* typecheck/rust-tyty.h: new flag for synthetic constant

gcc/testsuite/ChangeLog:

	* rust/execute/torture/const-generics-5.rs: New test.
	* rust/execute/torture/const-generics-6.rs: New test.
	* rust/execute/torture/const-generics-7.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
@philberty philberty added this to the Deferred const inference milestone Nov 9, 2025
@philberty philberty added this pull request to the merge queue Nov 9, 2025
Merged via the queue into master with commit e16958b Nov 9, 2025
13 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in libcore 1.49 Nov 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants