implement RFC 1192 inclusive ranges#30884
Conversation
|
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
f2cb048 to
e4ea932
Compare
|
There's actually an rfc for that: rust-lang/rfcs#1320 The current proposed syntax is actually: pub enum RangeInclusive<T> {
Empty {
at: T,
},
NonEmpty {
start: T,
end: T,
}
}This way, we don't loose any information or throw anything away (see the discussion on the RFC). It also means we should also be able to implement |
|
@Stebalien oh I completely missed that RFC! I will add that field and impl later tonight. |
|
Fixed tidy issues. The things @Stebalien brought up will need to wait until tomorrow. But those only affect the libcore part, not the rest of the PR. What's the policy on accepting RFC amendments? |
|
The failing test is the one that ensures HIR node IDs are reproducible. For debugging I removed some custom Debug impls in favor of #[derive]s which will print the ID. I isolated the difference (full output here): But I don't have time to investigate right now. |
Edit: I'm wrong |
|
Do you mean in the HIR? I did not remove ExprRange from the AST.
|
|
Oh gah yes, sorry about that, my mistake! |
|
So just to be clear, pretty printing of the AST is not affected, but pretty
|
|
Regarding the ID caching stuff, I need help from @nrc or someone else familiar with this code. I believe it was only working accidentally before, but I'm not sure. "Fixing" the bug I think I found causes an ICE elsewhere. Notably, in Here is a trace of some debugging output I added, with manual indentation to show nested This is from the test that lowers a for loop twice to check the HIR equivalence. You can see that the first call to Okay, but I tried to fix this, by adding this line, but uncommenting that causes an ICE somewhere else (backtrace): Wat do? |
f7b0624 to
7f40006
Compare
|
@Stebalien I added |
7f40006 to
dfb4293
Compare
There was a problem hiding this comment.
It should be possible to impl From<RangeTo> for this.
There was a problem hiding this comment.
Agreed, should be pretty much the same.
On Sat, Jan 16, 2016 at 3:41 PM, Steven Allen notifications@github.com
wrote:
In src/libcore/ops.rs
#30884 (comment):+impl<Idx: PartialOrd + One + Sub<Output=Idx>> From<Range> for RangeInclusive {
- fn from(range: Range) -> RangeInclusive {
use self::RangeInclusive::*;
if range.start < range.end {NonEmpty { start: range.start, end: range.end - Idx::one() }} else {Empty { at: range.start } // FIXME range.start or range.end?}- }
+}
+/// An inclusive range which is only bounded above.
+#[derive(Copy, Clone, PartialEq, Eq)]
+#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
+pub struct RangeToInclusive {It should be possible to impl From for this.
—
Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust/pull/30884/files#r49936902.
|
@durka, looks good. However, I don't know if we want to implement |
|
I note that the extant ops::Range implements Iterator directly, but there is also an impl for |
|
@Stebalien what do you think about underflow on the conversions? For example what should |
|
@durka hm. Yeah, we probably shouldn't implement |
|
@Stebalien I suppose so. Just to be clear, the intention for the For |
|
☔ The latest upstream changes (presumably #30567) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@durka Hi, I'm not sure off the top of my head what to do. TBH, I'm not sure we need to keep all that stuff, but I need to think about that. Fixing it would be nice. Let me get back to you a bit later... |
dfb4293 to
96b15c5
Compare
|
Rebased. |
96b15c5 to
4c3ab86
Compare
|
First, I would expect the conversion should convert the endpoints to keep the same meaning. As for negative/empty ranges, if end <= start {
RangeInclusive::Empty { at: start }
} else {
// Can't underflow as `end > start >= MIN`
RangeInclusive::NonEmpty {
start: start,
end: end - 1,
} |
|
Another question (foreshadowed by a FIXME buried in my code). Are we settled on |
1473d16 to
54cb2d9
Compare
|
Hi, more apologies: been away much of this week due to family catching flu. I looked at the underflow commit and left a comment. Once that's been addressed, I think this is good to go! |
|
@aturon should be good to go. |
|
📌 Commit 430b3e1 has been approved by |
|
⌛ Testing commit 430b3e1 with merge deb9109... |
|
💔 Test failed - auto-win-msvc-32-opt |
|
Looks like it just timed out? |
|
@bors: retry |
This PR implements [RFC 1192](https://github.com/rust-lang/rfcs/blob/master/text/1192-inclusive-ranges.md), which is triple-dot syntax for inclusive range expressions. The new stuff is behind two feature gates (one for the syntax and one for the std::ops types). This replaces the deprecated functionality in std::iter. Along the way I simplified the desugaring for all ranges. This is my first contribution to rust which changes more than one character outside of a test or comment, so please review carefully! Some of the individual commit messages have more of my notes. Also thanks for putting up with my dumb questions in #rust-internals. - For implementing `std::ops::RangeInclusive`, I took @Stebalien's suggestion from rust-lang/rfcs#1192 (comment). It seemed to me to make the implementation easier and increase type safety. If that stands, the RFC should be amended to avoid confusion. - I also kind of like @glaebhoerl's [idea](rust-lang/rfcs#1254 (comment)), which is unified inclusive/exclusive range syntax something like `x>..=y`. We can experiment with this while everything is behind a feature gate. - There are a couple of FIXMEs left (see the last commit). I didn't know what to do about `RangeArgument` and I haven't added `Index` impls yet. Those should be discussed/finished before merging. cc @gankro since you [complained](https://www.reddit.com/r/rust/comments/3xkfro/what_happened_to_inclusive_ranges/cy5j0yq) cc #27777 #30877 #1192 rust-lang/rfcs#1254 relevant to #28237 (tracking issue)
|
⌛ Testing commit 430b3e1 with merge 8484831... |
This variant became unused in rust-lang#30884.
…r=alexcrichton Remove TypeOrigin::RangeExpression This variant became unused in rust-lang#30884.
This PR implements RFC 1192, which is triple-dot syntax for inclusive range expressions. The new stuff is behind two feature gates (one for the syntax and one for the std::ops types). This replaces the deprecated functionality in std::iter. Along the way I simplified the desugaring for all ranges.
This is my first contribution to rust which changes more than one character outside of a test or comment, so please review carefully! Some of the individual commit messages have more of my notes. Also thanks for putting up with my dumb questions in #rust-internals.
Notes
std::ops::RangeInclusive, I took @Stebalien's suggestion from RFC for inclusive ranges with ... rfcs#1192 (comment). It seemed to me to make the implementation easier and increase type safety. If that stands, the RFC should be amended to avoid confusion.x>..=y. We can experiment with this while everything is behind a feature gate.RangeArgumentand I haven't addedIndeximpls yet. Those should be discussed/finished before merging.cc @gankro since you complained
cc #27777 #30877 #1192 rust-lang/rfcs#1254
relevant to #28237 (tracking issue)