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

std::vec: Use a valid value as lifetime dummy in iterator #9557

Merged
merged 1 commit into from
Sep 28, 2013
Merged

std::vec: Use a valid value as lifetime dummy in iterator #9557

merged 1 commit into from
Sep 28, 2013

Conversation

bluss
Copy link
Member

@bluss bluss commented Sep 27, 2013

std::vec: Use a valid value as lifetime dummy in iterator

The current implementation uses &v[0] for the lifetime struct field,
but that is a dangling pointer for iterators derived from zero-length
slices.

Example:

let v: [int, ..0] = [];  println!("{:?}", v.iter())

std::vec::VecIterator<,int>{ptr: (0x7f3768626100 as *()), end: (0x7f3768626100 as *()), lifetime: &139875951207128}

To replace this parameter, use a field of type Option<&'self ()>
that is simply initialized with None, but still allows the iterator to
have a lifetime parameter.

@thestinger
Copy link
Contributor

@blake2-ppc: if you use Option<&'self ()>, you can set this to None and avoid the static

The current implementation uses `&v[0]` for the lifetime struct field,
but that is a dangling pointer for iterators derived from zero-length
slices.

Example:

    let v: [int, ..0] = [];  println!("{:?}", v.iter())

    std::vec::VecIterator<,int>{ptr: (0x7f3768626100 as *()), end: (0x7f3768626100 as *()), lifetime: &139875951207128}

To replace this parameter, use a field of type `Option<&'self ()>`
that is simply initialized with `None`, but still allows the iterator to
have a lifetime parameter.
@bluss
Copy link
Member Author

bluss commented Sep 27, 2013

Updated to use your Option idea, this is so much better! Also I had "cleverly" used &'self () even in the VecMutIterator in the first revision, making it implicitly copyable.. that was not intended.

bors added a commit that referenced this pull request Sep 28, 2013
std::vec: Use a valid value as lifetime dummy in iterator

The current implementation uses `&v[0]` for the lifetime struct field,
but that is a dangling pointer for iterators derived from zero-length
slices.

Example:

    let v: [int, ..0] = [];  println!("{:?}", v.iter())

    std::vec::VecIterator<,int>{ptr: (0x7f3768626100 as *()), end: (0x7f3768626100 as *()), lifetime: &139875951207128}

To replace this parameter, use a field of type `Option<&'self ()>`
that is simply initialized with `None`, but still allows the iterator to
have a lifetime parameter.
@bors bors closed this Sep 28, 2013
@bors bors merged commit c0e1c09 into rust-lang:master Sep 28, 2013
@bluss bluss deleted the vec-lifetime-token branch September 28, 2013 03:27
flip1995 pushed a commit to flip1995/rust that referenced this pull request Oct 6, 2022
…arth

doc: make the usage of clippy.toml more clear

Added a subsection for it, and move it after `Allowing/denying lints` which is more frequently used.

fix rust-lang/rust-clippy#9265

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants