Skip to content

Commit

Permalink
Rollup merge of #119948 - asquared31415:unsafe_op_in_unsafe_fn_fix, r…
Browse files Browse the repository at this point in the history
…=TaKO8Ki

Make `unsafe_op_in_unsafe_fn` migrated in edition 2024

fixes #119823
  • Loading branch information
matthiaskrgr committed Jan 22, 2024
2 parents 8c3c8bb + a8bb418 commit 34bab29
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 51 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
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
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/rfcs/rfc-2396-target_feature-11/safe-calls.stderr
Expand Up @@ -97,6 +97,7 @@ error: call to function `sse2` with `#[target_feature]` is unsafe and requires u
LL | sse2();
| ^^^^^^ call to function with `#[target_feature]`
|
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
note: an unsafe function restricts its caller, but its body is safe by default
Expand Down
1 change: 1 addition & 0 deletions tests/ui/unsafe/edition-2024-unsafe_op_in_unsafe_fn.stderr
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
@@ -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
@@ -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

@@ -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
}
@@ -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

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
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
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

0 comments on commit 34bab29

Please sign in to comment.