@@ -4,16 +4,15 @@ use hir::{ConstContext, LangItem};
44use rustc_errors:: codes:: * ;
55use rustc_errors:: { Applicability , Diag , MultiSpan } ;
66use rustc_hir as hir;
7- use rustc_hir:: def:: DefKind ;
87use rustc_hir:: def_id:: DefId ;
98use rustc_infer:: infer:: TyCtxtInferExt ;
109use rustc_infer:: traits:: { ImplSource , Obligation , ObligationCause } ;
1110use rustc_middle:: mir:: CallSource ;
1211use rustc_middle:: span_bug;
1312use rustc_middle:: ty:: print:: { PrintTraitRefExt as _, with_no_trimmed_paths} ;
1413use rustc_middle:: ty:: {
15- self , Closure , FnDef , FnPtr , GenericArgKind , GenericArgsRef , Param , TraitRef , Ty ,
16- suggest_constraining_type_param,
14+ self , AssocItemContainer , Closure , FnDef , FnPtr , GenericArgKind , GenericArgsRef , Param ,
15+ TraitRef , Ty , suggest_constraining_type_param,
1716} ;
1817use rustc_session:: parse:: add_feature_diagnostics;
1918use rustc_span:: { BytePos , Pos , Span , Symbol , sym} ;
@@ -212,7 +211,6 @@ fn build_error_for_const_call<'tcx>(
212211
213212 debug ! ( ?call_kind) ;
214213
215- let mut note = true ;
216214 let mut err = match call_kind {
217215 CallKind :: Normal { desugaring : Some ( ( kind, self_ty) ) , .. } => {
218216 macro_rules! error {
@@ -366,16 +364,9 @@ fn build_error_for_const_call<'tcx>(
366364 kind : ccx. const_kind ( ) ,
367365 non_or_conditionally,
368366 } ) ;
369- let context_span = ccx. tcx . def_span ( ccx. def_id ( ) ) ;
370- err. span_label ( context_span, format ! (
371- "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
372- ccx. const_kind( ) ,
373- ) ) ;
374- note = false ;
375- let def_kind = ccx. tcx . def_kind ( callee) ;
376- if let DefKind :: AssocTy | DefKind :: AssocConst | DefKind :: AssocFn = def_kind {
377- let parent = ccx. tcx . parent ( callee) ;
378- if let DefKind :: Trait = ccx. tcx . def_kind ( parent)
367+ if let Some ( item) = ccx. tcx . opt_associated_item ( callee) {
368+ if let AssocItemContainer :: Trait = item. container
369+ && let parent = item. container_id ( ccx. tcx )
379370 && !ccx. tcx . is_const_trait ( parent)
380371 {
381372 let assoc_span = ccx. tcx . def_span ( callee) ;
@@ -410,7 +401,7 @@ fn build_error_for_const_call<'tcx>(
410401 trait_span. shrink_to_lo ( ) ,
411402 format ! ( "consider making trait `{trait_name}` const" ) ,
412403 format ! ( "#[const_trait]\n {indentation}" ) ,
413- Applicability :: MachineApplicable ,
404+ Applicability :: MaybeIncorrect ,
414405 ) ;
415406 } else if !ccx. tcx . sess . is_nightly_build ( ) {
416407 err. help ( "const traits are not yet supported on stable Rust" ) ;
@@ -427,12 +418,10 @@ fn build_error_for_const_call<'tcx>(
427418 }
428419 } ;
429420
430- if note {
431- err. note ( format ! (
432- "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
433- ccx. const_kind( ) ,
434- ) ) ;
435- }
421+ err. note ( format ! (
422+ "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
423+ ccx. const_kind( ) ,
424+ ) ) ;
436425
437426 err
438427}
0 commit comments