-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Make sure type inference with a..b
as good as range(a,b)
#21806
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
6020ff0
to
d132a1a
Compare
d132a1a
to
5d681c9
Compare
I am curious what happens with a negative range eg. |
It should work as expected since after type inference, the numeric fallback will kick in and |
Does this still allow |
return Some(result); | ||
} | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<A: Int> ExactSizeIterator for ::ops::Range<A> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately with the definition as-is I don't think that we can have this ExactSizeIterator
impl here. As defined it's possible for size_hint
to return (0, None)
which goes against the contract of ExactSizeIterator
.
While we don't have negative bounds, perhaps the ExactSizeIterator
trait could still just be manually defined for each primitive type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, the hybrid implementations for range work!
Hmm, good question. I think so offhand, as long as |
my local compilation reports this:
|
The new `::ops::Range` has separated implementations for each of the numeric types, while the old `::iter::Range` has one for type `Int`. However, we do not take output bindings into account when selecting traits. So it confuses `typeck` and makes the new range does not work as good as the old one when it comes to type inference. This patch implements `Iterator` for the new range for one type `Int`. This limitation could be lifted, however, if we ever reconsider the output types' role in type inference. Closes rust-lang#21595 Closes rust-lang#21649 Closes rust-lang#21672
Thanks @emanueLczirai, the fallout has been fixed. |
5d681c9
to
b9c055c
Compare
Compiles successfully for me now. Thanks! (and numeric fallback is as you said earlier: i32) One question: is that commented FIXME that you removed no longer necessary there? as in, is it fixed or no longer makes sense due to this PR? |
It is simply not there any more. I doubt it has anything to do with this PR though. |
The new `::ops::Range` has separated implementations for each of the numeric types, while the old `::iter::Range` has one for type `Int`. However, we do not take output bindings into account when selecting traits. So it confuses `typeck` and makes the new range does not work as good as the old one when it comes to type inference. This patch implements `Iterator` for the new range for one type `Int`. This limitation could be lifted, however, if we ever reconsider the output types' role in type inference. Closes #21595 Closes #21649 Closes #21672
The new
::ops::Range
has separated implementations for each of thenumeric types, while the old
::iter::Range
has one for typeInt
.However, we do not take output bindings into account when selecting
traits. So it confuses
typeck
and makes the new range does not work asgood as the old one when it comes to type inference.
This patch implements
Iterator
for the new range for one typeInt
.This limitation could be lifted, however, if we ever reconsider the
output types' role in type inference.
Closes #21595
Closes #21649
Closes #21672