@@ -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 {
@@ -363,16 +361,9 @@ fn build_error_for_const_call<'tcx>(
363361 kind : ccx. const_kind ( ) ,
364362 non_or_conditionally,
365363 } ) ;
366- let context_span = ccx. tcx . def_span ( ccx. def_id ( ) ) ;
367- err. span_label ( context_span, format ! (
368- "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
369- ccx. const_kind( ) ,
370- ) ) ;
371- note = false ;
372- let def_kind = ccx. tcx . def_kind ( callee) ;
373- if let DefKind :: AssocTy | DefKind :: AssocConst | DefKind :: AssocFn = def_kind {
374- let parent = ccx. tcx . parent ( callee) ;
375- if let DefKind :: Trait = ccx. tcx . def_kind ( parent)
364+ if let Some ( item) = ccx. tcx . opt_associated_item ( callee) {
365+ if let AssocItemContainer :: Trait = item. container
366+ && let parent = item. container_id ( ccx. tcx )
376367 && !ccx. tcx . is_const_trait ( parent)
377368 {
378369 let assoc_span = ccx. tcx . def_span ( callee) ;
@@ -407,7 +398,7 @@ fn build_error_for_const_call<'tcx>(
407398 trait_span. shrink_to_lo ( ) ,
408399 format ! ( "consider making trait `{trait_name}` const" ) ,
409400 format ! ( "#[const_trait]\n {indentation}" ) ,
410- Applicability :: MachineApplicable ,
401+ Applicability :: MaybeIncorrect ,
411402 ) ;
412403 } else if !ccx. tcx . sess . is_nightly_build ( ) {
413404 err. help ( "const traits are not yet supported on stable Rust" ) ;
@@ -424,12 +415,10 @@ fn build_error_for_const_call<'tcx>(
424415 }
425416 } ;
426417
427- if note {
428- err. note ( format ! (
429- "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
430- ccx. const_kind( ) ,
431- ) ) ;
432- }
418+ err. note ( format ! (
419+ "calls in {}s are limited to constant functions, tuple structs and tuple variants" ,
420+ ccx. const_kind( ) ,
421+ ) ) ;
433422
434423 err
435424}
0 commit comments