有什么区别[T; N]和U如果U总是设置为[T; N]?

oli*_*obk 5 generics undefined-behavior rust

我试图实现IntoIterator[T; N].我用Defaultswap(PlayPen)写了一个完全安全的版本.然后,我把它移植使用uninitialized,ptr::copy,Dropforget(围栏).我的Iterator结构如下所示:

struct IntoIter<T> {
    inner: Option<[T; N]>,
    i: usize,
}
impl<T> Iterator for IntoIter<T> { ... }
Run Code Online (Sandbox Code Playgroud)

由于我不想为每个值创建一个Iterator结构N,我将结构更改为

struct IntoIter<U> {
    inner: Option<U>,
    i: usize,
}
impl<T> Iterator for IntoIter<[T; N]> { ... }
Run Code Online (Sandbox Code Playgroud)

显然我必须调整IteratorDrop实现(PlayPen).

但现在我以某种方式介绍了未定义的行为.根据printlns,优化水平或黄道标志发生或不发生恐慌.

thread '<main>' panicked at 'index out of bounds: the len is 5 but the index is 139924442675478', <anon>:25
thread '<main>' panicked at 'index out of bounds: the len is 5 but the index is 140451355506257', <anon>:25
application terminated abnormally with signal 4 (Illegal instruction)
Run Code Online (Sandbox Code Playgroud)

我的第二个实现已经展示了未定义的行为,或者第二个和第三个实现之间存在差异.看看生成的(未优化的)LLVM-IR,我发现唯一的根本差异发生在第三个版本中,最终会出现[[Box<i32>; 5]; 5]类型.我可以看到我可能会意外地创建这样的类型,但我特意检查了第三个版本是否存在这样的错误并且无法找到它.

She*_*ter 2

我相信您遇到了一些错误#[unsafe_destructor]。我将你的代码简化为:

#![feature(unsafe_destructor)]

struct IntoIter<U> {
    inner: Option<U>,
}

impl<T> Iterator for IntoIter<[T; 8]> {
    type Item = T;
    fn next(&mut self) -> Option<T> { None }
}

#[unsafe_destructor]
impl<T> Drop for IntoIter<[T; 8]> {
    fn drop(&mut self) {
        // destroy the remaining elements
        for _ in self.by_ref() {}

        unsafe { std::intrinsics::forget(self.inner.take()) }
    }
}

fn main() {
    let arr = [1; 8];
    IntoIter { inner: Some(arr) };
}
Run Code Online (Sandbox Code Playgroud)

然后我编译( rustc -g unsafe.rs)并在rust-lldb. 我在 drop 实现上设置了一个断点并打印出来self

(lldb) p self
(unsafe::IntoIter<[[i32; 8]; 8]> *) $0 = &0x7fff5fbff568
Run Code Online (Sandbox Code Playgroud)

您可以看到它认为类型参数是数组的数组,就像您注意到的那样。此时,如果我们真的放弃了,我们就会浪费内存。我相信 Rust 仍然会将内存归零,因此我们可能会在任意内存块上写入零。

为了更好地衡量:

rustc --verbose --version
rustc 1.0.0-dev (cfea8ec41 2015-03-10) (built 2015-03-10)
binary: rustc
commit-hash: cfea8ec41699e25c8fb524d625190f0cb860dc71
commit-date: 2015-03-10
build-date: 2015-03-10
host: x86_64-apple-darwin
release: 1.0.0-dev
Run Code Online (Sandbox Code Playgroud)