-
Notifications
You must be signed in to change notification settings - Fork 664
spirv-opt: iterator_template has wrong return type #3397
Description
IntrusiveList::iterator_template serves as a base class for some iterators in the library (e.g. InstructionList::iterator). It also defines pre-increment and pre-decrement operators. The problem lies in the return type of those operators. If one uses operator++ on InstructionList::iterator, he will get a reference to IntrusiveList::iterator_template, not InstructionList::iterator. This means that some methods defined in the InstructionList::iterator won't be accessible to the user. A solution could be to use a curiously recurring template pattern in pretty much the same way it's used in IntrusiveList class.
Example code:
opt::InstructionList::iterator iter = fuzzerutil::GetIteratorForInstruction(...);
auto it = ++iter;
it.InsertBefore(...); // compiler error: IntrusiveList::iterator_template has no InsertBefore methodA workaround, suggested by @paulthomson, is to use the old value of the iterator, which has the desired type. This wouldn't work for post-increment if we had them, though.
opt::InstructionList::iterator iter = fuzzerutil::GetIteratorForInstruction(...);
++iter;
iter.InsertBefore(...);