Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c3cf17

Browse files
committedJan 22, 2024
Check lifetimes for some pointer casts
Specifically check them for trait objects, i.e. `*const Trait + 'a` -> `*const Trait + b`, etc.
1 parent f3de343 commit 3c3cf17

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed
 

‎compiler/rustc_hir_typeck/src/cast.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,39 @@ impl<'a, 'tcx> CastCheck<'tcx> {
611611
} else {
612612
match self.try_coercion_cast(fcx) {
613613
Ok(()) => {
614-
if self.expr_ty.is_unsafe_ptr() && self.cast_ty.is_unsafe_ptr() {
615-
// When casting a raw pointer to another raw pointer, we cannot convert the cast into
616-
// a coercion because the pointee types might only differ in regions, which HIR typeck
617-
// cannot distinguish. This would cause us to erroneously discard a cast which will
618-
// lead to a borrowck error like #113257.
619-
// We still did a coercion above to unify inference variables for `ptr as _` casts.
620-
// This does cause us to miss some trivial casts in the trival cast lint.
621-
debug!(" -> PointerCast");
614+
if let ty::RawPtr(src_pointee) = self.expr_ty.kind()
615+
&& let ty::RawPtr(tgt_pointee) = self.cast_ty.kind()
616+
{
617+
if let Ok(Some(src_kind)) = fcx.pointer_kind(src_pointee.ty, self.expr_span)
618+
&& let Ok(Some(tgt_kind)) =
619+
fcx.pointer_kind(tgt_pointee.ty, self.cast_span)
620+
{
621+
match (src_kind, tgt_kind) {
622+
// When casting a raw pointer to another raw pointer, we cannot convert the cast into
623+
// a coercion because the pointee types might only differ in regions, which HIR typeck
624+
// cannot distinguish. This would cause us to erroneously discard a cast which will
625+
// lead to a borrowck error like #113257.
626+
// We still did a coercion above to unify inference variables for `ptr as _` casts.
627+
// This does cause us to miss some trivial casts in the trivial cast lint.
628+
(PointerKind::Thin, PointerKind::Thin)
629+
| (PointerKind::Length, PointerKind::Length) => {
630+
debug!(" -> PointerCast");
631+
}
632+
633+
// If we are not casting pointers to sized types or slice-ish DSTs
634+
// (handled above), we need to make a coercion cast. This prevents
635+
// casts like `*const dyn Trait<'a> -> *const dyn Trait<'b>` which
636+
// are unsound.
637+
//
638+
// See <https://github.com/rust-lang/rust/issues/120217>
639+
(_, _) => {
640+
debug!(" -> CoercionCast");
641+
fcx.typeck_results
642+
.borrow_mut()
643+
.set_coercion_cast(self.expr.hir_id.local_id);
644+
}
645+
}
646+
}
622647
} else {
623648
self.trivial_cast_lint(fcx);
624649
debug!(" -> CoercionCast");

‎library/std/src/thread/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ use crate::ffi::{CStr, CString};
164164
use crate::fmt;
165165
use crate::io;
166166
use crate::marker::PhantomData;
167+
use crate::mem::transmute;
167168
use crate::mem::{self, forget};
168169
use crate::num::NonZeroU64;
169170
use crate::num::NonZeroUsize;
@@ -545,10 +546,11 @@ impl Builder {
545546
scope_data.increment_num_running_threads();
546547
}
547548

548-
let main = Box::new(main);
549+
let main: Box<dyn FnOnce() + '_> = Box::new(main);
549550
// SAFETY: dynamic size and alignment of the Box remain the same. See below for why the
550551
// lifetime change is justified.
551-
let main = unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + 'static)) };
552+
let main =
553+
unsafe { transmute::<Box<dyn FnOnce() + '_>, Box<dyn FnOnce() + 'static>>(main) };
552554

553555
Ok(JoinInner {
554556
// SAFETY:

‎tests/ui/cast/ptr-to-ptr-different-regions.rs

-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ fn extend_lifetime_very_very_safely<'a>(v: *const Foo<'a>) -> *const Foo<'static
1111
v as *const Foo<'static>
1212
}
1313

14-
trait Trait {}
15-
16-
fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) {
17-
ptr as _
18-
}
19-
2014
fn main() {
2115
let unit = ();
2216
let foo = Foo { a: &unit };

‎tests/ui/cast/ptr-to-trait-obj-different-regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// check-pass
1+
// check-fail
22
//
33
// issue: <https://github.com/rust-lang/rust/issues/120217>
44

@@ -9,7 +9,7 @@ trait Static<'a> {
99
}
1010

1111
fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
12-
x as _
12+
x as _ //~ error: lifetime may not live long enough
1313
}
1414

1515
impl Static<'static> for () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/ptr-to-trait-obj-different-regions.rs:12:5
3+
|
4+
LL | fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
5+
| -- lifetime `'a` defined here
6+
LL | x as _
7+
| ^^^^^^ returning this value requires that `'a` must outlive `'static`
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)
Failed to load comments.