Skip to content

Commit 9fc633e

Browse files
Merge pull request #39652 from AnthonyLatsis/no-context
TypeResolution: Abolish TypeResolutionStage::Contextual
2 parents d835783 + f0264de commit 9fc633e

28 files changed

+432
-438
lines changed

include/swift/AST/TypeResolutionStage.h

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ enum class TypeResolutionStage : uint8_t {
3131
/// Produces a complete interface type where all member references have been
3232
/// resolved.
3333
Interface,
34-
35-
/// Produces a contextual type involving archetypes within the context of
36-
/// the type.
37-
Contextual,
3834
};
3935

4036
/// Display a type resolution stage.

include/swift/Sema/ConstraintSystem.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -4037,9 +4037,12 @@ class ConstraintSystem {
40374037
/// parent type by introducing fresh type variables for generic parameters
40384038
/// and constructing a bound generic type from these type variables.
40394039
///
4040+
/// \param isTypeResolution Whether we are in the process of resolving a type.
4041+
///
40404042
/// \returns The opened type.
40414043
Type openUnboundGenericType(GenericTypeDecl *decl, Type parentTy,
4042-
ConstraintLocatorBuilder locator);
4044+
ConstraintLocatorBuilder locator,
4045+
bool isTypeResolution);
40434046

40444047
/// Replace placeholder types with fresh type variables, and unbound generic
40454048
/// types with bound generic types whose generic args are fresh type
@@ -5275,7 +5278,8 @@ class OpenUnboundGenericType {
52755278

52765279
Type operator()(UnboundGenericType *unboundTy) const {
52775280
return cs.openUnboundGenericType(unboundTy->getDecl(),
5278-
unboundTy->getParent(), locator);
5281+
unboundTy->getParent(), locator,
5282+
/*isTypeResolution=*/true);
52795283
}
52805284
};
52815285

