Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phantom lifetime is ignored #18331

Closed
gifnksm opened this issue Oct 26, 2014 · 9 comments
Closed

Phantom lifetime is ignored #18331

gifnksm opened this issue Oct 26, 2014 · 9 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@gifnksm
Copy link
Contributor

gifnksm commented Oct 26, 2014

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)
@gifnksm
Copy link
Contributor Author

gifnksm commented Oct 26, 2014

The output of rustc --pretty typed --cfg phantom. node's lifetime is '<empty>.

#![feature(phase)]
#![no_std]
#![feature(globs)]
#[phase(plugin, link)]
extern crate "std" as std;
extern crate "native" as rt;
#[prelude_import]
use std::prelude::*;
struct Doc;


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

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

}

fn main() {
    let node;
    ({
         let doc = (Doc as Doc);
         ((node as Node<'<empty>>) = ((doc as Doc).node() as Node<'_>) as ());
     } as ())
}

@sfackler
Copy link
Member

Unused lifetimes are currently interpreted as bivariant, yes. You can use the marker types in std::kinds::marker to control the behavior: http://doc.rust-lang.org/std/kinds/marker/index.html

cc #5922

@kmcallister kmcallister added A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Oct 26, 2014
@kmcallister
Copy link
Contributor

This is a pretty big safety hazard. We need to get a lint working for this.

@thehydroimpulse
Copy link
Contributor

A lint for this would definitely be useful!

@apasel422
Copy link
Contributor

Unused lifetimes are now an error:

> rustc --version
rustc 1.3.0-nightly (e4e93196e 2015-07-14)
> rustc --cfg phantom foo.rs
foo.rs:9:13: 9:15 error: parameter `'a` is never used [E0392]
foo.rs:9 struct Node<'a>;
                     ^~
foo.rs:9:13: 9:15 help: consider removing `'a` or using a marker such as `core::marker::PhantomData`

I think this can be closed.

@Gankra Gankra added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 19, 2015
@Gankra
Copy link
Contributor

Gankra commented Jul 19, 2015

Needs test, still?

@Gankra
Copy link
Contributor

Gankra commented Jul 19, 2015

Actually this is so ancient, I'm guessing a test was added.

@apasel422
Copy link
Contributor

There are tests for this in src/test/compile-fail/variance-regions-unused-{direct, indirect}.rs.

@Gankra
Copy link
Contributor

Gankra commented Jul 19, 2015

Sweet.

@Gankra Gankra closed this as completed Jul 19, 2015
lnicola pushed a commit to lnicola/rust that referenced this issue Oct 22, 2024
…Veykril

internal: Add more trivially `Sized` types to `is_sized` check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

6 participants