Skip to content

GSB/RequirementMachine: Remove unsound cycle-breaking hack #41013

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
Jan 26, 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
6 changes: 0 additions & 6 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5209,12 +5209,6 @@ class GenericSignatureBuilder::InferRequirementsWalker : public TypeWalker {
// Infer from generic nominal types.
auto decl = ty->getAnyNominal();
if (!decl) return Action::Continue;

// FIXME: The GSB and the request evaluator both detect a cycle here if we
// force a recursive generic signature. We should look into moving cycle
// detection into the generic signature request(s) - see rdar://55263708
if (!decl->hasComputedGenericSignature())
return Action::Continue;

auto genericSig = decl->getGenericSignature();
if (!genericSig)
Expand Down
6 changes: 0 additions & 6 deletions lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,6 @@ struct InferRequirementsWalker : public TypeWalker {
auto decl = ty->getAnyNominal();
if (!decl) return Action::Continue;

// FIXME: The GSB and the request evaluator both detect a cycle here if we
// force a recursive generic signature. We should look into moving cycle
// detection into the generic signature request(s) - see rdar://55263708
if (!decl->hasComputedGenericSignature())
return Action::Continue;

auto genericSig = decl->getGenericSignature();
if (!genericSig)
return Action::Continue;
Expand Down
2 changes: 1 addition & 1 deletion test/Generics/generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Top {}
class Bottom<T : Bottom<Top>> {}
// expected-error@-1 {{'Bottom' requires that 'Top' inherit from 'Bottom<Top>'}}
// expected-note@-2 {{requirement specified as 'T' : 'Bottom<Top>' [with T = Top]}}
// expected-error@-3 {{generic class 'Bottom' has self-referential generic requirements}}
// expected-error@-3 4{{generic class 'Bottom' has self-referential generic requirements}}
// expected-note@-4 {{while resolving type 'Bottom<Top>'}}
// expected-note@-5 {{through reference here}}

Expand Down
25 changes: 25 additions & 0 deletions test/Generics/requirement_inference_funny_order.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %target-typecheck-verify-swift -requirement-machine-inferred-signatures=on
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s

protocol P1 {}

protocol P2 {}

protocol IteratorProtocol {
associatedtype Element

func next() -> Element?
}

// CHECK: requirement_inference_funny_order.(file).LocalArray@
// CHECK: Generic signature: <Element where Element : P1>

// CHECK: ExtensionDecl line={{[0-9]+}} base=LocalArray
// CHECK: Generic signature: <Element where Element : P1, Element : P2>
extension LocalArray where Element : P2 {
static func ==(lhs: Self, rhs: Self) -> Bool {}
}

struct LocalArray<Element : P1>: IteratorProtocol {
func next() -> Element? {}
}
7 changes: 5 additions & 2 deletions test/decl/protocol/req/recursion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extension SomeProtocol where T == Optional<T> { } // expected-error{{same-type c

class X<T> where T == X { // expected-error{{same-type constraint 'T' == 'X<T>' is recursive}}
// expected-error@-1{{same-type requirement makes generic parameter 'T' non-generic}}
// expected-error@-2 3{{generic class 'X' has self-referential generic requirements}}
var type: T { return Swift.type(of: self) } // expected-error{{cannot convert return expression of type 'X<T>.Type' to return type 'T'}}
}

Expand Down Expand Up @@ -44,7 +45,7 @@ public protocol P {
}

public struct S<A: P> where A.T == S<A> {
// expected-error@-1 {{generic struct 'S' has self-referential generic requirements}}
// expected-error@-1 4{{generic struct 'S' has self-referential generic requirements}}
// expected-note@-2 {{while resolving type 'S<A>'}}
func f(a: A.T) {
g(a: id(t: a)) // `a` has error type which is diagnosed as circular reference
Expand All @@ -70,7 +71,7 @@ protocol PI {
}

struct SI<A: PI> : I where A : I, A.T == SI<A> {
// expected-error@-1 {{generic struct 'SI' has self-referential generic requirements}}
// expected-error@-1 4{{generic struct 'SI' has self-referential generic requirements}}
// expected-note@-2 {{while resolving type 'SI<A>'}}
func ggg<T : I>(t: T.Type) -> T {
return T()
Expand Down Expand Up @@ -98,7 +99,9 @@ struct S5<A: PI> : I where A : I, A.T == S4<A> { }

// Used to hit ArchetypeBuilder assertions
struct SU<A: P> where A.T == SU {
// expected-error@-1 3{{generic struct 'SU' has self-referential generic requirements}}
}

struct SIU<A: PI> : I where A : I, A.T == SIU {
// expected-error@-1 3{{generic struct 'SIU' has self-referential generic requirements}}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %target-swift-frontend %s -emit-ir
// RUN: %target-typecheck-verify-swift

enum Term<S> where S: Sequence, S.Iterator.Element == Term {
// expected-error@-1 3{{generic enum 'Term' has self-referential generic requirements}}
case Cons(head: String, tail: S)
}

Expand Down