Skip to content

Commit ec597c8

Browse files
committed
[Completion] Only complete @unchecked et al in inheritance clauses
Add a case for completing type attributes in inheritance clause position, and limit the completion of `@unchecked`, `@preconcurrency`, and `@retroactive` to that case.
1 parent 42f563d commit ec597c8

File tree

11 files changed

+97
-23
lines changed

11 files changed

+97
-23
lines changed

include/swift/IDE/CodeCompletionResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ enum class CompletionKind : uint8_t {
229229
StmtLabel,
230230
ForEachPatternBeginning,
231231
TypeAttrBeginning,
232+
TypeAttrInheritanceBeginning,
232233
OptionalBinding,
233234

234235
/// Completion after `~` in an inheritance clause.

include/swift/IDE/CompletionLookup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
605605
void getAttributeDeclParamCompletions(CustomSyntaxAttributeKind AttrKind,
606606
int ParamIndex, bool HasLabel);
607607

608-
void getTypeAttributeKeywordCompletions();
608+
void getTypeAttributeKeywordCompletions(CompletionKind completionKind);
609609

610610
void collectPrecedenceGroups();
611611

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ class CodeCompletionCallbacks {
288288

289289
virtual void completeTypeAttrBeginning() {};
290290

291+
virtual void completeTypeAttrInheritanceBeginning() {};
292+
291293
virtual void completeOptionalBinding(){};
292294

293295
virtual void completeWithoutConstraintType(){};

include/swift/Parse/Parser.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,8 @@ class Parser {
13981398

13991399
/// Whether the type is for a closure attribute.
14001400
CustomAttribute,
1401+
/// A type in an inheritance clause.
1402+
InheritanceClause,
14011403
};
14021404

14031405
ParserResult<TypeRepr> parseTypeScalar(
@@ -1455,6 +1457,7 @@ class Parser {
14551457
ParserResult<TypeRepr> parseTypeDotted(ParserResult<TypeRepr> Base);
14561458

14571459
struct ParsedTypeAttributeList {
1460+
ParseTypeReason ParseReason;
14581461
ParamDecl::Specifier Specifier = ParamDecl::Specifier::Default;
14591462
SourceLoc SpecifierLoc;
14601463
SourceLoc IsolatedLoc;
@@ -1463,6 +1466,8 @@ class Parser {
14631466
SmallVector<TypeOrCustomAttr> Attributes;
14641467
SmallVector<LifetimeDependenceSpecifier> lifetimeDependenceSpecifiers;
14651468

1469+
ParsedTypeAttributeList(ParseTypeReason reason) : ParseReason(reason) {}
1470+
14661471
/// Main entry point for parsing.
14671472
///
14681473
/// Inline we just have the fast path of failing to match. We call slowParse
@@ -1484,7 +1489,7 @@ class Parser {
14841489
};
14851490

14861491
ParserStatus parseTypeAttribute(TypeOrCustomAttr &result, SourceLoc AtLoc,
1487-
SourceLoc AtEndLoc,
1492+
SourceLoc AtEndLoc, ParseTypeReason reason,
14881493
PatternBindingInitializer *&initContext,
14891494
bool justChecking = false);
14901495

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks,
303303
void completeStmtLabel(StmtKind ParentKind) override;
304304
void completeForEachPatternBeginning(bool hasTry, bool hasAwait) override;
305305
void completeTypeAttrBeginning() override;
306+
void completeTypeAttrInheritanceBeginning() override;
306307
void completeOptionalBinding() override;
307308
void completeWithoutConstraintType() override;
308309

@@ -655,6 +656,11 @@ void CodeCompletionCallbacksImpl::completeTypeAttrBeginning() {
655656
Kind = CompletionKind::TypeAttrBeginning;
656657
}
657658

659+
void CodeCompletionCallbacksImpl::completeTypeAttrInheritanceBeginning() {
660+
CurDeclContext = P.CurDeclContext;
661+
Kind = CompletionKind::TypeAttrInheritanceBeginning;
662+
}
663+
658664
bool swift::ide::isDynamicLookup(Type T) {
659665
return T->getRValueType()->isAnyObject();
660666
}
@@ -983,6 +989,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
983989
case CompletionKind::PrecedenceGroup:
984990
case CompletionKind::StmtLabel:
985991
case CompletionKind::TypeAttrBeginning:
992+
case CompletionKind::TypeAttrInheritanceBeginning:
986993
case CompletionKind::OptionalBinding:
987994
case CompletionKind::WithoutConstraintType:
988995
break;
@@ -1931,14 +1938,14 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
19311938
Lookup.getStmtLabelCompletions(Loc, ParentStmtKind == StmtKind::Continue);
19321939
break;
19331940
}
1934-
case CompletionKind::TypeAttrBeginning: {
1935-
Lookup.getTypeAttributeKeywordCompletions();
1941+
case CompletionKind::TypeAttrBeginning:
1942+
case CompletionKind::TypeAttrInheritanceBeginning: {
1943+
Lookup.getTypeAttributeKeywordCompletions(Kind);
19361944

19371945
// Type names at attribute position after '@'.
19381946
Lookup.getTypeCompletionsInDeclContext(
19391947
P.Context.SourceMgr.getIDEInspectionTargetLoc());
19401948
break;
1941-
19421949
}
19431950
case CompletionKind::OptionalBinding: {
19441951
SourceLoc Loc = P.Context.SourceMgr.getIDEInspectionTargetLoc();

lib/IDE/CompletionLookup.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,8 +3132,20 @@ void CompletionLookup::getAttributeDeclParamCompletions(
31323132
}
31333133
}
31343134

3135-
void CompletionLookup::getTypeAttributeKeywordCompletions() {
3136-
auto addTypeAttr = [&](StringRef Name) {
3135+
void CompletionLookup::getTypeAttributeKeywordCompletions(
3136+
CompletionKind completionKind) {
3137+
auto addTypeAttr = [&](TypeAttrKind Kind, StringRef Name) {
3138+
if (completionKind != CompletionKind::TypeAttrInheritanceBeginning) {
3139+
switch (Kind) {
3140+
case TypeAttrKind::Retroactive:
3141+
case TypeAttrKind::Preconcurrency:
3142+
case TypeAttrKind::Unchecked:
3143+
// These attributes are only available in inheritance clasuses.
3144+
return;
3145+
default:
3146+
break;
3147+
}
3148+
}
31373149
CodeCompletionResultBuilder Builder = makeResultBuilder(
31383150
CodeCompletionResultKind::Keyword, SemanticContextKind::None);
31393151
Builder.addAttributeKeyword(Name, "Type Attribute");
@@ -3142,17 +3154,17 @@ void CompletionLookup::getTypeAttributeKeywordCompletions() {
31423154
// Add simple user-accessible attributes.
31433155
#define SIL_TYPE_ATTR(SPELLING, C)
31443156
#define SIMPLE_SIL_TYPE_ATTR(SPELLING, C)
3145-
#define SIMPLE_TYPE_ATTR(SPELLING, C) \
3146-
if (!TypeAttribute::isUserInaccessible(TypeAttrKind::C)) \
3147-
addTypeAttr(#SPELLING);
3157+
#define SIMPLE_TYPE_ATTR(SPELLING, C) \
3158+
if (!TypeAttribute::isUserInaccessible(TypeAttrKind::C)) \
3159+
addTypeAttr(TypeAttrKind::C, #SPELLING);
31483160
#include "swift/AST/TypeAttr.def"
31493161

31503162
// Add non-simple cases.
3151-
addTypeAttr("convention(swift)");
3152-
addTypeAttr("convention(block)");
3153-
addTypeAttr("convention(c)");
3154-
addTypeAttr("convention(thin)");
3155-
addTypeAttr("isolated(any)");
3163+
addTypeAttr(TypeAttrKind::Convention, "convention(swift)");
3164+
addTypeAttr(TypeAttrKind::Convention, "convention(block)");
3165+
addTypeAttr(TypeAttrKind::Convention, "convention(c)");
3166+
addTypeAttr(TypeAttrKind::Convention, "convention(thin)");
3167+
addTypeAttr(TypeAttrKind::Isolated, "isolated(any)");
31563168
}
31573169

31583170
void CompletionLookup::collectPrecedenceGroups() {

lib/Parse/ParseDecl.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4483,7 +4483,8 @@ bool Parser::canParseTypeAttribute() {
44834483
TypeOrCustomAttr result; // ignored
44844484
PatternBindingInitializer *initContext = nullptr;
44854485
return !parseTypeAttribute(result, /*atLoc=*/SourceLoc(),
4486-
/*atEndLoc=*/SourceLoc(), initContext,
4486+
/*atEndLoc=*/SourceLoc(),
4487+
ParseTypeReason::Unspecified, initContext,
44874488
/*justChecking*/ true)
44884489
.isError();
44894490
}
@@ -4684,6 +4685,7 @@ bool Parser::parseUUIDString(UUID &uuid, Diag<> diagnostic, bool justChecking) {
46844685
/// no need to actually record the attribute
46854686
ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
46864687
SourceLoc AtLoc, SourceLoc AtEndLoc,
4688+
ParseTypeReason reason,
46874689
PatternBindingInitializer *&initContext,
46884690
bool justChecking) {
46894691
if (AtEndLoc != Tok.getLoc()) {
@@ -4699,7 +4701,14 @@ ParserStatus Parser::parseTypeAttribute(TypeOrCustomAttr &result,
46994701
if (Tok.is(tok::code_complete)) {
47004702
if (!justChecking) {
47014703
if (CodeCompletionCallbacks) {
4702-
CodeCompletionCallbacks->completeTypeAttrBeginning();
4704+
switch (reason) {
4705+
case ParseTypeReason::InheritanceClause:
4706+
CodeCompletionCallbacks->completeTypeAttrInheritanceBeginning();
4707+
break;
4708+
default:
4709+
CodeCompletionCallbacks->completeTypeAttrBeginning();
4710+
break;
4711+
}
47034712
}
47044713
}
47054714
consumeToken(tok::code_complete);
@@ -5525,7 +5534,8 @@ ParserStatus Parser::ParsedTypeAttributeList::slowParse(Parser &P) {
55255534
TypeOrCustomAttr result;
55265535
SourceLoc AtEndLoc = Tok.getRange().getEnd();
55275536
SourceLoc AtLoc = P.consumeToken();
5528-
status |= P.parseTypeAttribute(result, AtLoc, AtEndLoc, initContext);
5537+
status |=
5538+
P.parseTypeAttribute(result, AtLoc, AtEndLoc, ParseReason, initContext);
55295539
if (status.isError())
55305540
return status;
55315541
if (result)
@@ -6806,7 +6816,8 @@ ParserStatus Parser::parseInheritance(
68066816
continue;
68076817
}
68086818

6809-
auto ParsedTypeResult = parseType();
6819+
auto ParsedTypeResult =
6820+
parseType(diag::expected_type, ParseTypeReason::InheritanceClause);
68106821
Status |= ParsedTypeResult;
68116822

68126823
// Record the type if its a single type.

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ SourceLoc Parser::tryCompleteFunctionParamTypeBeginning() {
159159
// Skip over any starting parameter specifiers.
160160
{
161161
CancellableBacktrackingScope backtrack(*this);
162-
ParsedTypeAttributeList attrs;
162+
ParsedTypeAttributeList attrs(ParseTypeReason::Unspecified);
163163
attrs.parse(*this);
164164
if (!Tok.is(tok::code_complete))
165165
return SourceLoc();

lib/Parse/ParseType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,15 @@ ParserResult<TypeRepr> Parser::parseTypeScalar(
396396
ParserStatus status;
397397

398398
// Parse attributes.
399-
ParsedTypeAttributeList parsedAttributeList;
399+
ParsedTypeAttributeList parsedAttributeList(reason);
400400
status |= parsedAttributeList.parse(*this);
401401

402+
// If we have a completion, create an ErrorType.
403+
if (status.hasCodeCompletion()) {
404+
auto *ET = ErrorTypeRepr::create(Context, PreviousLoc);
405+
return makeParserCodeCompletionResult<TypeRepr>(ET);
406+
}
407+
402408
// Parse generic parameters in SIL mode.
403409
GenericParamList *generics = nullptr;
404410
SourceLoc substitutedLoc;

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@ bool SILParser::parseSILType(SILType &Result,
11261126
}
11271127

11281128
// Parse attributes.
1129-
Parser::ParsedTypeAttributeList parsedAttrs;
1129+
Parser::ParsedTypeAttributeList parsedAttrs(
1130+
Parser::ParseTypeReason::Unspecified);
11301131
parsedAttrs.parse(P);
11311132

11321133
// Global functions are implicitly @convention(thin) if not specified otherwise.

0 commit comments

Comments
 (0)