- private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements(
- final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
- final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
- final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
+ private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements() {
+ final var declared = prototype.mutableDeclaredSubstatements();
+ final var effective = prototype.mutableEffectiveSubstatements();
+
+ // We are about to instantiate some substatements. The simple act of materializing them may end up triggering
+ // namespace lookups, which in turn can materialize copies by themselves, running ahead of our materialization.
+ // We therefore need a meeting place for, which are the partially-materialized substatements. If we do not have
+ // them yet, instantiate them and we need to populate them as well.
+ final int expectedSize = declared.size() + effective.size();
+ var materializedSchemaTree = castMaterialized(substatements);
+ if (materializedSchemaTree == null) {
+ substatements = materializedSchemaTree = Maps.newHashMapWithExpectedSize(expectedSize);
+ }