Skip to content

Commit a5e6800

Browse files
authored
Merge pull request #39646 from slavapestov/fix-rqm-reference-invalidation
RequirementMachine: Fix reference invalidation bug
2 parents e72f819 + 526feeb commit a5e6800

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,13 @@ RequirementMachine *RewriteContext::getRequirementMachine(
367367
// Store this requirement machine before adding the signature,
368368
// to catch re-entrant construction via initWithGenericSignature()
369369
// below.
370-
machine = new rewriting::RequirementMachine(*this);
371-
machine->initWithGenericSignature(sig);
370+
auto *newMachine = new rewriting::RequirementMachine(*this);
371+
machine = newMachine;
372372

373-
return machine;
373+
// This might re-entrantly invalidate 'machine', which is a reference
374+
// into Protos.
375+
newMachine->initWithGenericSignature(sig);
376+
return newMachine;
374377
}
375378

376379
bool RewriteContext::isRecursivelyConstructingRequirementMachine(
@@ -484,14 +487,19 @@ RequirementMachine *RewriteContext::getRequirementMachine(
484487
llvm::errs() << " " << proto->getName();
485488
abort();
486489
}
487-
} else {
488-
// Construct a requirement machine from the structural requirements of
489-
// the given set of protocols.
490-
machine = new RequirementMachine(*this);
491-
machine->initWithProtocols(component.Protos);
490+
491+
return machine;
492492
}
493493

494-
return machine;
494+
// Construct a requirement machine from the structural requirements of
495+
// the given set of protocols.
496+
auto *newMachine = new RequirementMachine(*this);
497+
machine = newMachine;
498+
499+
// This might re-entrantly invalidate 'machine', which is a reference
500+
// into Protos.
501+
newMachine->initWithProtocols(component.Protos);
502+
return newMachine;
495503
}
496504

497505
/// We print stats in the destructor, which should get executed at the end of

0 commit comments

Comments
 (0)