Skip to content

Commit

Permalink
Rollup merge of rust-lang#115862 - clubby789:migrate-callee-translata…
Browse files Browse the repository at this point in the history
…ble, r=compiler-errors

Migrate `compiler/rustc_hir_typeck/src/callee.rs` to translatable diagnostics
  • Loading branch information
Dylan-DPC authored Sep 17, 2023
2 parents 0c5f5b6 + 8696ee8 commit f082f1d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 54 deletions.
14 changes: 13 additions & 1 deletion compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ hir_typeck_expected_default_return_type = expected `()` because of default retur
hir_typeck_expected_return_type = expected `{$expected}` because of return type
hir_typeck_explicit_destructor = explicit use of destructor method
.label = explicit destructor calls not allowed
.suggestion = consider using `drop` function
hir_typeck_field_multiply_specified_in_initializer =
field `{$ident}` specified more than once
.label = used more than once
Expand All @@ -52,8 +56,10 @@ hir_typeck_functional_record_update_on_non_struct =
hir_typeck_help_set_edition_cargo = set `edition = "{$edition}"` in `Cargo.toml`
hir_typeck_help_set_edition_standalone = pass `--edition {$edition}` to `rustc`
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
hir_typeck_invalid_callee = expected function, found {$ty}
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
hir_typeck_lang_start_incorrect_number_params = incorrect number of parameters for the `start` lang item
hir_typeck_lang_start_incorrect_number_params_note_expected_count = the `start` lang item should have four parameters, but found {$found_param_count}
Expand All @@ -66,6 +72,9 @@ hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang ite
hir_typeck_method_call_on_unknown_raw_pointee =
cannot call a method on a raw pointer with an unknown pointee type
hir_typeck_missing_fn_lang_items = failed to find an overloaded call trait for closure call
.help = make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods
hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`
hir_typeck_no_associated_item = no {$item_kind} named `{$item_name}` found for {$ty_prefix} `{$ty_str}`{$trait_missing_method ->
Expand All @@ -92,6 +101,9 @@ hir_typeck_return_stmt_outside_of_fn_body =
.encl_body_label = the {$statement_kind} is part of this body...
.encl_fn_label = ...not the enclosing function body
hir_typeck_rustcall_incorrect_args =
functions with the "rust-call" ABI must take a single non-self tuple argument
hir_typeck_struct_expr_non_exhaustive =
cannot create non-exhaustive {$what} using struct expression
Expand Down
65 changes: 24 additions & 41 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::method::probe::ProbeScope;
use super::method::MethodCallee;
use super::{Expectation, FnCtxt, TupleArgumentsFlag};

use crate::type_error_struct;
use crate::errors;
use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed, StashKey};
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{self, CtorKind, DefKind, Namespace, Res};
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -44,23 +44,15 @@ pub fn check_legal_trait_for_method_call(
trait_id: DefId,
) {
if tcx.lang_items().drop_trait() == Some(trait_id) {
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
err.span_label(span, "explicit destructor calls not allowed");

let (sp, suggestion) = receiver
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
.filter(|snippet| !snippet.is_empty())
.map(|snippet| (expr_span, format!("drop({snippet})")))
.unwrap_or_else(|| (span, "drop".to_string()));

err.span_suggestion(
sp,
"consider using `drop` function",
suggestion,
Applicability::MaybeIncorrect,
);

err.emit();
let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) {
errors::ExplicitDestructorCallSugg::Snippet {
lo: expr_span.shrink_to_lo(),
hi: receiver.shrink_to_hi().to(expr_span.shrink_to_hi()),
}
} else {
errors::ExplicitDestructorCallSugg::Empty(span)
};
tcx.sess.emit_err(errors::ExplicitDestructorCall { span, sugg });
}
}

Expand Down Expand Up @@ -387,6 +379,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Unit testing: function items annotated with
// `#[rustc_evaluate_where_clauses]` trigger special output
// to let us test the trait evaluation system.
// Untranslatable diagnostics are okay for rustc internals
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
if self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses) {
let predicates = self.tcx.predicates_of(def_id);
let predicates = predicates.instantiate(self.tcx, args);
Expand Down Expand Up @@ -478,10 +473,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
self.require_type_is_sized(ty, sp, traits::RustCall);
} else {
self.tcx.sess.span_err(
sp,
"functions with the \"rust-call\" ABI must take a single non-self tuple argument",
);
self.tcx.sess.emit_err(errors::RustCallIncorrectArgs { span: sp });
}
}

Expand Down Expand Up @@ -610,17 +602,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

let callee_ty = self.resolve_vars_if_possible(callee_ty);
let mut err = type_error_struct!(
self.tcx.sess,
callee_expr.span,
callee_ty,
E0618,
"expected function, found {}",
match &unit_variant {
let mut err = self.tcx.sess.create_err(errors::InvalidCallee {
span: callee_expr.span,
ty: match &unit_variant {
Some((_, kind, path)) => format!("{kind} `{path}`"),
None => format!("`{callee_ty}`"),
}
);
},
});
if callee_ty.references_error() {
err.downgrade_to_delayed_bug();
}

self.identify_bad_closure_def_and_call(
&mut err,
Expand Down Expand Up @@ -891,15 +882,7 @@ impl<'a, 'tcx> DeferredCallResolution<'tcx> {
None => {
// This can happen if `#![no_core]` is used and the `fn/fn_mut/fn_once`
// lang items are not defined (issue #86238).
let mut err = fcx.inh.tcx.sess.struct_span_err(
self.call_expr.span,
"failed to find an overloaded call trait for closure call",
);
err.help(
"make sure the `fn`/`fn_mut`/`fn_once` lang items are defined \
and have correctly defined `call`/`call_mut`/`call_once` methods",
);
err.emit();
fcx.inh.tcx.sess.emit_err(errors::MissingFnLangItems { span: self.call_expr.span });
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ impl IntoDiagnosticArg for ReturnLikeStatementKind {
}
}

#[derive(Diagnostic)]
#[diag(hir_typeck_rustcall_incorrect_args)]
pub struct RustCallIncorrectArgs {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
pub struct YieldExprOutsideOfGenerator {
Expand All @@ -76,6 +83,14 @@ pub struct MethodCallOnUnknownRawPointee {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_typeck_missing_fn_lang_items)]
#[help]
pub struct MissingFnLangItems {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
pub struct FunctionalRecordUpdateOnNonStruct {
Expand Down Expand Up @@ -129,6 +144,29 @@ pub enum ExpectedReturnTypeLabel<'tcx> {
},
}

#[derive(Diagnostic)]
#[diag(hir_typeck_explicit_destructor, code = "E0040")]
pub struct ExplicitDestructorCall {
#[primary_span]
#[label]
pub span: Span,
#[subdiagnostic]
pub sugg: ExplicitDestructorCallSugg,
}

#[derive(Subdiagnostic)]
pub enum ExplicitDestructorCallSugg {
#[suggestion(hir_typeck_suggestion, code = "drop", applicability = "maybe-incorrect")]
Empty(#[primary_span] Span),
#[multipart_suggestion(hir_typeck_suggestion, style = "short")]
Snippet {
#[suggestion_part(code = "drop(")]
lo: Span,
#[suggestion_part(code = ")")]
hi: Span,
},
}

#[derive(Diagnostic)]
#[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
pub struct MissingParenthesesInRange {
Expand Down Expand Up @@ -252,6 +290,14 @@ impl HelpUseLatestEdition {
}
}

#[derive(Diagnostic)]
#[diag(hir_typeck_invalid_callee, code = "E0618")]
pub struct InvalidCallee {
#[primary_span]
pub span: Span,
pub ty: String,
}

#[derive(Subdiagnostic)]
pub enum OptionResultRefMismatch {
#[suggestion(
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/error-codes/E0040.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
--> $DIR/E0040.rs:16:7
|
LL | x.drop();
| --^^^^--
| | |
| | explicit destructor calls not allowed
| help: consider using `drop` function: `drop(x)`
| ^^^^ explicit destructor calls not allowed
|
help: consider using `drop` function
|
LL | drop(x);
| +++++ ~

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/explicit/explicit-call-to-dtor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
--> $DIR/explicit-call-to-dtor.rs:15:7
|
LL | x.drop();
| --^^^^--
| | |
| | explicit destructor calls not allowed
| help: consider using `drop` function: `drop(x)`
| ^^^^ explicit destructor calls not allowed
|
help: consider using `drop` function
|
LL | drop(x);
| +++++ ~

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions tests/ui/explicit/explicit-call-to-supertrait-dtor.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error[E0040]: explicit use of destructor method
--> $DIR/explicit-call-to-supertrait-dtor.rs:22:14
|
LL | self.drop();
| -----^^^^--
| | |
| | explicit destructor calls not allowed
| help: consider using `drop` function: `drop(self)`
| ^^^^ explicit destructor calls not allowed
|
help: consider using `drop` function
|
LL | drop(self);
| +++++ ~

error: aborting due to previous error

Expand Down

0 comments on commit f082f1d

Please sign in to comment.