E4012

E4012 — A call to the drop method of a Drop impl by hand: x.drop(). drop runs when the value is dropped, so calling it as well would run it twice. Write drop(x), from core::mem, to drop a value early.

What the compiler reports

Error[E4012] at 31:5: `drop` is called for you when a value is dropped
Error[E4012] at 36:5: `drop` is called for you when a value is dropped
2 error(s) emitted

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// A `drop` method runs when its value is dropped, so calling it by hand would run it a second
// time. As in Rust, the call is refused, for a type of the program and for a `Vec`.

import core::ops;
import std::vec;

struct Noisy {
  id : i32,
}

impl Drop for Noisy {
  fn drop(self : &mut Noisy) -> void {
    print(self.id);
  }
}

fn own_type(mut n : Noisy) -> void {
  n.drop();
}

fn library_type() -> void {
  let mut v = Vec<i32>::new();
  v.drop();
}

fn main() -> i32 {
  return 0;
}

From tests/frontend/fail/drop_called_by_hand.vx, which asserts this diagnostic on every commit.

Related