Skip to content

Commit 847f007

Browse files
committed
typeck: Introduce reification for fn ptr casts.
1 parent 8f07f8a commit 847f007

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/librustc/middle/ty/cast.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ impl<'tcx> CastTy<'tcx> {
6969
Some(CastTy::Int(IntTy::CEnum)),
7070
ty::TyRawPtr(ref mt) => Some(CastTy::Ptr(mt)),
7171
ty::TyRef(_, ref mt) => Some(CastTy::RPtr(mt)),
72-
// FIXME: Treating TyFnDef as a pointer here is a bit dubious;
73-
// we should be coercing the operand to an actual pointer.
74-
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(CastTy::FnPtr),
72+
ty::TyFnPtr(..) => Some(CastTy::FnPtr),
7573
_ => None,
7674
}
7775
}

src/librustc_typeck/check/cast.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ impl<'tcx> CastCheck<'tcx> {
235235
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty),
236236
CastTy::from_ty(self.cast_ty)) {
237237
(Some(t_from), Some(t_cast)) => (t_from, t_cast),
238+
// Function item types may need to be reified before casts.
239+
(None, Some(t_cast)) => {
240+
if let ty::TyFnDef(_, _, f) = self.expr_ty.sty {
241+
// Attempt a coercion to a fn pointer type.
242+
let res = coercion::mk_assignty(fcx, &self.expr,
243+
self.expr_ty, fcx.tcx().mk_ty(ty::TyFnPtr(f)));
244+
if !res.is_ok() {
245+
return Err(CastError::NonScalar);
246+
}
247+
(FnPtr, t_cast)
248+
} else {
249+
return Err(CastError::NonScalar);
250+
}
251+
}
238252
_ => {
239253
return Err(CastError::NonScalar)
240254
}

0 commit comments

Comments
 (0)