diff options
| author | Ralf Jung <post@ralfj.de> | 2018-07-10 10:52:05 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2018-07-10 11:01:44 +0200 |
| commit | f511c5ea4ab2209b83dbab200f2bdac37351d2d2 (patch) | |
| tree | a058ff09f1e94e06af6f95bb7ca4099778040e3f /src/test/ui | |
| parent | c30acc7187fe5ab9ca8a34411bb89bfe241a0662 (diff) | |
improve error message shown for unsafe operations: explain why undefined behavior could arise
Inspired by @gnzlbg at https://github.com/rust-lang/rust/issues/46043#issuecomment-381544673
Diffstat (limited to 'src/test/ui')
| -rw-r--r-- | src/test/ui/error-codes/E0133.stderr | 4 | ||||
| -rw-r--r-- | src/test/ui/issue-27060.rs | 43 | ||||
| -rw-r--r-- | src/test/ui/issue-27060.stderr | 27 | ||||
| -rw-r--r-- | src/test/ui/issue-28776.stderr | 4 | ||||
| -rw-r--r-- | src/test/ui/trait-safety-fn-body.stderr | 4 | ||||
| -rw-r--r-- | src/test/ui/unsafe-const-fn.stderr | 4 |
6 files changed, 82 insertions, 4 deletions
diff --git a/src/test/ui/error-codes/E0133.stderr b/src/test/ui/error-codes/E0133.stderr index dc57a627444..9be80f8f21b 100644 --- a/src/test/ui/error-codes/E0133.stderr +++ b/src/test/ui/error-codes/E0133.stderr @@ -1,8 +1,10 @@ -error[E0133]: call to unsafe function requires unsafe function or block +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block --> $DIR/E0133.rs:14:5 | LL | f(); | ^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior error: aborting due to previous error diff --git a/src/test/ui/issue-27060.rs b/src/test/ui/issue-27060.rs new file mode 100644 index 00000000000..f88c2137e77 --- /dev/null +++ b/src/test/ui/issue-27060.rs @@ -0,0 +1,43 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[repr(packed)] +pub struct Good { + data: &'static u32, + data2: [&'static u32; 2], + aligned: [u8; 32], +} + +#[repr(packed)] +pub struct JustArray { + array: [u32] +} + +#[deny(safe_packed_borrows)] +fn main() { + let good = Good { + data: &0, + data2: [&0, &0], + aligned: [0; 32] + }; + + unsafe { + let _ = &good.data; // ok + let _ = &good.data2[0]; // ok + } + + let _ = &good.data; //~ ERROR borrow of packed field is unsafe + //~| hard error + let _ = &good.data2[0]; //~ ERROR borrow of packed field is unsafe + //~| hard error + let _ = &*good.data; // ok, behind a pointer + let _ = &good.aligned; // ok, has align 1 + let _ = &good.aligned[2]; // ok, has align 1 +} diff --git a/src/test/ui/issue-27060.stderr b/src/test/ui/issue-27060.stderr new file mode 100644 index 00000000000..bd01f75d8fb --- /dev/null +++ b/src/test/ui/issue-27060.stderr @@ -0,0 +1,27 @@ +error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) + --> $DIR/issue-27060.rs:36:13 + | +LL | let _ = &good.data; //~ ERROR borrow of packed field is unsafe + | ^^^^^^^^^^ + | +note: lint level defined here + --> $DIR/issue-27060.rs:23:8 + | +LL | #[deny(safe_packed_borrows)] + | ^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043> + = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior + +error: borrow of packed field is unsafe and requires unsafe function or block (error E0133) + --> $DIR/issue-27060.rs:38:13 + | +LL | let _ = &good.data2[0]; //~ ERROR borrow of packed field is unsafe + | ^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043> + = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/issue-28776.stderr b/src/test/ui/issue-28776.stderr index 3b468a88205..aef0d9cd1d8 100644 --- a/src/test/ui/issue-28776.stderr +++ b/src/test/ui/issue-28776.stderr @@ -1,8 +1,10 @@ -error[E0133]: call to unsafe function requires unsafe function or block +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block --> $DIR/issue-28776.rs:14:5 | LL | (&ptr::write)(1 as *mut _, 42); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior error: aborting due to previous error diff --git a/src/test/ui/trait-safety-fn-body.stderr b/src/test/ui/trait-safety-fn-body.stderr index 432df438222..0b7b6e61678 100644 --- a/src/test/ui/trait-safety-fn-body.stderr +++ b/src/test/ui/trait-safety-fn-body.stderr @@ -1,8 +1,10 @@ -error[E0133]: dereference of raw pointer requires unsafe function or block +error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block --> $DIR/trait-safety-fn-body.rs:21:9 | LL | *self += 1; | ^^^^^^^^^^ dereference of raw pointer + | + = note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior error: aborting due to previous error diff --git a/src/test/ui/unsafe-const-fn.stderr b/src/test/ui/unsafe-const-fn.stderr index 270b90ec3fc..d4b3ed687e5 100644 --- a/src/test/ui/unsafe-const-fn.stderr +++ b/src/test/ui/unsafe-const-fn.stderr @@ -1,8 +1,10 @@ -error[E0133]: call to unsafe function requires unsafe function or block +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block --> $DIR/unsafe-const-fn.rs:19:18 | LL | const VAL: u32 = dummy(0xFFFF); | ^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior error: aborting due to previous error |
