@@ -2,9 +2,9 @@ use super::method::probe::ProbeScope;
22use super :: method:: MethodCallee ;
33use super :: { Expectation , FnCtxt , TupleArgumentsFlag } ;
44
5- use crate :: type_error_struct ;
5+ use crate :: errors ;
66use rustc_ast:: util:: parser:: PREC_POSTFIX ;
7- use rustc_errors:: { struct_span_err , Applicability , Diagnostic , ErrorGuaranteed , StashKey } ;
7+ use rustc_errors:: { Applicability , Diagnostic , ErrorGuaranteed , StashKey } ;
88use rustc_hir as hir;
99use rustc_hir:: def:: { self , CtorKind , DefKind , Namespace , Res } ;
1010use rustc_hir:: def_id:: DefId ;
@@ -44,23 +44,15 @@ pub fn check_legal_trait_for_method_call(
4444 trait_id : DefId ,
4545) {
4646 if tcx. lang_items ( ) . drop_trait ( ) == Some ( trait_id) {
47- let mut err = struct_span_err ! ( tcx. sess, span, E0040 , "explicit use of destructor method" ) ;
48- err. span_label ( span, "explicit destructor calls not allowed" ) ;
49-
50- let ( sp, suggestion) = receiver
51- . and_then ( |s| tcx. sess . source_map ( ) . span_to_snippet ( s) . ok ( ) )
52- . filter ( |snippet| !snippet. is_empty ( ) )
53- . map ( |snippet| ( expr_span, format ! ( "drop({snippet})" ) ) )
54- . unwrap_or_else ( || ( span, "drop" . to_string ( ) ) ) ;
55-
56- err. span_suggestion (
57- sp,
58- "consider using `drop` function" ,
59- suggestion,
60- Applicability :: MaybeIncorrect ,
61- ) ;
62-
63- err. emit ( ) ;
47+ let sugg = if let Some ( receiver) = receiver. filter ( |s| !s. is_empty ( ) ) {
48+ errors:: ExplicitDestructorCallSugg :: Snippet {
49+ lo : expr_span. shrink_to_lo ( ) ,
50+ hi : receiver. shrink_to_hi ( ) . to ( expr_span. shrink_to_hi ( ) ) ,
51+ }
52+ } else {
53+ errors:: ExplicitDestructorCallSugg :: Empty ( span)
54+ } ;
55+ tcx. sess . emit_err ( errors:: ExplicitDestructorCall { span, sugg } ) ;
6456 }
6557}
6658
@@ -387,6 +379,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
387379 // Unit testing: function items annotated with
388380 // `#[rustc_evaluate_where_clauses]` trigger special output
389381 // to let us test the trait evaluation system.
382+ // Untranslatable diagnostics are okay for rustc internals
383+ #[ allow( rustc:: untranslatable_diagnostic) ]
384+ #[ allow( rustc:: diagnostic_outside_of_impl) ]
390385 if self . tcx . has_attr ( def_id, sym:: rustc_evaluate_where_clauses) {
391386 let predicates = self . tcx . predicates_of ( def_id) ;
392387 let predicates = predicates. instantiate ( self . tcx , args) ;
@@ -478,10 +473,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
478473 ) ;
479474 self . require_type_is_sized ( ty, sp, traits:: RustCall ) ;
480475 } else {
481- self . tcx . sess . span_err (
482- sp,
483- "functions with the \" rust-call\" ABI must take a single non-self tuple argument" ,
484- ) ;
476+ self . tcx . sess . emit_err ( errors:: RustCallIncorrectArgs { span : sp } ) ;
485477 }
486478 }
487479
@@ -610,17 +602,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
610602 }
611603
612604 let callee_ty = self . resolve_vars_if_possible ( callee_ty) ;
613- let mut err = type_error_struct ! (
614- self . tcx. sess,
615- callee_expr. span,
616- callee_ty,
617- E0618 ,
618- "expected function, found {}" ,
619- match & unit_variant {
605+ let mut err = self . tcx . sess . create_err ( errors:: InvalidCallee {
606+ span : callee_expr. span ,
607+ ty : match & unit_variant {
620608 Some ( ( _, kind, path) ) => format ! ( "{kind} `{path}`" ) ,
621609 None => format ! ( "`{callee_ty}`" ) ,
622- }
623- ) ;
610+ } ,
611+ } ) ;
612+ if callee_ty. references_error ( ) {
613+ err. downgrade_to_delayed_bug ( ) ;
614+ }
624615
625616 self . identify_bad_closure_def_and_call (
626617 & mut err,
@@ -891,15 +882,7 @@ impl<'a, 'tcx> DeferredCallResolution<'tcx> {
891882 None => {
892883 // This can happen if `#![no_core]` is used and the `fn/fn_mut/fn_once`
893884 // lang items are not defined (issue #86238).
894- let mut err = fcx. inh . tcx . sess . struct_span_err (
895- self . call_expr . span ,
896- "failed to find an overloaded call trait for closure call" ,
897- ) ;
898- err. help (
899- "make sure the `fn`/`fn_mut`/`fn_once` lang items are defined \
900- and have correctly defined `call`/`call_mut`/`call_once` methods",
901- ) ;
902- err. emit ( ) ;
885+ fcx. inh . tcx . sess . emit_err ( errors:: MissingFnLangItems { span : self . call_expr . span } ) ;
903886 }
904887 }
905888 }
0 commit comments