Skip to content

Commit e22b6d6

Browse files
committed
Handle subscripts in formDeclNameRef()
In particular, we were unconditionally dropping argument labels on accessors; now we only do that for property accessors, not subscript accessors. Doing this unconditionally causes ClangImporter failures when a method parameter is called “subscript” (really!), so this behavior is enabled only by the caller’s request.
1 parent edd3f35 commit e22b6d6

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

include/swift/Parse/Parser.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,10 +1822,10 @@ struct ParsedDeclName {
18221822
}
18231823

18241824
/// Form a declaration name from this parsed declaration name.
1825-
DeclName formDeclName(ASTContext &ctx) const;
1825+
DeclName formDeclName(ASTContext &ctx, bool isSubscript = false) const;
18261826

18271827
/// Form a declaration name from this parsed declaration name.
1828-
DeclNameRef formDeclNameRef(ASTContext &ctx) const;
1828+
DeclNameRef formDeclNameRef(ASTContext &ctx, bool isSubscript = false) const;
18291829
};
18301830

18311831
/// To assist debugging parser crashes, tell us the location of the
@@ -1846,14 +1846,16 @@ DeclName formDeclName(ASTContext &ctx,
18461846
StringRef baseName,
18471847
ArrayRef<StringRef> argumentLabels,
18481848
bool isFunctionName,
1849-
bool isInitializer);
1849+
bool isInitializer,
1850+
bool isSubscript = false);
18501851

18511852
/// Form a Swift declaration name referemce from its constituent parts.
18521853
DeclNameRef formDeclNameRef(ASTContext &ctx,
18531854
StringRef baseName,
18541855
ArrayRef<StringRef> argumentLabels,
18551856
bool isFunctionName,
1856-
bool isInitializer);
1857+
bool isInitializer,
1858+
bool isSubscript = false);
18571859

18581860
/// Parse a stringified Swift declaration name, e.g. "init(frame:)".
18591861
DeclName parseDeclName(ASTContext &ctx, StringRef name);

lib/AST/AccessNotes.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ AccessNoteDeclName::AccessNoteDeclName(ASTContext &ctx, StringRef str) {
4646
else
4747
accessorKind = None;
4848

49-
name = parsedName.formDeclName(ctx);
50-
51-
// FIXME: parseDeclName() doesn't handle the special `subscript` name.
52-
// Fixing this without affecting existing uses in import-as-member will need
53-
// a bit of work. Hack around the problem for this specific caller instead.
54-
if (name.getBaseName() == ctx.getIdentifier("subscript"))
55-
name = DeclName(ctx, DeclBaseName::createSubscript(),
56-
name.getArgumentNames());
49+
name = parsedName.formDeclName(ctx, /*isSubscript=*/true);
5750
}
5851

5952
bool AccessNoteDeclName::matches(ValueDecl *VD) const {

lib/Parse/Parser.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,49 +1429,53 @@ ParsedDeclName swift::parseDeclName(StringRef name) {
14291429
}
14301430
} while (!parameters.empty());
14311431

1432-
// Drop the argument labels for a property accessor; they aren't used.
1433-
if (result.isPropertyAccessor())
1434-
result.ArgumentLabels.clear();
1435-
14361432
return result;
14371433
}
14381434

1439-
DeclName ParsedDeclName::formDeclName(ASTContext &ctx) const {
1440-
return formDeclNameRef(ctx).getFullName();
1435+
DeclName ParsedDeclName::formDeclName(ASTContext &ctx, bool isSubscript) const {
1436+
return formDeclNameRef(ctx, isSubscript).getFullName();
14411437
}
14421438

1443-
DeclNameRef ParsedDeclName::formDeclNameRef(ASTContext &ctx) const {
1439+
DeclNameRef ParsedDeclName::formDeclNameRef(ASTContext &ctx,
1440+
bool isSubscript) const {
14441441
return swift::formDeclNameRef(ctx, BaseName, ArgumentLabels, IsFunctionName,
1445-
/*IsInitializer=*/true);
1442+
/*IsInitializer=*/true, isSubscript);
14461443
}
14471444

14481445
DeclName swift::formDeclName(ASTContext &ctx,
14491446
StringRef baseName,
14501447
ArrayRef<StringRef> argumentLabels,
14511448
bool isFunctionName,
1452-
bool isInitializer) {
1449+
bool isInitializer,
1450+
bool isSubscript) {
14531451
return formDeclNameRef(ctx, baseName, argumentLabels, isFunctionName,
1454-
isInitializer).getFullName();
1452+
isInitializer, isSubscript).getFullName();
14551453
}
14561454

14571455
DeclNameRef swift::formDeclNameRef(ASTContext &ctx,
14581456
StringRef baseName,
14591457
ArrayRef<StringRef> argumentLabels,
14601458
bool isFunctionName,
1461-
bool isInitializer) {
1459+
bool isInitializer,
1460+
bool isSubscript) {
14621461
// We cannot import when the base name is not an identifier.
14631462
if (baseName.empty())
14641463
return DeclNameRef();
14651464
if (!Lexer::isIdentifier(baseName) && !Lexer::isOperator(baseName))
14661465
return DeclNameRef();
14671466

14681467
// Get the identifier for the base name. Special-case `init`.
1469-
DeclBaseName baseNameId = ((isInitializer && baseName == "init")
1470-
? DeclBaseName::createConstructor()
1471-
: ctx.getIdentifier(baseName));
1468+
DeclBaseName baseNameId;
1469+
if (isInitializer && baseName == "init")
1470+
baseNameId = DeclBaseName::createConstructor();
1471+
else if (isSubscript && baseName == "subscript")
1472+
baseNameId = DeclBaseName::createSubscript();
1473+
else
1474+
baseNameId = ctx.getIdentifier(baseName);
14721475

14731476
// For non-functions, just use the base name.
1474-
if (!isFunctionName) return DeclNameRef(baseNameId);
1477+
if (!isFunctionName && !baseNameId.isSubscript())
1478+
return DeclNameRef(baseNameId);
14751479

14761480
// For functions, we need to form a complete name.
14771481

0 commit comments

Comments
 (0)