Skip to content

Migrate compiler/rustc_hir_typeck/src/callee.rs to translatable diagnostics #115862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 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 Down
29 changes: 11 additions & 18 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use super::method::probe::ProbeScope;
use super::method::MethodCallee;
use super::{Expectation, FnCtxt, TupleArgumentsFlag};

use crate::errors;
use crate::type_error_struct;
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 +45,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
23 changes: 23 additions & 0 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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
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