Skip to content

Fix default witness checking #13364

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
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: 0 additions & 4 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ namespace swift {
unsigned WarnLongExpressionTypeChecking = 0,
unsigned ExpressionTimeoutThreshold = 0);

/// Once type checking is complete, this walks protocol requirements
/// to resolve default witnesses.
void finishTypeCheckingFile(SourceFile &SF);

/// Now that we have type-checked an entire module, perform any type
/// checking that requires the full module, e.g., Objective-C method
/// override checking.
Expand Down
1 change: 0 additions & 1 deletion lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ void CompilerInstance::finishTypeChecking(
performWholeModuleTypeChecking(SF);
});
}
forEachFileToTypeCheck([&](SourceFile &SF) { finishTypeCheckingFile(SF); });
}

void CompilerInstance::performParseOnly(bool EvaluateConditionals) {
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,8 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {

// Build a default witness table if this is a protocol.
if (auto protocol = dyn_cast<ProtocolDecl>(theType)) {
if (!protocol->isObjC())
if (!protocol->isObjC() &&
!protocol->hasFixedLayout())
SGM.emitDefaultWitnessTable(protocol);
return;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3969,6 +3969,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
TC.checkDeclCircularity(nominal);
}
if (auto protocol = dyn_cast<ProtocolDecl>(decl)) {
if (!protocol->hasFixedLayout())
TC.inferDefaultWitnesses(protocol);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ struct swift::RequirementMatch {
/// The set of optional adjustments performed on the witness.
SmallVector<OptionalAdjustment, 2> OptionalAdjustments;

/// Substitutions mapping the type of the witness to the requirement
/// environment.
SmallVector<Substitution, 2> WitnessSubstitutions;

/// \brief Determine whether this match is viable.
bool isViable() const {
switch(Kind) {
Expand Down Expand Up @@ -438,8 +442,6 @@ struct swift::RequirementMatch {
llvm_unreachable("Unhandled MatchKind in switch.");
}

SmallVector<Substitution, 2> WitnessSubstitutions;

swift::Witness getWitness(ASTContext &ctx) const {
SmallVector<Substitution, 2> syntheticSubs;
auto syntheticEnv = ReqEnv->getSyntheticEnvironment();
Expand Down
9 changes: 0 additions & 9 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,15 +672,6 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
}
}

void swift::finishTypeCheckingFile(SourceFile &SF) {
auto &Ctx = SF.getASTContext();
TypeChecker TC(Ctx);

for (auto D : SF.Decls)
if (auto PD = dyn_cast<ProtocolDecl>(D))
TC.inferDefaultWitnesses(PD);
}

void swift::performWholeModuleTypeChecking(SourceFile &SF) {
SharedTimer("performWholeModuleTypeChecking");
auto &Ctx = SF.getASTContext();
Expand Down
18 changes: 0 additions & 18 deletions test/SILGen/protocol_resilience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,6 @@ func inoutResilientProtocol(_ x: inout OtherConformingType) {
inoutFunc(&OtherConformingType.staticPropertyInExtension)
}

// Protocol is not public -- make sure default witnesses have the right linkage
protocol InternalProtocol {
func noDefaultF()
func defaultG()
}

extension InternalProtocol {

// CHECK-LABEL: sil private [transparent] [thunk] @_T019protocol_resilience16InternalProtocolP8defaultGyyF
// CHECK: return
func defaultG() {}
}

// CHECK-LABEL: sil_default_witness_table P {
// CHECK-NEXT: }

Expand Down Expand Up @@ -328,8 +315,3 @@ extension InternalProtocol {
// CHECK-NEXT: method #ReabstractSelfRefined.callback!setter.1: {{.*}} : @_T019protocol_resilience21ReabstractSelfRefinedP8callbackxxcvs
// CHECK-NEXT: method #ReabstractSelfRefined.callback!materializeForSet.1: {{.*}} : @_T019protocol_resilience21ReabstractSelfRefinedP8callbackxxcvm
// CHECK-NEXT: }

// CHECK-LABEL: sil_default_witness_table hidden InternalProtocol {
// CHECK-NEXT: no_default
// CHECK-NEXT: method #InternalProtocol.defaultG!1: {{.*}} : @_T019protocol_resilience16InternalProtocolP8defaultGyyF
// CHECK-NEXT: }
2 changes: 1 addition & 1 deletion test/SILGen/witness_accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ extension R {

public struct S : R {}

// CHECK-LABEL: sil private @_T021witness_accessibility1R{{.*}}AE18privateRequirementyyF
// CHECK-LABEL: sil private @_T021witness_accessibility1R{{.*}}E17publicRequirementyyF
// CHECK-LABEL: sil private @_T021witness_accessibility1R{{.*}}E19internalRequirementyyF
// CHECK-LABEL: sil private @_T021witness_accessibility1R{{.*}}AE18privateRequirementyyF

// CHECK-LABEL: sil private [transparent] [thunk] @_T021witness_accessibility1SVAA1R{{.*}}dELLP18privateRequirementyyFTW
// CHECK-LABEL: sil private [transparent] [thunk] @_T021witness_accessibility1SVAA1QA2aDP19internalRequirementyyFTW
Expand Down
12 changes: 0 additions & 12 deletions test/SILOptimizer/dead_function_elimination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,3 @@ internal func donotEliminate() {

// CHECK-TESTING-LABEL: sil_witness_table [serialized] Adopt: Prot
// CHECK-TESTING: DeadWitness{{.*}}: @{{.*}}DeadWitness

// CHECK-LABEL: sil_default_witness_table hidden Prot
// CHECK: no_default
// CHECK: no_default
// CHECK: method #Prot.aliveDefaultWitness!1: {{.*}} : @{{.*}}aliveDefaultWitness
// CHECK: no_default

// CHECK-TESTING-LABEL: sil_default_witness_table Prot
// CHECK-TESTING: no_default
// CHECK-TESTING: no_default
// CHECK-TESTING: method #Prot.aliveDefaultWitness!1: {{.*}} : @{{.*}}aliveDefaultWitness
// CHECK-TESTING: method #Prot.DeadDefaultWitness!1: {{.*}} : @{{.*}}DeadDefaultWitness
9 changes: 9 additions & 0 deletions test/multifile/Inputs/default-witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public protocol Node: Source { }

public protocol Source {
associatedtype Output
}

public final class GraphNode<U>: Node {
public typealias Output = U
}
10 changes: 10 additions & 0 deletions test/multifile/default-witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-frontend -typecheck %S/Inputs/default-witness.swift -primary-file %s -enable-resilience

public protocol GraphType {
func insert<U>(_: GraphNode<U>, _: GraphNode<U>)
}

public extension GraphType {
func insert<N: Node>(_: N, _: GraphNode<N.Output>) {}
}