Skip to content

Commit

Permalink
Temporarily switch invalid_reference_casting lint to allow-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jul 13, 2023
1 parent fa15df6 commit f25ad54
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare_lint! {
/// ### Example
///
/// ```rust,compile_fail
/// # #![deny(invalid_reference_casting)]
/// fn x(r: &i32) {
/// unsafe {
/// *(r as *const i32 as *mut i32) += 1;
Expand All @@ -29,7 +30,7 @@ declare_lint! {
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
/// mutable.
INVALID_REFERENCE_CASTING,
Deny,
Allow,
"casts of `&T` to `&mut T` without interior mutability"
}

Expand Down
1 change: 0 additions & 1 deletion tests/ui/const-generics/issues/issue-100313.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ impl <const B: &'static bool> T<B> {
unsafe {
*(B as *const bool as *mut bool) = false;
//~^ ERROR evaluation of constant value failed [E0080]
//~| ERROR casting `&T` to `&mut T` is undefined behavior
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions tests/ui/const-generics/issues/issue-100313.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/issue-100313.rs:10:13
|
LL | *(B as *const bool as *mut bool) = false;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[deny(invalid_reference_casting)]` on by default

error[E0080]: evaluation of constant value failed
--> $DIR/issue-100313.rs:10:13
|
Expand All @@ -18,11 +10,11 @@ note: inside `T::<&true>::set_false`
LL | *(B as *const bool as *mut bool) = false;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `_`
--> $DIR/issue-100313.rs:19:5
--> $DIR/issue-100313.rs:18:5
|
LL | x.set_false();
| ^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// check-fail

#![feature(ptr_from_ref)]
#![deny(invalid_reference_casting)]

extern "C" {
// N.B., mutability can be easily incorrect in FFI calls -- as
Expand Down
26 changes: 15 additions & 11 deletions tests/ui/lint/reference_casting.stderr
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:19:9
--> $DIR/reference_casting.rs:20:9
|
LL | (*(a as *const _ as *mut String)).push_str(" world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[deny(invalid_reference_casting)]` on by default
note: the lint level is defined here
--> $DIR/reference_casting.rs:4:9
|
LL | #![deny(invalid_reference_casting)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:21:9
--> $DIR/reference_casting.rs:22:9
|
LL | *(a as *const _ as *mut _) = String::from("Replaced");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:23:9
--> $DIR/reference_casting.rs:24:9
|
LL | *(a as *const _ as *mut String) += " world";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:25:25
--> $DIR/reference_casting.rs:26:25
|
LL | let _num = &mut *(num as *const i32 as *mut i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:27:25
--> $DIR/reference_casting.rs:28:25
|
LL | let _num = &mut *(num as *const i32).cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:29:20
--> $DIR/reference_casting.rs:30:20
|
LL | let _num = *{ num as *const i32 }.cast_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:31:9
--> $DIR/reference_casting.rs:32:9
|
LL | *std::ptr::from_ref(num).cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:33:9
--> $DIR/reference_casting.rs:34:9
|
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:35:9
--> $DIR/reference_casting.rs:36:9
|
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
--> $DIR/reference_casting.rs:37:9
--> $DIR/reference_casting.rs:38:9
|
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit f25ad54

Please sign in to comment.