Skip to content

Commit 7a019b1

Browse files
Check for trait methods on concrete types in const checking
1 parent 5e422ef commit 7a019b1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/librustc_mir/transform/check_consts/validation.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::middle::lang_items;
44
use rustc::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
55
use rustc::mir::*;
66
use rustc::ty::cast::CastTy;
7-
use rustc::ty::{self, TyCtxt};
7+
use rustc::ty::{self, Instance, InstanceDef, TyCtxt};
88
use rustc_errors::struct_span_err;
99
use rustc_hir::{def_id::DefId, HirId};
1010
use rustc_index::bit_set::BitSet;
@@ -501,8 +501,8 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
501501
TerminatorKind::Call { func, .. } => {
502502
let fn_ty = func.ty(*self.body, self.tcx);
503503

504-
let def_id = match fn_ty.kind {
505-
ty::FnDef(def_id, _) => def_id,
504+
let (def_id, substs) = match fn_ty.kind {
505+
ty::FnDef(def_id, substs) => (def_id, substs),
506506

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

522+
// See if this is a trait method for a concrete type whose impl of that trait is
523+
// `const`.
524+
if self.tcx.features().const_trait_impl {
525+
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs);
526+
debug!("Resolving ({:?}) -> {:?}", def_id, instance);
527+
if let Some(func) = instance {
528+
if let InstanceDef::Item(def_id) = func.def {
529+
if is_const_fn(self.tcx, def_id) {
530+
return;
531+
}
532+
}
533+
}
534+
}
535+
522536
if is_lang_panic_fn(self.tcx, def_id) {
523537
self.check_op(ops::Panic);
524538
} else if let Some(feature) = is_unstable_const_fn(self.tcx, def_id) {

0 commit comments

Comments
 (0)