Skip to content
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

[SR-9462] ProtocolConformanceAnalysis should also search Extensions for protocols #51925

Closed
gottesmm opened this issue Dec 10, 2018 · 1 comment
Labels
compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution SILOptimizer Area → compiler: SIL optimization passes task

Comments

@gottesmm
Copy link
Member

Previous ID SR-9462
Radar None
Original Reporter @gottesmm
Type Task
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Task, Optimizer, RunTimeCrash
Assignee rajbarik (JIRA)
Priority Medium

md5: d43d6caac553c7ebbe23a8755a74a266

Issue Description:

Otherwise, we will miscompile the following test:

import Foundation

protocol Foo {
  var myName: String { get }
}

extension URL : Foo {
  var myName : String { return "URL" }
}

struct MyStruct : Foo {
  var myName : String { return "MyStruct" }
}

@inline(never)
func printName(_ f: Foo) {
  print(f.myName)
}

@inline(never)
func main() {
  let u = URL(string: "file:///foobar")!
  printName(u)
}

main()

The problem is that the code today just looks through top level nominal types and doesn't check extensions. This is going to cause miscompiles on master and should be fixed ASAP.

I think the following patch will fix this:

diff --git a/lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp b/lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cppindex b16d47ddbb..3d93ecd786 100644
--- a/lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
+++ b/lib/SILOptimizer/Analysis/ProtocolConformanceAnalysis.cpp
@@ -17,6 +17,7 @@
 #include "swift/AST/ASTContext.h"
 #include "swift/AST/ASTWalker.h"
 #include "swift/AST/Module.h"
+#include "swift/AST/ProtocolConformance.h"
 #include "swift/SIL/SILInstruction.h"
 #include "swift/SIL/SILModule.h"
 #include "swift/SIL/SILValue.h"
@@ -41,7 +42,24 @@ public:
           ProtocolConformanceCache[Protocol].push_back(NTD);
         }
       }
+      return true;
     }
+
+    if (auto *e = dyn_cast<ExtensionDecl>(D)) {
+      auto *ntd = e->getExtendedNominal();
+      if (!isa<ProtocolDecl>(ntd)) {
+        for (auto *conformance : e->getLocalConformances()) {
+          if (isa<NormalProtocolConformance>(conformance)) {
+            auto *prot = conformance->getProtocol();
+            if (prot->getEffectiveAccess() <= AccessLevel::Internal) {
+              ProtocolConformanceCache[prot].push_back(ntd);
+            }
+          }
+        }
+      }
+      return true;
+    }
+
     return true;
   }
 };

Please add both a SIL and an interpreter test to make sure we do not break this again.

@swift-ci
Copy link
Collaborator

Comment by Raj Barik (JIRA)

Fix: #21195

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added SILOptimizer Area → compiler: SIL optimization passes and removed Optimizer labels Nov 3, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution SILOptimizer Area → compiler: SIL optimization passes task
Projects
None yet
Development

No branches or pull requests

3 participants