From: Utkarsh Bahuguna <[email protected]>
A `where Self: Bound` on a trait method was added to the trait's shared
Self, so every method of the trait then required it. That is why none
of Iterator's default methods resolve once `rev` is around. Put the
bound on the item's own Self substitution instead.
Fixes Rust-GCC/gccrs#4917
Assisted-by: Claude Opus (Anthropic)
gcc/rust/ChangeLog:
* typecheck/rust-hir-type-check-type.h (ResolveWhereClauseItem): Take
the trait item's own Self.
* typecheck/rust-hir-type-check-type.cc
(ResolveWhereClauseItem::Resolve): Pass it along.
(ResolveWhereClauseItem::visit (TypeBoundWhereClauseItem)): Apply
bounds on the implicit Self to it.
* typecheck/rust-hir-type-check.cc
(TraitItemReference::get_type_from_fn): Hand the item's Self to the
where clause resolver.
gcc/testsuite/ChangeLog:
* rust/compile/issues/issue-4917.rs: New test.
* rust/compile/issues/issue-4917-2.rs: New test.
* rust/compile/issues/issue-4917-3.rs: New test.
Signed-off-by: Utkarsh Bahuguna <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/1c75af8b5307c2adeeadb5be2044068acbc1caba
The commit has been mentioned in the following issue(s):
- Rust-GCC/gccrs#4917: https://github.com/Rust-GCC/gccrs/issues/4917
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5020
.../typecheck/rust-hir-type-check-type.cc | 13 +++-
gcc/rust/typecheck/rust-hir-type-check-type.h | 13 +++-
gcc/rust/typecheck/rust-hir-type-check.cc | 15 +++-
.../rust/compile/issues/issue-4917-2.rs | 54 +++++++++++++
.../rust/compile/issues/issue-4917-3.rs | 35 +++++++++
.../rust/compile/issues/issue-4917.rs | 78 +++++++++++++++++++
6 files changed, 202 insertions(+), 6 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/issues/issue-4917-2.rs
create mode 100644 gcc/testsuite/rust/compile/issues/issue-4917-3.rs
create mode 100644 gcc/testsuite/rust/compile/issues/issue-4917.rs
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 135cbc290..9c50a0de7 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -1043,9 +1043,10 @@ ResolveWhereClauseItem::Resolve (HIR::WhereClauseItem
&item,
void
ResolveWhereClauseItem::Resolve (HIR::WhereClause &clause,
- TyTy::RegionConstraints ®ion_constraints)
+ TyTy::RegionConstraints ®ion_constraints,
+ TyTy::BaseType *trait_item_self)
{
- ResolveWhereClauseItem resolver (region_constraints);
+ ResolveWhereClauseItem resolver (region_constraints, trait_item_self);
class PlainTypePath : public HIR::HIRTypeVisitor
{
@@ -1139,6 +1140,14 @@ ResolveWhereClauseItem::visit
(HIR::TypeBoundWhereClauseItem &item)
if (defer_bindings && binding->get_kind () != TyTy::TypeKind::PARAM)
return;
+ // `Self: Bound` on a trait item constrains that item only
+ if (trait_item_self != nullptr)
+ {
+ auto *param = binding->try_as<TyTy::ParamType> ();
+ if (param != nullptr && param->is_implicit_self_trait ())
+ binding = trait_item_self;
+ }
+
// FIXME double check there might be a trait cycle here see TypeParam
handling
std::vector<TyTy::TypeBoundPredicate> specified_bounds;
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h
b/gcc/rust/typecheck/rust-hir-type-check-type.h
index ee1d0c97d..6e30be379 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.h
@@ -131,9 +131,15 @@ class ResolveWhereClauseItem : public TypeCheckBase
// pair(a, b) => a: b
TyTy::RegionConstraints ®ion_constraints;
+ // When resolving the where clause of a trait item, bounds placed on the
+ // trait's implicit Self constrain that item only, so they are applied to
+ // the item's own copy of the Self parameter instead of the trait's.
+ TyTy::BaseType *trait_item_self;
+
public:
static void Resolve (HIR::WhereClause &clause,
- TyTy::RegionConstraints ®ion_constraints);
+ TyTy::RegionConstraints ®ion_constraints,
+ TyTy::BaseType *trait_item_self = nullptr);
protected:
void visit (HIR::LifetimeWhereClauseItem &item);
@@ -146,8 +152,9 @@ private:
bool defer_bindings = false;
bool complete_bindings = false;
- ResolveWhereClauseItem (TyTy::RegionConstraints ®ion_constraints)
- : region_constraints (region_constraints)
+ ResolveWhereClauseItem (TyTy::RegionConstraints ®ion_constraints,
+ TyTy::BaseType *trait_item_self = nullptr)
+ : region_constraints (region_constraints), trait_item_self
(trait_item_self)
{}
};
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc
b/gcc/rust/typecheck/rust-hir-type-check.cc
index 892dcb359..c0e9d9542 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -179,9 +179,22 @@ TraitItemReference::get_type_from_fn (/*const*/
HIR::TraitItemFunc &fn) const
ABI::RUST);
}
+ // A `where Self: Bound` on this item must not end up on the trait's
+ // shared Self parameter, or every other item would require it too.
+ TyTy::BaseType *item_self = nullptr;
+ for (auto &mapping : substitutions)
+ {
+ auto *param = mapping.get_param_ty ()->try_as<TyTy::ParamType> ();
+ if (param != nullptr && param->is_implicit_self_trait ())
+ {
+ item_self = param;
+ break;
+ }
+ }
+
TyTy::RegionConstraints region_constraints;
ResolveWhereClauseItem::Resolve (function.get_where_clause (),
- region_constraints);
+ region_constraints, item_self);
TyTy::BaseType *ret_type = nullptr;
if (!function.has_return_type ())
diff --git a/gcc/testsuite/rust/compile/issues/issue-4917-2.rs
b/gcc/testsuite/rust/compile/issues/issue-4917-2.rs
new file mode 100644
index 000000000..2ed37040e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issues/issue-4917-2.rs
@@ -0,0 +1,54 @@
+#![feature(no_core, lang_items)]
+#![no_core]
+
+// Rust-GCC/gccrs#4917: the `where Self: DoubleEndedIterator` on `rev` must
+// still be checked for the item it is written on.
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub enum Option<T> {
+ None,
+ Some(T),
+}
+
+pub trait Iterator {
+ type Item;
+ fn next(&mut self) -> Option<Self::Item>;
+
+ fn rev(self) -> Rev<Self>
+ where
+ Self: Sized + DoubleEndedIterator,
+ {
+ Rev { iter: self }
+ }
+}
+
+pub trait DoubleEndedIterator: Iterator {
+ fn next_back(&mut self) -> Option<Self::Item>;
+}
+
+pub struct Rev<I> {
+ iter: I,
+}
+
+impl<I: DoubleEndedIterator> Iterator for Rev<I> {
+ type Item = I::Item;
+ fn next(&mut self) -> Option<I::Item> {
+ self.iter.next_back()
+ }
+}
+
+pub struct Forward;
+
+impl Iterator for Forward {
+ type Item = u32;
+ fn next(&mut self) -> Option<u32> {
+ Option::None
+ }
+}
+
+fn main() {
+ let _ = Forward.rev();
+ // { dg-error "not satisfied" "" { target *-*-* } .-1 }
+}
diff --git a/gcc/testsuite/rust/compile/issues/issue-4917-3.rs
b/gcc/testsuite/rust/compile/issues/issue-4917-3.rs
new file mode 100644
index 000000000..75a15bbb8
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issues/issue-4917-3.rs
@@ -0,0 +1,35 @@
+#![feature(no_core, lang_items)]
+#![no_core]
+
+// Rust-GCC/gccrs#4917: a bound on one of the item's own generics is not a
+// bound on Self, so it has to stay on that generic.
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub trait Marker {}
+
+pub trait Picker {
+ fn pick<T>(self, t: T) -> T
+ where
+ Self: Sized,
+ T: Marker,
+ {
+ t
+ }
+}
+
+pub struct Forward;
+
+impl Picker for Forward {}
+
+pub struct Tagged;
+
+impl Marker for Tagged {}
+
+fn main() {
+ let _ = Forward.pick(Tagged);
+ let _ = Forward.pick(Forward);
+ // { dg-error "not satisfied" "" { target *-*-* } .-1 }
+ // { dg-error "failed to lookup type" "" { target *-*-* } .-2 }
+}
diff --git a/gcc/testsuite/rust/compile/issues/issue-4917.rs
b/gcc/testsuite/rust/compile/issues/issue-4917.rs
new file mode 100644
index 000000000..22da2c1e6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issues/issue-4917.rs
@@ -0,0 +1,78 @@
+// { dg-additional-options "-w" }
+#![feature(no_core, lang_items)]
+#![no_core]
+
+// Rust-GCC/gccrs#4917: the `where Self: DoubleEndedIterator` on `rev` used
+// to be added to the trait's shared Self parameter, so every default method
+// of Iterator, like `count`, required DoubleEndedIterator and was not found.
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub enum Option<T> {
+ None,
+ Some(T),
+}
+
+pub trait Iterator {
+ type Item;
+ fn next(&mut self) -> Option<Self::Item>;
+
+ fn rev(self) -> Rev<Self>
+ where
+ Self: Sized + DoubleEndedIterator,
+ {
+ Rev { iter: self }
+ }
+
+ fn count(self) -> usize
+ where
+ Self: Sized,
+ {
+ 0
+ }
+}
+
+pub trait DoubleEndedIterator: Iterator {
+ fn next_back(&mut self) -> Option<Self::Item>;
+}
+
+pub struct Rev<I> {
+ iter: I,
+}
+
+impl<I: DoubleEndedIterator> Iterator for Rev<I> {
+ type Item = I::Item;
+ fn next(&mut self) -> Option<I::Item> {
+ self.iter.next_back()
+ }
+}
+
+pub struct MyIter;
+
+impl Iterator for MyIter {
+ type Item = u32;
+ fn next(&mut self) -> Option<u32> {
+ Option::None
+ }
+}
+
+impl DoubleEndedIterator for MyIter {
+ fn next_back(&mut self) -> Option<u32> {
+ Option::None
+ }
+}
+
+pub struct Forward;
+
+impl Iterator for Forward {
+ type Item = u32;
+ fn next(&mut self) -> Option<u32> {
+ Option::None
+ }
+}
+
+fn main() {
+ let _ = MyIter.rev().count();
+ let _ = Forward.count();
+}
base-commit: 5a902905fdb37f6cb0e41a8e119eb2966552f393
--
2.56.1