Handle Fn family trait call errror
This commit is contained in:
parent
6d6314f878
commit
d3acb9d00e
10 changed files with 57 additions and 13 deletions
|
@ -196,8 +196,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||
.map(|n| format!("`{}`", n))
|
||||
.unwrap_or_else(|| "value".to_owned());
|
||||
match kind {
|
||||
CallKind::FnCall(once_did)
|
||||
if Some(once_did) == self.infcx.tcx.lang_items().fn_once_trait() =>
|
||||
CallKind::FnCall { fn_trait_id, .. }
|
||||
if Some(fn_trait_id) == self.infcx.tcx.lang_items().fn_once_trait() =>
|
||||
{
|
||||
err.span_label(
|
||||
fn_call_span,
|
||||
|
|
|
@ -8,7 +8,9 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
|
|||
use rustc_middle::mir;
|
||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
|
||||
use rustc_middle::ty::{suggest_constraining_type_param, Adt, Param, TraitPredicate, Ty};
|
||||
use rustc_middle::ty::{
|
||||
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, TraitPredicate, Ty,
|
||||
};
|
||||
use rustc_middle::ty::{Binder, BoundConstness, ImplPolarity, TraitRef};
|
||||
use rustc_session::parse::feature_err;
|
||||
use rustc_span::symbol::sym;
|
||||
|
@ -155,7 +157,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
|
|||
CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => {
|
||||
macro_rules! error {
|
||||
($fmt:literal) => {
|
||||
struct_span_err!(tcx.sess, span, E0015, $fmt, self_ty, ccx.const_kind(),)
|
||||
struct_span_err!(tcx.sess, span, E0015, $fmt, self_ty, ccx.const_kind())
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -176,6 +178,41 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
|
|||
|
||||
diag_trait(err, self_ty, kind.trait_def_id(tcx))
|
||||
}
|
||||
CallKind::FnCall { fn_trait_id, self_ty } => {
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
span,
|
||||
E0015,
|
||||
"cannot call non-const closure in {}s",
|
||||
ccx.const_kind(),
|
||||
);
|
||||
|
||||
match self_ty.kind() {
|
||||
FnDef(def_id, ..) => {
|
||||
let span = tcx.sess.source_map().guess_head_span(tcx.def_span(*def_id));
|
||||
if ccx.tcx.is_const_fn_raw(*def_id) {
|
||||
span_bug!(span, "calling const FnDef errored when it shouldn't");
|
||||
}
|
||||
|
||||
err.span_note(span, "function defined here, but it is not `const`");
|
||||
}
|
||||
FnPtr(..) => {
|
||||
err.note(&format!(
|
||||
"function pointers need an RFC before allowed to be called in {}s",
|
||||
ccx.const_kind()
|
||||
));
|
||||
}
|
||||
Closure(..) => {
|
||||
err.note(&format!(
|
||||
"closures need an RFC before allowed to be called in {}s",
|
||||
ccx.const_kind()
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
diag_trait(err, self_ty, fn_trait_id)
|
||||
}
|
||||
CallKind::Operator { trait_id, self_ty, .. } => {
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
|
|
|
@ -44,7 +44,7 @@ pub enum CallKind<'tcx> {
|
|||
is_option_or_result: bool,
|
||||
},
|
||||
/// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)`
|
||||
FnCall(DefId),
|
||||
FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> },
|
||||
/// A call to an operator trait, desuraged from operator syntax (e.g. `a << b`)
|
||||
Operator { self_arg: Option<Ident>, trait_id: DefId, self_ty: Ty<'tcx> },
|
||||
DerefCoercion {
|
||||
|
@ -85,7 +85,7 @@ pub fn call_kind<'tcx>(
|
|||
// an FnOnce call, an operator (e.g. `<<`), or a
|
||||
// deref coercion.
|
||||
let kind = if let Some(&trait_id) = fn_call {
|
||||
Some(CallKind::FnCall(trait_id))
|
||||
Some(CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_substs.type_at(0) })
|
||||
} else if let Some(&trait_id) = operator {
|
||||
Some(CallKind::Operator { self_arg, trait_id, self_ty: method_substs.type_at(0) })
|
||||
} else if is_deref {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue