Skip to content

Commit 0d904ce

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40602 - oli-obk:fn_const, r=eddyb
Represent function pointers in mir-constants as a Value instead of Item r? @eddyb
2 parents c1a864d + 187a922 commit 0d904ce

File tree

8 files changed

+39
-48
lines changed

8 files changed

+39
-48
lines changed

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,8 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
875875
func: Operand::Constant(Constant {
876876
span: c.source_info.span,
877877
ty: fty,
878-
literal: Literal::Item {
879-
def_id: free_func,
880-
substs: substs
878+
literal: Literal::Value {
879+
value: ConstVal::Function(free_func, substs),
881880
}
882881
}),
883882
args: vec![Operand::Consume(c.lvalue.clone())],

src/librustc_mir/build/scope.rs

+3-3
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::*;
@@ -783,9 +784,8 @@ fn build_free<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
783784
func: Operand::Constant(Constant {
784785
span: data.span,
785786
ty: tcx.item_type(free_func).subst(tcx, substs),
786-
literal: Literal::Item {
787-
def_id: free_func,
788-
substs: substs
787+
literal: Literal::Value {
788+
value: ConstVal::Function(free_func, substs),
789789
}
790790
}),
791791
args: vec![Operand::Consume(data.value.clone())],

src/librustc_mir/hair/cx/expr.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,8 @@ fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
711711
ty: callee.ty,
712712
span: expr.span,
713713
kind: ExprKind::Literal {
714-
literal: Literal::Item {
715-
def_id: callee.def_id,
716-
substs: callee.substs,
714+
literal: Literal::Value {
715+
value: ConstVal::Function(callee.def_id, callee.substs),
717716
},
718717
},
719718
}
@@ -740,22 +739,32 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
740739
-> ExprKind<'tcx> {
741740
let substs = cx.tables().node_id_item_substs(expr.id)
742741
.unwrap_or_else(|| cx.tcx.intern_substs(&[]));
743-
let def_id = match def {
742+
match def {
744743
// A regular function, constructor function or a constant.
745744
Def::Fn(def_id) |
746745
Def::Method(def_id) |
747746
Def::StructCtor(def_id, CtorKind::Fn) |
748-
Def::VariantCtor(def_id, CtorKind::Fn) |
747+
Def::VariantCtor(def_id, CtorKind::Fn) => ExprKind::Literal {
748+
literal: Literal::Value {
749+
value: ConstVal::Function(def_id, substs),
750+
},
751+
},
752+
749753
Def::Const(def_id) |
750-
Def::AssociatedConst(def_id) => def_id,
754+
Def::AssociatedConst(def_id) => ExprKind::Literal {
755+
literal: Literal::Item {
756+
def_id: def_id,
757+
substs: substs,
758+
},
759+
},
751760

752761
Def::StructCtor(def_id, CtorKind::Const) |
753762
Def::VariantCtor(def_id, CtorKind::Const) => {
754763
match cx.tables().node_id_to_type(expr.id).sty {
755764
// A unit struct/variant which is used as a value.
756765
// We return a completely different ExprKind here to account for this special case.
757766
ty::TyAdt(adt_def, substs) => {
758-
return ExprKind::Adt {
767+
ExprKind::Adt {
759768
adt_def: adt_def,
760769
variant_index: adt_def.variant_index_with_id(def_id),
761770
substs: substs,
@@ -767,17 +776,11 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
767776
}
768777
}
769778

770-
Def::Static(node_id, _) => return ExprKind::StaticRef { id: node_id },
779+
Def::Static(node_id, _) => ExprKind::StaticRef { id: node_id },
771780

772-
Def::Local(..) | Def::Upvar(..) => return convert_var(cx, expr, def),
781+
Def::Local(..) | Def::Upvar(..) => convert_var(cx, expr, def),
773782

774783
_ => span_bug!(expr.span, "def `{:?}` not yet implemented", def),
775-
};
776-
ExprKind::Literal {
777-
literal: Literal::Item {
778-
def_id: def_id,
779-
substs: substs,
780-
},
781784
}
782785
}
783786

src/librustc_mir/hair/cx/mod.rs

+2-3
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/transform/qualify_consts.rs

-5
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

+4-1
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_trans/mir/analyze.rs

+5-2
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

+5-16
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
};
@@ -477,13 +480,6 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
477480
let ty = self.monomorphize(&constant.ty);
478481
match constant.literal.clone() {
479482
mir::Literal::Item { def_id, substs } => {
480-
// Shortcut for zero-sized types, including function item
481-
// types, which would not work with MirConstContext.
482-
if common::type_is_zero_size(self.ccx, ty) {
483-
let llty = type_of::type_of(self.ccx, ty);
484-
return Ok(Const::new(C_null(llty), ty));
485-
}
486-
487483
let substs = self.monomorphize(&substs);
488484
let instance = Instance::new(def_id, substs);
489485
MirConstContext::trans_def(self.ccx, instance, IndexVec::new())
@@ -927,13 +923,6 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
927923
let ty = self.monomorphize(&constant.ty);
928924
let result = match constant.literal.clone() {
929925
mir::Literal::Item { def_id, substs } => {
930-
// Shortcut for zero-sized types, including function item
931-
// types, which would not work with MirConstContext.
932-
if common::type_is_zero_size(bcx.ccx, ty) {
933-
let llty = type_of::type_of(bcx.ccx, ty);
934-
return Const::new(C_null(llty), ty);
935-
}
936-
937926
let substs = self.monomorphize(&substs);
938927
let instance = Instance::new(def_id, substs);
939928
MirConstContext::trans_def(bcx.ccx, instance, IndexVec::new())

0 commit comments

Comments
 (0)