From f6c86fc8cdea9db5259ae67a62618b13a57ea9a1 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 17 Mar 2022 10:38:58 +0000 Subject: [PATCH] Support placeholders becoming slices 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. Fixes #1034 --- gcc/rust/typecheck/rust-hir-type-check-expr.h | 1 - .../typecheck/rust-hir-type-check-implitem.h | 2 -- gcc/rust/typecheck/rust-tyty-cmp.h | 2 ++ gcc/rust/typecheck/rust-tyty.cc | 14 ++++++++++++++ gcc/testsuite/rust/compile/issue-1034.rs | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/rust/compile/issue-1034.rs diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index 93aa868505c0..1b14693aa8d1 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -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 (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 1722d16e4a9b..f1b5e35450e3 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -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); diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h index 436bde9a2954..6504e5aee1db 100644 --- a/gcc/rust/typecheck/rust-tyty-cmp.h +++ b/gcc/rust/typecheck/rust-tyty-cmp.h @@ -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; diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index c12095f26ebf..1d197f5a8459 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -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) { @@ -212,6 +213,19 @@ BaseType::get_root () const const PointerType *r = static_cast (root); root = r->get_base ()->get_root (); } + + // these are an unsize + else if (get_kind () == TyTy::SLICE) + { + const SliceType *r = static_cast (root); + root = r->get_element_type ()->get_root (); + } + // else if (get_kind () == TyTy::ARRAY) + // { + // const ArrayType *r = static_cast (root); + // root = r->get_element_type ()->get_root (); + // } + return root; } diff --git a/gcc/testsuite/rust/compile/issue-1034.rs b/gcc/testsuite/rust/compile/issue-1034.rs new file mode 100644 index 000000000000..23d770054524 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-1034.rs @@ -0,0 +1,16 @@ +trait Foo { + type Output; + + fn test(self, slice: &T) -> &Self::Output; +} + +struct Bar(T); +// { dg-warning "struct is never constructed" "" { target *-*-* } .-1 } + +impl Foo<[T]> for Bar { + type Output = [T]; + + fn test(self, slice: &[T]) -> &[T] { + slice + } +}