Skip to content

Commit

Permalink
Check for trait methods on concrete types in const checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstatic-morse committed Feb 19, 2020
1 parent 5e422ef commit 7a019b1
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/librustc_mir/transform/check_consts/validation.rs
Expand Up @@ -4,7 +4,7 @@ use rustc::middle::lang_items;
use rustc::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
use rustc::mir::*;
use rustc::ty::cast::CastTy;
use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, Instance, InstanceDef, TyCtxt};
use rustc_errors::struct_span_err;
use rustc_hir::{def_id::DefId, HirId};
use rustc_index::bit_set::BitSet;
Expand Down Expand Up @@ -501,8 +501,8 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
TerminatorKind::Call { func, .. } => {
let fn_ty = func.ty(*self.body, self.tcx);

let def_id = match fn_ty.kind {
ty::FnDef(def_id, _) => def_id,
let (def_id, substs) = match fn_ty.kind {
ty::FnDef(def_id, substs) => (def_id, substs),

ty::FnPtr(_) => {
self.check_op(ops::FnCallIndirect);
Expand All @@ -519,6 +519,20 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
return;
}

// See if this is a trait method for a concrete type whose impl of that trait is
// `const`.
if self.tcx.features().const_trait_impl {
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs);
debug!("Resolving ({:?}) -> {:?}", def_id, instance);
if let Some(func) = instance {
if let InstanceDef::Item(def_id) = func.def {
if is_const_fn(self.tcx, def_id) {
return;
}
}
}
}

if is_lang_panic_fn(self.tcx, def_id) {
self.check_op(ops::Panic);
} else if let Some(feature) = is_unstable_const_fn(self.tcx, def_id) {
Expand Down

0 comments on commit 7a019b1

Please sign in to comment.