Skip to content

[AST] Strip InOutType when cloning a parameter. #12271

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 10 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4197,12 +4197,20 @@ ParamDecl::ParamDecl(Specifier specifier,
"'var' cannot appear on parameters; you meant 'inout'");
}

// Remove the top-level inout type, if there is one.
static Type removeInOutType(Type type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

getInOutObjectType() already handles this and I would rather keep usages of getAs<InOutType>() centralized.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh, how did I miss getInOutObjectType()? Search fail, thank you!

if (auto inOutType = type->getAs<InOutType>())
return inOutType->getObjectType();

return type;
}

/// Clone constructor, allocates a new ParamDecl identical to the first.
/// Intentionally not defined as a copy constructor to avoid accidental copies.
ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
: VarDecl(DeclKind::Param, /*IsStatic*/false, PD->getSpecifier(),
/*IsCaptureList*/false, PD->getNameLoc(), PD->getName(),
PD->hasType() && withTypes? PD->getType() : Type(),
PD->hasType() && withTypes? removeInOutType(PD->getType()) : Type(),
PD->getDeclContext()),
ArgumentName(PD->getArgumentName()),
ArgumentNameLoc(PD->getArgumentNameLoc()),
Expand All @@ -4215,7 +4223,7 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
typeLoc.setType(Type());

if (withTypes && PD->hasInterfaceType())
setInterfaceType(PD->getInterfaceType());
setInterfaceType(removeInOutType(PD->getInterfaceType()));
}


Expand Down
31 changes: 31 additions & 0 deletions test/decl/inherit/initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,34 @@ func testClassInGenericFunc<T>(t: T) {

_ = B(t: t)
}

// rdar://problem/34789779
public class Node {
var data : Data

public struct Data {
var index: Int32 = 0// for helpers
}

init(data: inout Data/*, context: Context*/) {
self.data = data
}

public required init(node: Node) {
data = node.data
}
}

final class SubNode : Node {
var a: Int

required init(node: Node) {
a = 1
super.init(node: node)
}

init(data: inout Data, additionalParam: Int) {
a = additionalParam
super.init(data: &data)
}
}
36 changes: 36 additions & 0 deletions validation-test/compiler_crashers_2/0127-sr5546.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: not --crash %target-swift-frontend %s -typecheck
public struct Foo<A, B, C> {}

public protocol P {
associatedtype PA
associatedtype PB = Never
associatedtype PC = Never

typealias Context = Foo<PA, PB, PC>

func f1(_ x: Context, _ y: PA)
func f2(_ x: Context, _ y: PB)
func f3(_ x: Context, _ y: PC)
func f4(_ x: Context)
}

public extension P {
public func f1(_ x: Context, _ y: PA) {
}
public func f2(_ x: Context, _ y: PB) {
}
public func f3(_ x: Context, _ y: PC) {
}
public func f4(_ x: Context) {
}
}

public struct S: P {
public typealias PA = String
public typealias PB = Int

public func f1(_ x: Context, _ y: PA) {
}
public func f2(_ x: Context, _ y: PB) {
}
}