Skip to content

Commit d2f1362

Browse files
committed
AST: Remove fromDefault and inferred from StructuralRequirement
1 parent dcca5ce commit d2f1362

File tree

10 files changed

+38
-96
lines changed

10 files changed

+38
-96
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "swift/AST/ASTAllocated.h"
2121
#include "swift/AST/Evaluator.h"
22-
#include "swift/AST/GenericSignature.h"
2322
#include "swift/AST/Identifier.h"
2423
#include "swift/AST/Import.h"
2524
#include "swift/AST/SILOptions.h"
@@ -86,6 +85,7 @@ namespace swift {
8685
class ForeignRepresentationInfo;
8786
class FuncDecl;
8887
class GenericContext;
88+
class GenericSignature;
8989
class InFlightDiagnostic;
9090
class IterableDeclContext;
9191
class LazyContextData;

include/swift/AST/Attr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "swift/AST/MacroDeclaration.h"
2828
#include "swift/AST/Ownership.h"
2929
#include "swift/AST/PlatformKind.h"
30-
#include "swift/AST/Requirement.h"
3130
#include "swift/AST/StorageImpl.h"
3231
#include "swift/Basic/Debug.h"
3332
#include "swift/Basic/EnumTraits.h"

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define SWIFT_AST_PROTOCOLCONFORMANCEREF_H
1818

1919
#include "swift/AST/ProtocolConformanceRef.h"
20-
#include "swift/AST/Requirement.h"
2120
#include "swift/AST/Type.h"
2221
#include "swift/AST/TypeAlignments.h"
2322
#include "swift/Basic/Debug.h"
@@ -36,6 +35,7 @@ class BuiltinProtocolConformance;
3635
class ConcreteDeclRef;
3736
class PackConformance;
3837
class ProtocolConformance;
38+
class Requirement;
3939
enum class EffectKind : uint8_t;
4040

4141
/// A ProtocolConformanceRef is a handle to a protocol conformance which

include/swift/AST/Requirement.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,11 @@ CheckRequirementsResult checkRequirements(
233233
/// A requirement as written in source, together with a source location. See
234234
/// ProtocolDecl::getStructuralRequirements().
235235
struct StructuralRequirement {
236-
/// The actual requirement, where the types were resolved with the
237-
/// 'Structural' type resolution stage.
236+
/// A requirement with resolved in the structural resolution stage.
238237
Requirement req;
239238

240-
/// The source location where the requirement is written, used for redundancy
241-
/// and conflict diagnostics.
239+
/// The source location where the requirement is written, for diagnostics.
242240
SourceLoc loc;
243-
244-
/// A flag indicating whether the requirement was inferred from the
245-
/// application of a type constructor. Also used for diagnostics, because
246-
/// an inferred requirement made redundant by an explicit requirement is not
247-
/// diagnosed as redundant, since we want to give users the option of
248-
/// spelling out these requirements explicitly.
249-
bool inferred = false;
250-
251-
/// A flag indicating whether this requirement was produced via the expansion
252-
/// of default conformances to invertible protocols.
253-
bool fromDefault = false;
254241
};
255242

256243
/// An "anti-conformance" requirement `Subject: ~Protocol`.

include/swift/AST/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "swift/AST/KnownProtocols.h"
2828
#include "swift/AST/Ownership.h"
2929
#include "swift/AST/ProtocolConformanceRef.h"
30-
#include "swift/AST/Requirement.h"
3130
#include "swift/AST/SubstitutionMap.h"
3231
#include "swift/AST/Type.h"
3332
#include "swift/AST/TypeAlignments.h"

lib/AST/Requirement.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,7 @@ void InverseRequirement::expandDefaults(
363363

364364
auto protoTy = proto->getDeclaredInterfaceType();
365365
result.push_back({{RequirementKind::Conformance, gp, protoTy},
366-
SourceLoc(),
367-
/*inferred=*/true,
368-
/*default=*/true,
369-
});
366+
SourceLoc()});
370367
}
371368
}
372369
}

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -691,32 +691,15 @@ bool ConcreteContraction::performConcreteContraction(
691691
// requirement where the left hand side is not a type parameter.
692692
SmallVector<Requirement, 4> reqs;
693693
SmallVector<InverseRequirement, 4> ignoreInverses;
694-
if (req.inferred) {
695-
// Discard errors from desugaring a substituted requirement that
696-
// was inferred. For example, if we have something like
697-
//
698-
// <T, U where T == Int, U == Set<T>>
699-
//
700-
// The inferred requirement 'T : Hashable' from 'Set<>' will
701-
// be substituted with 'T == Int' to get 'Int : Hashable'.
702-
//
703-
// Desugaring will diagnose a redundant conformance requirement,
704-
// but we want to ignore that, since the user did not explicitly
705-
// write 'Int : Hashable' (or 'T : Hashable') anywhere.
706-
SmallVector<RequirementError, 4> discardErrors;
707-
desugarRequirement(substReq, SourceLoc(), reqs,
708-
ignoreInverses, discardErrors);
709-
} else {
710-
desugarRequirement(substReq, req.loc, reqs, ignoreInverses, errors);
711-
}
694+
desugarRequirement(substReq, req.loc, reqs, ignoreInverses, errors);
712695

713696
for (auto desugaredReq : reqs) {
714697
if (Debug) {
715698
llvm::dbgs() << "@@ Desugared requirement: ";
716699
desugaredReq.dump(llvm::dbgs());
717700
llvm::dbgs() << "\n";
718701
}
719-
result.push_back({desugaredReq, req.loc, req.inferred});
702+
result.push_back({desugaredReq, req.loc});
720703
}
721704

722705
if (preserveSameTypeRequirement(req.req) &&
@@ -730,7 +713,7 @@ bool ConcreteContraction::performConcreteContraction(
730713

731714
// Make the duplicated requirement 'inferred' so that we don't diagnose
732715
// it as redundant.
733-
result.push_back({req.req, SourceLoc(), /*inferred=*/true});
716+
result.push_back({req.req, SourceLoc()});
734717
}
735718
}
736719

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,11 @@ void swift::rewriting::desugarRequirements(
455455
SmallVector<StructuralRequirement, 2> result;
456456
for (auto req : reqs) {
457457
SmallVector<Requirement, 2> desugaredReqs;
458-
SmallVector<RequirementError, 2> ignoredErrors;
459-
460-
if (req.inferred)
461-
desugarRequirement(req.req, SourceLoc(), desugaredReqs,
462-
inverses, ignoredErrors);
463-
else
464-
desugarRequirement(req.req, req.loc, desugaredReqs,
465-
inverses, errors);
458+
desugarRequirement(req.req, req.loc, desugaredReqs,
459+
inverses, errors);
466460

467461
for (auto desugaredReq : desugaredReqs)
468-
result.push_back({desugaredReq, req.loc, req.inferred});
462+
result.push_back({desugaredReq, req.loc});
469463
}
470464

471465
std::swap(reqs, result);
@@ -510,11 +504,11 @@ static void realizeTypeRequirement(DeclContext *dc,
510504
if (constraintType->isConstraintType()) {
511505
result.push_back({Requirement(RequirementKind::Conformance,
512506
subjectType, constraintType),
513-
loc, /*wasInferred=*/false});
507+
loc});
514508
} else if (constraintType->getClassOrBoundGenericClass()) {
515509
result.push_back({Requirement(RequirementKind::Superclass,
516510
subjectType, constraintType),
517-
loc, /*wasInferred=*/false});
511+
loc});
518512
} else {
519513
errors.push_back(
520514
RequirementError::forInvalidTypeRequirement(subjectType,
@@ -541,6 +535,9 @@ struct InferRequirementsWalker : public TypeWalker {
541535
if (ty->is<UnboundGenericType>())
542536
return Action::Stop;
543537

538+
if (!ty->hasTypeParameter())
539+
return Action::SkipChildren;
540+
544541
return Action::Continue;
545542
}
546543

@@ -570,7 +567,7 @@ struct InferRequirementsWalker : public TypeWalker {
570567
if (skipRequirement(rawReq, decl))
571568
continue;
572569

573-
reqs.push_back({rawReq.subst(subMap), SourceLoc(), /*inferred=*/true});
570+
reqs.push_back({rawReq.subst(subMap), SourceLoc()});
574571
}
575572

576573
return Action::Continue;
@@ -586,7 +583,7 @@ struct InferRequirementsWalker : public TypeWalker {
586583
auto countType = packExpansion->getCountType();
587584
for (auto pack : packReferences)
588585
reqs.push_back({Requirement(RequirementKind::SameShape, countType, pack),
589-
SourceLoc(), /*inferred=*/true});
586+
SourceLoc()});
590587
}
591588

592589
// Infer requirements from `@differentiable` function types.
@@ -600,7 +597,7 @@ struct InferRequirementsWalker : public TypeWalker {
600597
auto addConformanceConstraint = [&](Type type, ProtocolDecl *protocol) {
601598
reqs.push_back({Requirement(RequirementKind::Conformance, type,
602599
protocol->getDeclaredInterfaceType()),
603-
SourceLoc(), /*inferred=*/true});
600+
SourceLoc()});
604601
};
605602

