Skip to content

Commit

Permalink
Merge #1037
Browse files Browse the repository at this point in the history
1037: Support placeholders becoming slices r=philberty a=philberty

When we setup trait-impls the type-alias are allowed to become any type
this interface was missing a visitor. We also need to support constraining
type-parameters behind slices.

The get_root interface is currently unsafe, it needs a flag for allowing
unsized and for keeping a map of adjustments along the way. This will
be added down the line when we support unsized method resolution.

Fixes #1034
Addresses #849 

Co-authored-by: Philip Herron <philip.herron@embecosm.com>
  • Loading branch information
bors[bot] and philberty authored Mar 17, 2022
2 parents e48bce4 + f6c86fc commit b7baee8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 0 additions & 1 deletion gcc/rust/typecheck/rust-hir-type-check-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ class TypeCheckExpr : public TypeCheckBase
// Get the adjusted self
Adjuster adj (receiver_tyty);
TyTy::BaseType *adjusted_self = adj.adjust_type (candidate.adjustments);
adjusted_self->debug ();

// store the adjustments for code-generation to know what to do
context->insert_autoderef_mappings (expr.get_mappings ().get_hirid (),
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/typecheck/rust-hir-type-check-implitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,6 @@ class TypeCheckImplItemWithTrait : public TypeCheckImplItem

void visit (HIR::TypeAlias &type) override
{
// resolved_trait_item = trait_reference.lookup_trait_item (
// type.get_new_type_name (), TraitItemReference::TraitItemType::TYPE);
trait_reference.lookup_trait_item_by_type (
type.get_new_type_name (), TraitItemReference::TraitItemType::TYPE,
&resolved_trait_item);
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/typecheck/rust-tyty-cmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,8 @@ class PlaceholderCmp : public BaseCmp

void visit (const NeverType &) override { ok = true; }

void visit (const SliceType &) override { ok = true; }

void visit (const PlaceholderType &type) override
{
ok = base->get_symbol ().compare (type.get_symbol ()) == 0;
Expand Down
14 changes: 14 additions & 0 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ BaseType::inherit_bounds (
const BaseType *
BaseType::get_root () const
{
// FIXME this needs to be it its own visitor class with a vector adjustments
const TyTy::BaseType *root = this;
if (get_kind () == TyTy::REF)
{
Expand All @@ -212,6 +213,19 @@ BaseType::get_root () const
const PointerType *r = static_cast<const PointerType *> (root);
root = r->get_base ()->get_root ();
}

// these are an unsize
else if (get_kind () == TyTy::SLICE)
{
const SliceType *r = static_cast<const SliceType *> (root);
root = r->get_element_type ()->get_root ();
}
// else if (get_kind () == TyTy::ARRAY)
// {
// const ArrayType *r = static_cast<const ArrayType *> (root);
// root = r->get_element_type ()->get_root ();
// }

return root;
}

Expand Down
16 changes: 16 additions & 0 deletions gcc/testsuite/rust/compile/issue-1034.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait Foo<T> {
type Output;

fn test(self, slice: &T) -> &Self::Output;
}

struct Bar<T>(T);
// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 }

impl<T> Foo<[T]> for Bar<usize> {
type Output = [T];

fn test(self, slice: &[T]) -> &[T] {
slice
}
}

0 comments on commit b7baee8

Please sign in to comment.