E4012
E4012 — A call to thedropmethod of aDropimpl by hand:x.drop().dropruns when the value is dropped, so calling it as well would run it twice. Writedrop(x), fromcore::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
Ownership and borrowingThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E4011A view of a tensor (a row
q[i] or a field h.t) used where a tensor of its own is held: stored in a variable that already exists (t = q[i]) or a struct field (h.t = q[i], Holder { t : q[i] }), or passed to a function that takes the tensor by value (f(q[i])). A view shares its owner's memory and is not a copy. Bind it to a new variable with let, pass q[i].clone(), or take the parameter by reference.