Skip to content

Commit

Permalink
typecheck: Add basic handling for applying auto trait bounds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CohenArthur authored and philberty committed Jan 21, 2025
1 parent 9e87635 commit 53dfc6a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
11 changes: 6 additions & 5 deletions gcc/rust/hir/rust-ast-lower-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/typecheck/rust-tyty-bounds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions gcc/rust/util/rust-hir-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ class Mappings
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 ();

Expand Down Expand Up @@ -380,6 +383,9 @@ class Mappings
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;
Expand Down
5 changes: 2 additions & 3 deletions gcc/testsuite/rust/compile/auto_traits2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ fn foo(a: &(dyn A + Send + Sync)) {
struct S;

impl A for S {
fn a_method(&self) {}
fn a_method(&self) {} // { dg-warning "unused name" }
}

fn main() {
let s = S;

foo(&s); // { dg-error "bounds not satisfied" }
// { dg-error "mismatched type" "" { target *-*-* } .-1 }
foo(&s);
}
34 changes: 0 additions & 34 deletions gcc/testsuite/rust/compile/auto_traits3.rs

This file was deleted.

14 changes: 14 additions & 0 deletions gcc/testsuite/rust/compile/auto_traits4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(optin_builtin_traits)]

unsafe auto trait Send {}
unsafe auto trait Sync {}

fn take_send(_: &dyn Send) {}
fn take_sync(_: &dyn Sync) {}

fn main() {
let a = i32;

take_send(&a);
take_sync(&a);
}
3 changes: 2 additions & 1 deletion gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ issue-2907.rs
issue-2423.rs
issue-266.rs
additional-trait-bounds2.rs
auto_traits3.rs
auto_traits2.rs
auto_traits4.rs
issue-3140.rs
cmp1.rs
derive_clone_enum1.rs
Expand Down

0 comments on commit 53dfc6a

Please sign in to comment.