Open
Description
The following code
pub struct Foo<'a, B: ?Sized>(&'a B);
struct Bar;
impl Drop for Bar { fn drop(&mut self) {} }
// work
const BAR0: Bar = Bar;
const BAR1: &'static [Bar] = &[Bar];
// this is equivalent to the not working examples, but works
const BAR2: Foo<'static, [Bar]> = Foo(BAR1);
// don't work
const BAR3: Foo<'static, [Bar]> = Foo(&[Bar]);
const BAR4: Foo<'static, [Bar]> = Foo(&[Bar] as &'static [Bar]);
results in these errors
error[E0493]: destructors cannot be evaluated at compile-time
--> src/lib.rs:12:40
|
12 | const BAR3: Foo<'static, [Bar]> = Foo(&[Bar]);
| ^^^^^- value is dropped here
| |
| constants cannot evaluate destructors
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:12:40
|
12 | const BAR3: Foo<'static, [Bar]> = Foo(&[Bar]);
| -----^^^^^-
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary which is freed while still in use
| using this value as a constant requires that borrow lasts for `'static`
error[E0493]: destructors cannot be evaluated at compile-time
--> src/lib.rs:13:40
|
13 | const BAR4: Foo<'static, [Bar]> = Foo(&[Bar] as &'static [Bar]);
| ^^^^^ - value is dropped here
| |
| constants cannot evaluate destructors
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:13:40
|
13 | const BAR4: Foo<'static, [Bar]> = Foo(&[Bar] as &'static [Bar]);
| -^^^^^------------------- temporary value is freed at the end of this statement
| ||
| |creates a temporary which is freed while still in use
| type annotation requires that borrow lasts for `'static`
BAR2
is semantically the same as BAR3
and BAR4
, but while BAR3
and BAR4
don't compile, BAR2
does. That can be used as a workaround for now.