Skip to content

Commit e4ac96e

Browse files
authored
Merge pull request #1866 from swiftwasm/main
[pull] swiftwasm from main
2 parents 1bc21f4 + a73a808 commit e4ac96e

14 files changed

+122
-74
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ class SuperRefExpr : public Expr {
13321332
: Expr(ExprKind::SuperRef, Implicit, SuperTy), Self(Self), Loc(Loc) {}
13331333

13341334
VarDecl *getSelf() const { return Self; }
1335+
void setSelf(VarDecl *self) { Self = self; }
13351336

13361337
SourceLoc getSuperLoc() const { return Loc; }
13371338
SourceRange getSourceRange() const { return Loc; }

lib/AST/UnqualifiedLookup.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ namespace {
144144
static const unsigned targetLookup;
145145
#endif
146146

147-
public: // for exp debugging
148-
unsigned resultsSizeBeforeLocalsPass = ~0;
149-
150147
public:
151148
// clang-format off
152149
UnqualifiedLookupFactory(DeclNameRef Name,
@@ -708,16 +705,10 @@ void UnqualifiedLookupFactory::printScopes(raw_ostream &out) const {
708705

709706
void UnqualifiedLookupFactory::printResults(raw_ostream &out) const {
710707
for (auto i : indices(Results)) {
711-
if (i == resultsSizeBeforeLocalsPass)
712-
out << "============== next pass ============\n";
713708
out << i << ": ";
714709
Results[i].print(out);
715710
out << "\n";
716711
}
717-
if (resultsSizeBeforeLocalsPass == Results.size())
718-
out << "============== next pass ============\n";
719-
if (resultsSizeBeforeLocalsPass == ~0u)
720-
out << "never tried locals\n\n";
721712
}
722713

723714
void UnqualifiedLookupFactory::print(raw_ostream &OS) const {

lib/IDE/CodeCompletionCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static void writeCachedModule(llvm::raw_ostream &out,
343343
if (R->getKind() == CodeCompletionResult::Declaration)
344344
LE.write(static_cast<uint8_t>(R->getAssociatedDeclKind()));
345345
else
346-
LE.write(~static_cast<uint8_t>(0u));
346+
LE.write(static_cast<uint8_t>(~0u));
347347
if (R->isOperator())
348348
LE.write(static_cast<uint8_t>(R->getOperatorKind()));
349349
else

lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -853,29 +853,6 @@ UnresolvedDeclRefExpr *Parser::parseExprOperator() {
853853
return new (Context) UnresolvedDeclRefExpr(name, refKind, DeclNameLoc(loc));
854854
}
855855

856-
static VarDecl *getImplicitSelfDeclForSuperContext(Parser &P,
857-
DeclContext *DC,
858-
SourceLoc Loc) {
859-
auto *methodContext = DC->getInnermostMethodContext();
860-
if (!methodContext) {
861-
P.diagnose(Loc, diag::super_not_in_class_method);
862-
return nullptr;
863-
}
864-
865-
// Do an actual lookup for 'self' in case it shows up in a capture list.
866-
auto *methodSelf = methodContext->getImplicitSelfDecl();
867-
auto *lookupSelf = P.lookupInScope(DeclNameRef(P.Context.Id_self));
868-
if (lookupSelf && lookupSelf != methodSelf) {
869-
// FIXME: This is the wrong diagnostic for if someone manually declares a
870-
// variable named 'self' using backticks.
871-
P.diagnose(Loc, diag::super_in_closure_with_capture);
872-
P.diagnose(lookupSelf->getLoc(), diag::super_in_closure_with_capture_here);
873-
return nullptr;
874-
}
875-
876-
return methodSelf;
877-
}
878-
879856
/// parseExprSuper
880857
///
881858
/// expr-super:
@@ -903,12 +880,8 @@ ParserResult<Expr> Parser::parseExprSuper() {
903880
return nullptr;
904881
}
905882

906-
VarDecl *selfDecl =
907-
getImplicitSelfDeclForSuperContext(*this, CurDeclContext, superLoc);
908-
if (!selfDecl)
909-
return makeParserResult(new (Context) ErrorExpr(superLoc));
910-
911-
return makeParserResult(new (Context) SuperRefExpr(selfDecl, superLoc,
883+
return makeParserResult(new (Context) SuperRefExpr(/*selfDecl=*/nullptr,
884+
superLoc,
912885
/*Implicit=*/false));
913886
}
914887

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,13 @@ static void lookupVisibleMemberDeclsImpl(
693693
}
694694
}
695695

696+
auto lookupTy = BaseTy;
697+
696698
const auto synthesizeAndLookupTypeMembers = [&](NominalTypeDecl *NTD) {
697699
synthesizeMemberDeclsForLookup(NTD, CurrDC);
698700

699701
// Look in for members of a nominal type.
700-
lookupTypeMembers(BaseTy, BaseTy, Consumer, CurrDC, LS, Reason);
702+
lookupTypeMembers(BaseTy, lookupTy, Consumer, CurrDC, LS, Reason);
701703
};
702704

703705
llvm::SmallPtrSet<ClassDecl *, 8> Ancestors;
@@ -725,7 +727,7 @@ static void lookupVisibleMemberDeclsImpl(
725727
Ancestors.insert(CD);
726728

727729
Reason = getReasonForSuper(Reason);
728-
BaseTy = CD->getSuperclass();
730+
lookupTy = CD->getSuperclass();
729731

730732
LS = LS.withOnSuperclass();
731733
if (CD->inheritsSuperclassInitializers())
@@ -734,7 +736,7 @@ static void lookupVisibleMemberDeclsImpl(
734736

735737
// Look into the inheritance chain.
736738
do {
737-
const auto CurClass = BaseTy->getClassOrBoundGenericClass();
739+
const auto CurClass = lookupTy->getClassOrBoundGenericClass();
738740

739741
// FIXME: This path is no substitute for an actual circularity check.
740742
// The real fix is to check that the superclass doesn't introduce a
@@ -744,10 +746,10 @@ static void lookupVisibleMemberDeclsImpl(
744746

745747
synthesizeAndLookupTypeMembers(CurClass);
746748

747-
BaseTy = CurClass->getSuperclass();
749+
lookupTy = CurClass->getSuperclass();
748750
if (!CurClass->inheritsSuperclassInitializers())
749751
LS = LS.withoutInheritsSuperclassInitializers();
750-
} while (BaseTy);
752+
} while (lookupTy);
751753
}
752754

753755
swift::DynamicLookupInfo::DynamicLookupInfo(

lib/Sema/MiscDiagnostics.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,8 @@ bool swift::computeFixitsForOverridenDeclaration(
22822282
namespace {
22832283

22842284
class VarDeclUsageChecker : public ASTWalker {
2285+
DeclContext *DC;
2286+
22852287
DiagnosticEngine &Diags;
22862288
// Keep track of some information about a variable.
22872289
enum {
@@ -2318,7 +2320,8 @@ class VarDeclUsageChecker : public ASTWalker {
23182320
void operator=(const VarDeclUsageChecker &) = delete;
23192321

23202322
public:
2321-
VarDeclUsageChecker(DiagnosticEngine &Diags) : Diags(Diags) {}
2323+
VarDeclUsageChecker(DeclContext *DC,
2324+
DiagnosticEngine &Diags) : DC(DC), Diags(Diags) {}
23222325

23232326
// After we have scanned the entire region, diagnose variables that could be
23242327
// declared with a narrower usage kind.
@@ -3140,14 +3143,27 @@ std::pair<bool, Expr *> VarDeclUsageChecker::walkToExprPre(Expr *E) {
31403143
void VarDeclUsageChecker::handleIfConfig(IfConfigDecl *ICD) {
31413144
struct ConservativeDeclMarker : public ASTWalker {
31423145
VarDeclUsageChecker &VDUC;
3143-
ConservativeDeclMarker(VarDeclUsageChecker &VDUC) : VDUC(VDUC) {}
3146+
SourceFile *SF;
3147+
3148+
ConservativeDeclMarker(VarDeclUsageChecker &VDUC)
3149+
: VDUC(VDUC), SF(VDUC.DC->getParentSourceFile()) {}
31443150

31453151
Expr *walkToExprPost(Expr *E) override {
31463152
// If we see a bound reference to a decl in an inactive #if block, then
31473153
// conservatively mark it read and written. This will silence "variable
31483154
// unused" and "could be marked let" warnings for it.
31493155
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
31503156
VDUC.addMark(DRE->getDecl(), RK_Read|RK_Written);
3157+
else if (auto *declRef = dyn_cast<UnresolvedDeclRefExpr>(E)) {
3158+
auto name = declRef->getName();
3159+
auto loc = declRef->getLoc();
3160+
if (name.isSimpleName() && loc.isValid()) {
3161+
auto *varDecl = dyn_cast_or_null<VarDecl>(
3162+
ASTScope::lookupSingleLocalDecl(SF, name.getFullName(), loc));
3163+
if (varDecl)
3164+
VDUC.addMark(varDecl, RK_Read|RK_Written);
3165+
}
3166+
}
31513167
return E;
31523168
}
31533169
};
@@ -3166,7 +3182,7 @@ void VarDeclUsageChecker::handleIfConfig(IfConfigDecl *ICD) {
31663182
void swift::
31673183
performTopLevelDeclDiagnostics(TopLevelCodeDecl *TLCD) {
31683184
auto &ctx = TLCD->getDeclContext()->getASTContext();
3169-
VarDeclUsageChecker checker(ctx.Diags);
3185+
VarDeclUsageChecker checker(TLCD, ctx.Diags);
31703186
TLCD->walk(checker);
31713187
}
31723188

@@ -3181,7 +3197,7 @@ void swift::performAbstractFuncDeclDiagnostics(AbstractFunctionDecl *AFD) {
31813197
// be checked as part of their parent function or TopLevelCodeDecl.
31823198
if (!AFD->getDeclContext()->isLocalContext()) {
31833199
auto &ctx = AFD->getDeclContext()->getASTContext();
3184-
VarDeclUsageChecker checker(ctx.Diags);
3200+
VarDeclUsageChecker checker(AFD, ctx.Diags);
31853201
AFD->walk(checker);
31863202
}
31873203

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,18 +3731,23 @@ enum class AbstractFunctionDeclLookupErrorKind {
37313731
CandidateNotFunctionDeclaration
37323732
};
37333733

3734-
/// Returns the function declaration corresponding to the given base type
3735-
/// (optional), function name, and lookup context.
3734+
/// Returns the original function (in the context of a derivative or transpose
3735+
/// function) declaration corresponding to the given base type (optional),
3736+
/// function name, lookup context, and the expected original function type.
37363737
///
37373738
/// If the base type of the function is specified, member lookup is performed.
37383739
/// Otherwise, unqualified lookup is performed.
37393740
///
3741+
/// If the expected original function type has a generic signature, any
3742+
/// candidate with a less constrained type signature than the expected original
3743+
/// function type will be treated as a viable candidate.
3744+
///
37403745
/// If the function declaration cannot be resolved, emits a diagnostic and
37413746
/// returns nullptr.
37423747
///
37433748
/// Used for resolving the referenced declaration in `@derivative` and
37443749
/// `@transpose` attributes.
3745-
static AbstractFunctionDecl *findAbstractFunctionDecl(
3750+
static AbstractFunctionDecl *findAutoDiffOriginalFunctionDecl(
37463751
DeclAttribute *attr, Type baseType, DeclNameRefWithLoc funcNameWithLoc,
37473752
DeclContext *lookupContext, NameLookupOptions lookupOptions,
37483753
const llvm::function_ref<Optional<AbstractFunctionDeclLookupErrorKind>(
@@ -4671,7 +4676,7 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
46714676
}
46724677

46734678
// Look up original function.
4674-
auto *originalAFD = findAbstractFunctionDecl(
4679+
auto *originalAFD = findAutoDiffOriginalFunctionDecl(
46754680
attr, baseType, originalName, derivativeTypeCtx, lookupOptions,
46764681
isValidOriginalCandidate, originalFnType);
46774682
if (!originalAFD) {
@@ -5230,7 +5235,7 @@ void AttributeChecker::visitTransposeAttr(TransposeAttr *attr) {
52305235
auto funcLoc = originalName.Loc.getBaseNameLoc();
52315236
if (attr->getBaseTypeRepr())
52325237
funcLoc = attr->getBaseTypeRepr()->getLoc();
5233-
auto *originalAFD = findAbstractFunctionDecl(
5238+
auto *originalAFD = findAutoDiffOriginalFunctionDecl(
52345239
attr, baseType, originalName, transposeTypeCtx, lookupOptions,
52355240
isValidOriginalCandidate, expectedOriginalFnType);
52365241
if (!originalAFD) {

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,10 @@ class FindCapturedVars : public ASTWalker {
572572

573573
// When we see a reference to the 'super' expression, capture 'self' decl.
574574
if (auto *superE = dyn_cast<SuperRefExpr>(E)) {
575-
if (CurDC->isChildContextOf(superE->getSelf()->getDeclContext()))
576-
addCapture(CapturedValue(superE->getSelf(), 0, superE->getLoc()));
575+
if (auto *selfDecl = superE->getSelf()) {
576+
if (CurDC->isChildContextOf(selfDecl->getDeclContext()))
577+
addCapture(CapturedValue(selfDecl, 0, superE->getLoc()));
578+
}
577579
return { false, superE };
578580
}
579581

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,29 @@ namespace {
11141114

11151115
bool shouldWalkCaptureInitializerExpressions() override { return true; }
11161116

1117+
VarDecl *getImplicitSelfDeclForSuperContext(SourceLoc Loc) {
1118+
auto *methodContext = DC->getInnermostMethodContext();
1119+
if (!methodContext) {
1120+
Ctx.Diags.diagnose(Loc, diag::super_not_in_class_method);
1121+
return nullptr;
1122+
}
1123+
1124+
// Do an actual lookup for 'self' in case it shows up in a capture list.
1125+
auto *methodSelf = methodContext->getImplicitSelfDecl();
1126+
auto *lookupSelf = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
1127+
Ctx.Id_self, Loc);
1128+
if (lookupSelf && lookupSelf != methodSelf) {
1129+
// FIXME: This is the wrong diagnostic for if someone manually declares a
1130+
// variable named 'self' using backticks.
1131+
Ctx.Diags.diagnose(Loc, diag::super_in_closure_with_capture);
1132+
Ctx.Diags.diagnose(lookupSelf->getLoc(),
1133+
diag::super_in_closure_with_capture_here);
1134+
return nullptr;
1135+
}
1136+
1137+
return methodSelf;
1138+
}
1139+
11171140
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
11181141
// If this is a call, record the argument expression.
11191142
if (auto call = dyn_cast<ApplyExpr>(expr)) {
@@ -1156,6 +1179,17 @@ namespace {
11561179
return std::make_pair(recursive, expr);
11571180
};
11581181

1182+
// Resolve 'super' references.
1183+
if (auto *superRef = dyn_cast<SuperRefExpr>(expr)) {
1184+
auto loc = superRef->getLoc();
1185+
auto *selfDecl = getImplicitSelfDeclForSuperContext(loc);
1186+
if (selfDecl == nullptr)
1187+
return finish(true, new (Ctx) ErrorExpr(loc));
1188+
1189+
superRef->setSelf(selfDecl);
1190+
return finish(true, superRef);
1191+
}
1192+
11591193
// For closures, type-check the patterns and result type as written,
11601194
// but do not walk into the body. That will be type-checked after
11611195
// we've determine the complete function type.

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5799,6 +5799,9 @@ class SwiftToClangBasicReader :
57995799

58005800
llvm::Expected<const clang::Type *>
58015801
ModuleFile::getClangType(ClangTypeID TID) {
5802+
if (!getContext().LangOpts.UseClangFunctionTypes)
5803+
return nullptr;
5804+
58025805
if (TID == 0)
58035806
return nullptr;
58045807

0 commit comments

Comments
 (0)