Skip to content

Commit 86bd6ee

Browse files
committed
[CodeComplete] Offer completions after ~ in an inheritance clause
We should only be suggesting `Copyable` after `~` in an inheritance clause, in accordance with [SE-0390](https://github.com/apple/swift-evolution/blob/main/proposals/0390-noncopyable-structs-and-enums.md) rdar://109063223
1 parent e13fe5b commit 86bd6ee

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-0
lines changed

include/swift/IDE/CodeCompletionResult.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ enum class CompletionKind : uint8_t {
228228
ForEachPatternBeginning,
229229
TypeAttrBeginning,
230230
OptionalBinding,
231+
232+
/// Completion after `~` in an inheritance clause.
233+
WithoutConstraintType
231234
};
232235

233236
enum class CodeCompletionDiagnosticSeverity : uint8_t {

include/swift/IDE/CompletionLookup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
620620
void getStmtLabelCompletions(SourceLoc Loc, bool isContinue);
621621

622622
void getOptionalBindingCompletions(SourceLoc Loc);
623+
624+
void getWithoutConstraintTypes();
623625
};
624626

625627
} // end namespace ide

include/swift/Parse/IDEInspectionCallbacks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class CodeCompletionCallbacks {
261261
virtual void completeTypeAttrBeginning() {};
262262

263263
virtual void completeOptionalBinding(){};
264+
265+
virtual void completeWithoutConstraintType(){};
264266
};
265267

266268
class DoneParsingCallback {

lib/IDE/CodeCompletion.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks,
305305
void completeForEachPatternBeginning(bool hasTry, bool hasAwait) override;
306306
void completeTypeAttrBeginning() override;
307307
void completeOptionalBinding() override;
308+
void completeWithoutConstraintType() override;
308309

309310
void doneParsing(SourceFile *SrcFile) override;
310311

@@ -664,6 +665,11 @@ void CodeCompletionCallbacksImpl::completeOptionalBinding() {
664665
Kind = CompletionKind::OptionalBinding;
665666
}
666667

668+
void CodeCompletionCallbacksImpl::completeWithoutConstraintType() {
669+
CurDeclContext = P.CurDeclContext;
670+
Kind = CompletionKind::WithoutConstraintType;
671+
}
672+
667673
void CodeCompletionCallbacksImpl::completeTypeAttrBeginning() {
668674
CurDeclContext = P.CurDeclContext;
669675
Kind = CompletionKind::TypeAttrBeginning;
@@ -975,6 +981,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
975981
case CompletionKind::StmtLabel:
976982
case CompletionKind::TypeAttrBeginning:
977983
case CompletionKind::OptionalBinding:
984+
case CompletionKind::WithoutConstraintType:
978985
break;
979986

980987
case CompletionKind::EffectsSpecifier:
@@ -2057,6 +2064,11 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
20572064
break;
20582065
}
20592066

2067+
case CompletionKind::WithoutConstraintType: {
2068+
Lookup.getWithoutConstraintTypes();
2069+
break;
2070+
}
2071+
20602072
case CompletionKind::AfterIfStmtElse:
20612073
case CompletionKind::CaseStmtKeyword:
20622074
case CompletionKind::EffectsSpecifier:

lib/IDE/CompletionLookup.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,3 +3348,9 @@ void CompletionLookup::getOptionalBindingCompletions(SourceLoc Loc) {
33483348
lookupVisibleDecls(FilteringConsumer, CurrDeclContext,
33493349
/*IncludeTopLevel=*/false, Loc);
33503350
}
3351+
3352+
void CompletionLookup::getWithoutConstraintTypes() {
3353+
// FIXME: Once we have a typealias declaration for copyable, we should be
3354+
// returning that instead of a keyword (rdar://109107817).
3355+
addKeyword("Copyable");
3356+
}

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5783,6 +5783,13 @@ ParserStatus Parser::parseInheritance(
57835783
TildeCopyableLoc = tildeLoc;
57845784

57855785
continue;
5786+
} else if (nextTok.is(tok::code_complete)) {
5787+
consumeToken(); // consume '~'
5788+
Status.setHasCodeCompletionAndIsError();
5789+
if (CodeCompletionCallbacks) {
5790+
CodeCompletionCallbacks->completeWithoutConstraintType();
5791+
}
5792+
consumeToken(tok::code_complete);
57865793
}
57875794

57885795
// can't suppress whatever is between '~' and ',' or '{'.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
struct FileDescriptor: ~#^COMPLETE^# {}
5+
// FIXME: This should be emitting a `Copyable` declaration instead of a keyword, once there is a declaration for `Copyable`
6+
// COMPLETE: Keyword/None: Copyable; name=Copyable

0 commit comments

Comments
 (0)