Skip to content

Commit

Permalink
make unsafe_op_in_unsafe_fn MachineApplicable and add it to 2024 comp…
Browse files Browse the repository at this point in the history
…atibility
  • Loading branch information
asquared31415 committed Jan 15, 2024
1 parent bfcc027 commit 0608661
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 51 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,11 @@ declare_lint! {
pub UNSAFE_OP_IN_UNSAFE_FN,
Allow,
"unsafe operations in unsafe functions without an explicit unsafe block are deprecated",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::EditionSemanticsChange(Edition::Edition2024),
reference: "issue #71668 <https://github.com/rust-lang/rust/issues/71668>",
explain_reason: false
};
@edition Edition2024 => Warn;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl AddToDiagnostic for UnsafeNotInheritedLintNote {
diag.tool_only_multipart_suggestion(
fluent::mir_build_wrap_suggestion,
vec![(body_start, "{ unsafe ".into()), (body_end, "}".into())],
Applicability::MaybeIncorrect,
Applicability::MachineApplicable,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: call to unsafe function `unsf` is unsafe and requires unsafe block (err
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/edition-2024-unsafe_op_in_unsafe_fn.rs:9:1
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/unsafe/unsafe_op_in_unsafe_fn/edition_2024_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// edition: 2024
// compile-flags: -Zunstable-options
// check-pass

// Tests that `unsafe_op_in_unsafe_fn` is warn-by-default in edition 2024 and that the
// `unused_unsafe` lint does not consider the inner unsafe block to be unused.
#![crate_type = "lib"]
#![deny(unused_unsafe)]

unsafe fn unsf() {}

unsafe fn foo() {
unsf();
//~^ WARN

// no unused_unsafe
unsafe {
unsf();
}
}
17 changes: 17 additions & 0 deletions tests/ui/unsafe/unsafe_op_in_unsafe_fn/edition_2024_default.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
warning: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/edition_2024_default.rs:13:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/edition_2024_default.rs:12:1
|
LL | unsafe fn foo() {
| ^^^^^^^^^^^^^^^
= note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default

warning: 1 warning emitted

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![deny(rust_2024_compatibility)]
#![crate_type = "lib"]

unsafe fn unsf() {}

unsafe fn foo() {
unsf();
//~^ ERROR call to unsafe function `unsf` is unsafe and requires unsafe block
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: call to unsafe function `unsf` is unsafe and requires unsafe block (error E0133)
--> $DIR/in_2024_compatibility.rs:7:5
|
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/in_2024_compatibility.rs:6:1
|
LL | unsafe fn foo() {
| ^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/in_2024_compatibility.rs:1:9
|
LL | #![deny(rust_2024_compatibility)]
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[deny(unsafe_op_in_unsafe_fn)]` implied by `#[deny(rust_2024_compatibility)]`

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: call to unsafe function `unsf` is unsafe and requires unsafe block (error
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:8:1
Expand All @@ -22,6 +23,7 @@ error: dereference of raw pointer is unsafe and requires unsafe block (error E01
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= 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: use of mutable static is unsafe and requires unsafe block (error E0133)
Expand All @@ -30,6 +32,7 @@ error: use of mutable static is unsafe and requires unsafe block (error E0133)
LL | VOID = ();
| ^^^^ use of mutable static
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
Expand All @@ -50,6 +53,7 @@ error: call to unsafe function `unsf` is unsafe and requires unsafe block (error
LL | unsf();
| ^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
--> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:23:1
Expand All @@ -69,6 +73,7 @@ error: dereference of raw pointer is unsafe and requires unsafe block (error E01
LL | *PTR;
| ^^^^ dereference of raw pointer
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= 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: use of mutable static is unsafe and requires unsafe block (error E0133)
Expand All @@ -77,6 +82,7 @@ error: use of mutable static is unsafe and requires unsafe block (error E0133)
LL | VOID = ();
| ^^^^ use of mutable static
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: unnecessary `unsafe` block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,60 @@ unsafe fn unsf() {}
pub unsafe fn foo() { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
//~^ NOTE
//~| NOTE
//~^ NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
//~^ NOTE
//~| NOTE
//~^ NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
}}

pub unsafe fn bar(x: *const i32) -> i32 { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE dereference of raw pointer
//~| NOTE for more information, see issue #71668
//~| NOTE raw pointers may be null
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE dereference of raw pointer
//~| NOTE for more information, see issue #71668
//~| NOTE raw pointers may be null
}}

static mut BAZ: i32 = 0;
pub unsafe fn baz() -> i32 { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE use of mutable static
//~| NOTE for more information, see issue #71668
//~| NOTE mutable statics can be mutated by multiple threads
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE use of mutable static
//~| NOTE for more information, see issue #71668
//~| NOTE mutable statics can be mutated by multiple threads
}}

macro_rules! unsafe_macro { () => (unsf()) }
//~^ ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
//~| NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
//~| ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
//~| NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation

pub unsafe fn unsafe_in_macro() { unsafe {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsafe_macro!();
//~^ NOTE
//~| NOTE
//~^ NOTE in this expansion
//~| NOTE in this expansion
//~| NOTE in this expansion
unsafe_macro!();
//~^ NOTE
//~| NOTE
//~^ NOTE in this expansion
//~| NOTE in this expansion
//~| NOTE in this expansion
}}

pub unsafe fn unsafe_in_external_macro() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,60 @@ unsafe fn unsf() {}
pub unsafe fn foo() {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
//~^ NOTE
//~| NOTE
//~^ NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
unsf(); //~ ERROR call to unsafe function `unsf` is unsafe
//~^ NOTE
//~| NOTE
//~^ NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
}

pub unsafe fn bar(x: *const i32) -> i32 {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = *x; //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE dereference of raw pointer
//~| NOTE for more information, see issue #71668
//~| NOTE raw pointers may be null
y + *x //~ ERROR dereference of raw pointer is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE dereference of raw pointer
//~| NOTE for more information, see issue #71668
//~| NOTE raw pointers may be null
}

static mut BAZ: i32 = 0;
pub unsafe fn baz() -> i32 {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
let y = BAZ; //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE use of mutable static
//~| NOTE for more information, see issue #71668
//~| NOTE mutable statics can be mutated by multiple threads
y + BAZ //~ ERROR use of mutable static is unsafe and requires unsafe block
//~^ NOTE
//~| NOTE
//~^ NOTE use of mutable static
//~| NOTE for more information, see issue #71668
//~| NOTE mutable statics can be mutated by multiple threads
}

macro_rules! unsafe_macro { () => (unsf()) }
//~^ ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
//~| NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation
//~| ERROR call to unsafe function `unsf` is unsafe
//~| NOTE
//~| NOTE
//~| NOTE call to unsafe function
//~| NOTE for more information, see issue #71668
//~| NOTE consult the function's documentation

pub unsafe fn unsafe_in_macro() {
//~^ NOTE an unsafe function restricts its caller, but its body is safe by default
unsafe_macro!();
//~^ NOTE
//~| NOTE
//~^ NOTE in this expansion
//~| NOTE in this expansion
//~| NOTE in this expansion
unsafe_macro!();
//~^ NOTE
//~| NOTE
//~^ NOTE in this expansion
//~| NOTE in this expansion
//~| NOTE in this expansion
}

pub unsafe fn unsafe_in_external_macro() {
Expand Down
Loading

0 comments on commit 0608661

Please sign in to comment.