Skip to content

Phantom lifetime is ignored #18331

Closed
Closed
@gifnksm

Description

@gifnksm

The phantom lifetime (I mean the struct's lifetime that is not linked to any of its members) seems to be ignored.

// phantom-lifetime.rs
struct Doc;

#[cfg(not(phantom))]
struct Node<'a> {
    doc: &'a Doc
}

#[cfg(phantom)]
struct Node<'a>;

impl Doc {
    #[cfg(phantom)]
    fn node<'a>(&'a self) -> Node<'a> {
        Node
    }

    #[cfg(not(phantom))]
    fn node<'a>(&'a self) -> Node<'a> {
        Node { doc: self }
    }
}

fn main() {
    let node;
    {
        let doc = Doc;
        node = doc.node();
    }
}

Without --cfg phantom, this isn't be compiled.

$ rustc phantom-lifetime.rs
phantom-lifetime.rs:27:16: 27:19 error: `doc` does not live long enough
phantom-lifetime.rs:27         node = doc.node();
                                      ^~~
phantom-lifetime.rs:23:11: 29:2 note: reference must be valid for the block at 23:10...
phantom-lifetime.rs:23 fn main() {
phantom-lifetime.rs:24     let node;
phantom-lifetime.rs:25     {
phantom-lifetime.rs:26         let doc = Doc;
phantom-lifetime.rs:27         node = doc.node();
phantom-lifetime.rs:28     }
                       ...
phantom-lifetime.rs:25:5: 28:6 note: ...but borrowed value is only valid for the block at 25:4
phantom-lifetime.rs:25     {
phantom-lifetime.rs:26         let doc = Doc;
phantom-lifetime.rs:27         node = doc.node();
phantom-lifetime.rs:28     }
error: aborting due to previous error

With --cfg phantom, this is compiled.

$ rustc phantom-lifetime.rs --cfg phantom
phantom-lifetime.rs:24:9: 24:13 warning: variable `node` is assigned to, but never used, #[warn(unused_variables)] on by default
phantom-lifetime.rs:24     let node;
                               ^~~~
phantom-lifetime.rs:27:9: 27:13 warning: value assigned to `node` is never read, #[warn(unused_assignments)] on by default
phantom-lifetime.rs:27         node = doc.node();
                               ^~~~
$ rustc --version
rustc 0.13.0-dev (80e5fe1a5 2014-10-25 09:17:05 +0000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions