Skip to content

Commit 55add43

Browse files
committed
Modified apply_adjustments, add unsafety coercion
1 parent bb4ab24 commit 55add43

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
184184
let cast = bx.cx().layout_of(self.monomorphize(&mir_cast_ty));
185185

186186
let val = match *kind {
187-
mir::CastKind::Pointer(
188-
PointerCast::ReifyFnPointer,
189-
) => match *operand.layout.ty.kind() {
190-
ty::FnDef(def_id, substs) => {
191-
if bx.cx().tcx().has_attr(def_id, sym::rustc_args_required_const) {
192-
bug!("reifying a fn ptr that requires const arguments");
187+
mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
188+
match *operand.layout.ty.kind() {
189+
ty::FnDef(def_id, substs) => {
190+
if bx.cx().tcx().has_attr(def_id, sym::rustc_args_required_const) {
191+
bug!("reifying a fn ptr that requires const arguments");
192+
}
193+
let instance = ty::Instance::resolve_for_fn_ptr(
194+
bx.tcx(),
195+
ty::ParamEnv::reveal_all(),
196+
def_id,
197+
substs,
198+
)
199+
.unwrap()
200+
.polymorphize(bx.cx().tcx());
201+
OperandValue::Immediate(bx.get_fn_addr(instance))
193202
}
194-
let instance = ty::Instance::resolve_for_fn_ptr(
195-
bx.tcx(),
196-
ty::ParamEnv::reveal_all(),
197-
def_id,
198-
substs,
199-
)
200-
.unwrap()
201-
.polymorphize(bx.cx().tcx());
202-
OperandValue::Immediate(bx.get_fn_addr(instance))
203+
_ => bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
203204
}
204-
_ => bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
205-
},
205+
}
206206
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
207207
match *operand.layout.ty.kind() {
208208
ty::Closure(def_id, substs) => {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,7 @@ impl<'tcx> TyS<'tcx> {
20812081
FnPtr(f) => *f,
20822082
Error(_) => {
20832083
// ignore errors (#54954)
2084+
20842085
ty::Binder::dummy(FnSig::fake())
20852086
}
20862087
Closure(..) => bug!(

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ use rustc_middle::ty::adjustment::PointerCast;
2626
use rustc_middle::ty::cast::CastTy;
2727
use rustc_middle::ty::fold::TypeFoldable;
2828
use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef, UserSubsts};
29-
use rustc_middle::ty::{
30-
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPredicate, Ty,
31-
TyCtxt, UserType, UserTypeAnnotationIndex, WithConstness,
32-
};
29+
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex, WithConstness, Binder};
3330
use rustc_span::{Span, DUMMY_SP};
3431
use rustc_target::abi::VariantIdx;
3532
use rustc_trait_selection::infer::InferCtxtExt as _;
@@ -2051,7 +2048,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20512048
// function definition. When we extract the
20522049
// signature, it comes from the `fn_sig` query,
20532050
// and hence may contain unnormalized results.
2054-
let fn_sig = self.normalize(fn_sig, location);
2051+
let mut fn_sig = self.normalize(fn_sig, location);
2052+
2053+
if let ty::FnPtr(pol_sig) = ty.kind() {
2054+
let mut sig = fn_sig.skip_binder();
2055+
sig.constness = pol_sig.constness();
2056+
sig.unsafety = pol_sig.unsafety();
2057+
fn_sig = Binder::bind(sig);
2058+
}
20552059

20562060
let ty_fn_ptr_from = tcx.mk_fn_ptr(fn_sig);
20572061

compiler/rustc_typeck/src/check/coercion.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
994994
new_sig = self.tcx.signature_not_const_fn(prev_sig);
995995
new_drop_const = true;
996996
}
997+
if prev_sig.unsafety() != new_sig.unsafety() {
998+
prev_sig = prev_sig.map_bound(|sig| ty::FnSig { unsafety: hir::Unsafety::Unsafe, ..sig });
999+
new_sig = new_sig.map_bound(|sig| ty::FnSig { unsafety: hir::Unsafety::Unsafe, ..sig });=
1000+
}
9971001
}
9981002
(Some(prev_sig), Some(new_sig))
9991003
}
@@ -1043,12 +1047,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10431047
ty::Closure(..) => {
10441048
Some(Adjust::Pointer(PointerCast::ClosureFnPointer(a_sig.unsafety())))
10451049
}
1046-
ty::FnDef(..) => {
1047-
Some(Adjust::Pointer(PointerCast::ReifyFnPointer))
1048-
}
1050+
ty::FnDef(..) => Some(Adjust::Pointer(PointerCast::ReifyFnPointer)),
10491051
ty::FnPtr(..) => {
10501052
if prev_drop_const {
1051-
Some(Adjust::Pointer(PointerCast::NotConstFnPointer))
1053+
if a_sig.unsafety() != b_sig.unsafety() {
1054+
Some(Adjust::Pointer(PointerCast::UnsafeNotConstFnPointer))
1055+
} else {
1056+
Some(Adjust::Pointer(PointerCast::NotConstFnPointer))
1057+
}
10521058
} else {
10531059
None
10541060
}
@@ -1059,12 +1065,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10591065
ty::Closure(..) => {
10601066
Some(Adjust::Pointer(PointerCast::ClosureFnPointer(b_sig.unsafety())))
10611067
}
1062-
ty::FnDef(..) => {
1063-
Some(Adjust::Pointer(PointerCast::ReifyFnPointer))
1064-
}
1068+
ty::FnDef(..) => Some(Adjust::Pointer(PointerCast::ReifyFnPointer)),
10651069
ty::FnPtr(..) => {
10661070
if new_drop_const {
1067-
Some(Adjust::Pointer(PointerCast::NotConstFnPointer))
1071+
if a_sig.unsafety() != b_sig.unsafety() {
1072+
Some(Adjust::Pointer(PointerCast::UnsafeNotConstFnPointer))
1073+
} else {
1074+
Some(Adjust::Pointer(PointerCast::NotConstFnPointer))
1075+
}
10681076
} else {
10691077
None
10701078
}
@@ -1075,7 +1083,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10751083
for expr in exprs.iter().map(|e| e.as_coercion_site()) {
10761084
self.apply_adjustments(
10771085
expr,
1078-
vec![Adjustment { kind: prev_adjustment.clone().unwrap().clone(), target: fn_ptr }],
1086+
vec![Adjustment {
1087+
kind: prev_adjustment.clone().unwrap().clone(),
1088+
target: fn_ptr,
1089+
}],
10791090
);
10801091
}
10811092
}

0 commit comments

Comments
 (0)