Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolution for additional trait bounds #3324

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions gcc/rust/resolve/rust-ast-resolve-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "rust-ast-resolve-type.h"
#include "rust-ast-resolve-expr.h"
#include "rust-canonical-path.h"
#include "rust-type.h"

namespace Rust {
namespace Resolver {
Expand Down Expand Up @@ -495,10 +497,59 @@ ResolveTypeToCanonicalPath::visit (AST::TraitObjectTypeOneBound &type)
}

void
ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &)
ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &type)
{
// FIXME is this actually allowed? dyn A+B
rust_unreachable ();
rust_assert (!type.get_type_param_bounds ().empty ());

auto &first_bound = type.get_type_param_bounds ().front ();

// Is it allowed or even possible to have a lifetime bound as a first bound?
if (first_bound->get_bound_type () == AST::TraitBound::LIFETIME)
rust_unreachable ();

auto &trait = static_cast<AST::TraitBound &> (*first_bound);

CanonicalPath path = CanonicalPath::create_empty ();
bool ok = ResolveTypeToCanonicalPath::go (trait.get_type_path (), path);

// right?
rust_assert (ok);

auto slice_path = "<dyn " + path.get ();

for (size_t idx = 1; idx < type.get_type_param_bounds ().size (); idx++)
{
auto &additional_bound = type.get_type_param_bounds ()[idx];

std::string str;

switch (additional_bound->get_bound_type ())
{
case AST::TypeParamBound::TRAIT: {
auto bound_path = CanonicalPath::create_empty ();

auto &bound_type_path
= static_cast<AST::TraitBound &> (*additional_bound)
.get_type_path ();
bool ok
= ResolveTypeToCanonicalPath::go (bound_type_path, bound_path);

if (!ok)
continue;

str = bound_path.get ();
break;
}
case AST::TypeParamBound::LIFETIME:
rust_unreachable ();
break;
}
slice_path += " + " + str;
}

slice_path += ">";

result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
}

void
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/resolve/rust-late-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ Late::visit (AST::TypePath &type)
ctx.map_usage (Usage (type.get_node_id ()),
Definition (resolved->get_node_id ()));
else
rust_unreachable ();
rust_error_at (type.get_locus (), "could not resolve type path %qs",
str.c_str ());

DefaultResolver::visit (type);
}
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/typecheck/rust-hir-type-check-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,11 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
std::vector<TyTy::TypeBoundPredicate> specified_bounds;
for (auto &bound : type.get_type_param_bounds ())
{
// TODO: here we need to check if there are additional bounds that aren't
// auto traits. this is an error. for example, `dyn A + Sized + Sync` is
// okay, because Sized and Sync are both auto traits but `dyn A + Copy +
// Clone` is not okay and should error out.

if (bound->get_bound_type ()
!= HIR::TypeParamBound::BoundType::TRAITBOUND)
continue;
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/util/rust-lang-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const BiMap<std::string, LangItem::Kind> Rust::LangItem::lang_items = {{
{"copy", Kind::COPY},
{"clone", Kind::CLONE},
{"sized", Kind::SIZED},
{"sync", Kind::SYNC},
{"slice_alloc", Kind::SLICE_ALLOC},
{"slice_u8_alloc", Kind::SLICE_U8_ALLOC},
{"str_alloc", Kind::STR_ALLOC},
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/util/rust-lang-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class LangItem
COPY,
CLONE,
SIZED,
SYNC,

// https://github.com/Rust-GCC/gccrs/issues/1896
// https://github.com/rust-lang/rust/commit/afbecc0f68c4dcfc4878ba5bcb1ac942544a1bdc
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/additional-trait-bounds1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(optin_builtin_traits)]

pub unsafe auto trait Send {}
#[lang = "sync"]
pub unsafe auto trait Sync {}

trait A {}

impl dyn A + Send {}
impl dyn A + Send + Sync {}
9 changes: 9 additions & 0 deletions gcc/testsuite/rust/compile/additional-trait-bounds2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(optin_builtin_traits)]

pub unsafe auto trait Send {}
#[lang = "sync"]
pub unsafe auto trait Sync {}

trait A {}

impl dyn A + Send + Sync + NonExist {} // { dg-error "failed to resolve TypePath: NonExist in this scope" }
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/additional-trait-bounds2nr2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// { dg-additional-options "-frust-name-resolution-2.0" }

#![feature(optin_builtin_traits)]

pub unsafe auto trait Send {}
#[lang = "sync"]
pub unsafe auto trait Sync {}

trait A {}

impl dyn A + Send + Sync + NonExist {} // { dg-error "could not resolve type path .NonExist." }
1 change: 1 addition & 0 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,5 @@ issue-2905-2.rs
issue-2907.rs
issue-2423.rs
issue-266.rs
additional-trait-bounds2.rs
# please don't delete the trailing newline
Loading