606603
auto &ctx = module->getASTContext();
@@ -614,7 +611,7 @@ struct InferRequirementsWalker : public TypeWalker {
614611
->substBaseType(module, firstType);
615612
reqs.push_back({Requirement(RequirementKind::SameType,
616613
firstType, secondType),
617-
SourceLoc(), /*inferred=*/true});
614+
SourceLoc()});
618615
};
619616
auto *tangentVectorAssocType =
620617
differentiableProtocol->getAssociatedType(ctx.Id_TangentVector);
@@ -662,7 +659,7 @@ struct InferRequirementsWalker : public TypeWalker {
662659
if (skipRequirement(rawReq, decl))
663660
continue;
664661

665-
reqs.push_back({rawReq.subst(subMap), SourceLoc(), /*inferred=*/true});
662+
reqs.push_back({rawReq.subst(subMap), SourceLoc()});
666663
}
667664

668665
return Action::Continue;
@@ -731,7 +728,7 @@ void swift::rewriting::realizeRequirement(
731728
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);
732729
}
733730

734-
result.push_back({req, loc, /*wasInferred=*/false});
731+
result.push_back({req, loc});
735732
break;
736733
}
737734

@@ -748,7 +745,7 @@ void swift::rewriting::realizeRequirement(
748745
inferRequirements(secondType, secondLoc, moduleForInference, dc, result);
749746
}
750747

