Skip to content

spirv-opt: iterator_template has wrong return type #3397

@Vasniktel

Description

@Vasniktel

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 method

A 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(...);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions