Skip to content

RequirementMachine: Add some test cases reduced from https://github.com/RemiBardon/swift-geo #42044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ namespace swift {
/// enabled. It can be disabled for debugging and testing.
bool EnableRequirementMachineLoopNormalization = true;

/// Enable reuse of requirement machines for minimization. Usually you want
/// this enabled. It can be disabled for debugging and testing.
bool EnableRequirementMachineReuse = true;

/// Enable experimental, more correct support for opaque result types as
/// concrete types. This will sometimes fail to produce a convergent
/// rewrite system.
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ def disable_requirement_machine_concrete_contraction : Flag<["-"], "disable-requ
def disable_requirement_machine_loop_normalization : Flag<["-"], "disable-requirement-machine-loop-normalization">,
HelpText<"Disable stronger minimization algorithm, for debugging only">;

def disable_requirement_machine_reuse : Flag<["-"], "disable-requirement-machine-reuse">,
HelpText<"Disable re-use of requirement machines for minimization, for debugging only">;

def enable_requirement_machine_opaque_archetypes : Flag<["-"], "enable-requirement-machine-opaque-archetypes">,
HelpText<"Enable more correct opaque archetype support, which is off by default because it might fail to produce a convergent rewrite system">;

Expand Down
28 changes: 15 additions & 13 deletions lib/AST/RequirementMachine/RewriteContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,15 @@ bool RewriteContext::isRecursivelyConstructingRequirementMachine(
void RewriteContext::installRequirementMachine(
CanGenericSignature sig,
std::unique_ptr<RequirementMachine> machine) {
auto *machinePtr = machine.release();
if (!Context.LangOpts.EnableRequirementMachineReuse)
return;

machinePtr->freeze();
auto &entry = Machines[sig];
if (entry != nullptr)
return;

auto inserted = Machines.insert(std::make_pair(sig, machinePtr)).second;
if (!inserted)
delete machinePtr;
machine->freeze();
entry = machine.release();
}

/// Implement Tarjan's algorithm to compute strongly-connected components in
Expand Down Expand Up @@ -455,22 +457,22 @@ bool RewriteContext::isRecursivelyConstructingRequirementMachine(
!component->second.ComputedRequirementSignatures);
}

/// Given a reuirement machine that built the requirement signatures for a
/// Given a requirement machine that built the requirement signatures for a
/// protocol connected component, attempt to re-use it for subsequent
/// queries against the connected component, instead of building a new one
/// later.
void RewriteContext::installRequirementMachine(
const ProtocolDecl *proto,
std::unique_ptr<RequirementMachine> machine) {
auto *machinePtr = machine.release();

machinePtr->freeze();
if (!Context.LangOpts.EnableRequirementMachineReuse)
return;

auto &component = getProtocolComponentImpl(proto);
if (component.Machine == nullptr)
component.Machine = machinePtr;
else
delete machinePtr;
if (component.Machine != nullptr)
return;

machine->freeze();
component.Machine = machine.release();
}

/// We print stats in the destructor, which should get executed at the end of
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (Args.hasArg(OPT_disable_requirement_machine_loop_normalization))
Opts.EnableRequirementMachineLoopNormalization = false;

if (Args.hasArg(OPT_disable_requirement_machine_reuse))
Opts.EnableRequirementMachineReuse = false;

if (Args.hasArg(OPT_enable_requirement_machine_opaque_archetypes))
Opts.EnableRequirementMachineOpaqueArchetypes = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -disable-requirement-machine-reuse -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

// CHECK-LABEL: .NonEmptyProtocol@
// CHECK-NEXT: Requirement signature: <Self where Self : Collection, Self.[NonEmptyProtocol]C : Collection, Self.[Sequence]Element == Self.[NonEmptyProtocol]C.[Sequence]Element, Self.[Collection]Index == Self.[NonEmptyProtocol]C.[Collection]Index>

public protocol NonEmptyProtocol: Collection
where Element == C.Element,
Index == C.Index {
associatedtype C: Collection
}

// CHECK-LABEL: .MultiPoint@
// CHECK-NEXT: Requirement signature: <Self where Self.[MultiPoint]C : CoordinateSystem, Self.[MultiPoint]X : NonEmptyProtocol, Self.[MultiPoint]C.[CoordinateSystem]P == Self.[MultiPoint]X.[Sequence]Element, Self.[MultiPoint]X.[NonEmptyProtocol]C : NonEmptyProtocol>

public protocol MultiPoint {
associatedtype C: CoordinateSystem

typealias P = Self.C.P
// expected-warning@-1 {{redundant same-type constraint 'Self.P' == 'Self.C.P'}}
// FIXME: This is bogus

associatedtype X: NonEmptyProtocol
where X.C: NonEmptyProtocol,
X.Element == Self.P
}

