rust/tests/ui/cast/ptr-to-ptr-different-regions.rs
Nilstrieb b6657a8ad4 Never consider raw pointer casts to be trival
HIR typeck tries to figure out which casts are trivial by doing them as
coercions and seeing whether this works. Since HIR typeck is oblivious
of lifetimes, this doesn't work for pointer casts that only change the
lifetime of the pointee, which are, as borrowck will tell you, not
trivial.

This change makes it so that raw pointer casts are never considered
trivial.

This also incidentally fixes the "trivial cast" lint false positive on
the same code. Unfortunately, "trivial cast" lints are now never emitted
on raw pointer casts, even if they truly are trivial. This could be
fixed by also doing the lint in borrowck for raw pointers specifically.
2023-10-25 23:15:18 +02:00

25 lines
605 B
Rust

// check-pass
// https://github.com/rust-lang/rust/issues/113257
#![deny(trivial_casts)] // The casts here are not trivial.
struct Foo<'a> { a: &'a () }
fn extend_lifetime_very_very_safely<'a>(v: *const Foo<'a>) -> *const Foo<'static> {
// This should pass because raw pointer casts can do anything they want.
v as *const Foo<'static>
}
trait Trait {}
fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) {
ptr as _
}
fn main() {
let unit = ();
let foo = Foo { a: &unit };
let _long: *const Foo<'static> = extend_lifetime_very_very_safely(&foo);
}