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 c471b9b

Browse files
author
Lukas Markeffsky
committedJan 22, 2024
borrowck
1 parent f3de343 commit c471b9b

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed
 

‎compiler/rustc_borrowck/src/type_check/mod.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
2020
use rustc_infer::infer::region_constraints::RegionConstraintData;
2121
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2222
use rustc_infer::infer::{
23-
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
23+
BoundRegion, BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, NllRegionVariableOrigin,
2424
};
2525
use rustc_middle::mir::tcx::PlaceTy;
2626
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
@@ -2294,7 +2294,34 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22942294
let cast_ty_from = CastTy::from_ty(ty_from);
22952295
let cast_ty_to = CastTy::from_ty(*ty);
22962296
match (cast_ty_from, cast_ty_to) {
2297-
(Some(CastTy::Ptr(_)), Some(CastTy::Ptr(_))) => (),
2297+
(Some(CastTy::Ptr(from_pointee)), Some(CastTy::Ptr(to_pointee))) => {
2298+
if let ty::Dynamic(to_preds, _, ty::Dyn) = *to_pointee.ty.kind()
2299+
&& let Some(to_principal) = to_preds.principal()
2300+
&& let ty::Dynamic(from_preds, _, ty::Dyn) =
2301+
*from_pointee.ty.kind()
2302+
&& let Some(from_principal) = from_preds.principal()
2303+
{
2304+
let param_env = self.param_env;
2305+
let op = CustomTypeOp::new(
2306+
|ocx| {
2307+
let cause = ObligationCause::dummy_with_span(span);
2308+
let infer_ok = ocx.infcx.at(&cause, param_env).eq(
2309+
DefineOpaqueTypes::No,
2310+
from_principal,
2311+
to_principal,
2312+
)?;
2313+
ocx.register_infer_ok_obligations(infer_ok);
2314+
Ok(())
2315+
},
2316+
"equate PtrToPtr dyn trait principal",
2317+
);
2318+
let _: Result<(), ErrorGuaranteed> = self.fully_perform_op(
2319+
location.to_locations(),
2320+
ConstraintCategory::Cast { unsize_to: None },
2321+
op,
2322+
);
2323+
}
2324+
}
22982325
_ => {
22992326
span_mirbug!(
23002327
self,

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