Skip to content

Commit 026d3f0

Browse files
authored
Merge pull request swiftlang#70311 from slavapestov/type-witness-system-inference-stdlib
Get the standard library to build with -enable-experimental-associated-type-inference
2 parents e4436ba + 4bf0cc2 commit 026d3f0

26 files changed

+583
-90
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6761,11 +6761,13 @@ class DependentMemberType : public TypeBase {
67616761
/// Substitute the base type, looking up our associated type in it if it is
67626762
/// non-dependent. Returns null if the member could not be found in the new
67636763
/// base.
6764-
Type substBaseType(Type base, LookupConformanceFn lookupConformance);
6764+
Type substBaseType(Type base, LookupConformanceFn lookupConformance,
6765+
SubstOptions options);
67656766

67666767
/// Substitute the root generic type, looking up the chain of associated types.
67676768
/// Returns null if the member could not be found in the new root.
6768-
Type substRootParam(Type newRoot, LookupConformanceFn lookupConformance);
6769+
Type substRootParam(Type newRoot, LookupConformanceFn lookupConformance,
6770+
SubstOptions options);
67696771

67706772
// Implement isa/cast/dyncast/etc.
67716773
static bool classof(const TypeBase *T) {

include/swift/Basic/Features.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ EXPERIMENTAL_FEATURE(MoveOnlyTuples, true)
151151
EXPERIMENTAL_FEATURE(MoveOnlyPartialConsumption, true)
152152

153153
EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
154-
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
155154
EXPERIMENTAL_FEATURE(LayoutPrespecialization, true)
156155

157156
EXPERIMENTAL_FEATURE(AccessLevelOnImport, true)

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ namespace swift {
585585
/// Enable warnings for redundant requirements in generic signatures.
586586
bool WarnRedundantRequirements = false;
587587

588+
/// Enable experimental associated type inference improvements.
589+
bool EnableExperimentalAssociatedTypeInference = false;
590+
588591
/// Enables dumping type witness systems from associated type inference.
589592
bool DumpTypeWitnessSystems = false;
590593

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,11 @@ def disable_experimental_string_processing :
654654

655655
def enable_experimental_associated_type_inference :
656656
Flag<["-"], "enable-experimental-associated-type-inference">,
657-
HelpText<"Enable experimental associated type inference via type witness systems">;
657+
HelpText<"Enable experimental associated type inference improvements">;
658+
659+
def disable_experimental_associated_type_inference :
660+
Flag<["-"], "disable-experimental-associated-type-inference">,
661+
HelpText<"Disable experimental associated type inference improvements">;
658662

659663
def disable_availability_checking : Flag<["-"],
660664
"disable-availability-checking">,

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,10 +3485,6 @@ static bool usesFeatureOneWayClosureParameters(Decl *decl) {
34853485
return false;
34863486
}
34873487

3488-
static bool usesFeatureTypeWitnessSystemInference(Decl *decl) {
3489-
return false;
3490-
}
3491-
34923488
static bool usesFeatureOpaqueTypeErasure(Decl *decl) {
34933489
return false;
34943490
}

lib/AST/ProtocolConformance.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,27 +535,48 @@ ProtocolConformance::getAssociatedConformance(Type assocType,
535535
ProtocolConformanceRef
536536
NormalProtocolConformance::getAssociatedConformance(Type assocType,
537537
ProtocolDecl *protocol) const {
538+
if (Loader)
539+
resolveLazyInfo();
540+
538541
assert(assocType->isTypeParameter() &&
539542
"associated type must be a type parameter");
540543

541544
llvm::Optional<ProtocolConformanceRef> result;
542545

546+
auto &ctx = getDeclContext()->getASTContext();
547+
543548
forEachAssociatedConformance(
544549
[&](Type t, ProtocolDecl *p, unsigned index) {
545550
if (t->isEqual(assocType) && p == protocol) {
546-
// Fill in the signature conformances, if we haven't done so yet.
547-
if (!hasComputedAssociatedConformances()) {
548-
const_cast<NormalProtocolConformance *>(this)
549-
->finishSignatureConformances();
551+
if (!ctx.LangOpts.EnableExperimentalAssociatedTypeInference) {
552+
// Fill in the signature conformances, if we haven't done so yet.
553+
if (!hasComputedAssociatedConformances()) {
554+
const_cast<NormalProtocolConformance *>(this)
555+
->finishSignatureConformances();
556+
}
557+
}
558+
559+
// Not strictly necessary, but avoids a bit of request evaluator
560+
// overhead in the happy case.
561+
if (hasComputedAssociatedConformances()) {
562+
result = AssociatedConformances[index];
563+
if (result)
564+
return true;
550565
}
551566

552-
result = getAssociatedConformance(index);
567+
result = evaluateOrDefault(ctx.evaluator,
568+
AssociatedConformanceRequest{
569+
const_cast<NormalProtocolConformance *>(this),
570+
t->getCanonicalType(), p, index
571+
}, ProtocolConformanceRef::forInvalid());
553572
return true;
554573
}
555574

556575
return false;
557576
});
558577

578+
assert(result && "Subject type must be exactly equal to left-hand side of a"
579+
"conformance requirement in protocol requirement signature");
559580
return *result;
560581
}
561582

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ static Type substPrefixType(Type type, unsigned suffixLength, Type prefixType,
346346
auto substBaseType = substPrefixType(memberType->getBase(), suffixLength - 1,
347347
prefixType, sig);
348348
return memberType->substBaseType(substBaseType,
349-
LookUpConformanceInSignature(sig.getPointer()));
349+
LookUpConformanceInSignature(sig.getPointer()),
350+
llvm::None);
350351
}
351352

352353
/// Unlike most other queries, the input type can be any type, not just a

lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5580,7 +5580,7 @@ TypeBase::getAutoDiffTangentSpace(LookupConformanceFn lookupConformance) {
55805580

55815581
// Try to get the `TangentVector` associated type of `base`.
55825582
// Return the associated type if it is valid.
5583-
auto assocTy = dependentType->substBaseType(this, lookupConformance);
5583+
auto assocTy = dependentType->substBaseType(this, lookupConformance, llvm::None);
55845584
if (!assocTy->hasError())
55855585
return cache(TangentSpace::getTangentVector(assocTy));
55865586

lib/AST/TypeSubstitution.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,30 +274,32 @@ operator()(CanType dependentType, Type conformingReplacementType,
274274
}
275275

276276
Type DependentMemberType::substBaseType(ModuleDecl *module, Type substBase) {
277-
return substBaseType(substBase, LookUpConformanceInModule(module));
277+
return substBaseType(substBase, LookUpConformanceInModule(module), llvm::None);
278278
}
279279

280280
Type DependentMemberType::substBaseType(Type substBase,
281-
LookupConformanceFn lookupConformance) {
281+
LookupConformanceFn lookupConformance,
282+
SubstOptions options) {
282283
if (substBase.getPointer() == getBase().getPointer() &&
283284
substBase->hasTypeParameter())
284285
return this;
285286

286-
InFlightSubstitution IFS(nullptr, lookupConformance, llvm::None);
287+
InFlightSubstitution IFS(nullptr, lookupConformance, options);
287288
return getMemberForBaseType(IFS, getBase(), substBase,
288289
getAssocType(), getName(),
289290
/*level=*/0);
290291
}
291292

292293
Type DependentMemberType::substRootParam(Type newRoot,
293-
LookupConformanceFn lookupConformance){
294+
LookupConformanceFn lookupConformance,
295+
SubstOptions options) {
294296
auto base = getBase();
295297
if (base->is<GenericTypeParamType>()) {
296-
return substBaseType(newRoot, lookupConformance);
298+
return substBaseType(newRoot, lookupConformance, options);
297299
}
298300
if (auto depMem = base->getAs<DependentMemberType>()) {
299-
return substBaseType(depMem->substRootParam(newRoot, lookupConformance),
300-
lookupConformance);
301+
return substBaseType(depMem->substRootParam(newRoot, lookupConformance, options),
302+
lookupConformance, options);
301303
}
302304
return Type();
303305
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
910910
}
911911
if (Args.hasArg(OPT_experimental_one_way_closure_params))
912912
Opts.Features.insert(Feature::OneWayClosureParameters);
913-
if (Args.hasArg(OPT_enable_experimental_associated_type_inference))
914-
Opts.Features.insert(Feature::TypeWitnessSystemInference);
915913
if (Args.hasArg(OPT_enable_experimental_forward_mode_differentiation))
916914
Opts.Features.insert(Feature::ForwardModeDifferentiation);
917915
if (Args.hasArg(OPT_enable_experimental_additive_arithmetic_derivation))
@@ -1351,6 +1349,11 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
13511349
if (Args.hasArg(OPT_warn_redundant_requirements))
13521350
Opts.WarnRedundantRequirements = true;
13531351

1352+
if (Args.hasArg(OPT_enable_experimental_associated_type_inference))
1353+
Opts.EnableExperimentalAssociatedTypeInference = true;
1354+
if (Args.hasArg(OPT_disable_experimental_associated_type_inference))
1355+
Opts.EnableExperimentalAssociatedTypeInference = false;
1356+
13541357
Opts.DumpTypeWitnessSystems = Args.hasArg(OPT_dump_type_witness_systems);
13551358

13561359
for (auto &block: FrontendOpts.BlocklistConfigFilePaths)

0 commit comments

Comments
 (0)