From 2af66ebd1d83f8b0706805fd650f18bd8c8effe7 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Wed, 1 Feb 2023 12:41:47 +0100 Subject: [PATCH] gccrs: parser: Improve parsing of complex generic arguments The parser was missing code for handling complex type arguments such as type paths or nested generics. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_generic_arg): Handle type paths and nested generics properly. gcc/testsuite/ChangeLog: * rust/compile/parse_complex_generic_application.rs: New test. * rust/compile/parse_complex_generic_application2.rs: New test. --- gcc/rust/parse/rust-parse-impl.h | 4 +++- .../rust/compile/parse_complex_generic_application.rs | 17 +++++++++++++++++ .../rust/compile/parse_complex_generic_application2.rs | 10 ++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/rust/compile/parse_complex_generic_application.rs create mode 100644 gcc/testsuite/rust/compile/parse_complex_generic_application2.rs diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 23b033f..3610790 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -6309,7 +6309,9 @@ Parser::parse_generic_arg () // could either have a valid type or a macro (FIXME: anything else?). So // we need one bit of lookahead to differentiate if this is really auto next_tok = lexer.peek_token (1); - if (next_tok->get_id () == EXCLAM) + if (next_tok->get_id () == LEFT_ANGLE + || next_tok->get_id () == SCOPE_RESOLUTION + || next_tok->get_id () == EXCLAM) { auto type = parse_type (); if (type) diff --git a/gcc/testsuite/rust/compile/parse_complex_generic_application.rs b/gcc/testsuite/rust/compile/parse_complex_generic_application.rs new file mode 100644 index 0000000..d5c7bf4 --- /dev/null +++ b/gcc/testsuite/rust/compile/parse_complex_generic_application.rs @@ -0,0 +1,17 @@ +pub enum Either { + Left(T), + Right(E), +} + +pub mod err { + pub struct Error; + pub struct ErrorWrap(T); +} + +pub fn foo_err() -> Either<(), err::Error> { + Either::Left(()) +} + +pub fn foo_err_wrap() -> Either<(), err::ErrorWrap> { + Either::Left(()) +} diff --git a/gcc/testsuite/rust/compile/parse_complex_generic_application2.rs b/gcc/testsuite/rust/compile/parse_complex_generic_application2.rs new file mode 100644 index 0000000..0361931 --- /dev/null +++ b/gcc/testsuite/rust/compile/parse_complex_generic_application2.rs @@ -0,0 +1,10 @@ +pub enum Either { + Left(L), + Right(R), +} + +pub struct Wrap(T); + +pub fn foo_wrap() -> Either<(), Wrap> { + Either::Left(()) +} -- 2.7.4