Skip to content

Commit 7e8ce6c

Browse files
Merge pull request #25631 from aschwaighofer/ReplaceOpaqueTypesWithUnderlyingTypes_primary_file_mode
ReplaceOpaqueTypesWithUnderlyingTypes: In primary-file mode there mig…
2 parents 7e03051 + 9b3206f commit 7e8ce6c

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

lib/AST/Type.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,10 +2582,10 @@ operator()(SubstitutableType *maybeOpaqueType) const {
25822582
}
25832583

25842584
auto subs = opaqueRoot->getDecl()->getUnderlyingTypeSubstitutions();
2585-
// TODO: Check the resilience expansion, and handle opaque types with
2586-
// unknown underlying types. For now, all opaque types are always
2587-
// fragile.
2588-
assert(subs.hasValue() && "resilient opaque types not yet supported");
2585+
// If the body of the opaque decl providing decl has not been type checked we
2586+
// don't have a underlying subsitution.
2587+
if (!subs.hasValue())
2588+
return maybeOpaqueType;
25892589

25902590
// Apply the underlying type substitutions to the interface type of the
25912591
// archetype in question. This will map the inner generic signature of the
@@ -2633,7 +2633,10 @@ operator()(CanType maybeOpaqueType, Type replacementType,
26332633
}
26342634

26352635
auto subs = opaqueRoot->getDecl()->getUnderlyingTypeSubstitutions();
2636-
assert(subs.hasValue());
2636+
// If the body of the opaque decl providing decl has not been type checked we
2637+
// don't have a underlying subsitution.
2638+
if (!subs.hasValue())
2639+
return abstractRef;
26372640

26382641
// Apply the underlying type substitutions to the interface type of the
26392642
// archetype in question. This will map the inner generic signature of the

lib/SILOptimizer/Transforms/SpecializeOpaqueArchetypes.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#include "swift/SILOptimizer/PassManager/Transforms.h"
2424
#include "swift/SILOptimizer/Utils/CFG.h"
2525

26+
#include "llvm/Support/CommandLine.h"
27+
28+
llvm::cl::opt<bool>
29+
EnableOpaqueArchetypeSpecializer("enable-opaque-archetype-specializer",
30+
llvm::cl::init(true));
2631

2732
using namespace swift;
2833

@@ -459,6 +464,9 @@ void OpaqueSpecializerCloner::insertOpaqueToConcreteAddressCasts(
459464
namespace {
460465
class OpaqueArchetypeSpecializer : public SILFunctionTransform {
461466
void run() override {
467+
if (!EnableOpaqueArchetypeSpecializer)
468+
return;
469+
462470
auto *context = getFunction();
463471

464472
if (!context->shouldOptimize())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@inline(never)
2+
func bar2(_ x: Int) -> some P {
3+
return x
4+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -O -Xllvm -enable-opaque-archetype-specializer -disable-availability-checking -primary-file %s %S/Inputs/specialize_opaque_type_archetypes_multifile_A.swift -emit-sil | %FileCheck %s
3+
4+
protocol P {}
5+
6+
extension Int : P {}
7+
8+
struct Pair {
9+
var x: Int64 = 0
10+
var y: Int64 = 0
11+
}
12+
13+
extension Pair : P {}
14+
15+
@inline(never)
16+
func bar(_ x: Int) -> some P {
17+
return x
18+
}
19+
20+
// CHECK-LABEL: sil @$s43specialize_opaque_type_archetypes_multifile4testyyF : $@convention(thin) () -> () {
21+
// CHECK: function_ref @$s43specialize_opaque_type_archetypes_multifile4bar2yQrSiF : $@convention(thin) (Int) -> @out @_opaqueReturnTypeOf("$s43specialize_opaque_type_archetypes_multifile4bar2yQrSiF", 0)
22+
public func test() {
23+
print(bar(5))
24+
print(bar2(5))
25+
}

0 commit comments

Comments
 (0)