#4926·antlr4

[C++] Failed assertion in `Parser::getParseListeners` : "vector iterators in range are from different containers", bug in `Parser::setTrimParseTree`

Author: Olivier11515Created Mar 11, 2026Updated Jul 3, 2026

Hello,

There is a bug at this line:

https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Cpp/runtime/src/Parser.cpp#L189

getParseListeners returns a std::vector (not a reference) so the iterators used in find() come from different containers, which is not allowed in C++. In debug this results in a failed assertion "vector iterators in range are from different containers".

The fix would be for example:

cpp
bool Parser::getTrimParseTree() {
  const std::vector<tree::ParseTreeListener *> vec = getParseListeners();
  return std::find(vec.begin(), vec.end(), &TrimToSizeListener::INSTANCE) != vec.end();
}

Note that this code path is used when we use the setTrimParseTree method of Parser. As a result, setTrimParseTree may not behave the way it should.

Thanks!