Skip to content

Commit

Permalink
borrowck
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Jan 22, 2024
1 parent f3de343 commit c471b9b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
31 changes: 29 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::region_constraints::RegionConstraintData;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
BoundRegion, BoundRegionConversionTime, DefineOpaqueTypes, InferCtxt, NllRegionVariableOrigin,
};
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
Expand Down Expand Up @@ -2294,7 +2294,34 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let cast_ty_from = CastTy::from_ty(ty_from);
let cast_ty_to = CastTy::from_ty(*ty);
match (cast_ty_from, cast_ty_to) {
(Some(CastTy::Ptr(_)), Some(CastTy::Ptr(_))) => (),
(Some(CastTy::Ptr(from_pointee)), Some(CastTy::Ptr(to_pointee))) => {
if let ty::Dynamic(to_preds, _, ty::Dyn) = *to_pointee.ty.kind()
&& let Some(to_principal) = to_preds.principal()
&& let ty::Dynamic(from_preds, _, ty::Dyn) =
*from_pointee.ty.kind()
&& let Some(from_principal) = from_preds.principal()
{
let param_env = self.param_env;
let op = CustomTypeOp::new(
|ocx| {
let cause = ObligationCause::dummy_with_span(span);
let infer_ok = ocx.infcx.at(&cause, param_env).eq(
DefineOpaqueTypes::No,
from_principal,
to_principal,
)?;
ocx.register_infer_ok_obligations(infer_ok);
Ok(())
},
"equate PtrToPtr dyn trait principal",
);
let _: Result<(), ErrorGuaranteed> = self.fully_perform_op(
location.to_locations(),
ConstraintCategory::Cast { unsize_to: None },
op,
);
}
}
_ => {
span_mirbug!(
self,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/cast/ptr-to-trait-obj-different-regions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// check-pass
// check-fail
//
// issue: <https://github.com/rust-lang/rust/issues/120217>

Expand All @@ -9,7 +9,7 @@ trait Static<'a> {
}

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

impl Static<'static> for () {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/cast/ptr-to-trait-obj-different-regions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: lifetime may not live long enough
--> $DIR/ptr-to-trait-obj-different-regions.rs:12:5
|
LL | fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
| -- lifetime `'a` defined here
LL | x as _
| ^^^^^^ returning this value requires that `'a` must outlive `'static`

error: aborting due to 1 previous error

0 comments on commit c471b9b

Please sign in to comment.