Skip to content

Error with static lifetime inference in consts when using types with drop implementation #86672

Open
@oberien

Description

@oberien

Playground

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-destructorsArea: Destructors (`Drop`, …)A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions