2018-08-22 15:56:37 +02:00
|
|
|
use rustc::hir::def_id::DefId;
|
|
|
|
use rustc::hir;
|
|
|
|
use rustc::mir::*;
|
|
|
|
use rustc::ty::{self, Predicate, TyCtxt};
|
2018-12-01 14:09:30 +01:00
|
|
|
use rustc_target::spec::abi;
|
2018-08-22 15:56:37 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use syntax_pos::Span;
|
|
|
|
|
2018-08-31 11:47:45 +02:00
|
|
|
type McfResult = Result<(), (Span, Cow<'static, str>)>;
|
2018-08-22 15:56:37 +02:00
|
|
|
|
|
|
|
pub fn is_min_const_fn(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
) -> McfResult {
|
|
|
|
let mut current = def_id;
|
|
|
|
loop {
|
|
|
|
let predicates = tcx.predicates_of(current);
|
2018-09-16 20:15:49 +03:00
|
|
|
for (predicate, _) in &predicates.predicates {
|
2018-08-22 15:56:37 +02:00
|
|
|
match predicate {
|
|
|
|
| Predicate::RegionOutlives(_)
|
|
|
|
| Predicate::TypeOutlives(_)
|
|
|
|
| Predicate::WellFormed(_)
|
|
|
|
| Predicate::ConstEvaluatable(..) => continue,
|
|
|
|
| Predicate::ObjectSafe(_) => {
|
|
|
|
bug!("object safe predicate on function: {:#?}", predicate)
|
|
|
|
}
|
|
|
|
Predicate::ClosureKind(..) => {
|
|
|
|
bug!("closure kind predicate on function: {:#?}", predicate)
|
|
|
|
}
|
|
|
|
Predicate::Subtype(_) => bug!("subtype predicate on function: {:#?}", predicate),
|
|
|
|
Predicate::Projection(_) => {
|
|
|
|
let span = tcx.def_span(current);
|
|
|
|
// we'll hit a `Predicate::Trait` later which will report an error
|
|
|
|
tcx.sess
|
|
|
|
.delay_span_bug(span, "projection without trait bound");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Predicate::Trait(pred) => {
|
|
|
|
if Some(pred.def_id()) == tcx.lang_items().sized_trait() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match pred.skip_binder().self_ty().sty {
|
|
|
|
ty::Param(ref p) => {
|
|
|
|
let generics = tcx.generics_of(current);
|
|
|
|
let def = generics.type_param(p, tcx);
|
|
|
|
let span = tcx.def_span(def.def_id);
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"trait bounds other than `Sized` \
|
|
|
|
on const fn parameters are unstable"
|
|
|
|
.into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
// other kinds of bounds are either tautologies
|
|
|
|
// or cause errors in other passes
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match predicates.parent {
|
|
|
|
Some(parent) => current = parent,
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for local in &mir.local_decls {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_ty(tcx, local.ty, local.source_info.span)?;
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
// impl trait is gone in MIR, so check the return type manually
|
2018-08-31 11:47:45 +02:00
|
|
|
check_ty(
|
2018-08-22 15:56:37 +02:00
|
|
|
tcx,
|
|
|
|
tcx.fn_sig(def_id).output().skip_binder(),
|
|
|
|
mir.local_decls.iter().next().unwrap().source_info.span,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
for bb in mir.basic_blocks() {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_terminator(tcx, mir, bb.terminator())?;
|
2018-08-22 15:56:37 +02:00
|
|
|
for stmt in &bb.statements {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_statement(tcx, mir, stmt)?;
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:47:45 +02:00
|
|
|
Ok(())
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_ty(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: ty::Ty<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> McfResult {
|
|
|
|
for ty in ty.walk() {
|
|
|
|
match ty.sty {
|
|
|
|
ty::Ref(_, _, hir::Mutability::MutMutable) => return Err((
|
|
|
|
span,
|
|
|
|
"mutable references in const fn are unstable".into(),
|
|
|
|
)),
|
2018-08-25 11:53:26 -06:00
|
|
|
ty::Opaque(..) => return Err((span, "`impl Trait` in const fn is unstable".into())),
|
2018-08-22 15:56:37 +02:00
|
|
|
ty::FnPtr(..) => {
|
|
|
|
return Err((span, "function pointers in const fn are unstable".into()))
|
|
|
|
}
|
|
|
|
ty::Dynamic(preds, _) => {
|
|
|
|
for pred in preds.iter() {
|
|
|
|
match pred.skip_binder() {
|
|
|
|
| ty::ExistentialPredicate::AutoTrait(_)
|
|
|
|
| ty::ExistentialPredicate::Projection(_) => {
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"trait bounds other than `Sized` \
|
|
|
|
on const fn parameters are unstable"
|
|
|
|
.into(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
ty::ExistentialPredicate::Trait(trait_ref) => {
|
|
|
|
if Some(trait_ref.def_id) != tcx.lang_items().sized_trait() {
|
|
|
|
return Err((
|
|
|
|
span,
|
|
|
|
"trait bounds other than `Sized` \
|
|
|
|
on const fn parameters are unstable"
|
|
|
|
.into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:47:45 +02:00
|
|
|
Ok(())
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_rvalue(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
rvalue: &Rvalue<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> McfResult {
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::Repeat(operand, _) | Rvalue::Use(operand) => {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_operand(tcx, mir, operand, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
Rvalue::Len(place) | Rvalue::Discriminant(place) | Rvalue::Ref(_, _, place) => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, place, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
2018-10-13 23:38:31 +02:00
|
|
|
Rvalue::Cast(CastKind::Misc, operand, cast_ty) => {
|
2018-08-22 15:56:37 +02:00
|
|
|
use rustc::ty::cast::CastTy;
|
|
|
|
let cast_in = CastTy::from_ty(operand.ty(mir, tcx)).expect("bad input type for cast");
|
|
|
|
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
|
|
|
|
match (cast_in, cast_out) {
|
|
|
|
(CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) => Err((
|
|
|
|
span,
|
|
|
|
"casting pointers to ints is unstable in const fn".into(),
|
|
|
|
)),
|
|
|
|
(CastTy::RPtr(_), CastTy::Float) => bug!(),
|
|
|
|
(CastTy::RPtr(_), CastTy::Int(_)) => bug!(),
|
|
|
|
(CastTy::Ptr(_), CastTy::RPtr(_)) => bug!(),
|
2018-08-31 11:47:45 +02:00
|
|
|
_ => check_operand(tcx, mir, operand, span),
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 19:58:21 +02:00
|
|
|
Rvalue::Cast(CastKind::UnsafeFnPointer, _, _) |
|
2018-10-17 09:04:10 +02:00
|
|
|
Rvalue::Cast(CastKind::ClosureFnPointer, _, _) |
|
2018-10-15 19:58:21 +02:00
|
|
|
Rvalue::Cast(CastKind::ReifyFnPointer, _, _) => Err((
|
2018-10-13 23:38:31 +02:00
|
|
|
span,
|
2018-10-15 19:58:21 +02:00
|
|
|
"function pointer casts are not allowed in const fn".into(),
|
|
|
|
)),
|
|
|
|
Rvalue::Cast(CastKind::Unsize, _, _) => Err((
|
|
|
|
span,
|
|
|
|
"unsizing casts are not allowed in const fn".into(),
|
2018-10-13 23:38:31 +02:00
|
|
|
)),
|
2018-08-22 15:56:37 +02:00
|
|
|
// binops are fine on integers
|
|
|
|
Rvalue::BinaryOp(_, lhs, rhs) | Rvalue::CheckedBinaryOp(_, lhs, rhs) => {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_operand(tcx, mir, lhs, span)?;
|
|
|
|
check_operand(tcx, mir, rhs, span)?;
|
2018-08-22 15:56:37 +02:00
|
|
|
let ty = lhs.ty(mir, tcx);
|
|
|
|
if ty.is_integral() || ty.is_bool() || ty.is_char() {
|
2018-08-31 11:47:45 +02:00
|
|
|
Ok(())
|
2018-08-22 15:56:37 +02:00
|
|
|
} else {
|
|
|
|
Err((
|
|
|
|
span,
|
|
|
|
"only int, `bool` and `char` operations are stable in const fn".into(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 23:38:31 +02:00
|
|
|
Rvalue::NullaryOp(NullOp::SizeOf, _) => Ok(()),
|
|
|
|
Rvalue::NullaryOp(NullOp::Box, _) => Err((
|
|
|
|
span,
|
|
|
|
"heap allocations are not allowed in const fn".into(),
|
|
|
|
)),
|
2018-08-22 15:56:37 +02:00
|
|
|
Rvalue::UnaryOp(_, operand) => {
|
|
|
|
let ty = operand.ty(mir, tcx);
|
|
|
|
if ty.is_integral() || ty.is_bool() {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_operand(tcx, mir, operand, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
} else {
|
|
|
|
Err((
|
|
|
|
span,
|
|
|
|
"only int and `bool` operations are stable in const fn".into(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Rvalue::Aggregate(_, operands) => {
|
|
|
|
for operand in operands {
|
2018-08-31 11:47:45 +02:00
|
|
|
check_operand(tcx, mir, operand, span)?;
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
2018-08-31 11:47:45 +02:00
|
|
|
Ok(())
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_statement(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
) -> McfResult {
|
|
|
|
let span = statement.source_info.span;
|
|
|
|
match &statement.kind {
|
|
|
|
StatementKind::Assign(place, rval) => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, place, span)?;
|
2018-08-31 11:47:45 +02:00
|
|
|
check_rvalue(tcx, mir, rval, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 20:05:22 +01:00
|
|
|
StatementKind::FakeRead(_, place) => check_place(tcx, mir, place, span),
|
2018-08-22 15:56:37 +02:00
|
|
|
|
|
|
|
// just an assignment
|
2018-08-31 11:47:45 +02:00
|
|
|
StatementKind::SetDiscriminant { .. } => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
|
|
|
|
| StatementKind::InlineAsm { .. } => {
|
|
|
|
Err((span, "cannot use inline assembly in const fn".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are all NOPs
|
|
|
|
| StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
2018-10-24 11:47:17 +02:00
|
|
|
| StatementKind::Retag { .. }
|
2018-09-04 16:32:34 -04:00
|
|
|
| StatementKind::AscribeUserType(..)
|
2018-08-31 11:47:45 +02:00
|
|
|
| StatementKind::Nop => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_operand(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
operand: &Operand<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> McfResult {
|
|
|
|
match operand {
|
|
|
|
Operand::Move(place) | Operand::Copy(place) => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, place, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
2018-08-31 11:47:45 +02:00
|
|
|
Operand::Constant(_) => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_place(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
place: &Place<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> McfResult {
|
|
|
|
match place {
|
2018-12-28 20:05:22 +01:00
|
|
|
Place::Local(_) => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
// promoteds are always fine, they are essentially constants
|
2018-08-31 11:47:45 +02:00
|
|
|
Place::Promoted(_) => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
Place::Static(_) => Err((span, "cannot access `static` items in const fn".into())),
|
|
|
|
Place::Projection(proj) => {
|
|
|
|
match proj.elem {
|
2019-01-09 11:32:56 +01:00
|
|
|
| ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. }
|
2018-08-22 15:56:37 +02:00
|
|
|
| ProjectionElem::Deref | ProjectionElem::Field(..) | ProjectionElem::Index(_) => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, &proj.base, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
| ProjectionElem::Downcast(..) => {
|
|
|
|
Err((span, "`match` or `if let` in `const fn` is unstable".into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_terminator(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
|
|
|
terminator: &Terminator<'tcx>,
|
|
|
|
) -> McfResult {
|
|
|
|
let span = terminator.source_info.span;
|
|
|
|
match &terminator.kind {
|
|
|
|
| TerminatorKind::Goto { .. }
|
|
|
|
| TerminatorKind::Return
|
2018-08-31 11:47:45 +02:00
|
|
|
| TerminatorKind::Resume => Ok(()),
|
2018-08-22 15:56:37 +02:00
|
|
|
|
|
|
|
TerminatorKind::Drop { location, .. } => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, location, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { location, value, .. } => {
|
2018-12-28 20:05:22 +01:00
|
|
|
check_place(tcx, mir, location, span)?;
|
2018-08-31 11:47:45 +02:00
|
|
|
check_operand(tcx, mir, value, span)
|
2018-08-22 15:56:37 +02:00
|
|
|
},
|
2018-10-27 13:34:35 +02:00
|
|
|
|
|
|
|
TerminatorKind::FalseEdges { .. } | TerminatorKind::SwitchInt { .. } => Err((
|
2018-08-22 15:56:37 +02:00
|
|
|
span,
|
|
|
|
"`if`, `match`, `&&` and `||` are not stable in const fn".into(),
|
|
|
|
)),
|
|
|
|
| TerminatorKind::Abort | TerminatorKind::Unreachable => {
|
|
|
|
Err((span, "const fn with unreachable code is not stable".into()))
|
|
|
|
}
|
|
|
|
| TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => {
|
|
|
|
Err((span, "const fn generators are unstable".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::Call {
|
|
|
|
func,
|
|
|
|
args,
|
2018-09-29 10:34:12 +01:00
|
|
|
from_hir_call: _,
|
2018-08-22 15:56:37 +02:00
|
|
|
destination: _,
|
|
|
|
cleanup: _,
|
|
|
|
} => {
|
|
|
|
let fn_ty = func.ty(mir, tcx);
|
|
|
|
if let ty::FnDef(def_id, _) = fn_ty.sty {
|
|
|
|
|
2018-12-01 14:09:30 +01:00
|
|
|
// some intrinsics are waved through if called inside the
|
|
|
|
// standard library. Users never need to call them directly
|
|
|
|
match tcx.fn_sig(def_id).abi() {
|
2018-12-24 18:53:28 +01:00
|
|
|
abi::Abi::RustIntrinsic => if !is_intrinsic_whitelisted(tcx, def_id) {
|
|
|
|
return Err((
|
2018-12-01 14:09:30 +01:00
|
|
|
span,
|
|
|
|
"can only call a curated list of intrinsics in `min_const_fn`".into(),
|
2018-12-24 18:53:28 +01:00
|
|
|
))
|
2018-12-01 14:09:30 +01:00
|
|
|
},
|
|
|
|
abi::Abi::Rust if tcx.is_min_const_fn(def_id) => {},
|
|
|
|
abi::Abi::Rust => return Err((
|
2018-08-22 15:56:37 +02:00
|
|
|
span,
|
|
|
|
"can only call other `min_const_fn` within a `min_const_fn`".into(),
|
2018-12-01 14:09:30 +01:00
|
|
|
)),
|
|
|
|
abi => return Err((
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"cannot call functions with `{}` abi in `min_const_fn`",
|
|
|
|
abi,
|
|
|
|
).into(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
|
|
|
|
check_operand(tcx, mir, func, span)?;
|
|
|
|
|
|
|
|
for arg in args {
|
|
|
|
check_operand(tcx, mir, arg, span)?;
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
2018-12-01 14:09:30 +01:00
|
|
|
Ok(())
|
2018-08-22 15:56:37 +02:00
|
|
|
} else {
|
|
|
|
Err((span, "can only call other const fns within const fn".into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::Assert {
|
|
|
|
cond,
|
|
|
|
expected: _,
|
|
|
|
msg: _,
|
|
|
|
target: _,
|
|
|
|
cleanup: _,
|
2018-08-31 11:47:45 +02:00
|
|
|
} => check_operand(tcx, mir, cond, span),
|
2018-08-22 15:56:37 +02:00
|
|
|
|
2018-11-19 10:40:09 +01:00
|
|
|
TerminatorKind::FalseUnwind { .. } => {
|
|
|
|
Err((span, "loops are not allowed in const fn".into()))
|
|
|
|
},
|
2018-08-22 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-24 18:53:28 +01:00
|
|
|
|
2018-12-29 12:58:17 +01:00
|
|
|
/// Returns true if the `def_id` refers to an intrisic which we've whitelisted
|
|
|
|
/// for being called from stable `const fn`s (`min_const_fn`).
|
2018-12-24 18:53:28 +01:00
|
|
|
///
|
|
|
|
/// Adding more intrinsics requires sign-off from @rust-lang/lang.
|
|
|
|
fn is_intrinsic_whitelisted(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
|
|
|
|
match &tcx.item_name(def_id).as_str()[..] {
|
|
|
|
| "size_of"
|
|
|
|
| "min_align_of"
|
|
|
|
| "needs_drop"
|
|
|
|
// Arithmetic:
|
2018-12-24 19:24:48 +01:00
|
|
|
| "overflowing_add" // ~> .wrapping_add
|
|
|
|
| "overflowing_sub" // ~> .wrapping_sub
|
|
|
|
| "overflowing_mul" // ~> .wrapping_mul
|
|
|
|
| "unchecked_shl" // ~> .wrapping_shl
|
|
|
|
| "unchecked_shr" // ~> .wrapping_shr
|
|
|
|
| "rotate_left" // ~> .rotate_left
|
|
|
|
| "rotate_right" // ~> .rotate_right
|
2018-12-31 16:11:03 +01:00
|
|
|
| "ctpop" // ~> .count_ones
|
|
|
|
| "ctlz" // ~> .leading_zeros
|
|
|
|
| "cttz" // ~> .trailing_zeros
|
|
|
|
| "bswap" // ~> .swap_bytes
|
|
|
|
| "bitreverse" // ~> .reverse_bits
|
2018-12-24 18:53:28 +01:00
|
|
|
=> true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|