]> git.ipfire.org Git - thirdparty/gcc.git/commit
gccrs: desugar APIT impl traits
authorPhilip Herron <herron.philip@googlemail.com>
Wed, 30 Apr 2025 13:37:49 +0000 (14:37 +0100)
committerPhilip Herron <philip.herron@embecosm.com>
Wed, 7 May 2025 15:07:22 +0000 (15:07 +0000)
commit927d067721e91ad3d702f9b9efaf939afd319ef1
treef8d3afae5dc6176f72e1bd8702b39812012f4296
parentef44f649655dcbba63b925c06e923ed1aefd6678
gccrs: desugar APIT impl traits

Argument position impl traits are simply syntatic sugar for generics. This
adds a new desugar pass to do this. So for example:

    fn foo(a: impl Value, b: impl Value) -> i32

Is desugared into:

    fn foo<T: Value, U: Value> (a: T, b: U) -> i32

So it just works like any normal generic function. There are more complex cases such as:

    fn foo(_value: impl Bar<Baz = impl Foo>) -> i32

Which has a generic argument binding which needs to be turned into a where constraint:

    fn foo<T, U>(_value: T) -> i32
        where
            T: Bar<Baz = U>,
            U: Foo,

Fixes Rust-GCC#2015
Fixes Rust-GCC#1487
Fixes Rust-GCC#3454
Fixes Rust-GCC#1482

gcc/rust/ChangeLog:

* Make-lang.in: new desugar file
* ast/rust-ast.cc (ImplTraitTypeOneBound::as_string): its a unique_ptr now
(FormatArgs::set_outer_attrs): reformat
* ast/rust-path.h: remove has_generic_args assertion (can be empty because of desugar)
* ast/rust-type.h (class ImplTraitTypeOneBound): add copy ctor and use unique_ptr
* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): update to use unique_ptr
* parse/rust-parse-impl.h (Parser::parse_type): reuse the existing unique_ptr instead
(Parser::parse_type_no_bounds): likewise
(Parser::parse_pattern): likewise
* resolve/rust-ast-resolve-type.cc (ResolveType::visit): its a unique_ptr now
* rust-session-manager.cc (Session::compile_crate): call desugar
* ast/rust-desugar-apit.cc: New file.
* ast/rust-desugar-apit.h: New file.

gcc/testsuite/ChangeLog:

* rust/compile/issue-2015.rs: fully supported now
* rust/compile/nr2/exclude: nr2 cant handle some of these
* rust/compile/issue-1487.rs: New test.
* rust/compile/issue-3454.rs: New test.
* rust/execute/torture/impl_desugar-2.rs: New test.
* rust/execute/torture/impl_desugar.rs: New test.
* rust/execute/torture/impl_trait1.rs: New test.
* rust/execute/torture/impl_trait2.rs: New test.
* rust/execute/torture/impl_trait3.rs: New test.
* rust/execute/torture/impl_trait4.rs: New test.
* rust/execute/torture/issue-1482.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
21 files changed:
gcc/rust/Make-lang.in
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-desugar-apit.cc [new file with mode: 0644]
gcc/rust/ast/rust-desugar-apit.h [new file with mode: 0644]
gcc/rust/ast/rust-path.h
gcc/rust/ast/rust-type.h
gcc/rust/hir/rust-ast-lower-type.cc
gcc/rust/parse/rust-parse-impl.h
gcc/rust/resolve/rust-ast-resolve-type.cc
gcc/rust/rust-session-manager.cc
gcc/testsuite/rust/compile/issue-1487.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/issue-2015.rs
gcc/testsuite/rust/compile/issue-3454.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/nr2/exclude
gcc/testsuite/rust/execute/torture/impl_desugar-2.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/impl_desugar.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/impl_trait1.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/impl_trait2.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/impl_trait3.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/impl_trait4.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/issue-1482.rs [new file with mode: 0644]