aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2025-01-16 17:10:02 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-24 13:06:51 +0100
commit3ee9affeb3bd5e5ef6f2b6f9a73252b937536f4e (patch)
tree19e79fc544a9ee5b1bbbfeb32b1660293f10e0ff /gcc/rust
parentecb0e17c41944d340595816e2890f2a7d2053044 (diff)
downloadgcc-3ee9affeb3bd5e5ef6f2b6f9a73252b937536f4e.zip
gcc-3ee9affeb3bd5e5ef6f2b6f9a73252b937536f4e.tar.gz
gcc-3ee9affeb3bd5e5ef6f2b6f9a73252b937536f4e.tar.bz2
gccrs: typecheck: Add basic handling for applying auto trait bounds
gcc/rust/ChangeLog: * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Register auto traits in mappings. * util/rust-hir-map.cc (Mappings::insert_auto_trait): New. (Mappings::get_auto_traits): New. * util/rust-hir-map.h: Declare them. * typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Add auto trait bounds when scanning. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Some parts of nr2.0 can't handle auto traits yet. * rust/compile/auto_traits3.rs: Removed in favor of... * rust/compile/auto_traits2.rs: ...this one. * rust/compile/auto_traits4.rs: New test.
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/hir/rust-ast-lower-item.cc11
-rw-r--r--gcc/rust/typecheck/rust-tyty-bounds.cc4
-rw-r--r--gcc/rust/util/rust-hir-map.cc12
-rw-r--r--gcc/rust/util/rust-hir-map.h6
4 files changed, 28 insertions, 5 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index ae938d9..5dbcad5 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -606,17 +606,18 @@ ASTLoweringItem::visit (AST::Trait &trait)
mappings.get_next_hir_id (crate_num),
mappings.get_next_localdef_id (crate_num));
- auto trait_unsafety = Unsafety::Normal;
- if (trait.is_unsafe ())
- {
- trait_unsafety = Unsafety::Unsafe;
- }
+ auto trait_unsafety
+ = trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
HIR::Trait *hir_trait
= new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety,
std::move (generic_params), std::move (type_param_bounds),
where_clause, std::move (trait_items), vis,
trait.get_outer_attrs (), trait.get_locus ());
+
+ if (trait.is_auto ())
+ mappings.insert_auto_trait (hir_trait);
+
translated = hir_trait;
for (auto trait_item_id : trait_item_ids)
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc
index 73d686b..3e42427 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -97,6 +97,10 @@ TypeBoundsProbe::scan ()
// marker traits...
assemble_sized_builtin ();
+
+ // add auto trait bounds
+ for (auto *auto_trait : mappings.get_auto_traits ())
+ add_trait_bound (auto_trait);
}
void
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index b94591e..ac18e57 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1309,5 +1309,17 @@ Mappings::get_lang_item_node (LangItem::Kind item_type)
LangItem::ToString (item_type).c_str ());
}
+void
+Mappings::insert_auto_trait (HIR::Trait *trait)
+{
+ auto_traits.emplace_back (trait);
+}
+
+std::vector<HIR::Trait *> &
+Mappings::get_auto_traits ()
+{
+ return auto_traits;
+}
+
} // namespace Analysis
} // namespace Rust
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 21e5328..8ea8646 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -342,6 +342,9 @@ public:
tl::optional<HIR::TraitItem *>
lookup_trait_item_lang_item (LangItem::Kind item, location_t locus);
+ void insert_auto_trait (HIR::Trait *trait);
+ std::vector<HIR::Trait *> &get_auto_traits ();
+
private:
Mappings ();
@@ -380,6 +383,9 @@ private:
std::map<HirId, HIR::Trait *> hirTraitItemsToTraitMappings;
std::map<HirId, HIR::Pattern *> hirPatternMappings;
+ // FIXME: Add documentation
+ std::vector<HIR::Trait *> auto_traits;
+
// We need to have two maps here, as lang-items need to be used for both AST
// passes and HIR passes. Thus those two maps are created at different times.
std::map<LangItem::Kind, DefId> lang_item_mappings;