Skip to content

Commit f082f1d

Browse files
authored
Rollup merge of rust-lang#115862 - clubby789:migrate-callee-translatable, r=compiler-errors
Migrate `compiler/rustc_hir_typeck/src/callee.rs` to translatable diagnostics
2 parents 0c5f5b6 + 8696ee8 commit f082f1d

File tree

6 files changed

+101
-54
lines changed

6 files changed

+101
-54
lines changed

compiler/rustc_hir_typeck/messages.ftl

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ hir_typeck_expected_default_return_type = expected `()` because of default retur
3333
3434
hir_typeck_expected_return_type = expected `{$expected}` because of return type
3535
36+
hir_typeck_explicit_destructor = explicit use of destructor method
37+
.label = explicit destructor calls not allowed
38+
.suggestion = consider using `drop` function
39+
3640
hir_typeck_field_multiply_specified_in_initializer =
3741
field `{$ident}` specified more than once
3842
.label = used more than once
@@ -52,8 +56,10 @@ hir_typeck_functional_record_update_on_non_struct =
5256
5357
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
5458
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
55-
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
5659
60+
hir_typeck_invalid_callee = expected function, found {$ty}
61+
62+
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
5763
hir_typeck_lang_start_incorrect_number_params = incorrect number of parameters for the `start` lang item
5864
hir_typeck_lang_start_incorrect_number_params_note_expected_count = the `start` lang item should have four parameters, but found {$found_param_count}
5965
@@ -66,6 +72,9 @@ hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang ite
6672
hir_typeck_method_call_on_unknown_raw_pointee =
6773
cannot call a method on a raw pointer with an unknown pointee type
6874
75+
hir_typeck_missing_fn_lang_items = failed to find an overloaded call trait for closure call
76+
.help = make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods
77+
6978
hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`
7079
7180
hir_typeck_no_associated_item = no {$item_kind} named `{$item_name}` found for {$ty_prefix} `{$ty_str}`{$trait_missing_method ->
@@ -92,6 +101,9 @@ hir_typeck_return_stmt_outside_of_fn_body =
92101
.encl_body_label = the {$statement_kind} is part of this body...
93102
.encl_fn_label = ...not the enclosing function body
94103
104+
hir_typeck_rustcall_incorrect_args =
105+
functions with the "rust-call" ABI must take a single non-self tuple argument
106+
95107
hir_typeck_struct_expr_non_exhaustive =
96108
cannot create non-exhaustive {$what} using struct expression
97109

compiler/rustc_hir_typeck/src/callee.rs

+24-41
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::method::probe::ProbeScope;
22
use super::method::MethodCallee;
33
use super::{Expectation, FnCtxt, TupleArgumentsFlag};
44

5-
use crate::type_error_struct;
5+
use crate::errors;
66
use 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};
88
use rustc_hir as hir;
99
use rustc_hir::def::{self, CtorKind, DefKind, Namespace, Res};
1010
use 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
}

compiler/rustc_hir_typeck/src/errors.rs

+46
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ impl IntoDiagnosticArg for ReturnLikeStatementKind {
5454
}
5555
}
5656

57+
#[derive(Diagnostic)]
58+
#[diag(hir_typeck_rustcall_incorrect_args)]
59+
pub struct RustCallIncorrectArgs {
60+
#[primary_span]
61+
pub span: Span,
62+
}
63+
5764
#[derive(Diagnostic)]
5865
#[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
5966
pub struct YieldExprOutsideOfGenerator {
@@ -76,6 +83,14 @@ pub struct MethodCallOnUnknownRawPointee {
7683
pub span: Span,
7784
}
7885

86+
#[derive(Diagnostic)]
87+
#[diag(hir_typeck_missing_fn_lang_items)]
88+
#[help]
89+
pub struct MissingFnLangItems {
90+
#[primary_span]
91+
pub span: Span,
92+
}
93+
7994
#[derive(Diagnostic)]
8095
#[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
8196
pub struct FunctionalRecordUpdateOnNonStruct {
@@ -129,6 +144,29 @@ pub enum ExpectedReturnTypeLabel<'tcx> {
129144
},
130145
}
131146

147+
#[derive(Diagnostic)]
148+
#[diag(hir_typeck_explicit_destructor, code = "E0040")]
149+
pub struct ExplicitDestructorCall {
150+
#[primary_span]
151+
#[label]
152+
pub span: Span,
153+
#[subdiagnostic]
154+
pub sugg: ExplicitDestructorCallSugg,
155+
}
156+
157+
#[derive(Subdiagnostic)]
158+
pub enum ExplicitDestructorCallSugg {
159+
#[suggestion(hir_typeck_suggestion, code = "drop", applicability = "maybe-incorrect")]
160+
Empty(#[primary_span] Span),
161+
#[multipart_suggestion(hir_typeck_suggestion, style = "short")]
162+
Snippet {
163+
#[suggestion_part(code = "drop(")]
164+
lo: Span,
165+
#[suggestion_part(code = ")")]
166+
hi: Span,
167+
},
168+
}
169+
132170
#[derive(Diagnostic)]
133171
#[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
134172
pub struct MissingParenthesesInRange {
@@ -252,6 +290,14 @@ impl HelpUseLatestEdition {
252290
}
253291
}
254292

293+
#[derive(Diagnostic)]
294+
#[diag(hir_typeck_invalid_callee, code = "E0618")]
295+
pub struct InvalidCallee {
296+
#[primary_span]
297+
pub span: Span,
298+
pub ty: String,
299+
}
300+
255301
#[derive(Subdiagnostic)]
256302
pub enum OptionResultRefMismatch {
257303
#[suggestion(

tests/ui/error-codes/E0040.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/E0040.rs:16:7
33
|
44
LL | x.drop();
5-
| --^^^^--
6-
| | |
7-
| | explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(x)`
5+
| ^^^^ explicit destructor calls not allowed
6+
|
7+
help: consider using `drop` function
8+
|
9+
LL | drop(x);
10+
| +++++ ~
911

1012
error: aborting due to previous error
1113

tests/ui/explicit/explicit-call-to-dtor.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/explicit-call-to-dtor.rs:15:7
33
|
44
LL | x.drop();
5-
| --^^^^--
6-
| | |
7-
| | explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(x)`
5+
| ^^^^ explicit destructor calls not allowed
6+
|
7+
help: consider using `drop` function
8+
|
9+
LL | drop(x);
10+
| +++++ ~
911

1012
error: aborting due to previous error
1113

tests/ui/explicit/explicit-call-to-supertrait-dtor.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/explicit-call-to-supertrait-dtor.rs:22:14
33
|
44
LL | self.drop();
5-
| -----^^^^--
6-
| | |
7-
| | explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(self)`
5+
| ^^^^ explicit destructor calls not allowed
6+
|
7+
help: consider using `drop` function
8+
|
9+
LL | drop(self);
10+
| +++++ ~
911

1012
error: aborting due to previous error
1113

0 commit comments

Comments
 (0)