Skip to content

gccrs: Initial support for Return Position Impl Trait#3807

Merged
philberty merged 2 commits into
masterfrom
phil/rpt
May 26, 2025
Merged

gccrs: Initial support for Return Position Impl Trait#3807
philberty merged 2 commits into
masterfrom
phil/rpt

Conversation

@philberty

@philberty philberty commented May 26, 2025

Copy link
Copy Markdown
Member

This is the initial patch for RPIT, we can build on this to handle the more complex cases but there are enough distinct changes going on here that it should just get merged now.

RPIT is really a sneaky generic so for example:

  fn foo() -> impl Bar {
      Baz
  }

This is represented as: fn () -> OpaqueType Bar. But when we handle the coercion site for Baz on impl Bar when we type resolve the function we know that the underlying type is Baz. Note this function is not generic so its using this special OpaqueType and keeping track of the underlying type in its ty_ref reference hir-id which will resolve to Baz.

This also means if we have a case where maybe this was in an if statement:

  fn foo(a: i32) -> impl Bar {
      if a > 10 {
        Baz
      } else {
        Qux
      }
  }

The rules of impl Bar is that Baz is handled but Baz and Qux are different underlying types so this is not allowed. The reason is impl traits are not generic and although from a programmer perspective the callers dont know what the underlying type is, the compiler knows what it is. So really when you call a function and get its return position impl trait the compiler knows what to do and does all whats nessecary to handle calling functions using that type etc.

gcc/rust/ChangeLog:

* backend/rust-compile-type.cc (TyTyResolveCompile::visit): we need to resolve the underlying type
* typecheck/rust-substitution-mapper.cc (SubstMapperInternal::visit): just clone
* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): ensure we monomphize to get the underlying
* typecheck/rust-tyty.cc (BaseType::destructure): handle opaque types (OpaqueType::resolve): this is much simpler now (OpaqueType::handle_substitions): no longer needed
* typecheck/rust-tyty.h: update header
* typecheck/rust-unify.cc (UnifyRules::expect_opaque): unify rules for opaque

gcc/testsuite/ChangeLog:

* rust/compile/bad-rpit1.rs: New test.
* rust/execute/torture/impl_rpit1.rs: New test.
* rust/execute/torture/impl_rpit2.rs: New test.
* rust/execute/torture/impl_rpit3.rs: New test.

philberty added 2 commits May 26, 2025 19:41
When working on rpit we needed to change to use a monomorphized clone of
the result of function calls. This ended up causing a verify gimple issue
with tuples because:

  fn test<A, B>(a: A, b: B) -> (A, B)

When passing for example:

  let a = test::<i32, i32> (123, 456) -> (A=i32, B=i32)

The resulting gimple types became:

  const struct (A=i32, B=i32) vs struct (i32, i32)

We removed the VIEW_CONVERT_EXPR support to auto fix this stuff a good
while ago because it hides these kinds of issues because the type hasher
included the A=i32, B=i32 vs the i32, i32 name so updating this to use
get_name instead keeps the naming the same as well as the fields meaning
these types are 100% equivilant and therefore no conversion is required.
This only occurs because tuples are not named types we should really add
more rust specific info on our gimple TYPES.

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): use get_name
	* typecheck/rust-tyty.cc (TupleType::get_name): likewise

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
This is the initial patch for RPIT, we can build on this to handle the
more complex cases but there are enough distinct changes going on here
that it should just get merged now.

RPIT is really a sneaky generic so for example:

  fn foo() -> impl Bar {
      Baz
  }

This is represented as: fn () -> OpaqueType Bar. But when we handle the
coercion site for Baz on impl Bar when we type resolve the function we
know that the underlying type  is Baz. Note this function is _not_ generic
so its using this special OpaqueType and keeping track of the underlying type
in its ty_ref reference hir-id which will resolve to Baz.

This also means if we have a case where maybe this was in an if statement:

  fn foo(a: i32) -> impl Bar {
      if a > 10 {
        Baz
      } else {
        Qux
      }
  }

The rules of impl Bar is that Baz is handled but Baz and Qux are different
underlying types so this is not allowed. The reason is impl traits are not
generic and although from a programmer perspective the callers dont know what
the underlying type is, the compiler _knows_ what it is. So really when
you call a function and get its return position impl trait the compiler knows
what to do and does all whats nessecary to handle calling functions using that
type etc.

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): we need to resolve the
	underlying type
	* typecheck/rust-substitution-mapper.cc (SubstMapperInternal::visit): just clone
	* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit):
	ensure we monomphize to get the underlying
	* typecheck/rust-tyty.cc (BaseType::destructure): handle opaque types
	(OpaqueType::resolve): this is much simpler now
	(OpaqueType::handle_substitions): no longer needed
	* typecheck/rust-tyty.h: update header
	* typecheck/rust-unify.cc (UnifyRules::expect_opaque): unify rules for opaque

gcc/testsuite/ChangeLog:

	* rust/compile/bad-rpit1.rs: New test.
	* rust/execute/torture/impl_rpit1.rs: New test.
	* rust/execute/torture/impl_rpit2.rs: New test.
	* rust/execute/torture/impl_rpit3.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
@philberty philberty added this pull request to the merge queue May 26, 2025
Merged via the queue into master with commit a497ed0 May 26, 2025
12 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in libcore 1.49 May 26, 2025
@philberty philberty deleted the phil/rpt branch June 14, 2025 17:55
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.

1 participant