Skip to content

Commit 95d5108

Browse files
authored
Merge pull request #41159 from slavapestov/concrete-unification-vs-rewrite-loop
RequirementMachine: Relate rules introduced by concrete type unification via rewrite loops
2 parents 5cf6ee9 + 729dfc7 commit 95d5108

33 files changed

+2413
-389
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,8 +2559,10 @@ WARNING(associated_type_override_typealias,none,
25592559

25602560
ERROR(requirement_machine_completion_failed,none,
25612561
"cannot build rewrite system for %select{generic signature|protocol}0; "
2562-
"%select{step|depth}1 limit exceeded",
2562+
"%select{%error|rule count|rule length|concrete nesting}1 limit exceeded",
25632563
(unsigned, unsigned))
2564+
NOTE(requirement_machine_completion_rule,none,
2565+
"failed rewrite rule is %0", (StringRef))
25642566

25652567
ERROR(associated_type_objc,none,
25662568
"associated type %0 cannot be declared inside '@objc' protocol %1",

include/swift/Basic/LangOptions.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,17 @@ namespace swift {
510510
/// Enables fine-grained debug output from the requirement machine.
511511
std::string DebugRequirementMachine;
512512

513-
/// Maximum iteration count for requirement machine Knuth-Bendix completion
513+
/// Maximum rule count for requirement machine Knuth-Bendix completion
514514
/// algorithm.
515-
unsigned RequirementMachineStepLimit = 4000;
515+
unsigned RequirementMachineMaxRuleCount = 4000;
516516

517517
/// Maximum term length for requirement machine Knuth-Bendix completion
518518
/// algorithm.
519-
unsigned RequirementMachineDepthLimit = 12;
519+
unsigned RequirementMachineMaxRuleLength = 12;
520+
521+
/// Maximum concrete type nesting depth for requirement machine property map
522+
/// algorithm.
523+
unsigned RequirementMachineMaxConcreteNesting = 30;
520524

521525
/// Enable the new experimental protocol requirement signature minimization
522526
/// algorithm.

include/swift/Option/FrontendOptions.td

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,17 @@ def analyze_requirement_machine : Flag<["-"], "analyze-requirement-machine">,
343343
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
344344
HelpText<"Print out requirement machine statistics at the end of the compilation job">;
345345

346-
def requirement_machine_step_limit : Separate<["-"], "requirement-machine-step-limit">,
346+
def requirement_machine_max_rule_count : Joined<["-"], "requirement-machine-max-rule-count=">,
347347
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
348-
HelpText<"Set the maximum steps before we give up on confluent completion">;
348+
HelpText<"Set the maximum number of rules before giving up">;
349349

350-
def requirement_machine_depth_limit : Separate<["-"], "requirement-machine-depth-limit">,
350+
def requirement_machine_max_rule_length : Joined<["-"], "requirement-machine-max-rule-length=">,
351351
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
352-
HelpText<"Set the maximum depth before we give up on confluent completion">;
352+
HelpText<"Set the maximum rule length before giving up">;
353+
354+
def requirement_machine_max_concrete_nesting : Joined<["-"], "requirement-machine-max-concrete-nesting=">,
355+
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
356+
HelpText<"Set the maximum concrete type nesting depth before giving up">;
353357

354358
def disable_requirement_machine_merged_associated_types : Flag<["-"], "disable-requirement-machine-merged-associated-types">,
355359
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ add_swift_host_library(swiftAST STATIC
9191
RequirementMachine/RewriteSystem.cpp
9292
RequirementMachine/Symbol.cpp
9393
RequirementMachine/Term.cpp
94+
RequirementMachine/TypeDifference.cpp
9495
SearchPathOptions.cpp
9596
SILLayout.cpp
9697
Stmt.cpp

lib/AST/RequirementMachine/ConcreteTypeWitness.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,17 @@ void PropertyMap::recordConcreteConformanceRule(
458458
/*ruleID=*/concreteRuleID,
459459
/*inverse=*/true));
460460

461-
// Apply a concrete type adjustment to the concrete symbol if T' is shorter
462-
// than T.
461+
// If T' is a suffix of T, prepend the prefix to the concrete type's
462+
// substitutions.
463463
auto concreteSymbol = *concreteRule.isPropertyRule();
464-
unsigned adjustment = rhs.size() - concreteRule.getRHS().size();
464+
unsigned prefixLength = rhs.size() - concreteRule.getRHS().size();
465465

466-
if (adjustment > 0 &&
466+
if (prefixLength > 0 &&
467467
!concreteConformanceSymbol.getSubstitutions().empty()) {
468-
path.add(RewriteStep::forAdjustment(adjustment, /*endOffset=*/1,
469-
/*inverse=*/false));
468+
path.add(RewriteStep::forPrefixSubstitutions(prefixLength, /*endOffset=*/1,
469+
/*inverse=*/false));
470470

471-
MutableTerm prefix(rhs.begin(), rhs.begin() + adjustment);
471+
MutableTerm prefix(rhs.begin(), rhs.begin() + prefixLength);
472472
concreteSymbol = concreteSymbol.prependPrefixToConcreteSubstitutions(
473473
prefix, Context);
474474
}

0 commit comments

Comments
 (0)