Skip to content

Commit c03addb

Browse files
ahoppenxedin
authored andcommitted
[CodeCompletion] Insert code completion token into build block call to retrieve contextual type
rdar://94224692
1 parent 36a50c7 commit c03addb

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

lib/IDE/CodeCompletionResultType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ const USRBasedType *USRBasedType::fromType(Type Ty, USRBasedTypeArena &Arena) {
184184
Ty = Ty->getCanonicalType();
185185

186186
// For opaque types like 'some View', consider them equivalent to 'View'.
187-
if (auto OpaqueType = Ty->getAs<OpaqueTypeArchetypeType>()) {
187+
if (auto OpaqueType = Ty->getAs<ArchetypeType>()) {
188188
if (auto Existential = OpaqueType->getExistentialType()) {
189189
Ty = Existential;
190190
}

lib/Sema/BuilderTransform.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,20 @@ class ResultBuilderTransform
10581058
{Identifier()});
10591059
}
10601060

1061-
auto *capture = captureExpr(expr, newBody);
1062-
// A reference to the synthesized variable is passed as an argument
1063-
// to buildBlock.
1064-
buildBlockArguments.push_back(
1065-
builder.buildVarRef(capture, element.getStartLoc()));
1061+
if (isa<CodeCompletionExpr>(expr)) {
1062+
// Insert the CodeCompletionExpr directly into the buildBlock call. That
1063+
// way, we can extract the contextual type of the code completion token
1064+
// to rank code completion items that match the type expected by
1065+
// buildBlock higher.
1066+
buildBlockArguments.push_back(expr);
1067+
} else {
1068+
auto *capture = captureExpr(expr, newBody);
1069+
// A reference to the synthesized variable is passed as an argument
1070+
// to buildBlock.
1071+
buildBlockArguments.push_back(
1072+
builder.buildVarRef(capture, element.getStartLoc()));
1073+
}
1074+
10661075
return None;
10671076
}
10681077

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,6 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
998998
return found->second;
999999

10001000
if (auto archetypeType = dyn_cast<ArchetypeType>(origType)) {
1001-
// We leave opaque types and their nested associated types alone here.
1002-
// They're globally available.
1003-
if (isa<OpaqueTypeArchetypeType>(archetypeType))
1004-
return origType;
1005-
10061001
auto root = archetypeType->getRoot();
10071002
// For other nested types, fail here so the default logic in subst()
10081003
// for nested types applies.
@@ -1033,7 +1028,8 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
10331028
types[origType] = replacement;
10341029
return replacement;
10351030
},
1036-
MakeAbstractConformanceForGenericType());
1031+
MakeAbstractConformanceForGenericType(),
1032+
SubstFlags::SubstituteOpaqueArchetypes);
10371033
}
10381034

10391035
bool TypeChecker::typesSatisfyConstraint(Type type1, Type type2,

test/IDE/complete_from_swift_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func testAmbiguousResultBuilder() {
184184
// Results should only contain globalVar once
185185
// AMBIGOUS_RESULT_BUILER: Begin completions
186186
// AMBIGOUS_RESULT_BUILER-NOT: globalVar
187-
// AMBIGOUS_RESULT_BUILER-DAG: Decl[GlobalVar]/OtherModule[foo_swift_module]: globalVar[#Int#]; name=globalVar
187+
// AMBIGOUS_RESULT_BUILER-DAG: Decl[GlobalVar]/OtherModule[foo_swift_module]/TypeRelation[Convertible]: globalVar[#Int#]; name=globalVar
188188
// AMBIGOUS_RESULT_BUILER-NOT: globalVar
189189
// AMBIGOUS_RESULT_BUILER: End completions
190190
}

test/IDE/complete_issue-57061.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ public struct ForEach2<Data, ID, Content>: View2 where Data : RandomAccessCollec
3939

4040
// CHECK: Begin completions, 2 items
4141
// CHECK-NEXT: Keyword[self]/CurrNominal: self[#Text2#];
42-
// CHECK-NEXT: Decl[InstanceMethod]/Super: font()[#View2#];
42+
// CHECK-NEXT: Decl[InstanceMethod]/Super/TypeRelation[Convertible]: font()[#View2#];
4343
// CHECK-NEXT: End completions

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,6 +4395,8 @@ int main(int argc, char *argv[]) {
43954395
options::ImportObjCHeader;
43964396
InitInvok.getClangImporterOptions().BridgingHeader =
43974397
options::ImportObjCHeader;
4398+
InitInvok.getLangOptions().Features.insert(
4399+
Feature::ResultBuilderASTTransform);
43984400
InitInvok.getLangOptions().EnableAccessControl =
43994401
!options::DisableAccessControl;
44004402
InitInvok.getLangOptions().EnableSwift3ObjCInference =

0 commit comments

Comments
 (0)