Skip to content
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

Suggest using std::mem::drop function instead of explicit destructor call #72383

Merged
merged 3 commits into from
May 29, 2020
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
26 changes: 22 additions & 4 deletions src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@ use rustc_target::spec::abi;
/// Checks that it is legal to call methods of the trait corresponding
/// to `trait_id` (this only cares about the trait, not the specific
/// method that is called).
pub fn check_legal_trait_for_method_call(tcx: TyCtxt<'_>, span: Span, trait_id: DefId) {
pub fn check_legal_trait_for_method_call(
tcx: TyCtxt<'_>,
span: Span,
receiver: Option<Span>,
trait_id: DefId,
) {
if tcx.lang_items().drop_trait() == Some(trait_id) {
struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method")
.span_label(span, "explicit destructor calls not allowed")
.emit();
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
matthewjasper marked this conversation as resolved.
Show resolved Hide resolved
err.span_label(span, "explicit destructor calls not allowed");

let snippet = receiver
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
.unwrap_or_default();

let (suggestion, applicability) = if snippet.is_empty() {
(snippet, Applicability::Unspecified)
} else {
(format!("drop({})", snippet), Applicability::MachineApplicable)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if Applicability::MachineApplicable should be used here. The suggestion is valid, but I'm not aware how such applications work. As far as I can see, the suggestion is emitted for the Drop::drop span, not the full Drop::drop(x) expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be MaybeIncorrect since x could be a mutable reference and then drop(x) is probably not what the user wants.

};

err.span_suggestion(span, "consider using `drop` function", suggestion, applicability);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're suggesting drop(x) then the span for the suggestion will need to be the one for the whole method call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to merge spans, but I'm suppose it still won't do because the resulting span doesn't include round brackets: x.drop instead of x.drop(). Is there an easy way to achieve that?

Copy link
Contributor Author

@stanislav-tkach stanislav-tkach May 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it isn't worth the fuss and because applicability still would be MaybeIncorrect at best, I can simply print a message leaving the suggestion empty. Something like that:

let message = format!("consider using `drop` function{}", snippet);

err.span_suggestion(
    span,
    &message,
    String::new(),
    Applicability::Unspecified,
);

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewjasper Sorry for bothering you, but I'm not sure if I should try to extend the span somehow to include the whole method call or use a simpler solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the pull request accordingly.


err.emit();
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/librustc_typeck/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
// Disallow calls to the method `drop` defined in the `Drop` trait.
match pick.item.container {
ty::TraitContainer(trait_def_id) => {
callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
}
ty::TraitContainer(trait_def_id) => callee::check_legal_trait_for_method_call(
self.tcx,
self.span,
Some(self.self_expr.span),
trait_def_id,
),
ty::ImplContainer(..) => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5438,7 +5438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
match container {
ty::TraitContainer(trait_did) => {
callee::check_legal_trait_for_method_call(tcx, span, trait_did)
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
}
ty::ImplContainer(impl_def_id) => {
if segments.len() == 1 {
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/error-codes/E0040.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
--> $DIR/E0040.rs:13:7
|
LL | x.drop();
| ^^^^ explicit destructor calls not allowed
| ^^^^
| |
| explicit destructor calls not allowed
| help: consider using `drop` function: `drop(x)`

error: aborting due to previous error

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

error: aborting due to previous error

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

error: aborting due to previous error

Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/illegal-ufcs-drop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
--> $DIR/illegal-ufcs-drop.rs:8:5
|
LL | Drop::drop(&mut Foo)
| ^^^^^^^^^^ explicit destructor calls not allowed
| ^^^^^^^^^^
| |
| explicit destructor calls not allowed
| help: consider using `drop` function

error: aborting due to previous error

Expand Down