Skip to content

Store ProtocolDecl rather than TypeLoc for designated protocol. #19202

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 1 commit into from
Sep 10, 2018
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
18 changes: 4 additions & 14 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6272,7 +6272,7 @@ class OperatorDecl : public Decl {

Identifier DesignatedProtocolName;
SourceLoc DesignatedProtocolNameLoc;
TypeLoc DesignatedProtocolTypeLoc;
ProtocolDecl *DesignatedProtocol;

public:
OperatorDecl(DeclKind kind, DeclContext *DC, SourceLoc OperatorLoc,
Expand All @@ -6293,24 +6293,14 @@ class OperatorDecl : public Decl {
return DesignatedProtocolName;
}

void setDesignatedProtocolName(Identifier name) {
DesignatedProtocolName = name;
}

SourceLoc getDesignatedProtocolNameLoc() const {
return DesignatedProtocolNameLoc;
}

void setDesignatedProtocolNameLoc(SourceLoc loc) {
DesignatedProtocolNameLoc = loc;
}

TypeLoc getDesignatedProtocolTypeLoc() const {
return DesignatedProtocolTypeLoc;
}
ProtocolDecl *getDesignatedProtocol() const { return DesignatedProtocol; }

void setDesignatedProtocolTypeLoc(TypeLoc typeLoc) {
DesignatedProtocolTypeLoc = typeLoc;
void setDesignatedProtocol(ProtocolDecl *protocol) {
DesignatedProtocol = protocol;
}

static bool classof(const Decl *D) {
Expand Down
12 changes: 6 additions & 6 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,14 +2074,14 @@ static void checkDesignatedProtocol(OperatorDecl *OD, Identifier name,
}

if (!typeLoc.isError()) {
OD->setDesignatedProtocolTypeLoc(typeLoc);
auto *decl = typeLoc.getType()->getNominalOrBoundGenericNominal();
if (!decl || !isa<ProtocolDecl>(decl)) {
tc.diagnose(typeLoc.getLoc(),
diag::operators_designated_protocol_not_a_protocol,
typeLoc.getType());
OD->setInvalid();
} else {
OD->setDesignatedProtocol(cast<ProtocolDecl>(decl));
// FIXME: verify this operator has a declaration within this
// protocol with the same arity and fixity
}
Expand All @@ -2104,9 +2104,9 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {

// Pre- or post-fix operator?
if (!IOD) {
auto typeLoc = OD->getDesignatedProtocolTypeLoc();
auto *protocol = OD->getDesignatedProtocol();
auto protocolId = OD->getDesignatedProtocolName();
if (typeLoc.isNull() && !protocolId.empty() &&
if (!protocol && !protocolId.empty() &&
enableOperatorDesignatedProtocols) {
auto protocolIdLoc = OD->getDesignatedProtocolNameLoc();
checkDesignatedProtocol(OD, protocolId, protocolIdLoc, *this, Context);
Expand All @@ -2127,8 +2127,8 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {
}

auto secondId = IOD->getSecondIdentifier();
auto typeLoc = IOD->getDesignatedProtocolTypeLoc();
if (typeLoc.isNull() && enableOperatorDesignatedProtocols) {
auto *protocol = IOD->getDesignatedProtocol();
if (!protocol && enableOperatorDesignatedProtocols) {
auto secondIdLoc = IOD->getSecondIdentifierLoc();
assert(secondId.empty() || !firstId.empty());

Expand All @@ -2140,7 +2140,7 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {

if (!group && !IOD->isInvalid()) {
if (!firstId.empty() &&
(!secondId.empty() || IOD->getDesignatedProtocolTypeLoc().isNull())) {
(!secondId.empty() || !IOD->getDesignatedProtocol())) {
diagnose(firstIdLoc, diag::unknown_precedence_group, firstId);
IOD->setInvalid();
}
Expand Down