751-
result.push_back({req, loc, /*wasInferred=*/false});
748+
result.push_back({req, loc});
752749
break;
753750
}
754751
}
@@ -812,11 +809,7 @@ void swift::rewriting::applyInverses(
812809
if (req.getKind() != RequirementKind::Conformance)
813810
return false;
814811

815-
// Only consider requirements from defaults-expansion...
816-
if (!structReq.fromDefault)
817-
return false;
818-
819-
// involving an invertible protocol.
812+
// Only consider requirements involving an invertible protocol.
820813
llvm::Optional<InvertibleProtocolKind> proto;
821814
if (auto kp = req.getProtocolDecl()->getKnownProtocolKind())
822815
if (auto ip = getInvertibleProtocolKind(*kp))
@@ -917,7 +910,7 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
917910
result.push_back({
918911
Requirement(RequirementKind::Conformance,
919912
selfTy, inheritedProto->getDeclaredInterfaceType()),
920-
SourceLoc(), /*wasInferred=*/false});
913+
SourceLoc()});
921914
}
922915
}
923916

@@ -936,7 +929,7 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
936929
auto layout = LayoutConstraint::getLayoutConstraint(
937930
LayoutConstraintKind::Class, ctx);
938931
result.push_back({Requirement(RequirementKind::Layout, selfTy, layout),
939-
proto->getLoc(), /*inferred=*/true});
932+
SourceLoc()});
940933

