Skip to content

Commit df86e08

Browse files
Merge pull request #80172 from cachemeifyoucan/eng/PR-147513165
Fix `.swiftdeps` file deterministic issues
2 parents e46da50 + 4115be5 commit df86e08

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

lib/AST/Identifier.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ int Identifier::compare(Identifier other) const {
9898
}
9999

100100
int DeclName::compare(DeclName other) const {
101+
// Fast equality comparsion.
102+
if (getOpaqueValue() == other.getOpaqueValue())
103+
return 0;
104+
105+
101106
// Compare base names.
102107
if (int result = getBaseName().compare(other.getBaseName()))
103108
return result;
@@ -111,10 +116,13 @@ int DeclName::compare(DeclName other) const {
111116
return result;
112117
}
113118

114-
if (argNames.size() == otherArgNames.size())
115-
return 0;
119+
if (argNames.size() != otherArgNames.size())
120+
return argNames.size() < otherArgNames.size() ? -1 : 1;
116121

117-
return argNames.size() < otherArgNames.size() ? -1 : 1;
122+
// Order based on if it is compound name or not.
123+
assert(isSimpleName() != other.isSimpleName() &&
124+
"equality should be covered by opaque value comparsion");
125+
return isSimpleName() ? -1 : 1;
118126
}
119127

120128
static bool equals(ArrayRef<Identifier> idents, ArrayRef<StringRef> strings) {

lib/AST/Module.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,17 @@ void SourceLookupCache::lookupClassMembers(ImportPath::Access accessPath,
694694
VisibleDeclConsumer &consumer) {
695695
assert(accessPath.size() <= 1 && "can only refer to top-level decls");
696696

697-
if (!accessPath.empty()) {
698-
for (auto &member : ClassMembers) {
699-
// Non-simple names are also stored under their simple name, so make
700-
// sure to only report them once.
701-
if (!member.first.isSimpleName())
702-
continue;
697+
std::vector<std::pair<DeclName, TinyPtrVector<ValueDecl *>>> OrderedMembers;
698+
for (auto &member : ClassMembers) {
699+
if (!member.first.isSimpleName())
700+
continue;
701+
OrderedMembers.emplace_back(member.first, member.second);
702+
}
703+
llvm::sort(OrderedMembers,
704+
[](auto &LHS, auto &RHS) { return LHS.first < RHS.first; });
703705

706+
if (!accessPath.empty()) {
707+
for (auto &member : OrderedMembers) {
704708
for (ValueDecl *vd : member.second) {
705709
auto *nominal = vd->getDeclContext()->getSelfNominalTypeDecl();
706710
if (nominal && nominal->getName() == accessPath.front().Item)
@@ -712,12 +716,7 @@ void SourceLookupCache::lookupClassMembers(ImportPath::Access accessPath,
712716
return;
713717
}
714718

715-
for (auto &member : ClassMembers) {
716-
// Non-simple names are also stored under their simple name, so make sure to
717-
// only report them once.
718-
if (!member.first.isSimpleName())
719-
continue;
720-
719+
for (auto &member : OrderedMembers) {
721720
for (ValueDecl *vd : member.second)
722721
if (ABIRoleInfo(vd).matchesOptions(OptionSet<ModuleLookupFlags>())) // FIXME: figure this out
723722
consumer.foundDecl(vd, DeclVisibilityKind::DynamicLookup,

lib/FrontendTool/FrontendTool.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/ASTDumper.h"
2727
#include "swift/AST/ASTMangler.h"
2828
#include "swift/AST/AvailabilityScope.h"
29+
#include "swift/AST/DiagnosticConsumer.h"
2930
#include "swift/AST/DiagnosticsFrontend.h"
3031
#include "swift/AST/DiagnosticsSema.h"
3132
#include "swift/AST/FileSystem.h"
@@ -2100,6 +2101,11 @@ int swift::performFrontend(ArrayRef<const char *> Args,
21002101
// Setup a verfication instance to run.
21012102
std::unique_ptr<CompilerInstance> VerifyInstance =
21022103
std::make_unique<CompilerInstance>();
2104+
// Add a null diagnostic consumer to the diagnostic engine so the
2105+
// compilation will exercise all the diagnose code path but not emitting
2106+
// anything.
2107+
NullDiagnosticConsumer DC;
2108+
VerifyInstance->getDiags().addConsumer(DC);
21032109
std::string InstanceSetupError;
21042110
// This should not fail because it passed already.
21052111
(void)VerifyInstance->setup(Invocation, InstanceSetupError, Args);

test/Frontend/output_determinism_check.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,31 @@
3737
// PCM_OUTPUT: remark: produced matching output file '{{.*}}{{/|\\}}test.pcm'
3838

3939
public var x = 1
40-
public func test() {}
40+
public func test() {
41+
precondition(x == 1, "dummy check")
42+
}
43+
44+
class A {
45+
var a = 0
46+
var b = 0
47+
var c = 0
48+
var d = 0
49+
}
50+
class B {
51+
var a = 0
52+
var b = 0
53+
var c = 0
54+
var d = 0
55+
}
56+
class C {
57+
var a = 0
58+
var b = 0
59+
var c = 0
60+
var d = 0
61+
}
62+
class D {
63+
var a = 0
64+
var b = 0
65+
var c = 0
66+
var d = 0
67+
}

0 commit comments

Comments
 (0)