include/swift/Subsystems.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ namespace swift {
157157
/// emitted.
158158
void performWholeModuleTypeChecking(SourceFile &SF);
159159

160-
/// Resolve the given \c TypeRepr to a contextual type.
160+
/// Resolve the given \c TypeRepr to an interface type.
161161
///
162162
/// This is used when dealing with partial source files (e.g. SIL parsing,
163163
/// code completion).

lib/AST/GenericEnvironment.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ Optional<Type> GenericEnvironment::getMappingIfPresent(
8686

8787
Type GenericEnvironment::mapTypeIntoContext(GenericEnvironment *env,
8888
Type type) {
89-
assert(!type->hasArchetype() && "already have a contextual type");
89+
assert((!type->hasArchetype() || type->hasOpenedExistential()) &&
90+
"already have a contextual type");
9091
assert((env || !type->hasTypeParameter()) &&
9192
"no generic environment provided for type with type parameters");
9293

@@ -230,8 +231,8 @@ Type QueryInterfaceTypeSubstitutions::operator()(SubstitutableType *type) const{
230231
Type GenericEnvironment::mapTypeIntoContext(
231232
Type type,
232233
LookupConformanceFn lookupConformance) const {
233-
assert(!type->hasOpenedExistential() &&
234-
"Opened existentials are special and so are you");
234+
assert((!type->hasArchetype() || type->hasOpenedExistential()) &&
235+
"already have a contextual type");
235236

236237
Type result = type.subst(QueryInterfaceTypeSubstitutions(this),
237238
lookupConformance,

lib/AST/TypeCheckRequests.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ void swift::simple_display(llvm::raw_ostream &out,
5858
case TypeResolutionStage::Interface:
5959
out << "interface";
6060
break;
61-
62-
case TypeResolutionStage::Contextual:
63-
out << "contextual";
64-
break;
6561
}
6662
}
6763

lib/AST/TypeRepr.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ void AttributedTypeRepr::printAttrs(ASTPrinter &Printer,
225225

226226
if (hasAttr(TAK_async))
227227
Printer.printSimpleAttr("@async") << " ";
228+
if (hasAttr(TAK_opened))
229+
Printer.printSimpleAttr("@opened") << " ";
228230
}
229231

230232
IdentTypeRepr *IdentTypeRepr::create(ASTContext &C,

lib/IDE/CodeCompletion.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1580,11 +1580,13 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
15801580
/*GenericParams=*/nullptr,
15811581
CurDeclContext,
15821582
/*ProduceDiagnostics=*/false);
1583-
ParsedTypeLoc.setType(ty);
1584-
if (!ParsedTypeLoc.isError()) {
1583+
if (!ty->hasError()) {
1584+
ParsedTypeLoc.setType(CurDeclContext->mapTypeIntoContext(ty));
15851585
return true;
15861586
}
15871587

1588+
ParsedTypeLoc.setType(ty);
1589+
15881590
// It doesn't type check as a type, so see if it's a qualifying module name.
15891591
if (auto *ITR = dyn_cast<IdentTypeRepr>(ParsedTypeLoc.getTypeRepr())) {
15901592
const auto &componentRange = ITR->getComponentRange();

lib/IDE/ExprContextAnalysis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void swift::ide::collectPossibleReturnTypesFromContext(
308308
const_cast<DeclContext *>(DC), /*diagnostics=*/false);
309309

310310
if (!type->hasError()) {
311-
candidates.push_back(type);
311+
candidates.push_back(DC->mapTypeIntoContext(type));
312312
return;
313313
}
314314
}

lib/SIL/Parser/ParseSIL.cpp

+43-28
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ namespace {
150150

151151
Type performTypeResolution(TypeRepr *TyR, bool IsSILType,
152152
GenericEnvironment *GenericEnv,
153-
GenericParamList *GenericParams);
153+
GenericParamList *GenericParams) const;
154154

155155
void convertRequirements(ArrayRef<RequirementRepr> From,
156156
SmallVectorImpl<Requirement> &To);
@@ -915,9 +915,8 @@ void SILParser::convertRequirements(ArrayRef<RequirementRepr> From,
915915
// Use parser lexical scopes to resolve references
916916
// to the generic parameters.
917917
auto ResolveToInterfaceType = [&](TypeRepr *TyR) -> Type {
918-
return performTypeResolution(TyR, /*IsSILType=*/false,
919-
ContextGenericEnv, ContextGenericParams)
920-
->mapTypeOutOfContext();
918+
return performTypeResolution(TyR, /*IsSILType=*/false, ContextGenericEnv,
919+
ContextGenericParams);
921920
};
922921

923922
for (auto &Req : From) {
@@ -1187,7 +1186,7 @@ static bool parseDeclSILOptional(bool *isTransparent,
11871186

11881187
Type SILParser::performTypeResolution(TypeRepr *TyR, bool IsSILType,
11891188
GenericEnvironment *GenericEnv,
1190-
GenericParamList *GenericParams) {
1189+
GenericParamList *GenericParams) const {
11911190
if (GenericEnv == nullptr)
11921191
GenericEnv = ContextGenericEnv;
11931192

@@ -1251,26 +1250,26 @@ bool SILParser::parseASTType(CanType &result,
12511250
ParserResult<TypeRepr> parsedType = P.parseType();
12521251
if (parsedType.isNull()) return true;
12531252

1254-
bool wantInterfaceType = true;
1253+
bool wantContextualType = false;
12551254
if (genericEnv == nullptr) {
12561255
genericEnv = ContextGenericEnv;
1257-
wantInterfaceType = false;
1256+
wantContextualType = true;
12581257
}
12591258
if (genericParams == nullptr)
12601259
genericParams = ContextGenericParams;
12611260

12621261
bindSILGenericParams(parsedType.get());
12631262

1264-
const auto resolvedType =
1265-
performTypeResolution(parsedType.get(), /*isSILType=*/false,
1266-
genericEnv, genericParams);
1263+
auto resolvedType = performTypeResolution(
1264+
parsedType.get(), /*isSILType=*/false, genericEnv, genericParams);
1265+
if (wantContextualType && genericEnv) {
1266+
resolvedType = genericEnv->mapTypeIntoContext(resolvedType);
1267+
}
1268+
12671269
if (resolvedType->hasError())
12681270
return true;
12691271

1270-
if (wantInterfaceType)
1271-
result = resolvedType->mapTypeOutOfContext()->getCanonicalType();
1272-
else
1273-
result = resolvedType->getCanonicalType();
1272+
result = resolvedType->getCanonicalType();
12741273

12751274
return false;
12761275
}
@@ -1363,9 +1362,12 @@ bool SILParser::parseSILType(SILType &Result,
13631362
auto *attrRepr =
13641363
P.applyAttributeToType(
13651364
TyR.get(), attrs, specifier, specifierLoc, isolatedLoc);
1366-
const auto Ty =
1367-
performTypeResolution(attrRepr, /*IsSILType=*/true,
1368-
OuterGenericEnv, OuterGenericParams);
1365+
auto Ty = performTypeResolution(attrRepr, /*IsSILType=*/true, OuterGenericEnv,
1366+
OuterGenericParams);
1367+
if (OuterGenericEnv) {
1368+
Ty = OuterGenericEnv->mapTypeIntoContext(Ty);
1369+
}
1370+
13691371
if (Ty->hasError())
13701372
return true;
13711373

@@ -1930,9 +1932,12 @@ bool SILParser::parseSubstitutions(SmallVectorImpl<ParsedSubstitution> &parsed,
19301932
if (TyR.isNull())
19311933
return true;
19321934

1933-
const auto Ty =
1934-
performTypeResolution(TyR.get(), /*IsSILType=*/false,
1935-
GenericEnv, GenericParams);
1935+
auto Ty = performTypeResolution(TyR.get(), /*IsSILType=*/false, GenericEnv,
1936+
GenericParams);
1937+
if (GenericEnv) {
1938+
Ty = GenericEnv->mapTypeIntoContext(Ty);
1939+
}
1940+
19361941
if (Ty->hasError())
19371942
return true;
19381943
parsed.push_back({Loc, Ty});
@@ -2321,9 +2326,8 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired) {
23212326
genericParams = fnType->getGenericParams();
23222327
}
23232328

2324-
const auto Ty =
2325-
performTypeResolution(TyR.get(), /*IsSILType=*/false,
2326-
genericEnv, genericParams);
2329+
const auto Ty = performTypeResolution(TyR.get(), /*IsSILType=*/false,
2330+
genericEnv, genericParams);
23272331
if (Ty->hasError())
23282332
return true;
23292333

@@ -6841,9 +6845,12 @@ ProtocolConformanceRef SILParser::parseProtocolConformanceHelper(
68416845
if (witnessParams == nullptr)
68426846
witnessParams = ContextGenericParams;
68436847

6844-
const auto ConformingTy =
6845-
performTypeResolution(TyR.get(), /*IsSILType=*/false,
6846-
witnessEnv, witnessParams);
6848+
auto ConformingTy = performTypeResolution(TyR.get(), /*IsSILType=*/false,
6849+
witnessEnv, witnessParams);
6850+
if (witnessEnv) {
6851+
ConformingTy = witnessEnv->mapTypeIntoContext(ConformingTy);
6852+
}
6853+
68476854
if (ConformingTy->hasError())
68486855
return ProtocolConformanceRef();
68496856

@@ -6957,13 +6964,17 @@ static bool parseSILWitnessTableEntry(
69576964
if (TyR.isNull())
69586965
return true;
69596966

6960-
const auto Ty =
6967+
auto Ty =
69616968
swift::performTypeResolution(TyR.get(), P.Context,
69626969
/*isSILMode=*/false,
69636970
/*isSILType=*/false,
69646971
witnessEnv,
69656972
witnessParams,
69666973
&P.SF);
6974+
if (witnessEnv) {
6975+
Ty = witnessEnv->mapTypeIntoContext(Ty);
6976+
}
6977+
69676978
if (Ty->hasError())
69686979
return true;
69696980

@@ -7018,12 +7029,16 @@ static bool parseSILWitnessTableEntry(
70187029
if (TyR.isNull())
70197030
return true;
70207031

7021-
const auto Ty =
7032+
auto Ty =
70227033
swift::performTypeResolution(TyR.get(), P.Context,
70237034
/*isSILMode=*/false,
70247035
/*isSILType=*/false,
70257036
witnessEnv, witnessParams,
70267037
&P.SF);
7038+
if (witnessEnv) {
7039+
Ty = witnessEnv->mapTypeIntoContext(Ty);
7040+
}
7041+
70277042
if (Ty->hasError())
70287043
return true;
70297044

lib/Sema/CSBindings.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,10 @@ bool TypeVarBindingProducer::computeNext() {
18181818
// always preserved.
18191819
auto *BGT = type->castTo<BoundGenericType>();
18201820
auto dstLocator = TypeVar->getImpl().getLocator();
1821-
auto newType = CS.openUnboundGenericType(BGT->getDecl(), BGT->getParent(),
1822-
dstLocator)
1823-
->reconstituteSugar(/*recursive=*/false);
1821+
auto newType =
1822+
CS.openUnboundGenericType(BGT->getDecl(), BGT->getParent(),
1823+
dstLocator, /*isTypeResolution=*/false)
1824+
->reconstituteSugar(/*recursive=*/false);
18241825
addNewBinding(binding.withType(newType));
18251826
}
18261827

lib/Sema/CSGen.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,8 @@ namespace {
13211321
// Introduce type variables for unbound generics.
13221322
const auto genericOpener = OpenUnboundGenericType(CS, locator);
13231323
const auto placeholderHandler = HandlePlaceholderType(CS, locator);
1324-
const auto result = TypeResolution::forContextual(CS.DC, resCtx,
1325-
genericOpener,
1326-
placeholderHandler)
1327-
.resolveType(repr);
1324+
const auto result = TypeResolution::resolveContextualType(
1325+
repr, CS.DC, resCtx, genericOpener, placeholderHandler);
13281326
if (result->hasError()) {
13291327
return Type();
13301328
}
@@ -1597,13 +1595,12 @@ namespace {
15971595
auto *const locator = CS.getConstraintLocator(expr);
15981596
const auto options =
15991597
TypeResolutionOptions(TypeResolverContext::InExpression);
1600-
for (size_t i = 0, size = specializations.size(); i < size; ++i) {
1601-
const auto resolution = TypeResolution::forContextual(
1602-
CS.DC, options,
1598+
for (size_t i = 0, e = specializations.size(); i < e; ++i) {
1599+
const auto result = TypeResolution::resolveContextualType(
1600+
specializations[i], CS.DC, options,
16031601
// Introduce type variables for unbound generics.
16041602
OpenUnboundGenericType(CS, locator),
16051603
HandlePlaceholderType(CS, locator));
1606-
const auto result = resolution.resolveType(specializations[i]);
16071604
if (result->hasError())
16081605
return Type();
16091606

lib/Sema/CSSolver.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -1098,16 +1098,14 @@ void ConstraintSystem::shrink(Expr *expr) {
10981098
auto *const typeRepr = coerceExpr->getCastTypeRepr();
10991099

11001100
if (typeRepr && isSuitableCollection(typeRepr)) {
1101-
const auto coercionType =
1102-
TypeResolution::forContextual(
1103-
CS.DC, None,
1104-
// FIXME: Should we really be unconditionally complaining
1105-
// about unbound generics and placeholders here? For
1106-
// example:
1107-
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array]
1108-
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array<_>]
1109-
/*unboundTyOpener*/ nullptr, /*placeholderHandler*/ nullptr)
1110-
.resolveType(typeRepr);
1101+
const auto coercionType = TypeResolution::resolveContextualType(
1102+
typeRepr, CS.DC, None,
1103+
// FIXME: Should we really be unconditionally complaining
1104+
// about unbound generics and placeholders here? For
1105+
// example:
1106+
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array]
1107+
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array<_>]
1108+
/*unboundTyOpener*/ nullptr, /*placeholderHandler*/ nullptr);
11111109

11121110
// Looks like coercion type is invalid, let's skip this sub-tree.
11131111
if (coercionType->hasError())

0 commit comments

Comments
 (0)