941934
desugarRequirements(result, inverses, errors);
942935
InverseRequirement::expandDefaults(ctx, needsDefaultRequirements, result);
@@ -1000,8 +993,7 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
1000993
selfTy, typeAliasDecl->getName());
1001994
Requirement req(RequirementKind::SameType, subjectType,
1002995
underlyingType);
1003-
result.push_back({req, typeAliasDecl->getLoc(),
1004-
/*inferred=*/false});
996+
result.push_back({req, typeAliasDecl->getLoc()});
1005997
}
1006998
}
1007999
}

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,10 @@
126126
// redundant rules. This is implemented in HomotopyReduction.cpp and
127127
// MinimalConformances.cpp.
128128
//
129-
// Minimization emits warnings about redundant rules by producing that
130-
// correspond to user-written requirements by producing RequirementError
131-
// values.
132-
//
133129
// After minimization, the remaining non-redundant rules are converted into
134-
// the Requirements of a minimal generic signature using the
135-
// RequirementBuilder.
136-
//
137-
// After minimization, the requirement machine undergoes a final state
138-
// transition into the immutable "frozen" state:
130+
// the Requirements of a minimal generic signature by the RequirementBuilder.
131+
// Then, the requirement machine undergoes a final state transition into the
132+
// immutable "frozen" state:
139133
//
140134
// /-----------------------------\
141135
// | Complete RequirementMachine |

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ static void splitConcreteEquivalenceClasses(
188188
req.getFirstType(), concreteType);
189189
Requirement secondReq(RequirementKind::SameType,
190190
req.getSecondType(), concreteType);
191-
splitRequirements.push_back({firstReq, SourceLoc(), /*inferred=*/false});
192-
splitRequirements.push_back({secondReq, SourceLoc(), /*inferred=*/false});
191+
splitRequirements.push_back({firstReq, SourceLoc()});
192+
splitRequirements.push_back({secondReq, SourceLoc()});
193193

194194
if (debug) {
195195
llvm::dbgs() << "- First split: ";
@@ -201,7 +201,7 @@ static void splitConcreteEquivalenceClasses(
201201
continue;
202202
}
203203

204-
splitRequirements.push_back({req, SourceLoc(), /*inferred=*/false});
204+
splitRequirements.push_back({req, SourceLoc()});
205205

206206
if (debug) {
207207
llvm::dbgs() << "- Not split: ";
@@ -319,7 +319,7 @@ RequirementSignatureRequest::evaluate(Evaluator &evaluator,
319319
for (auto req : proto->getStructuralRequirements())
320320
requirements.push_back(req);
321321
for (auto req : proto->getTypeAliasRequirements())
322-
requirements.push_back({req, SourceLoc(), /*inferred=*/false});
322+
requirements.push_back({req, SourceLoc()});
323323
}
324324

325325
if (rewriteCtx.getDebugOptions().contains(DebugFlags::Timers)) {
@@ -638,11 +638,11 @@ AbstractGenericSignatureRequest::evaluate(
638638
// empty source locations.
639639
SmallVector<StructuralRequirement, 2> requirements;
640640
for (auto req : baseSignature.getRequirements())
641-
requirements.push_back({req, SourceLoc(), /*wasInferred=*/false});
641+
requirements.push_back({req, SourceLoc()});
642642

643643
// Add the new requirements.
644644
for (auto req : addedRequirements)
645-
requirements.push_back({req, SourceLoc(), /*wasInferred=*/false});
645+
requirements.push_back({req, SourceLoc()});
646646

647647
// The requirements passed to this request may have been substituted,
648648
// meaning the subject type might be a concrete type and not a type
@@ -780,7 +780,7 @@ InferredGenericSignatureRequest::evaluate(
780780
}();
781781

782782
for (const auto &req : parentSig.getRequirements())
783-
requirements.push_back({req, loc, /*wasInferred=*/false});
783+
requirements.push_back({req, loc});
784784

785785
DeclContext *lookupDC = nullptr;
786786

@@ -868,7 +868,7 @@ InferredGenericSignatureRequest::evaluate(
868868
// inferred same-type requirements when building the generic signature of
869869
// an extension whose extended type is a generic typealias.
870870
for (const auto &req : addedRequirements)
871-
requirements.push_back({req, SourceLoc(), /*inferred=*/true});
871+
requirements.push_back({req, SourceLoc()});
872872

873873
desugarRequirements(requirements, inverses, errors);
874874

@@ -883,15 +883,6 @@ InferredGenericSignatureRequest::evaluate(
883883
InverseRequirement::expandDefaults(ctx, paramTypes, requirements);
884884
applyInverses(ctx, paramTypes, inverses, requirements, errors);
885885

886-
// Re-order requirements so that inferred requirements appear last. This
887-
// ensures that if an inferred requirement is redundant with some other
888-
// requirement, it is the inferred requirement that becomes redundant,
889-
// which muffles the redundancy diagnostic.
890-
std::stable_partition(requirements.begin(), requirements.end(),
891-
[](const StructuralRequirement &req) {
892-
return !req.inferred;
893-
});
894-
895886
auto &rewriteCtx = ctx.getRewriteContext();
896887

897888
if (rewriteCtx.getDebugOptions().contains(DebugFlags::Timers)) {

0 commit comments

Comments
 (0)