Skip to content

Commit b1c2919

Browse files
unsafe_method and violations_coverage
1 parent c84b99b commit b1c2919

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: |
3838
cargo clippy
3939
cargo fmt --check
40-
cd violations_coverage/format_files
40+
cd violations_coverage/in_crate
4141
cargo fmt --check
4242
4343
- name: Doc (stable, nightly)

src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,21 @@ macro_rules! unsafe_method {
167167
// without upsetting operator precedence.
168168
{
169169
if false {
170-
// We **cannot** take $self by value, in case it's a non-Copy static variable.
171-
let rref = &( $self );
172-
let mref = ::prudent::shared_to_mut(rref);
173-
let mut owned_receiver = ::core::mem::replace(mref, unsafe{ ::core::mem::zeroed() });
174-
let _ = owned_receiver. $fn( $( $arg ),* );
175-
170+
if false {
171+
// This block makes an instance/owned value of the same type as $self. The
172+
// purpose is then to invoke the method inside unsafe {...}, BUT without
173+
// evaluating the given $self expression inside that unsafe {...} block, so that
174+
// we isolate/catch any unsafe code in $self.
175+
//
176+
// We **cannot** move/take/assign $self by value, in case it's a non-Copy
177+
// **static** variable.
178+
let rref = &( $self );
179+
let mref = ::prudent::shared_to_mut(rref);
180+
let mut owned_receiver = ::core::mem::replace(mref, unsafe{ ::core::mem::zeroed() });
181+
let _ = unsafe { owned_receiver. $fn( $( $arg ),* ) };
182+
} else {
183+
$( let _ = $arg; )*
184+
}
176185
unreachable!()
177186
} else {
178187
#[allow(unsafe_code)]

violations_coverage/in_crate/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
prudent = {path = "../..", version="0.0.3-alpha"}

violations_coverage/in_crate/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ Compilation fails - as intended.
22

33
This crate exists to run
44
- `cargo fmt`, so that symlinked-files get formatted.
5-
- `cargo expand`
5+
- `cargo expand`, for example: `cargo expand --bin unsafe_fn_some_args_arg`
66

77
This needs a filesystem that supports symlinks.

violations_coverage/in_crate/src/lib.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

violations_coverage/unsafe_method/zero_args/self.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ use prudent::unsafe_method;
22

33
fn main() {
44
#[allow(unused_unsafe)]
5+
// str::len is actually not unsafe, but that doesn't matter for this example
56
let _ = unsafe_method!(core::str::from_utf8_unchecked(b"hi"), len);
67
}

0 commit comments

Comments
 (0)