Skip to content

[Diagnostics] Make sure extension type repr is available before using it #37887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,9 @@ class ExtensionDecl final : public GenericContext, public Decl,
bool alreadyBoundToNominal() const { return NextExtension.getInt(); }

/// Retrieve the extended type definition as written in the source, if it exists.
///
/// Repr would not be available if the extension was been loaded
/// from a serialized module.
TypeRepr *getExtendedTypeRepr() const { return ExtendedTypeRepr; }

/// Retrieve the set of protocols that this type inherits (i.e,
Expand Down
12 changes: 9 additions & 3 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7432,9 +7432,15 @@ bool InvalidMemberRefOnProtocolMetatype::diagnoseAsError() {
if (auto *whereClause = extension->getTrailingWhereClause()) {
auto sourceRange = whereClause->getSourceRange();
note.fixItInsertAfter(sourceRange.End, ", Self == <#Type#> ");
} else {
auto nameRepr = extension->getExtendedTypeRepr();
note.fixItInsertAfter(nameRepr->getEndLoc(), " where Self == <#Type#>");
} else if (auto nameRepr = extension->getExtendedTypeRepr()) {
// Type repr is not always available so we need to be defensive
// about its presence and validity.
if (nameRepr->isInvalid())
return true;

if (auto noteLoc = nameRepr->getEndLoc()) {
note.fixItInsertAfter(noteLoc, " where Self == <#Type#>");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't have the extended type repr, you should have the extended type. Can that be diagnosed even if it can't be fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be diagnose, just wouldn't have the note.


return true;
Expand Down