// CHECK-LABEL: .CoordinateSystem@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[CoordinateSystem]B.[BoundingBox]C, Self.[CoordinateSystem]B : BoundingBox, Self.[CoordinateSystem]L : Line, Self.[CoordinateSystem]P : Point, Self.[CoordinateSystem]S : Size, Self.[CoordinateSystem]B.[BoundingBox]C == Self.[CoordinateSystem]L.[MultiPoint]C, Self.[CoordinateSystem]L.[MultiPoint]C == Self.[CoordinateSystem]P.[Point]C, Self.[CoordinateSystem]P.[Point]C == Self.[CoordinateSystem]S.[Size]C>

public protocol CoordinateSystem {
associatedtype P: Point where Self.P.C == Self
associatedtype S: Size where Self.S.C == Self
associatedtype L: Line where Self.L.C == Self
associatedtype B: BoundingBox where Self.B.C == Self
}

// CHECK-LABEL: .Line@
// CHECK-NEXT: Requirement signature: <Self where Self : MultiPoint>

public protocol Line: MultiPoint {}

// CHECK-LABEL: .Size@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Size]C.[CoordinateSystem]S, Self.[Size]C : CoordinateSystem>

public protocol Size {
associatedtype C: CoordinateSystem where Self.C.S == Self
}

// CHECK-LABEL: .BoundingBox@
// CHECK-NEXT: Requirement signature: <Self where Self.[BoundingBox]C : CoordinateSystem>

public protocol BoundingBox {
associatedtype C: CoordinateSystem
typealias P = Self.C.P
typealias S = Self.C.S
}

// CHECK-LABEL: .Point@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Point]C.[CoordinateSystem]P, Self.[Point]C : CoordinateSystem>

public protocol Point {
associatedtype C: CoordinateSystem where Self.C.P == Self
}

func sameType<T>(_: T, _: T) {}

func conformsToPoint<T : Point>(_: T.Type) {}

func testMultiPoint<T : MultiPoint>(_: T) {
sameType(T.C.P.self, T.X.Element.self)
conformsToPoint(T.P.self)
}

