Skip to content

Commit 4c9d933

Browse files
committed
[Parse] Fix hash accumulation in decl member parsing
For example, given: class C: P { func foo() {} } For the outer context (i.e. source file), the interface hash shoule be 'class C : P { }' for the member list, it's '{ func foo ( ) { } }'. This must be the same regardless delayed parsing is enabled.
1 parent 8ee108a commit 4c9d933

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,10 @@ bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
47644764
return true;
47654765
}
47664766

4767+
// Record '{' '}' to the current hash, nothing else.
4768+
recordTokenHash("}");
4769+
llvm::SaveAndRestore<Optional<StableHasher>> T(CurrentTokenHash, None);
4770+
47674771
bool HasOperatorDeclarations;
47684772
bool HasNestedClassDeclarations;
47694773

@@ -4808,12 +4812,12 @@ Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc, Diag<> ErrorDiag,
48084812
ParseDeclOptions Options, IterableDeclContext *IDC,
48094813
bool &hadError) {
48104814

4811-
// If we're hashing the type body separately, record the curly braces but
4812-
// nothing inside for the interface hash.
4815+
// Hash the type body separately.
48134816
llvm::SaveAndRestore<Optional<StableHasher>> MemberHashingScope{
48144817
CurrentTokenHash, StableHasher::defaultHasher()};
4818+
4819+
// Record '{' which has been consumed in callers.
48154820
recordTokenHash("{");
4816-
recordTokenHash("}");
48174821

48184822
std::vector<Decl *> decls;
48194823
ParserStatus Status;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
// RUN: %target-swift-frontend -dump-interface-hash -primary-file %t/a.swift 2> %t/a.hash
4+
// RUN: %target-swift-frontend -dump-interface-hash -primary-file %t/b.swift 2> %t/b.hash
5+
// RUN: %target-swift-frontend -dump-interface-hash -primary-file %t/a.swift -experimental-skip-all-function-bodies 2> %t/c.hash
6+
// RUN: %target-swift-frontend -dump-interface-hash -primary-file %t/b.swift -experimental-skip-all-function-bodies 2> %t/d.hash
7+
// RUN: cmp %t/a.hash %t/b.hash
8+
// RUN: cmp %t/a.hash %t/c.hash
9+
// RUN: cmp %t/a.hash %t/d.hash
10+
11+
// Make sure "interface hash" doesn't change after modifying type members, and
12+
13+
// BEGIN a.swift
14+
class C {
15+
func foo() {}
16+
}
17+
18+
// BEGIN b.swift
19+
class C {
20+
func foo() {}
21+
func bar() {}
22+
}

test/Serialization/sourceinfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ import MyModule
77
// RUN: %target-swift-ide-test -print-module-metadata -module-to-print MyModule -enable-swiftsourceinfo -I %t/Modules -source-filename %s | %FileCheck %s
88

99
// CHECK: filepath=SOURCE_DIR{{[/\\]}}test{{[/\\]}}Serialization{{[/\\]}}Inputs{{[/\\]}}SourceInfo{{[/\\]}}File1.swift; hash=9da710e9b2de1fff2915639236b8929c; mtime={{[0-9]{4}-[0-9]{2}-[0-9]{2} .*}}; size=35
10-
// CHECK: filepath=SOURCE_DIR{{[/\\]}}test{{[/\\]}}Serialization{{[/\\]}}Inputs{{[/\\]}}SourceInfo{{[/\\]}}File2.swift; hash=4ce628834bb98fd822ac840ea341de26; mtime={{[0-9]{4}-[0-9]{2}-[0-9]{2} .*}}; size=57
10+
// CHECK: filepath=SOURCE_DIR{{[/\\]}}test{{[/\\]}}Serialization{{[/\\]}}Inputs{{[/\\]}}SourceInfo{{[/\\]}}File2.swift; hash=22b75a7717318d48f7a979906f35195e; mtime={{[0-9]{4}-[0-9]{2}-[0-9]{2} .*}}; size=57

0 commit comments

Comments
 (0)