Skip to content

Commit b0d9afb

Browse files
committed
Represent function pointers in mir-constants as a Value instead of Item
1 parent 366386c commit b0d9afb

File tree

10 files changed

+55
-46
lines changed

10 files changed

+55
-46
lines changed

src/librustc/mir/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -983,16 +983,16 @@ impl<'tcx> Debug for Operand<'tcx> {
983983
}
984984

985985
impl<'tcx> Operand<'tcx> {
986-
pub fn item<'a>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
987-
def_id: DefId,
988-
substs: &'tcx Substs<'tcx>,
989-
span: Span)
990-
-> Self
991-
{
986+
pub fn function_handle<'a>(
987+
tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
988+
def_id: DefId,
989+
substs: &'tcx Substs<'tcx>,
990+
span: Span,
991+
) -> Self {
992992
Operand::Constant(Constant {
993993
span: span,
994994
ty: tcx.item_type(def_id).subst(tcx, substs),
995-
literal: Literal::Item { def_id, substs }
995+
literal: Literal::Value { value: ConstVal::Function(def_id, substs) },
996996
})
997997
}
998998

src/librustc_mir/build/scope.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ should go to.
8989
use build::{BlockAnd, BlockAndExtension, Builder, CFG};
9090
use rustc::middle::region::{CodeExtent, CodeExtentData};
9191
use rustc::middle::lang_items;
92+
use rustc::middle::const_val::ConstVal;
9293
use rustc::ty::subst::{Kind, Subst};
9394
use rustc::ty::{Ty, TyCtxt};
9495
use rustc::mir::*;
@@ -784,9 +785,8 @@ fn build_free<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
784785
func: Operand::Constant(Constant {
785786
span: data.span,
786787
ty: tcx.item_type(free_func).subst(tcx, substs),
787-
literal: Literal::Item {
788-
def_id: free_func,
789-
substs: substs
788+
literal: Literal::Value {
789+
value: ConstVal::Function(free_func, substs),
790790
}
791791
}),
792792
args: vec![Operand::Consume(data.value.clone())],

src/librustc_mir/hair/cx/expr.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,8 @@ fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
714714
ty: callee.ty,
715715
span: expr.span,
716716
kind: ExprKind::Literal {
717-
literal: Literal::Item {
718-
def_id: callee.def_id,
719-
substs: callee.substs,
717+
literal: Literal::Value {
718+
value: ConstVal::Function(callee.def_id, callee.substs),
720719
},
721720
},
722721
}
@@ -743,22 +742,32 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
743742
-> ExprKind<'tcx> {
744743
let substs = cx.tables().node_id_item_substs(expr.id)
745744
.unwrap_or_else(|| cx.tcx.intern_substs(&[]));
746-
let def_id = match def {
745+
match def {
747746
// A regular function, constructor function or a constant.
748747
Def::Fn(def_id) |
749748
Def::Method(def_id) |
750749
Def::StructCtor(def_id, CtorKind::Fn) |
751-
Def::VariantCtor(def_id, CtorKind::Fn) |
750+
Def::VariantCtor(def_id, CtorKind::Fn) => ExprKind::Literal {
751+
literal: Literal::Value {
752+
value: ConstVal::Function(def_id, substs),
753+
},
754+
},
755+
752756
Def::Const(def_id) |
753-
Def::AssociatedConst(def_id) => def_id,
757+
Def::AssociatedConst(def_id) => ExprKind::Literal {
758+
literal: Literal::Item {
759+
def_id: def_id,
760+
substs: substs,
761+
},
762+
},
754763

755764
Def::StructCtor(def_id, CtorKind::Const) |
756765
Def::VariantCtor(def_id, CtorKind::Const) => {
757766
match cx.tables().node_id_to_type(expr.id).sty {
758767
// A unit struct/variant which is used as a value.
759768
// We return a completely different ExprKind here to account for this special case.
760769
ty::TyAdt(adt_def, substs) => {
761-
return ExprKind::Adt {
770+
ExprKind::Adt {
762771
adt_def: adt_def,
763772
variant_index: adt_def.variant_index_with_id(def_id),
764773
substs: substs,
@@ -770,17 +779,11 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
770779
}
771780
}
772781

773-
Def::Static(node_id, _) => return ExprKind::StaticRef { id: node_id },
782+
Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
774783

775-
Def::Local(..) | Def::Upvar(..) => return convert_var(cx, expr, def),
784+
Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
776785

777786
_ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
778-
};
779-
ExprKind::Literal {
780-
literal: Literal::Item {
781-
def_id: def_id,
782-
substs: substs,
783-
},
784787
}
785788
}
786789

src/librustc_mir/hair/cx/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
132132
let method_ty = self.tcx.item_type(item.def_id);
133133
let method_ty = method_ty.subst(self.tcx, substs);
134134
return (method_ty,
135-
Literal::Item {
136-
def_id: item.def_id,
137-
substs: substs,
135+
Literal::Value {
136+
value: ConstVal::Function(item.def_id, substs),
138137
});
139138
}
140139
}

src/librustc_mir/shim.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc::hir;
1212
use rustc::hir::def_id::DefId;
1313
use rustc::infer;
1414
use rustc::middle::region::ROOT_CODE_EXTENT;
15+
use rustc::middle::const_val::ConstVal;
1516
use rustc::mir::*;
1617
use rustc::mir::transform::MirSource;
1718
use rustc::ty::{self, Ty};
@@ -335,7 +336,9 @@ fn build_call_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
335336
Operand::Constant(Constant {
336337
span: span,
337338
ty: tcx.item_type(def_id).subst(tcx, param_env.free_substs),
338-
literal: Literal::Item { def_id, substs: param_env.free_substs },
339+
literal: Literal::Value {
340+
value: ConstVal::Function(def_id, param_env.free_substs),
341+
},
339342
}),
340343
vec![rcvr]
341344
)

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
568568
});
569569
}
570570
Operand::Constant(ref constant) => {
571-
// Only functions and methods can have these types.
572-
if let ty::TyFnDef(..) = constant.ty.sty {
573-
return;
574-
}
575-
576571
if let Literal::Item { def_id, substs } = constant.literal {
577572
// Don't peek inside generic (associated) constants.
578573
if substs.types().next().is_some() {

src/librustc_mir/transform/type_check.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::infer::{self, InferCtxt, InferOk};
1515
use rustc::traits::{self, Reveal};
1616
use rustc::ty::fold::TypeFoldable;
1717
use rustc::ty::{self, Ty, TyCtxt, TypeVariants};
18+
use rustc::middle::const_val::ConstVal;
1819
use rustc::mir::*;
1920
use rustc::mir::tcx::LvalueTy;
2021
use rustc::mir::transform::{MirPass, MirSource, Pass};
@@ -526,7 +527,9 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
526527
fn is_box_free(&self, operand: &Operand<'tcx>) -> bool {
527528
match operand {
528529
&Operand::Constant(Constant {
529-
literal: Literal::Item { def_id, .. }, ..
530+
literal: Literal::Value {
531+
value: ConstVal::Function(def_id, _), ..
532+
}, ..
530533
}) => {
531534
Some(def_id) == self.tcx().lang_items.box_free_fn()
532535
}

src/librustc_mir/util/elaborate_drops.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
525525
}],
526526
terminator: Some(Terminator {
527527
kind: TerminatorKind::Call {
528-
func: Operand::item(tcx, drop_fn.def_id, substs,
529-
self.source_info.span),
528+
func: Operand::function_handle(tcx, drop_fn.def_id, substs,
529+
self.source_info.span),
530530
args: vec![Operand::Consume(Lvalue::Local(ref_lvalue))],
531531
destination: Some((unit_temp, succ)),
532532
cleanup: unwind,
@@ -629,7 +629,7 @@ impl<'l, 'b, 'tcx, D> DropCtxt<'l, 'b, 'tcx, D>
629629
let substs = tcx.mk_substs(iter::once(Kind::from(ty)));
630630

631631
let call = TerminatorKind::Call {
632-
func: Operand::item(tcx, free_func, substs, self.source_info.span),
632+
func: Operand::function_handle(tcx, free_func, substs, self.source_info.span),
633633
args: vec![Operand::Consume(self.lvalue.clone())],
634634
destination: Some((unit_temp, target)),
635635
cleanup: None

src/librustc_trans/mir/analyze.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
1414
use rustc_data_structures::bitvec::BitVector;
1515
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
16-
use rustc::mir::{self, Location, TerminatorKind};
16+
use rustc::middle::const_val::ConstVal;
17+
use rustc::mir::{self, Location, TerminatorKind, Literal};
1718
use rustc::mir::visit::{Visitor, LvalueContext};
1819
use rustc::mir::traversal;
1920
use common;
@@ -109,7 +110,9 @@ impl<'mir, 'a, 'tcx> Visitor<'tcx> for LocalAnalyzer<'mir, 'a, 'tcx> {
109110
match *kind {
110111
mir::TerminatorKind::Call {
111112
func: mir::Operand::Constant(mir::Constant {
112-
literal: mir::Literal::Item { def_id, .. }, ..
113+
literal: Literal::Value {
114+
value: ConstVal::Function(def_id, _), ..
115+
}, ..
113116
}),
114117
ref args, ..
115118
} if Some(def_id) == self.cx.ccx.tcx().lang_items.box_free_fn() => {

src/librustc_trans/mir/constant.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ impl<'tcx> Const<'tcx> {
101101
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
102102
ConstVal::ByteStr(ref v) => consts::addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
103103
ConstVal::Struct(_) | ConstVal::Tuple(_) |
104-
ConstVal::Array(..) | ConstVal::Repeat(..) |
104+
ConstVal::Array(..) | ConstVal::Repeat(..) => {
105+
bug!("MIR must not use `{:?}` (aggregates are expanded to MIR rvalues)", cv)
106+
}
105107
ConstVal::Function(..) => {
106-
bug!("MIR must not use `{:?}` (which refers to a local ID)", cv)
108+
let llty = type_of::type_of(ccx, ty);
109+
return Const::new(C_null(llty), ty);
107110
}
108111
ConstVal::Char(c) => C_integral(Type::char(ccx), c as u64, false),
109112
};
@@ -476,8 +479,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
476479
let ty = self.monomorphize(&constant.ty);
477480
match constant.literal.clone() {
478481
mir::Literal::Item { def_id, substs } => {
479-
// Shortcut for zero-sized types, including function item
480-
// types, which would not work with MirConstContext.
482+
// Shortcut for zero-sized types
483+
// which would not work with MirConstContext.
481484
if common::type_is_zero_size(self.ccx, ty) {
482485
let llty = type_of::type_of(self.ccx, ty);
483486
return Ok(Const::new(C_null(llty), ty));
@@ -924,8 +927,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
924927
let ty = self.monomorphize(&constant.ty);
925928
let result = match constant.literal.clone() {
926929
mir::Literal::Item { def_id, substs } => {
927-
// Shortcut for zero-sized types, including function item
928-
// types, which would not work with MirConstContext.
930+
// Shortcut for zero-sized types
931+
// which would not work with MirConstContext.
929932
if common::type_is_zero_size(bcx.ccx, ty) {
930933
let llty = type_of::type_of(bcx.ccx, ty);
931934
return Const::new(C_null(llty), ty);

0 commit comments

Comments
 (0)