Skip to content

Commit 2f03c95

Browse files
committed
[Serialization] ModularizationError keeps full objects
Preserve more information about the context of modularization errors by replacing the module names with pointers to ModuleDecl and ModuleFile objects.
1 parent 814ca43 commit 2f03c95

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ namespace swift {
143143
StringRef StringVal;
144144
DeclNameRef IdentifierVal;
145145
ObjCSelector ObjCSelectorVal;
146-
ValueDecl *TheValueDecl;
146+
const ValueDecl *TheValueDecl;
147147
Type TypeVal;
148148
TypeRepr *TyR;
149149
FullyQualified<Type> FullyQualifiedTypeVal;
@@ -194,7 +194,7 @@ namespace swift {
194194
: Kind(DiagnosticArgumentKind::ObjCSelector), ObjCSelectorVal(S) {
195195
}
196196

197-
DiagnosticArgument(ValueDecl *VD)
197+
DiagnosticArgument(const ValueDecl *VD)
198198
: Kind(DiagnosticArgumentKind::ValueDecl), TheValueDecl(VD) {
199199
}
200200

@@ -305,7 +305,7 @@ namespace swift {
305305
return ObjCSelectorVal;
306306
}
307307

308-
ValueDecl *getAsValueDecl() const {
308+
const ValueDecl *getAsValueDecl() const {
309309
assert(Kind == DiagnosticArgumentKind::ValueDecl);
310310
return TheValueDecl;
311311
}

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -875,16 +875,16 @@ ERROR(need_hermetic_seal_to_import_module,none,
875875
ERROR(modularization_issue_decl_moved,Fatal,
876876
"reference to %select{top-level|type}0 %1 broken by a context change; "
877877
"%1 was expected to be in %2, but now a candidate is found only in %3",
878-
(bool, DeclName, Identifier, Identifier))
878+
(bool, DeclName, const ModuleDecl*, const ModuleDecl*))
879879
ERROR(modularization_issue_decl_type_changed,Fatal,
880880
"reference to %select{top-level|type}0 %1 broken by a context change; "
881881
"the declaration kind of %1 %select{from %2|}5 changed since building '%3'"
882-
"%select{|, it was in %2 and is now found in %4}5",
883-
(bool, DeclName, Identifier, StringRef, Identifier, bool))
882+
"%select{|, it was in %2 and is now a candidate is found only in %4}5",
883+
(bool, DeclName, const ModuleDecl*, StringRef, const ModuleDecl*, bool))
884884
ERROR(modularization_issue_decl_not_found,Fatal,
885885
"reference to %select{top-level|type}0 %1 broken by a context change; "
886886
"%1 is not found, it was expected to be in %2",
887-
(bool, DeclName, Identifier))
887+
(bool, DeclName, const ModuleDecl*))
888888

889889
NOTE(modularization_issue_side_effect_extension_error,none,
890890
"could not deserialize extension",

lib/Serialization/Deserialization.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,21 @@ ModularizationError::diagnose(const ModuleFile *MF,
197197
auto diagnoseError = [&](Kind errorKind) {
198198
switch (errorKind) {
199199
case Kind::DeclMoved:
200-
return ctx.Diags.diagnose(MF->getSourceLoc(), diag::modularization_issue_decl_moved,
201-
declIsType, name, expectedModuleName,
202-
foundModuleName);
200+
return ctx.Diags.diagnose(getSourceLoc(),
201+
diag::modularization_issue_decl_moved,
202+
declIsType, name, expectedModule,
203+
foundModule);
203204
case Kind::DeclKindChanged:
204205
return
205-
ctx.Diags.diagnose(MF->getSourceLoc(), diag::modularization_issue_decl_type_changed,
206-
declIsType, name, expectedModuleName,
207-
referencedFromModuleName, foundModuleName,
208-
foundModuleName != expectedModuleName);
206+
ctx.Diags.diagnose(getSourceLoc(),
207+
diag::modularization_issue_decl_type_changed,
208+
declIsType, name, expectedModule,
209+
referenceModule->getName(), foundModule,
210+
foundModule != expectedModule);
209211
case Kind::DeclNotFound:
210-
return ctx.Diags.diagnose(MF->getSourceLoc(), diag::modularization_issue_decl_not_found,
211-
declIsType, name, expectedModuleName);
212+
return ctx.Diags.diagnose(getSourceLoc(),
213+
diag::modularization_issue_decl_not_found,
214+
declIsType, name, expectedModule);
212215
}
213216
llvm_unreachable("Unhandled ModularizationError::Kind in switch.");
214217
};
@@ -1957,7 +1960,7 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
19571960
SmallVector<char, 64> strScratch;
19581961

19591962
auto errorKind = ModularizationError::Kind::DeclNotFound;
1960-
Identifier foundIn;
1963+
ModuleDecl *foundIn = nullptr;
19611964
bool isType = false;
19621965

19631966
if (recordID == XREF_TYPE_PATH_PIECE ||
@@ -2011,28 +2014,26 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
20112014
// one because otherwise it would have succeeded on the first search.
20122015
// This is usually caused by the use of poorly modularized headers.
20132016
errorKind = ModularizationError::Kind::DeclMoved;
2014-
foundIn = otherModule->getName();
2017+
foundIn = otherModule;
20152018
break;
20162019
} else if (hadAMatchBeforeFiltering) {
20172020
// Found a match that was filtered out. This may be from the same
20182021
// expected module if there's a type difference. This can be caused
20192022
// by the use of different Swift language versions between a library
20202023
// with serialized SIL and a client.
20212024
errorKind = ModularizationError::Kind::DeclKindChanged;
2022-
foundIn = otherModule->getName();
2025+
foundIn = otherModule;
20232026
break;
20242027
}
20252028
}
20262029
}
20272030

20282031
auto declName = getXRefDeclNameForError();
2029-
auto expectedIn = baseModule->getName();
2030-
auto referencedFrom = getName();
20312032
auto error = llvm::make_error<ModularizationError>(declName,
20322033
isType,
20332034
errorKind,
2034-
expectedIn,
2035-
referencedFrom,
2035+
baseModule,
2036+
this,
20362037
foundIn,
20372038
pathTrace);
20382039

lib/Serialization/DeserializationErrors.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -355,40 +355,47 @@ class ModularizationError : public llvm::ErrorInfo<ModularizationError> {
355355
DeclName name;
356356
bool declIsType;
357357
Kind errorKind;
358-
Identifier expectedModuleName;
359-
StringRef referencedFromModuleName;
360-
Identifier foundModuleName;
358+
359+
/// Module targeted by the reference that should define the decl.
360+
const ModuleDecl *expectedModule;
361+
362+
/// Binary module file with the outgoing reference.
363+
const ModuleFile *referenceModule;
364+
365+
/// Module where the compiler did find the decl, if any.
366+
const ModuleDecl *foundModule;
367+
361368
XRefTracePath path;
362369

363370
public:
364371
explicit ModularizationError(DeclName name, bool declIsType, Kind errorKind,
365-
Identifier expectedModuleName,
366-
StringRef referencedFromModuleName,
367-
Identifier foundModuleName,
372+
const ModuleDecl *expectedModule,
373+
const ModuleFile *referenceModule,
374+
const ModuleDecl *foundModule,
368375
XRefTracePath path):
369376
name(name), declIsType(declIsType), errorKind(errorKind),
370-
expectedModuleName(expectedModuleName),
371-
referencedFromModuleName(referencedFromModuleName),
372-
foundModuleName(foundModuleName), path(path) {}
377+
expectedModule(expectedModule),
378+
referenceModule(referenceModule),
379+
foundModule(foundModule), path(path) {}
373380

374381
void diagnose(const ModuleFile *MF,
375382
DiagnosticBehavior limit = DiagnosticBehavior::Fatal) const;
376383

377384
void log(raw_ostream &OS) const override {
378385
OS << "modularization issue on '" << name << "', reference from '";
379-
OS << referencedFromModuleName << "' not resolvable: ";
386+
OS << referenceModule->getName() << "' not resolvable: ";
380387
switch (errorKind) {
381388
case Kind::DeclMoved:
382-
OS << "expected in '" << expectedModuleName << "' but found in '";
383-
OS << foundModuleName << "'";
389+
OS << "expected in '" << expectedModule->getName() << "' but found in '";
390+
OS << foundModule->getName() << "'";
384391
break;
385392
case Kind::DeclKindChanged:
386393
OS << "decl details changed between what was imported from '";
387-
OS << expectedModuleName << "' and what is now imported from '";
388-
OS << foundModuleName << "'";
394+
OS << expectedModule->getName() << "' and what is now imported from '";
395+
OS << foundModule->getName() << "'";
389396
break;
390397
case Kind::DeclNotFound:
391-
OS << "not found, expected in '" << expectedModuleName << "'";
398+
OS << "not found, expected in '" << expectedModule->getName() << "'";
392399
break;
393400
}
394401
OS << "\n";

test/Serialization/modularization-error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// RUN: %target-swift-frontend %t/LibTypeChanged.swift -emit-module-path %t/B.swiftmodule -module-name B
3535
// RUN: not %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t 2>&1 \
3636
// RUN: | %FileCheck --check-prefixes CHECK,CHECK-KIND-CHANGED-AND-MOVED %s
37-
// CHECK-KIND-CHANGED-AND-MOVED: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' changed since building 'LibWithXRef', it was in 'A' and is now found in 'B'
37+
// CHECK-KIND-CHANGED-AND-MOVED: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' changed since building 'LibWithXRef', it was in 'A' and is now a candidate is found only in 'B'
3838

3939
/// Remove MyType from all imported modules.
4040
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/A.swiftmodule -module-name A

0 commit comments

Comments
 (0)