Skip to content

Allow dynamic replacement initializers to be non delegating #22691

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
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: 3 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6342,8 +6342,10 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
// Prior to Swift 5, cross-module initializers were permitted to be
// non-delegating. However, if the struct isn't fixed-layout, we have to
// be delegating because, well, we don't know the layout.
// A dynamic replacement is permitted to be non-delegating.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the type is resilient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think you should be able to replace resilient constructors via dynamic replacement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the dynamic replacement module have visibility past the resilience boundary? It seems like that would require additional code, or building it with the IRGenOptions::EnableResilienceBypass flag set. Did you test this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is what I have tested:

$ cat ModuleA.swift
struct S {
    let i: Int
    init(i: Int) {
        self.i = i
    }

    init(y: Int) {
      self.init(i: y)
    }
}
$ bin/swiftc -emit-library -Xfrontend -enable-private-imports -Xfrontend -enable-implicit-dynamic ModuleA.swift  -emit-module -enable-resilience
$ cat ModuleB.swift
@_private(sourceFile: "ModuleA.swift") import ModuleA

extension S {
  @_dynamicReplacement(for: init(i:))
  private init(__preview_i: Int) {
    self.i = __preview_i
  }
  @_dynamicReplacement(for: init(y:))
  private init(__preview_y: Int) {
    self.init(i: __preview_y)
  }
}
$ bin/swiftc -emit-library -Xfrontend -enable-private-imports -Xfrontend -enable-implicit-dynamic ModuleB.swift -I. -L. -lModuleA -Xfrontend -enable-resilience -emit-sil
// S.init(__preview_i:)
sil [dynamic_replacement_for "$s7ModuleA1SV1iACSi_tcfC"] @$s7ModuleA1SV0A1BE11__preview_iACSi_tc33_261761554CC3BA78F2BD69544DB6E8C4LlfC : $@convention(method) (Int, @thin S.Type) -> S {
// %0                                             // users: %8, %6, %3
bb0(%0 : $Int, %1 : $@thin S.Type):
  %2 = alloc_stack $S, var, name "self"           // users: %4, %9
  debug_value %0 : $Int, let, name "__preview_i", argno 1 // id: %3
  %4 = begin_access [modify] [static] %2 : $*S    // users: %7, %5
  %5 = struct_element_addr %4 : $*S, #S.i         // user: %6
  store %0 to %5 : $*Int                          // id: %6
  end_access %4 : $*S                             // id: %7
  %8 = struct $S (%0 : $Int)                      // user: %10
  dealloc_stack %2 : $*S                          // id: %9
  return %8 : $S                                  // id: %10
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if S and S.init are both public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that just crashes the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could make it work, if the dynamic replacement module is built with EnableResilienceBypass on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It crashes in SILGen. There are more dragons. I will deal with that later.

if (NTD->isResilient() ||
containingModule->getASTContext().isSwiftVersionAtLeast(5)) {
(containingModule->getASTContext().isSwiftVersionAtLeast(5) &&
!getAttrs().getAttribute<DynamicReplacementAttr>())) {
if (containingModule != NTD->getParentModule())
Kind = BodyInitKind::Delegating;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4047,7 +4047,8 @@ void TypeChecker::validateDecl(ValueDecl *D) {
// (or the same file) to add vtable entries, we can re-evaluate this
// restriction.
if (extType->getClassOrBoundGenericClass() &&
isa<ExtensionDecl>(CD->getDeclContext())) {
isa<ExtensionDecl>(CD->getDeclContext()) &&
!(CD->getAttrs().hasAttribute<DynamicReplacementAttr>())) {
diagnose(CD->getLoc(), diag::designated_init_in_extension, extType)
.fixItInsert(CD->getLoc(), "convenience ");
CD->setInitKind(CtorInitializerKind::Convenience);
Expand Down
32 changes: 32 additions & 0 deletions test/Sema/Inputs/dynamic_replacements_init_A.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct S {
let i: Int
init(i: Int) {
self.i = i
}

init(y: Int) {
self.init(i: y)
}
}

class A {
let i: Int
init(i: Int) {
self.i = i
}
convenience init(c: Int) {
self.init(i: c)
}
}

class B : A {
let b: Int
init(b: Int, i: Int) {
self.b = b
super.init(i: i)
}

convenience init(x: Int) {
self.init(b: x, i: x)
}
}
41 changes: 41 additions & 0 deletions test/Sema/dynamic_replacement_init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -swift-version 5 -enable-implicit-dynamic -enable-private-imports -emit-module -primary-file %S/Inputs/dynamic_replacements_init_A.swift -emit-module -module-name ModuleA -emit-module-path %t/ModuleA.swiftmodule
// RUN: %target-swift-frontend -swift-version 5 -typecheck -verify %s -I %t

@_private(sourceFile: "dynamic_replacements_init_A.swift") import ModuleA

extension S {
@_dynamicReplacement(for: init(i:))
private init(_i: Int) {
self.i = _i
}
@_dynamicReplacement(for: init(y:))
private init(_y: Int) {
self.init(i: _y)
}
}

extension A {
@_dynamicReplacement(for: init(i:))
private init(_i: Int) {
self.i = _i
}

@_dynamicReplacement(for: init(c:))
private convenience init(_y: Int) {
self.init(i: _y)
}
}

extension B {
@_dynamicReplacement(for: init(b:i:))
private init(_i: Int, _b: Int) {
self.b = _b
super.init(i: _i)
}

@_dynamicReplacement(for: init(x:))
private convenience init(_i: Int) {
self.init(b: _i, i: _i)
}
}