Skip to content

Commit bf1d174

Browse files
authored
Merge pull request swiftlang#74631 from slavapestov/rqm-assertions
RequirementMachine: Convert to new assertions
2 parents 94aa0cd + 273c4b2 commit bf1d174

34 files changed

+393
-389
lines changed

include/swift/Basic/Assertions.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,27 @@
4141
// that are more expensive than you think. You can switch those to
4242
// `CONDITIONAL_ASSERT` or `DEBUG_ASSERT` as needed.
4343

44+
// Visual Studio doesn't have __FILE_NAME__
45+
#ifdef __FILE_NAME__
46+
4447
#define ASSERT(expr) \
4548
do { \
46-
if (ASSERT_UNLIKELY(!expr)) { \
49+
if (ASSERT_UNLIKELY(!(expr))) { \
4750
ASSERT_failure(#expr, __FILE_NAME__, __LINE__, __func__); \
4851
} \
4952
} while (0)
5053

54+
#else
55+
56+
#define ASSERT(expr) \
57+
do { \
58+
if (ASSERT_UNLIKELY(!(expr))) { \
59+
ASSERT_failure(#expr, __FILE__, __LINE__, __func__); \
60+
} \
61+
} while (0)
62+
63+
#endif
64+
5165
// Function that reports the actual failure when it occurs.
5266
void ASSERT_failure(const char *expr, const char *file, int line, const char *func);
5367

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ ConcreteContraction::substTypeParameterRec(Type type, Position position) const {
346346
/// it is the subject of a conformance requirement.
347347
Type ConcreteContraction::substTypeParameter(
348348
Type type, Position position) const {
349-
assert(type->isTypeParameter());
349+
ASSERT(type->isTypeParameter());
350350

351351
auto result = substTypeParameterRec(type, position);
352352
if (!result)
@@ -552,13 +552,13 @@ bool ConcreteContraction::performConcreteContraction(
552552
// subject type is a generic parameter.
553553
for (auto req : requirements) {
554554
auto subjectType = req.req.getFirstType();
555-
assert(subjectType->isTypeParameter() &&
556-
"You forgot to call desugarRequirement()");
555+
ASSERT(subjectType->isTypeParameter() &&
556+
"Forgot to call desugarRequirement()");
557557

558558
auto kind = req.req.getKind();
559559
switch (kind) {
560560
case RequirementKind::SameShape:
561-
assert(req.req.getSecondType()->isTypeParameter());
561+
ASSERT(req.req.getSecondType()->isTypeParameter());
562562
continue;
563563

564564
case RequirementKind::SameType: {
@@ -584,8 +584,8 @@ bool ConcreteContraction::performConcreteContraction(
584584
}
585585
case RequirementKind::Superclass: {
586586
auto constraintType = req.req.getSecondType();
587-
assert(!constraintType->isTypeParameter() &&
588-
"You forgot to call desugarRequirement()");
587+
ASSERT(!constraintType->isTypeParameter() &&
588+
"Forgot to call desugarRequirement()");
589589

590590
subjectType = stripBoundDependentMemberTypes(subjectType);
591591
if (typeOccursIn(subjectType,

lib/AST/RequirementMachine/ConcreteTypeWitness.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(
124124
ArrayRef<Term> substitutions,
125125
ArrayRef<unsigned> conformsToRules,
126126
ArrayRef<const ProtocolDecl *> conformsTo) {
127-
assert(requirementKind == RequirementKind::SameType ||
127+
ASSERT(requirementKind == RequirementKind::SameType ||
128128
requirementKind == RequirementKind::Superclass);
129-
assert(conformsTo.size() == conformsToRules.size());
129+
ASSERT(conformsTo.size() == conformsToRules.size());
130130

131131
for (unsigned i : indices(conformsTo)) {
132132
auto *proto = conformsTo[i];
@@ -227,7 +227,7 @@ void PropertyMap::concretizeTypeWitnessInConformance(
227227
auto substitutions = concreteConformanceSymbol.getSubstitutions();
228228

229229
auto *proto = assocType->getProtocol();
230-
assert(proto == concreteConformanceSymbol.getProtocol());
230+
ASSERT(proto == concreteConformanceSymbol.getProtocol());
231231

232232
if (Debug.contains(DebugFlags::ConcretizeNestedTypes)) {
233233
llvm::dbgs() << "^^ " << "Looking up type witness for "
@@ -284,7 +284,7 @@ void PropertyMap::concretizeTypeWitnessInConformance(
284284
key, requirementKind, concreteType, typeWitness, subjectType,
285285
substitutions, path);
286286

287-
assert(!path.empty());
287+
ASSERT(!path.empty());
288288
(void) System.addRule(constraintType, subjectType, &path);
289289
if (Debug.contains(DebugFlags::ConcretizeNestedTypes)) {
290290
llvm::dbgs() << "^^ Induced rule " << constraintType
@@ -411,7 +411,7 @@ MutableTerm PropertyMap::computeConstraintTermForTypeWitness(
411411
&substPath);
412412
if (differenceID) {
413413
const auto &difference = System.getTypeDifference(*differenceID);
414-
assert(difference.LHS == typeWitnessSymbol);
414+
ASSERT(difference.LHS == typeWitnessSymbol);
415415
typeWitnessSymbol = difference.RHS;
416416
substPath.invert();
417417
}
@@ -599,7 +599,7 @@ void PropertyMap::inferConditionalRequirements(
599599
builder.initWithConditionalRequirements(desugaredRequirements,
600600
substitutions);
601601

602-
assert(builder.PermanentRules.empty());
602+
ASSERT(builder.PermanentRules.empty());
603603

604604
System.addRules(std::move(builder.ImportedRules),
605605
std::move(builder.PermanentRules),

lib/AST/RequirementMachine/Diagnostics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool swift::rewriting::diagnoseRequirementErrors(
203203
if (requirement.hasError())
204204
break;
205205

206-
assert(requirement.getKind() == RequirementKind::SameType ||
206+
ASSERT(requirement.getKind() == RequirementKind::SameType ||
207207
requirement.getKind() == RequirementKind::Superclass);
208208

209209
ctx.Diags.diagnose(loc,
@@ -273,7 +273,7 @@ void RewriteSystem::computeConflictingRequirementDiagnostics(
273273
const auto &firstRule = getRule(pair.first);
274274
const auto &secondRule = getRule(pair.second);
275275

276-
assert(firstRule.isPropertyRule() && secondRule.isPropertyRule());
276+
ASSERT(firstRule.isPropertyRule() && secondRule.isPropertyRule());
277277

278278
if (firstRule.isSubstitutionSimplified() ||
279279
secondRule.isSubstitutionSimplified())
@@ -309,7 +309,7 @@ void RewriteSystem::computeRecursiveRequirementDiagnostics(
309309
for (unsigned ruleID : RecursiveRules) {
310310
const auto &rule = getRule(ruleID);
311311

312-
assert(isInMinimizationDomain(rule.getRHS()[0].getRootProtocol()));
312+
ASSERT(isInMinimizationDomain(rule.getRHS()[0].getRootProtocol()));
313313

314314
Type subjectType = propertyMap.getTypeForTerm(rule.getRHS(), genericParams);
315315
errors.push_back(RequirementError::forRecursiveRequirement(

lib/AST/RequirementMachine/Diagnostics.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ASTContext.h"
1717
#include "swift/AST/Requirement.h"
1818
#include "swift/AST/Type.h"
19+
#include "swift/Basic/Assertions.h"
1920

2021
namespace swift {
2122

@@ -84,14 +85,14 @@ struct RequirementError {
8485

8586
public:
8687
Requirement getRequirement() const {
87-
assert(kind != Kind::InvalidInverseOuterSubject &&
88+
ASSERT(kind != Kind::InvalidInverseOuterSubject &&
8889
kind != Kind::InvalidInverseSubject &&
8990
kind != Kind::ConflictingInverseRequirement);
9091
return requirement;
9192
}
9293

9394
InverseRequirement getInverse() const {
94-
assert(kind == Kind::InvalidInverseOuterSubject ||
95+
ASSERT(kind == Kind::InvalidInverseOuterSubject ||
9596
kind == Kind::InvalidInverseSubject ||
9697
kind == Kind::ConflictingInverseRequirement);
9798
return inverse;

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
243243
return prefix;
244244

245245
case Symbol::Kind::Protocol:
246-
assert(prefix.empty() &&
246+
ASSERT(prefix.empty() &&
247247
"Protocol symbol can only appear at the start of a type term");
248248
break;
249249

250250
case Symbol::Kind::GenericParam: {
251-
assert(prefix.empty() &&
251+
ASSERT(prefix.empty() &&
252252
"Generic parameter symbol can only appear at the start of a type term");
253253

254254
if (std::find_if(Params.begin(), Params.end(),
@@ -500,8 +500,6 @@ Type RequirementMachine::getReducedType(
500500
/// Determine if the given type parameter is valid with respect to this
501501
/// requirement machine's generic signature.
502502
bool RequirementMachine::isValidTypeParameter(Type type) const {
503-
assert(type->isTypeParameter());
504-
505503
auto term = Context.getMutableTermForType(type->getCanonicalType(),
506504
/*proto=*/nullptr);
507505
System.simplify(term);
@@ -525,23 +523,21 @@ bool RequirementMachine::isValidTypeParameter(Type type) const {
525523
ConformancePath
526524
RequirementMachine::getConformancePath(Type type,
527525
ProtocolDecl *protocol) {
528-
assert(type->isTypeParameter());
529-
530526
auto mutTerm = Context.getMutableTermForType(type->getCanonicalType(),
531527
/*proto=*/nullptr);
532528
System.simplify(mutTerm);
533529
verify(mutTerm);
534530

535-
#ifndef NDEBUG
536-
auto *props = Map.lookUpProperties(mutTerm);
537-
assert(props &&
538-
"Subject type of conformance access path should be known");
539-
assert(!props->isConcreteType() &&
540-
"Concrete types do not have conformance access paths");
541-
auto conformsTo = props->getConformsTo();
542-
assert(std::find(conformsTo.begin(), conformsTo.end(), protocol) &&
543-
"Subject type of conformance access path must conform to protocol");
544-
#endif
531+
if (CONDITIONAL_ASSERT_enabled()) {
532+
auto *props = Map.lookUpProperties(mutTerm);
533+
ASSERT(props &&
534+
"Subject type of conformance access path should be known");
535+
ASSERT(!props->isConcreteType() &&
536+
"Concrete types do not have conformance access paths");
537+
auto conformsTo = props->getConformsTo();
538+
ASSERT(std::find(conformsTo.begin(), conformsTo.end(), protocol) &&
539+
"Subject type of conformance access path must conform to protocol");
540+
}
545541

546542
auto term = Term::get(mutTerm, Context);
547543

@@ -565,8 +561,7 @@ RequirementMachine::getConformancePath(Type type,
565561
auto key = std::make_pair(term, proto);
566562
auto inserted = ConformancePaths.insert(
567563
std::make_pair(key, path));
568-
assert(inserted.second);
569-
(void) inserted;
564+
ASSERT(inserted.second);
570565

571566
if (Stats)
572567
++Stats->getFrontendCounters().NumConformancePathsRecorded;
@@ -724,7 +719,7 @@ RequirementMachine::lookupNestedType(Type depType, Identifier name) const {
724719
}
725720

726721
if (bestAssocType) {
727-
assert(bestAssocType->getOverriddenDecls().empty() &&
722+
ASSERT(bestAssocType->getOverriddenDecls().empty() &&
728723
"Lookup should never keep a non-anchor associated type");
729724
return bestAssocType;
730725

@@ -738,7 +733,7 @@ RequirementMachine::lookupNestedType(Type depType, Identifier name) const {
738733

739734
MutableTerm
740735
RequirementMachine::getReducedShapeTerm(Type type) const {
741-
assert(type->isParameterPack());
736+
ASSERT(type->isParameterPack());
742737

743738
auto term = Context.getMutableTermForType(type->getCanonicalType(),
744739
/*proto=*/nullptr);
@@ -780,7 +775,9 @@ bool RequirementMachine::haveSameShape(Type type1, Type type2) const {
780775
}
781776

782777
void RequirementMachine::verify(const MutableTerm &term) const {
783-
#ifndef NDEBUG
778+
if (!CONDITIONAL_ASSERT_enabled())
779+
return;
780+
784781
// If the term is in the generic parameter domain, ensure we have a valid
785782
// generic parameter.
786783
if (term.begin()->getKind() == Symbol::Kind::GenericParam) {
@@ -828,7 +825,7 @@ void RequirementMachine::verify(const MutableTerm &term) const {
828825

829826
switch (symbol.getKind()) {
830827
case Symbol::Kind::Name:
831-
assert(!erased.empty());
828+
ASSERT(!erased.empty());
832829
erased.add(symbol);
833830
break;
834831

@@ -865,5 +862,4 @@ void RequirementMachine::verify(const MutableTerm &term) const {
865862
dump(llvm::errs());
866863
abort();
867864
}
868-
#endif
869865
}

lib/AST/RequirementMachine/Histogram.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef SWIFT_HISTOGRAM_H
1616
#define SWIFT_HISTOGRAM_H
1717

18+
#include "swift/Basic/Assertions.h"
1819
#include "llvm/ADT/ArrayRef.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/Support/raw_ostream.h"
@@ -68,7 +69,7 @@ class Histogram {
6869
/// Start. The value is allowed to be greater than or equal to Start + Size,
6970
/// in which case it's counted in the OverflowBucket.
7071
void add(unsigned value) {
71-
assert(value >= Start);
72+
ASSERT(value >= Start);
7273
value -= Start;
7374
if (value >= Size)
7475
++OverflowBucket;

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ RewriteSystem::findRuleToDelete(EliminationPredicate isRedundantRuleFn) {
212212
// We should not find a rule that has already been marked redundant
213213
// here; it should have already been replaced with a rewrite path
214214
// in all homotopy generators.
215-
assert(!rule.isRedundant());
215+
ASSERT(!rule.isRedundant());
216216

217217
// Associated type introduction rules are 'permanent'. They're
218218
// not worth eliminating since they are re-added every time; it
@@ -436,9 +436,9 @@ void RewriteSystem::minimizeRewriteSystem(const PropertyMap &map) {
436436
llvm::dbgs() << "-----------------------------\n";
437437
}
438438

439-
assert(Complete);
440-
assert(!Minimized);
441-
assert(!Frozen);
439+
ASSERT(Complete);
440+
ASSERT(!Minimized);
441+
ASSERT(!Frozen);
442442
Minimized = 1;
443443

444444
propagateExplicitBits();
@@ -591,8 +591,8 @@ void RewriteSystem::minimizeRewriteSystem(const PropertyMap &map) {
591591
/// after minimization. Instead, we will rebuild a new rewrite system
592592
/// from the minimized requirements.
593593
GenericSignatureErrors RewriteSystem::getErrors() const {
594-
assert(Complete);
595-
assert(Minimized);
594+
ASSERT(Complete);
595+
ASSERT(Minimized);
596596

597597
GenericSignatureErrors result;
598598

@@ -644,8 +644,8 @@ GenericSignatureErrors RewriteSystem::getErrors() const {
644644
/// These rules form the requirement signatures of these protocols.
645645
llvm::DenseMap<const ProtocolDecl *, RewriteSystem::MinimizedProtocolRules>
646646
RewriteSystem::getMinimizedProtocolRules() const {
647-
assert(Minimized);
648-
assert(!Protos.empty());
647+
ASSERT(Minimized);
648+
ASSERT(!Protos.empty());
649649

650650
llvm::DenseMap<const ProtocolDecl *, MinimizedProtocolRules> rules;
651651
for (unsigned ruleID = FirstLocalRule, e = Rules.size();
@@ -676,8 +676,8 @@ RewriteSystem::getMinimizedProtocolRules() const {
676676
/// These rules form the top-level generic signature for this rewrite system.
677677
std::vector<unsigned>
678678
RewriteSystem::getMinimizedGenericSignatureRules() const {
679-
assert(Minimized);
680-
assert(Protos.empty());
679+
ASSERT(Minimized);
680+
ASSERT(Protos.empty());
681681

682682
std::vector<unsigned> rules;
683683
for (unsigned ruleID = FirstLocalRule, e = Rules.size();
@@ -713,11 +713,11 @@ void RewriteSystem::verifyRedundantConformances(
713713
const llvm::DenseSet<unsigned> &redundantConformances) const {
714714
for (unsigned ruleID : redundantConformances) {
715715
const auto &rule = getRule(ruleID);
716-
assert(!rule.isPermanent() &&
716+
ASSERT(!rule.isPermanent() &&
717717
"Permanent rule cannot be redundant");
718-
assert(!rule.isIdentityConformanceRule() &&
718+
ASSERT(!rule.isIdentityConformanceRule() &&
719719
"Identity conformance cannot be redundant");
720-
assert(rule.isAnyConformanceRule() &&
720+
ASSERT(rule.isAnyConformanceRule() &&
721721
"Redundant conformance is not a conformance rule?");
722722

723723
if (!rule.isRedundant()) {

0 commit comments

Comments
 (0)