Skip to content

new VecDeque::splice() method in nightly produce incorrect result #151758

@pkerichang

Description

@pkerichang

When I run the following code on the latest nightly channel, it prints incorrect result:

#![feature(deque_extend_front)]

use std::collections::VecDeque;
use itertools::join;


struct Point([i32; 2]);

fn main() {
    let mut vec_2 = VecDeque::with_capacity(10);
    vec_2.push_back(Point([0, 6]));
    vec_2.push_back(Point([4, 6]));
    vec_2.push_back(Point([4, 2]));
    vec_2.push_back(Point([2, 2]));
    vec_2.push_back(Point([2, 6]));
    vec_2.push_back(Point([0, 6]));

    let mut vec_3 = VecDeque::with_capacity(10);
    vec_3.push_back(Point([8, 6]));
    vec_3.push_back(Point([8, 4]));
    vec_3.push_back(Point([6, 4]));
    vec_3.push_back(Point([6, 6]));

    let mut vec = VecDeque::with_capacity(10);
    vec.push_front(Point([0, 0]));
    vec.push_back(Point([10, 0]));

    let mut points_str = join(
        vec.iter().map(|val| format!("({}, {})", val.0[0], val.0[1])),
            ", ",
    );
    println!("{}", points_str);

    vec.prepend(vec_2.drain(..));

    points_str = join(
        vec.iter().map(|val| format!("({}, {})", val.0[0], val.0[1])),
            ", ",
    );
    println!("{}", points_str);

    vec.splice(1..1, vec_3.drain(..));

    points_str = join(
        vec.iter().map(|val| format!("({}, {})", val.0[0], val.0[1])),
            ", ",
    );
    println!("{}", points_str);
}

I would expect the output of this simple program to be:

(0, 0), (10, 0)
(0, 6), (4, 6), (4, 2), (2, 2), (2, 6), (0, 6), (0, 0), (10, 0)
(0, 6), (8, 6), (8, 4), (6, 4), (6, 6), (4, 6), (4, 2), (2, 2), (2, 6), (0, 0), (10, 0)

but instead the following got print:

(0, 0), (10, 0)
(0, 6), (4, 6), (4, 2), (2, 2), (2, 6), (0, 6), (0, 0), (10, 0)
(0, 6), (8, 6), (8, 4), (6, 4), (6, 6), (4, 6), (4, 2), (2, 2), (2, 6), (0, 0), (0, 0), (0, 0)

As you can see, the last element got remvoed, and two extra elements got inserted. In my actual application (not in this simple example), the extra elements contain random values instead of just zeros.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-libsRelevant to the library team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions