Skip to content

Audit iterator specializations for side effects #28810

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

Closed
alexcrichton opened this issue Oct 2, 2015 · 16 comments
Closed

Audit iterator specializations for side effects #28810

alexcrichton opened this issue Oct 2, 2015 · 16 comments
Labels
P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

After some discussion about #28125 the libs subteam decided that iterator adaptors should always preserve the same semantics in terms of the convenience methods and such. This was not audited for when all the initial specializations landed, so we should take a look and make sure that everything adheres to this policy.

Additionally, documentation should be added to the Iterator trait methods indicating what form of guarantees you are given (e.g. calling last is equivalent to exhausting the iterator).

triage: I-nominated

@alexcrichton alexcrichton added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed I-nominated labels Oct 2, 2015
@Stebalien
Copy link
Contributor

I've checked libcore/iter.rs. core::iter::Chain::last should exhaust a before b but that's the only bug I found (and it wasn't my bug for once 🎉).

Unless, of course, you care about drop order. I haven't tried to reason about that.

@Stebalien
Copy link
Contributor

The slice iterators (libcore/slice.rs) are fine because they are side-effect free.

@Stebalien
Copy link
Contributor

libcollections/vec and libcore/str are fine because they are also side-effect free.

bors pushed a commit that referenced this issue Oct 3, 2015
@Stebalien
Copy link
Contributor

I'd appreciate it if someone else double checked but I think that's all of them.

@nagisa
Copy link
Member

nagisa commented Oct 4, 2015

Unless, of course, you care about drop order.

This is relevant as well, because implementations of Drop may also (and usually do, in form of memory deallocation) contain side effects.

Can we make a

  • list
  • of iterators which were specialised
  • like this?

@Stebalien
Copy link
Contributor

This is relevant as well, because implementations of Drop may also (and usually do, in form of memory deallocation) contain side effects.

I strongly disagree. Drop has side effects but programmers should never rely on drop order except stack drop order because drop order is often unspecified (e.g. within structs).


Drop out of order

  • core::iter::Chain::last
  • core::iter::Peekable::last
  • core::iter::Peekable::count (fixable)
  • core::iter::Skip::last (fixable?)

Audited

  • core::iter::Chain
  • core::iter::Enumerate
  • core::iter::Peekable
  • core::iter::Skip
  • core::iter::Take
  • core::iter::Fuse
  • core::str::Bytes
  • core::slice::Iter
  • core::slice::IterMut
  • core::slice::Windows
  • core::slice::Chunks
  • core::slice::ChunksMut
  • collections::vec::IntoIter

@nagisa
Copy link
Member

nagisa commented Oct 4, 2015

I strongly disagree. Drop has side effects but programmers should never rely on drop order except stack drop order because drop order is often unspecified (e.g. within structs).

Ah right, this will be decided by rust-lang/rfcs#744 anyway.

@alexcrichton
Copy link
Member Author

Awesome, thanks for the work here @Stebalien!

@Stebalien
Copy link
Contributor

Updated list of out-of-order drops (didn't notice Peekable::count).

@alexcrichton
Copy link
Member Author

triage: P-medium

@rust-highfive rust-highfive added P-medium Medium priority and removed I-nominated labels Oct 7, 2015
@aturon
Copy link
Member

aturon commented Oct 7, 2015

(Need to make sure this behavior is documented/explicitly promised.)

@huonw
Copy link
Member

huonw commented Jan 6, 2016

Is this done? (Everything in the list above is ✅'d)

@Stebalien
Copy link
Contributor

I believe this is left open as to-be-documented. Also, I don't see any decision as to whether out of order drop is ok.

@steveklabnik steveklabnik added A-allocators Area: Custom and system allocators A-docs and removed T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-allocators Area: Custom and system allocators labels Jan 6, 2016
@steveklabnik
Copy link
Member

re-tagging as docs

@steveklabnik
Copy link
Member

So I believe that all of these are then documented in Iterator's methods, at least as far as I can tell. Can someone from libs double check me here and help provide any specifics as to what's not documented?

@steveklabnik steveklabnik added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jan 7, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 9, 2016
Doing so is considered weaker writing. Thanks @Charlotteis!

Fixes rust-lang#28810
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 9, 2016
Doing so is considered weaker writing. Thanks @Charlotteis!

Fixes rust-lang#28810
@alexcrichton
Copy link
Member Author

Looks good to me, thanks @steveklabnik!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-medium Medium priority T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants