]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #64377 - GuillaumeGomez:E0493, r=estebank
authorTyler Mandry <tmandry@gmail.com>
Mon, 30 Sep 2019 21:38:19 +0000 (14:38 -0700)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2019 21:38:19 +0000 (14:38 -0700)
Add long error explanation for E0493

Part of #61137.

1  2 
src/librustc_mir/error_codes.rs
src/test/ui/consts/min_const_fn/min_const_fn.stderr
src/test/ui/consts/miri_unleashed/feature-gate-unleash_the_miri_inside_of_you.stderr

index 196bcf147f8f81ad2abc6b489a539b22598768f4,6208bb294d2054c3219defb3a4283ecd9856b983..fb1311de9a70659bf14855cfc9fa9f1bc302e176
@@@ -1128,6 -1128,51 +1128,51 @@@ Remember this solution is unsafe! You w
  cell are synchronized.
  "##,
  
+ E0493: r##"
+ A type with a `Drop` implementation was destructured when trying to initialize
+ a static item.
+ Erroneous code example:
+ ```compile_fail,E0493
+ enum DropType {
+     A,
+ }
+ impl Drop for DropType {
+     fn drop(&mut self) {}
+ }
+ struct Foo {
+     field1: DropType,
+ }
+ static FOO: Foo = Foo { ..Foo { field1: DropType::A } }; // error!
+ ```
+ The problem here is that if the given type or one of its fields implements the
+ `Drop` trait, this `Drop` implementation cannot be called during the static
+ type initialization which might cause a memory leak. To prevent this issue,
+ you need to instantiate all the static type's fields by hand.
+ ```
+ enum DropType {
+     A,
+ }
+ impl Drop for DropType {
+     fn drop(&mut self) {}
+ }
+ struct Foo {
+     field1: DropType,
+ }
+ static FOO: Foo = Foo { field1: DropType::A }; // We initialize all fields
+                                                // by hand.
+ ```
+ "##,
  E0499: r##"
  A variable was borrowed as mutable more than once. Erroneous code example:
  
@@@ -1993,69 -2038,6 +2038,69 @@@ fn get_owned_iterator() -> IntoIter<i32
  ```
  "##,
  
 +E0524: r##"
 +A variable which requires unique access is being used in more than one closure
 +at the same time.
 +
 +Erroneous code example:
 +
 +```compile_fail,E0524
 +fn set(x: &mut isize) {
 +    *x += 4;
 +}
 +
 +fn dragoooon(x: &mut isize) {
 +    let mut c1 = || set(x);
 +    let mut c2 = || set(x); // error!
 +
 +    c2();
 +    c1();
 +}
 +```
 +
 +To solve this issue, multiple solutions are available. First, is it required
 +for this variable to be used in more than one closure at a time? If it is the
 +case, use reference counted types such as `Rc` (or `Arc` if it runs
 +concurrently):
 +
 +```
 +use std::rc::Rc;
 +use std::cell::RefCell;
 +
 +fn set(x: &mut isize) {
 +    *x += 4;
 +}
 +
 +fn dragoooon(x: &mut isize) {
 +    let x = Rc::new(RefCell::new(x));
 +    let y = Rc::clone(&x);
 +    let mut c1 = || { let mut x2 = x.borrow_mut(); set(&mut x2); };
 +    let mut c2 = || { let mut x2 = y.borrow_mut(); set(&mut x2); }; // ok!
 +
 +    c2();
 +    c1();
 +}
 +```
 +
 +If not, just run closures one at a time:
 +
 +```
 +fn set(x: &mut isize) {
 +    *x += 4;
 +}
 +
 +fn dragoooon(x: &mut isize) {
 +    { // This block isn't necessary since non-lexical lifetimes, it's just to
 +      // make it more clear.
 +        let mut c1 = || set(&mut *x);
 +        c1();
 +    } // `c1` has been dropped here so we're free to use `x` again!
 +    let mut c2 = || set(&mut *x);
 +    c2();
 +}
 +```
 +"##,
 +
  E0595: r##"
  #### Note: this error code is no longer emitted by the compiler.
  
@@@ -2454,8 -2436,8 +2499,7 @@@ There are some known bugs that trigger 
  //  E0299, // mismatched types between arms
  //  E0471, // constant evaluation error (in pattern)
  //  E0385, // {} in an aliasable location
-     E0493, // destructors cannot be evaluated at compile-time
      E0521, // borrowed data escapes outside of closure
 -    E0524, // two closures require unique access to `..` at the same time
      E0526, // shuffle indices are not constant
      E0594, // cannot assign to {}
  //  E0598, // lifetime of {} is too short to guarantee its contents can be...
index 7919cfe987cfc5c149d875a90406cfab3776679c,db70a775b779ca675861a20364cb9ee47b8a54f8..3158b6284db94761732509b56d93137268249d9f
@@@ -286,7 -286,7 +286,7 @@@ LL | const fn no_dyn_trait_ret() -> &'s
     = note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
     = help: add `#![feature(const_fn)]` to the crate attributes to enable
  
 -warning[E0515]: cannot return reference to temporary value
 +error[E0515]: cannot return reference to temporary value
    --> $DIR/min_const_fn.rs:137:63
     |
  LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
     |                                                               ||
     |                                                               |temporary value created here
     |                                                               returns a reference to data owned by the current function
 -   |
 -   = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
 -   = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
 -   = note: for more information, try `rustc --explain E0729`
  
  error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
 -  --> $DIR/min_const_fn.rs:145:41
 +  --> $DIR/min_const_fn.rs:143:41
     |
  LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
     |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = help: add `#![feature(const_fn)]` to the crate attributes to enable
  
  error[E0723]: function pointers in const fn are unstable
 -  --> $DIR/min_const_fn.rs:148:21
 +  --> $DIR/min_const_fn.rs:146:21
     |
  LL | const fn no_fn_ptrs(_x: fn()) {}
     |                     ^^
     = help: add `#![feature(const_fn)]` to the crate attributes to enable
  
  error[E0723]: function pointers in const fn are unstable
 -  --> $DIR/min_const_fn.rs:150:27
 +  --> $DIR/min_const_fn.rs:148:27
     |
  LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
     |                           ^^^^
     = note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
     = help: add `#![feature(const_fn)]` to the crate attributes to enable
  
 -error: aborting due to 36 previous errors
 +error: aborting due to 37 previous errors
  
- Some errors have detailed explanations: E0515, E0723.
- For more information about an error, try `rustc --explain E0515`.
+ Some errors have detailed explanations: E0493, E0515, E0723.
+ For more information about an error, try `rustc --explain E0493`.
index 5bc7b70638c80f372c9160de20d3a292dba8a269,7ede44c65b83a0479aaf7a7c786a24eb6b5056bb..37016664ac58f38c913b3d1dbd36cf8ddd243c99
@@@ -4,5 -4,14 +4,6 @@@ error[E0493]: destructors cannot be eva
  LL |     const F: u32 = (U::X, 42).1;
     |                    ^^^^^^^^^^ constants cannot evaluate destructors
  
 -error: `std::vec::Vec::<T>::new` is not yet stable as a const fn
 -  --> $DIR/feature-gate-unleash_the_miri_inside_of_you.rs:18:25
 -   |
 -LL |     const X: Vec<u32> = Vec::new();
 -   |                         ^^^^^^^^^^
 -   |
 -   = help: add `#![feature(const_vec_new)]` to the crate attributes to enable
 -
 -error: aborting due to 2 previous errors
 +error: aborting due to previous error
  
+ For more information about this error, try `rustc --explain E0493`.