[gcc r15-8589] gccrs: lower: Correctly lower parenthesized types
Arthur Cohen
cohenarthur@gcc.gnu.org
Fri Mar 21 11:57:59 GMT 2025
https://gcc.gnu.org/g:cd8547f85e53b42eff5a7c7572b466c023c75daf
commit r15-8589-gcd8547f85e53b42eff5a7c7572b466c023c75daf
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date: Wed Dec 25 17:55:09 2024 +0000
gccrs: lower: Correctly lower parenthesized types
This is useful for handling multiple trait bounds, and required for better handling of auto traits.
gcc/rust/ChangeLog:
* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): Add implementation for
ParenthesizedType.
* hir/rust-ast-lower-type.h: Declare that new visitor.
gcc/testsuite/ChangeLog:
* rust/compile/auto_traits1.rs: New test.
Diff:
---
gcc/rust/hir/rust-ast-lower-type.cc | 19 +++++++++++++++++++
gcc/rust/hir/rust-ast-lower-type.h | 2 ++
gcc/testsuite/rust/compile/auto_traits1.rs | 27 +++++++++++++++++++++++++++
3 files changed, 48 insertions(+)
diff --git a/gcc/rust/hir/rust-ast-lower-type.cc b/gcc/rust/hir/rust-ast-lower-type.cc
index df06e48b801d..d1f95edc3456 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -19,6 +19,7 @@
#include "rust-ast-lower-type.h"
#include "rust-hir-map.h"
#include "rust-hir-path.h"
+#include "rust-hir-type.h"
#include "rust-path.h"
#include "rust-pattern.h"
@@ -471,6 +472,24 @@ ASTLoweringType::visit (AST::TraitObjectType &type)
type.get_locus (), type.is_dyn ());
}
+void
+ASTLoweringType::visit (AST::ParenthesisedType &type)
+{
+ auto *inner = ASTLoweringType::translate (*type.get_type_in_parens (),
+ default_to_static_lifetime);
+
+ auto crate_num = mappings.get_current_crate ();
+ Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
+ mappings.get_next_hir_id (crate_num),
+ mappings.get_next_localdef_id (crate_num));
+
+ // FIXME: Do we actually need to know if a type is parenthesized in the HIR?
+ // or can we just use the type in parens?
+ translated
+ = new HIR::ParenthesisedType (mapping, std::unique_ptr<HIR::Type> (inner),
+ type.get_locus ());
+}
+
HIR::GenericParam *
ASTLowerGenericParam::translate (AST::GenericParam ¶m)
{
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h
index 0429e3fcf986..26ca8684083c 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -22,6 +22,7 @@
#include "rust-ast-lower-base.h"
#include "rust-ast-lower-expr.h"
#include "rust-hir-path.h"
+#include "rust-type.h"
namespace Rust {
namespace HIR {
@@ -83,6 +84,7 @@ public:
void visit (AST::NeverType &type) override;
void visit (AST::TraitObjectTypeOneBound &type) override;
void visit (AST::TraitObjectType &type) override;
+ void visit (AST::ParenthesisedType &type) override;
private:
ASTLoweringType (bool default_to_static_lifetime)
diff --git a/gcc/testsuite/rust/compile/auto_traits1.rs b/gcc/testsuite/rust/compile/auto_traits1.rs
new file mode 100644
index 000000000000..192052d48151
--- /dev/null
+++ b/gcc/testsuite/rust/compile/auto_traits1.rs
@@ -0,0 +1,27 @@
+// { dg-additional-options "-frust-compile-until=typecheck" }
+
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+trait A {
+ fn a_method(&self) {}
+}
+
+fn foo(a: &(dyn A + Send + Sync)) {
+ a.a_method();
+}
+
+struct S;
+
+impl A for S {
+ fn a_method(&self) {}
+}
+
+fn main() {
+ let s = S;
+
+ foo(&s);
+}
More information about the Gcc-cvs
mailing list