Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upwhere clauses are only elaborated for supertraits, and not other things #20671
Comments
This comment has been minimized.
This comment has been minimized.
|
Possibly related to #20469 but it is not fixed by |
This comment has been minimized.
This comment has been minimized.
|
Note, this blocks the transition from |
aturon
referenced this issue
Jan 7, 2015
Closed
change `core::borrow::BorrowFrom` to `core::borrow::Borrow` #530
This comment has been minimized.
This comment has been minimized.
|
Sorry, but I don't see why this should work without an explicit #![crate_type = "lib"]
// these two are equivalent
struct Rev1<I> where I: DoubleEndedIterator {
it: I,
}
struct Rev2<I: DoubleEndedIterator> {
it: I,
}
// these two don't work because `Rev<I>` doesn't imply that `I: DoubleEndedIterator`
fn foo1<I>(mut rev: Rev1<I>) {
rev.it.next_back();
}
fn foo2<I>(mut rev: Rev2<I>) {
rev.it.next_back();
}
// even these won't work unless you explicitly add `I: DoubleEndedIterator` as a bound
fn bar1<I>(mut rev: Rev1<I>) {}
fn bar2<I>(mut rev: Rev2<I>) {}I skimmed over the |
This comment has been minimized.
This comment has been minimized.
|
@japaric My assumption that this would work was based partly on conversations with @nikomatsakis after the initial RFC. In particular, we've discussed that trait Foo<T> {
fn foo(&self) -> &T;
}
//trait Bar<A> where Self: Foo<A> {} <--- this produces an error
trait Bar<A>: Foo<A> {}
fn foobar<A, B: Bar<A>>(b: &B) -> &A {
b.foo()
}That seems inconsistent and I don't know of any good reason for it. (And of course it's a pain to have to rewrite bounds everywhere when those bounds must already hold.) I do take your point that in general we do not propagate bounds, but I think we want to head toward more propagation, as in http://smallcultfollowing.com/babysteps/blog/2014/07/06/implied-bounds/ -- and I thought we'd already started down this road. |
This comment has been minimized.
This comment has been minimized.
|
@japaric It is also my understanding (from conversations with @nikomatsakis) that this should work. It seems related to the generalization of bounds checking. One of the annoyances I started purging from the compiler is the I think this is an important question to resolve generally because like @aturon it was my understanding that any super trait bound should be identical to bounding the In my imagined scheme we should probably collect any Self type constraints and introduce them like we have been with super trait bounds. It seems this would be the simplest strategy, and avoids introducing a breaking change for anyone relying on the current behavior (esp. if this isn't resolved pre-1.0). |
This comment has been minimized.
This comment has been minimized.
|
I think this is a grey area :) At present we only "elaborate" supertraits (meaning |
This comment has been minimized.
This comment has been minimized.
So I was missing a piece of information.
I agree with this. But want to point out that this second example is a "supertrait", whereas the first one is not. So having to explicitly add the
I'm not opposed to having implied bounds, but I would like it to be implemented in a general way (such that my struct bounds example would also compile), instead of making it more ad hoc ("we only propagate bounds on traits with where-clause bounds that have a
I agree!
I think that at this point in time supertraits are ingrained in rustaceans' minds as something natural even if they are arbitrary. In fact, I just realized that it's a (very) special case of implied bounds. Thanks everyone for their explanations! |
kmcallister
added
A-traits
A-typesystem
labels
Jan 11, 2015
aepsil0n
referenced this issue
Feb 17, 2015
Closed
Trait bound on associated type not induced recursively #22446
nikomatsakis
added a commit
to nikomatsakis/rust
that referenced
this issue
Mar 6, 2015
SSheldon
referenced this issue
Apr 20, 2015
Closed
Constraints on supertraits associated types are ignored #24616
SSheldon
referenced this issue
May 16, 2015
Open
Guarantee that pointers to NSObjects can be encoded #2
This comment has been minimized.
This comment has been minimized.
|
Not sure if it is related, but this doesn't work either: use std::ops::*;
trait Vector<S> where
for<'a, 'b> &'a Self: Add<&'b Self>,
for<'a> &'a Self: Mul<S>,
{
fn test(&self) {}
}
fn test<S, V: Vector<S>>(v: V) { v.test() }
This is needed for cgmath to move full to operators, and remove our ugly |
brendanzab
added a commit
to rustgd/cgmath
that referenced
this issue
Sep 30, 2015
brendanzab
added a commit
to rustgd/cgmath
that referenced
this issue
Sep 30, 2015
brendanzab
added a commit
to rustgd/cgmath
that referenced
this issue
Sep 30, 2015
brendanzab
referenced this issue
Sep 30, 2015
Merged
Implement binary operators for points, vectors, quaternions, and matrices #236
This comment has been minimized.
This comment has been minimized.
|
Same "issue" |
nikomatsakis
referenced this issue
Oct 20, 2015
Closed
If `trait Subtrait: Super where Clause { }` then typeck should assume Clause, right? #29143
nikomatsakis
changed the title
where clauses for super traits do not fully propagate
where clauses are only elaborated for supertraits, and not other things
Oct 20, 2015
This comment has been minimized.
This comment has been minimized.
|
I ran into this issue as well, and was really surprised by it. One situation where it would be very useful is refining traits that are used in higher-kinded trait bounds like in this example: https://gist.github.com/anonymous/e3a8fe834bb383504bb4 If we don't propagate associated type bounds for where clause traits, I don't know how to actually put the bound I need in the linked example on the function: the bound itself is polymorphic over the lifetime, which seems to not be expressible in the grammar. So, it seems that without this propagation there are useful bounds that we can't express. |
This comment has been minimized.
This comment has been minimized.
|
@russellmcc what bound are you unable to express precisely? |
This comment has been minimized.
This comment has been minimized.
|
In that example, I want to say that Making the trait bounds propagate for associated types would allow me to express the exact same bound in a convenient way, as in my previous example. |
jld
referenced this issue
Dec 21, 2015
Open
RFC 1214 problems with bounds on associated type of type parameter #30311
This comment has been minimized.
This comment has been minimized.
|
I ran into something like this trying to create an |
This comment has been minimized.
This comment has been minimized.
soltanmm
commented
Dec 24, 2015
|
@jonas-schievink Please read RFC-0195 and re-read my note to see why I believe that's the wrong behavior. In particular, this line:
The The current supertrait-only behavior seems like a cultural momentum phenomenon (or oversight, or what-have-you) rather than something following from the accepted RFCs. |
soltanmm
referenced this issue
Jan 4, 2016
Closed
Elaborate predicates on associated types' associated types (and so on) #30704
This comment has been minimized.
This comment has been minimized.
|
Has there been any more discussion on the lang team about this lately? Is this something that is just missing a proper RFC? |
This comment has been minimized.
This comment has been minimized.
|
@sgrif There hasn't been any discussion, no. This is also (loosely) related to the implied bounds proposal. Rather than starting immediately with an RFC, I'd suggest perhaps opening up a discuss thread with some motivating examples/rationale for any change -- or some other way of drawing a bit more visibility. If we can build some consensus there, then we could proceed to an RFC. |
This comment has been minimized.
This comment has been minimized.
|
@aturon @sgrif I opened a thread about a motivating example some time ago. |
jonas-schievink
referenced this issue
Aug 29, 2016
Closed
Associated type bounds from trait where clauses must be restated #1731
This comment has been minimized.
This comment has been minimized.
chaaz
commented
Sep 3, 2016
|
I ran into the same issue as well: https://users.rust-lang.org/t/creating-alias-for-bounds/7145. As someone relatively new to Rust, It was a bit surprising to see. |
Popog
referenced this issue
Oct 26, 2016
Open
Constraints on associated types declared in subtraits do not propagate. #32722
andersk
referenced this issue
Dec 24, 2016
Closed
Numeric types should probably support "reference overloads" for operators. #94
jonas-schievink
referenced this issue
Jan 26, 2017
Closed
indirect restrictions on Self in trait where clauses are not available when the trait is used as a bound #25409
This was referenced Feb 24, 2017
nikomatsakis
referenced this issue
Feb 28, 2017
Closed
Reduce the number of constraints repeated when writing `T: SomeTrait` #1927
nikomatsakis
referenced this issue
Mar 23, 2017
Closed
Impl search fails in case of generalized trait inheritance #10950
cuviper
added a commit
to cuviper/num
that referenced
this issue
Apr 24, 2017
cuviper
referenced this issue
Apr 24, 2017
Merged
Add new traits for reference and assignment operators #283
nikomatsakis
referenced this issue
May 4, 2017
Open
RFC 1214 warnings with recursive associated traits #29981
cuviper
added a commit
to cuviper/num
that referenced
this issue
May 7, 2017
homu
added a commit
to rust-num/num
that referenced
this issue
May 7, 2017
Mark-Simulacrum
added
the
C-bug
label
Jul 22, 2017
arielb1
referenced this issue
Sep 17, 2017
Closed
Bounds on associated types of super-traits not propagated #44656
cuviper
referenced this issue
Sep 21, 2017
Closed
Improve pow speed for expensive types by multiplying references not cloned values (reopen) #153
cuviper
referenced this issue
Nov 8, 2017
Closed
Scalar trait: common trait for floating and complex numbers #338
shepmaster
referenced this issue
Apr 30, 2018
Open
Higher-ranked trait bounds on associated types are not elaborated #50346
cuviper
referenced this issue
May 16, 2018
Open
rustc: trait-level where clauses are mistakenly imposed when bounding by the trait #50792
This comment has been minimized.
This comment has been minimized.
p4l1ly
commented
Oct 23, 2018
•
|
Hi, if anyone ran into this problem and was interested in a working solution (or rather hack), I'd like to share mine. The solution is quite complicated, but the high-level code does not get polluted by the boilerplate where clauses, but instead only by annotations. My approach is generation of the where clause by use of a proc_macro, which leads to a slightly better-looking code (and raises another problem described -- and partially solved -- in the gist). https://gist.github.com/p4l1ly/6dbcb40fb3bbb450598876b068762939 As proc_macros are not hygienic, you'll also lose some guarantees... |
aturon commentedJan 7, 2015
The following example:
fails with "error: type
&Adoes not implement any method in scope namedfoo".This UFCS variant
fails with "error: the trait
Foo<_>is not implemented for the typeA".