-
Notifications
You must be signed in to change notification settings - Fork 13.4k
eq_unspanned
is incorrect
#141522
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
Comments
h/t @jameysharp for finding this |
This seems to only be used in tests, clippy, and rustdoc. It should probably be removed tbh. |
This is surprisingly tricky because I've discovered this function would work as intended on some iterators, and I don't know if this is one of them. But here's an example where zip iterates once, where one iterator yields two values but the other only yields one, and yet after zip returns None, both iterators also return None: This happens because the default implementation of next() for zip first calls next on the left iterator, and then on the right, so by the time it discovers that there is no next element in the second iterator it has already consumed an element from the first iterator. But on I don't know which category TokenStream::iter() falls into. But at minimum it seems fragile to me. |
You're right, I misunderstood it. |
The subtle part is only the arguments order: fn cmp(arr1: &[i32], arr2: &[i32]) -> bool {
let mut xi = arr1.into_iter();
let mut yi = arr2.into_iter();
for (x, y) in std::iter::zip(&mut xi, &mut yi) {
if x != y {
return false;
}
}
xi.next().is_none() && yi.next().is_none()
}
fn main() {
let arr1 = [1];
let arr2 = [1, 2];
eprintln!("res1: {:?}", cmp(&arr1, &arr2)); // return `false`
eprintln!("res2: {:?}", cmp(&arr2, &arr1)); // return `true`
} |
@the8472 I feel like I ought to poke you at this point. |
…, r=workingjubilee Fix incorrect eq_unspanned in TokenStream Fixes rust-lang#141522 r? `@workingjubilee` should we remove this function? since it's used in several places, i'd prefer to keep it.
Fix incorrect eq_unspanned in TokenStream Fixes #141522 r? `@workingjubilee` should we remove this function? since it's used in several places, i'd prefer to keep it. try-job: i686-msvc-1
…, r=workingjubilee Fix incorrect eq_unspanned in TokenStream Fixes rust-lang#141522 r? `@workingjubilee` should we remove this function? since it's used in several places, i'd prefer to keep it.
…ngjubilee Fix incorrect eq_unspanned in TokenStream Fixes rust-lang/rust#141522 r? ``@workingjubilee`` should we remove this function? since it's used in several places, i'd prefer to keep it.
Uh oh!
There was an error while loading. Please reload this page.
The following function is incorrect:
rust/compiler/rustc_ast/src/tokenstream.rs
Lines 698 to 707 in 038d599
Zipped iterators with uneven lengths will still consume an item from both sides of the zip on their final iteration (where one will return
None
and one will returnSome
). This allows for both iterators to be exhausted even though their lengths are unequal.The text was updated successfully, but these errors were encountered: