diff options
author | Philip Herron <herron.philip@googlemail.com> | 2025-04-30 14:37:49 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2025-05-07 15:07:22 +0000 |
commit | 927d067721e91ad3d702f9b9efaf939afd319ef1 (patch) | |
tree | f8d3afae5dc6176f72e1bd8702b39812012f4296 /gcc/rust/parse | |
parent | ef44f649655dcbba63b925c06e923ed1aefd6678 (diff) | |
download | gcc-927d067721e91ad3d702f9b9efaf939afd319ef1.zip gcc-927d067721e91ad3d702f9b9efaf939afd319ef1.tar.gz gcc-927d067721e91ad3d702f9b9efaf939afd319ef1.tar.bz2 |
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>
Diffstat (limited to 'gcc/rust/parse')
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 13 |
1 files changed, 2 insertions, 11 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index e47b9e0..e165998 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -9117,14 +9117,8 @@ Parser<ManagedTokenSource>::parse_type (bool save_errors) t = lexer.peek_token (); if (t->get_id () != PLUS) { - // convert trait bound to value object - AST::TraitBound value_bound (*initial_bound); - - // DEBUG: removed as unique ptr, so should auto-delete - // delete initial_bound; - return std::unique_ptr<AST::ImplTraitTypeOneBound> ( - new AST::ImplTraitTypeOneBound (std::move (value_bound), + new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus)); } @@ -9955,11 +9949,8 @@ Parser<ManagedTokenSource>::parse_type_no_bounds () return nullptr; } - // convert trait bound to value object - AST::TraitBound value_bound (*initial_bound); - return std::unique_ptr<AST::ImplTraitTypeOneBound> ( - new AST::ImplTraitTypeOneBound (std::move (value_bound), locus)); + new AST::ImplTraitTypeOneBound (std::move (initial_bound), locus)); } case DYN: case QUESTION_MARK: { |