Closed liy010 closed 3 years ago
win10 x64 + rustc 1.43 表示代码可以正常运行。 Playground 可以正常运行:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=23310eae264310b79ebff615331a0552
噢? 我抽空看看啥情况
@liy010 @zydxhs 这段代码出了UB
fn main() {
let x = [0, 1, 2, 3];
let y = &x as *const [i32; 4];
unsafe {
assert_eq!(y.read(), [0,1,2,3]);
}
}
这样没问题。
fn main() {
let x = vec![0, 1, 2, 3];
let y = &x as *const Vec<i32>;
// std::mem::forget(x); // 增加这一行可修复
unsafe {
assert_eq!(std::ptr::read(y), [0,1,2,3]); // std::ptr::read(y) 执行完也会执行析构,因为read并没有转移所有权
}
}
上面的代码,编译没问题,但是在运行时发生了 UB:双重释放(double free)。
@liy010 @zydxhs
这个问题需要在第二版里详细描述,暂时在第7次印刷版中增加对此issues的引用
#[derive(Debug)]
struct My;
impl Drop for My {
fn drop(&mut self) {
println!("Dropping!");
}
}
fn main() {
let x = My;
let y = &x as *const My;
// std::mem::forget(x);
unsafe {
let a = std::ptr::read(y);
println!("{:?}", a);
}
}
输出:
My
Dropping!
Dropping!
fn main() { let x = [0, 1, 2, 3]; let y = &x as *const [i32; 4]; unsafe { assert_eq!(y.read(), [0,1,2,3]); } }
这样没问题。
这段代码,win10 x64 + rustc v1.45.2 编译失败:
error[E0606]: casting `&std::vec::Vec<i32>` as `*const [i32; 4]` is invalid
--> src\main.rs:3:13
|
3 | let y = &x as *const [i32; 4];
| ^^^^^^^^^^^^^^^^^^^^^
fn main() { let x = [0, 1, 2, 3]; let y = &x as *const [i32; 4]; unsafe { assert_eq!(y.read(), [0,1,2,3]); } }
这样没问题。
这段代码,win10 x64 + rustc v1.45.2 编译失败:
error[E0606]: casting `&std::vec::Vec<i32>` as `*const [i32; 4]` is invalid --> src\main.rs:3:13 | 3 | let y = &x as *const [i32; 4]; | ^^^^^^^^^^^^^^^^^^^^^
@zydxhs 这个问题你报给 Rust 官方吧,去官方仓库那发个 issues。 估计是windows支持出了啥问题。
fn main() { let x = [0, 1, 2, 3]; let y = &x as *const [i32; 4]; unsafe { assert_eq!(y.read(), [0,1,2,3]); } }
这样没问题。
这段代码,win10 x64 + rustc v1.45.2 编译失败:
error[E0606]: casting `&std::vec::Vec<i32>` as `*const [i32; 4]` is invalid --> src\main.rs:3:13 | 3 | let y = &x as *const [i32; 4]; | ^^^^^^^^^^^^^^^^^^^^^
又验证了一次,编译成功了。估计是增量编译导致的问题。
代码错误
其他代码没问题,这一段执行的时候出现错误
Rust版本
错误信息