@@ -8,7 +8,9 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
88use rustc_middle:: mir;
99use rustc_middle:: ty:: print:: with_no_trimmed_paths;
1010use rustc_middle:: ty:: subst:: { GenericArgKind , SubstsRef } ;
11- use rustc_middle:: ty:: { suggest_constraining_type_param, Adt , Param , TraitPredicate , Ty } ;
11+ use rustc_middle:: ty:: {
12+ suggest_constraining_type_param, Adt , Closure , FnDef , FnPtr , Param , TraitPredicate , Ty ,
13+ } ;
1214use rustc_middle:: ty:: { Binder , BoundConstness , ImplPolarity , TraitRef } ;
1315use rustc_session:: parse:: feature_err;
1416use rustc_span:: symbol:: sym;
@@ -155,7 +157,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
155157 CallKind :: Normal { desugaring : Some ( ( kind, self_ty) ) , .. } => {
156158 macro_rules! error {
157159 ( $fmt: literal) => {
158- struct_span_err!( tcx. sess, span, E0015 , $fmt, self_ty, ccx. const_kind( ) , )
160+ struct_span_err!( tcx. sess, span, E0015 , $fmt, self_ty, ccx. const_kind( ) )
159161 } ;
160162 }
161163
@@ -176,6 +178,41 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
176178
177179 diag_trait ( err, self_ty, kind. trait_def_id ( tcx) )
178180 }
181+ CallKind :: FnCall { fn_trait_id, self_ty } => {
182+ let mut err = struct_span_err ! (
183+ tcx. sess,
184+ span,
185+ E0015 ,
186+ "cannot call non-const closure in {}s" ,
187+ ccx. const_kind( ) ,
188+ ) ;
189+
190+ match self_ty. kind ( ) {
191+ FnDef ( def_id, ..) => {
192+ let span = tcx. sess . source_map ( ) . guess_head_span ( tcx. def_span ( * def_id) ) ;
193+ if ccx. tcx . is_const_fn_raw ( * def_id) {
194+ span_bug ! ( span, "calling const FnDef errored when it shouldn't" ) ;
195+ }
196+
197+ err. span_note ( span, "function defined here, but it is not `const`" ) ;
198+ }
199+ FnPtr ( ..) => {
200+ err. note ( & format ! (
201+ "function pointers need an RFC before allowed to be called in {}s" ,
202+ ccx. const_kind( )
203+ ) ) ;
204+ }
205+ Closure ( ..) => {
206+ err. note ( & format ! (
207+ "closures need an RFC before allowed to be called in {}s" ,
208+ ccx. const_kind( )
209+ ) ) ;
210+ }
211+ _ => { }
212+ }
213+
214+ diag_trait ( err, self_ty, fn_trait_id)
215+ }
179216 CallKind :: Operator { trait_id, self_ty, .. } => {
180217 let mut err = struct_span_err ! (
181218 tcx. sess,
0 commit comments