Skip to content

Commit d28cf1c

Browse files
authored
[IRGen] Fix SynthesizedFileUnit. (#31082)
SynthesizedFileUnits are attached to a SourceFile. Make IRGen consistent with TBDGen: when processing a SourceFile, only process the attached SynthesizedFile. This avoids IRGen/TBDGen inconsistencies. Use SourceFile name in SynthesizedFileUnit::getDiscriminatorForPrivateValue. Resolves TF-1249.
1 parent 948372b commit d28cf1c

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lib/AST/Module.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,16 +2694,21 @@ SynthesizedFileUnit::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
26942694
if (!PrivateDiscriminator.empty())
26952695
return PrivateDiscriminator;
26962696

2697-
assert(1 == count_if(getParentModule()->getFiles(),
2698-
[](const FileUnit *FU) -> bool {
2699-
return isa<SynthesizedFileUnit>(FU);
2700-
}) &&
2701-
"Cannot promise uniqueness if multiple synthesized file units exist");
2697+
StringRef sourceFileName = getSourceFile().getFilename();
2698+
if (sourceFileName.empty()) {
2699+
assert(1 == count_if(getParentModule()->getFiles(),
2700+
[](const FileUnit *FU) -> bool {
2701+
return isa<SourceFile>(FU) &&
2702+
cast<SourceFile>(FU)->getFilename().empty();
2703+
}) &&
2704+
"Cannot promise uniqueness if multiple source files are nameless");
2705+
}
27022706

27032707
// Use a discriminator invariant across Swift version: a hash of the module
2704-
// name and a special string.
2708+
// name, the parent source file name, and a special string.
27052709
llvm::MD5 hash;
27062710
hash.update(getParentModule()->getName().str());
2711+
hash.update(llvm::sys::path::filename(sourceFileName));
27072712
// TODO: Use a more robust discriminator for synthesized files. Pick something
27082713
// that cannot conflict with `SourceFile` discriminators.
27092714
hash.update("SYNTHESIZED FILE");

lib/IRGen/IRGen.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,16 @@ performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
982982

983983
if (SF) {
984984
IGM.emitSourceFile(*SF);
985+
// Emit synthesized file unit, if it exists.
986+
if (auto *synthesizedFile = SF->getSynthesizedFile())
987+
IGM.emitSynthesizedFileUnit(*synthesizedFile);
985988
} else {
986989
for (auto *File : M->getFiles()) {
987990
if (auto *nextSF = dyn_cast<SourceFile>(File)) {
988991
if (nextSF->ASTStage >= SourceFile::TypeChecked)
989992
IGM.emitSourceFile(*nextSF);
993+
} else if (auto *nextSFU = dyn_cast<SynthesizedFileUnit>(File)) {
994+
IGM.emitSynthesizedFileUnit(*nextSFU);
990995
} else {
991996
File->collectLinkLibraries([&IGM](LinkLibrary LinkLib) {
992997
IGM.addLinkLibrary(LinkLib);
@@ -995,11 +1000,6 @@ performIRGeneration(const IRGenOptions &Opts, ModuleDecl *M,
9951000
}
9961001
}
9971002

998-
// Emit synthesized file units.
999-
for (auto *File : M->getFiles())
1000-
if (auto *nextSFU = dyn_cast<SynthesizedFileUnit>(File))
1001-
IGM.emitSynthesizedFileUnit(*nextSFU);
1002-
10031003
// Okay, emit any definitions that we suddenly need.
10041004
irgen.emitLazyDefinitions();
10051005

0 commit comments

Comments
 (0)