Skip to content

[Deserialization] Don't add an OverrideAttr if the 'overridden' decl is a convenience init when deserializing #24212

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
5 changes: 4 additions & 1 deletion lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2646,7 +2646,10 @@ class swift::DeclDeserializer {
if (auto *overridden = ctor->getOverriddenDecl()) {
if (!attributeChainContains<RequiredAttr>(DAttrs) ||
!overridden->isRequired()) {
AddAttribute(new (ctx) OverrideAttr(SourceLoc()));
// FIXME: why is a convenience init considered overridden when the
// overriding init can't be marked overriding in source?
if (!overridden->isConvenienceInit())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you leave a comment behind saying that this doesn't really make sense? Either the convenience init is an override or it isn't. cc @DougGregor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah sure, will update shortly.

AddAttribute(new (ctx) OverrideAttr(SourceLoc()));
}
}

Expand Down
51 changes: 51 additions & 0 deletions test/ParseableInterface/convenience-init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %empty-directory(%t)

// Generate the parseable interface of the current file via the merge-modules step
// RUN: %target-build-swift -emit-module -o %t/Test.swiftmodule -emit-parseable-module-interface-path %t/TestMerge.swiftinterface -module-name Test %s

// Generate the parseable interface of the current file via a single frontend invocation
// RUN: %target-swift-frontend -typecheck -enable-objc-interop -emit-parseable-module-interface-path %t/TestSingle.swiftinterface -module-name Test %s

// Make sure both don't add override for inits shadowing convenience initializers
// RUN: %FileCheck --check-prefixes=CHECK,SINGLE %s < %t/TestSingle.swiftinterface
// RUN: %FileCheck --check-prefixes=CHECK,MERGE %s < %t/TestMerge.swiftinterface

// Check we can consume the interface without issue
// RUN: %target-swift-frontend -swift-version 5 -build-module-from-parseable-interface -o %t/Test.swiftmodule %t/TestSingle.swiftinterface
// RUN: %target-swift-frontend -swift-version 5 -build-module-from-parseable-interface -o %t/Test.swiftmodule %t/TestMerge.swiftinterface

public class Base {
let x: Int
public init(x: Int) {
self.x = x
}
convenience public init() {
self.init(x: 1)
}
}

public class Derived: Base {
// MERGE: {{^}} public init(z: Swift.Int)
// SINGLE: {{^}} public init(z: Int)
public init(z: Int) {
super.init(x: z)
}
// MERGE: {{^}} public convenience init()
// SINGLE: {{^}} convenience public init()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This switched order here is because we have a 'ConvenienceAttr` on the initializer when generating via the single invocation (which is printed when printing the decl's attributes) and don't after serializing/deserializing (where we print it based on the initializer kind).

convenience public init() {
self.init(z: 1)
}
}

public class Derived2: Base {
// CHECK: {{^}} public init()
public init() {
super.init(x: 1)
}

// MERGE: {{^}} override public convenience init(x: Swift.Int)
// SINGLE: {{^}} override convenience public init(x: Int)
override convenience public init(x: Int) {
self.init()
}
}