Discussion:
[rust-dev] reference has a longer lifetime than the data it references
Christophe Pedretti
2014-09-11 19:26:16 UTC
Permalink
Hello,

with 0.11.0, this code was ok

pub struct Cursor<'a, 'b> {
pStmt : &'b Statement<'a>,
error : bool
}
now, with the nighly, i receive a

sql\connection.rs:50:1: 53:2 error: in type `&'b
sql::connection::Statement<'a>`, reference has a longer lifetime than the
data it references
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: the pointer is valid for the lifetime 'b
as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: but the referenced data is only valid
for the lifetime 'a as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
error: aborting due to previous error

why this change ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140911/58ed4019/attachment.html>
Chris Skittles
2014-09-11 19:35:16 UTC
Permalink
Probably because its wrong. It was likely a compiler bug that got fixed.

The compiler says pretty clearly what the issue is. What if A has a
shorter lifetime than B? Then you are basically holding a reference to
data that has been disposed, which is what "can't" happen in rust.

On Thu, Sep 11, 2014 at 9:26 PM, Christophe Pedretti
Post by Christophe Pedretti
Hello,
with 0.11.0, this code was ok
Post by Christophe Pedretti
pub struct Cursor<'a, 'b> {
pStmt : &'b Statement<'a>,
error : bool
}
now, with the nighly, i receive a
sql\connection.rs:50:1: 53:2 error: in type `&'b
sql::connection::Statement<'a>`, reference has a longer lifetime than the
data it references
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: the pointer is valid for the lifetime 'b
as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: but the referenced data is only valid for
the lifetime 'a as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
error: aborting due to previous error
why this change ?
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
Björn Steinbrink
2014-09-11 19:44:41 UTC
Permalink
Post by Christophe Pedretti
Hello,
with 0.11.0, this code was ok
pub struct Cursor<'a, 'b> {
pStmt : &'b Statement<'a>,
error : bool
}
You need a bound on the lifetime now, to guarantee that the reference(s)
with lifetime 'a contained in Statement<'a> are valid for at least as
long as the reference with lifetime 'b.

So the struct should now look like this:

pub struct Cursor<'a, 'b: 'a> {
pStmt: &'a Statement<'b>,
error: bool,
}

This is correctly handled now that RFC 49 [1] has been implemented.

The corresponding PR is #16453 [2] and it lists the relevant breaking changes.

Bj?rn

[1] https://github.com/rust-lang/rfcs/pull/192
[2] https://github.com/rust-lang/rust/pull/16453
Post by Christophe Pedretti
now, with the nighly, i receive a
sql\connection.rs:50:1: 53:2 error: in type `&'b
sql::connection::Statement<'a>`, reference has a longer lifetime than the
data it references
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: the pointer is valid for the lifetime 'b
as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
sql\connection.rs:50:1: 53:2 note: but the referenced data is only valid
for the lifetime 'a as defined on the struct at 50:0
sql\connection.rs:50 pub struct Cursor<'a, 'b> {
sql\connection.rs:51 pStmt : &'b Statement<'a>,
sql\connection.rs:52 error : bool
sql\connection.rs:53 }
error: aborting due to previous error
why this change ?
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
Loading...