func testCoordinateSystem<T : CoordinateSystem>(_: T) {
sameType(T.P.C.self, T.self)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -disable-requirement-machine-reuse -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

// CHECK-LABEL: .NonEmptyProtocol@
// CHECK-NEXT: Requirement signature: <Self where Self : Collection, Self.[NonEmptyProtocol]C : Collection, Self.[Sequence]Element == Self.[NonEmptyProtocol]C.[Sequence]Element, Self.[Collection]Index == Self.[NonEmptyProtocol]C.[Collection]Index>

public protocol NonEmptyProtocol: Collection
where Element == C.Element,
Index == C.Index {
associatedtype C: Collection
}

// CHECK-LABEL: .MultiPoint@
// CHECK-NEXT: Requirement signature: <Self where Self.[MultiPoint]C : CoordinateSystem, Self.[MultiPoint]P == Self.[MultiPoint]C.[CoordinateSystem]P, Self.[MultiPoint]X : NonEmptyProtocol, Self.[MultiPoint]C.[CoordinateSystem]P == Self.[MultiPoint]X.[Sequence]Element, Self.[MultiPoint]X.[NonEmptyProtocol]C : NonEmptyProtocol>

public protocol MultiPoint {
associatedtype C: CoordinateSystem
associatedtype P where Self.P == Self.C.P

associatedtype X: NonEmptyProtocol
where X.C: NonEmptyProtocol,
X.Element == Self.P
}

// CHECK-LABEL: .CoordinateSystem@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[CoordinateSystem]B.[BoundingBox]C, Self.[CoordinateSystem]B : BoundingBox, Self.[CoordinateSystem]L : Line, Self.[CoordinateSystem]P : Point, Self.[CoordinateSystem]S : Size, Self.[CoordinateSystem]B.[BoundingBox]C == Self.[CoordinateSystem]L.[MultiPoint]C, Self.[CoordinateSystem]L.[MultiPoint]C == Self.[CoordinateSystem]S.[Size]C>

public protocol CoordinateSystem {
associatedtype P: Point where Self.P.C == Self
associatedtype S: Size where Self.S.C == Self
associatedtype L: Line where Self.L.C == Self
associatedtype B: BoundingBox where Self.B.C == Self
}

// CHECK-LABEL: .Line@
// CHECK-NEXT: Requirement signature: <Self where Self : MultiPoint, Self.[MultiPoint]C == Self.[MultiPoint]P.[Point]C>

public protocol Line: MultiPoint {}

// CHECK-LABEL: .Size@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Size]C.[CoordinateSystem]S, Self.[Size]C : CoordinateSystem>

public protocol Size {
associatedtype C: CoordinateSystem where Self.C.S == Self
}

// CHECK-LABEL: .BoundingBox@
// CHECK-NEXT: Requirement signature: <Self where Self.[BoundingBox]C : CoordinateSystem>

public protocol BoundingBox {
associatedtype C: CoordinateSystem
typealias P = Self.C.P
typealias S = Self.C.S
}

// CHECK-LABEL: .Point@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Point]C.[CoordinateSystem]P, Self.[Point]C : CoordinateSystem>

public protocol Point {
associatedtype C: CoordinateSystem where Self.C.P == Self
}

func sameType<T>(_: T, _: T) {}

func conformsToPoint<T : Point>(_: T.Type) {}

func testMultiPoint<T : MultiPoint>(_: T) {
sameType(T.C.P.self, T.X.Element.self)
conformsToPoint(T.P.self)
}

func testCoordinateSystem<T : CoordinateSystem>(_: T) {
sameType(T.P.C.self, T.self)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -disable-requirement-machine-reuse -requirement-machine-protocol-signatures=on -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

// CHECK-LABEL: .NonEmptyProtocol@
// CHECK-NEXT: Requirement signature: <Self where Self : Collection, Self.[NonEmptyProtocol]C : Collection, Self.[Sequence]Element == Self.[NonEmptyProtocol]C.[Sequence]Element, Self.[Collection]Index == Self.[NonEmptyProtocol]C.[Collection]Index>

public protocol NonEmptyProtocol: Collection
where Element == C.Element,
Index == C.Index {
associatedtype C: Collection
}

// CHECK-LABEL: .MultiPoint@
// CHECK-NEXT: Requirement signature: <Self where Self.[MultiPoint]C : CoordinateSystem, Self.[MultiPoint]P == Self.[MultiPoint]C.[CoordinateSystem]P, Self.[MultiPoint]X : NonEmptyProtocol, Self.[MultiPoint]C.[CoordinateSystem]P == Self.[MultiPoint]X.[Sequence]Element, Self.[MultiPoint]X.[NonEmptyProtocol]C : NonEmptyProtocol>

public protocol MultiPoint {
associatedtype C: CoordinateSystem
associatedtype P: Point where Self.P == Self.C.P
// expected-warning@-1 {{redundant conformance constraint 'Self.P' : 'Point'}}

associatedtype X: NonEmptyProtocol
where X.C: NonEmptyProtocol,
X.Element == Self.P
}

// CHECK-LABEL: .CoordinateSystem@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[CoordinateSystem]B.[BoundingBox]C, Self.[CoordinateSystem]B : BoundingBox, Self.[CoordinateSystem]L : Line, Self.[CoordinateSystem]P : Point, Self.[CoordinateSystem]S : Size, Self.[CoordinateSystem]B.[BoundingBox]C == Self.[CoordinateSystem]L.[MultiPoint]C, Self.[CoordinateSystem]L.[MultiPoint]C == Self.[CoordinateSystem]S.[Size]C>

public protocol CoordinateSystem {
associatedtype P: Point where Self.P.C == Self
associatedtype S: Size where Self.S.C == Self
associatedtype L: Line where Self.L.C == Self
associatedtype B: BoundingBox where Self.B.C == Self
}

// CHECK-LABEL: .Line@
// CHECK-NEXT: Requirement signature: <Self where Self : MultiPoint, Self.[MultiPoint]C == Self.[MultiPoint]P.[Point]C>

public protocol Line: MultiPoint {}

// CHECK-LABEL: .Size@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Size]C.[CoordinateSystem]S, Self.[Size]C : CoordinateSystem>

public protocol Size {
associatedtype C: CoordinateSystem where Self.C.S == Self
}

// CHECK-LABEL: .BoundingBox@
// CHECK-NEXT: Requirement signature: <Self where Self.[BoundingBox]C : CoordinateSystem>

public protocol BoundingBox {
associatedtype C: CoordinateSystem
typealias P = Self.C.P
typealias S = Self.C.S
}

// CHECK-LABEL: .Point@
// CHECK-NEXT: Requirement signature: <Self where Self == Self.[Point]C.[CoordinateSystem]P, Self.[Point]C : CoordinateSystem>

public protocol Point {
associatedtype C: CoordinateSystem where Self.C.P == Self
}

func sameType<T>(_: T, _: T) {}

func conformsToPoint<T : Point>(_: T.Type) {}

func testMultiPoint<T : MultiPoint>(_: T) {
sameType(T.C.P.self, T.X.Element.self)
conformsToPoint(T.P.self)
}

func testCoordinateSystem<T : CoordinateSystem>(_: T) {
sameType(T.P.C.self, T.self)
}