Skip to content

Commit f03337c

Browse files
committed
SR-12802: Disambiguate functions with lvalue and rvalue reference parameters in the same overload set
1 parent c88a157 commit f03337c

File tree

6 files changed

+36
-24
lines changed

6 files changed

+36
-24
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,9 +2291,11 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
22912291
return nullptr;
22922292
}
22932293

2294-
paramTy = refType->getPointeeType();
2295-
if (!paramTy.isConstQualified())
2296-
isInOut = true;
2294+
paramTy = refType->getPointeeType();
2295+
if (isa<clang::LValueReferenceType>(paramTy)
2296+
&& !paramTy.isConstQualified()) {
2297+
isInOut = true;
2298+
}
22972299
}
22982300

22992301
if (!swiftParamTy) {

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,23 @@ static bool isClangTypeMoreIndirectThanSubstType(TypeConverter &TC,
13271327
// Pass C++ const reference types indirectly. Right now there's no way to
13281328
// express immutable borrowed params, so we have to have this hack.
13291329
// Eventually, we should just express these correctly: rdar://89647503
1330-
if (clangTy->isReferenceType() &&
1331-
clangTy->getPointeeType().isConstQualified())
1332-
return true;
1330+
1331+
/*
1332+
Why does this not compile?
1333+
if (clangTy->getPointeeType().isConstQualified() ||
1334+
clangTy->isRValueReferenceType()) {
1335+
1336+
return true;
1337+
}
1338+
*/
1339+
1340+
if (clangTy->isReferenceType()) {
1341+
if (clangTy->getPointeeType().isConstQualified() ||
1342+
clangTy->isRValueReferenceType()) {
1343+
1344+
return true;
1345+
}
1346+
}
13331347

13341348
return false;
13351349
}

test/Interop/Cxx/class/method/ambiguous-methods-module-interface.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// CHECK: func increment(_ a: Int32) -> Int32
44
// CHECK: mutating func incrementMutating(_ a: Int32) -> Int32
55

6-
// CHECK: mutating func incrementMutating(_ a: Int32, _ b: Int32, _ c: inout Int32)
7-
// CHECK: func increment(_ a: Int32, _ b: Int32, _ c: inout Int32)
6+
// CHECK: mutating func incrementMutating(_ a: Int32, _ b: Int32, _ c: Int32)
7+
// CHECK: func increment(_ a: Int32, _ b: Int32, _ c: Int32)
88

9-
// CHECK: mutating func incrementMutating(_ a: inout Int32, _ b: Int32)
10-
// CHECK: func increment(_ a: inout Int32, _ b: Int32)
9+
// CHECK: mutating func incrementMutating(_ a: Int32, _ b: Int32)
10+
// CHECK: func increment(_ a: Int32, _ b: Int32)
1111

1212
// CHECK: func numberOfMutableMethodsCalled() -> Int32
1313
// CHECK: mutating func numberOfMutableMethodsCalledMutating() -> Int32

test/Interop/Cxx/reference/reference-irgen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public func getConstCxxRvalueRef() -> UnsafePointer<CInt> {
3232

3333
public func setCxxRef() {
3434
var val: CInt = 21
35-
setStaticIntRef(&val)
35+
setStaticIntRef(val)
3636
}
3737

3838
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main9setCxxRefyyF"()
@@ -48,7 +48,7 @@ public func setCxxConstRef() {
4848

4949
public func setCxxRvalueRef() {
5050
var val: CInt = 21
51-
setStaticIntRvalueRef(&val)
51+
setStaticIntRvalueRef(val)
5252
}
5353

5454
// CHECK: define {{(protected |dllexport )?}}swiftcc void @"$s4main15setCxxRvalueRefyyF"()

test/Interop/Cxx/stdlib/use-std-iterator.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ var StdIteratorTestSuite = TestSuite("StdIterator")
1313

1414
StdIteratorTestSuite.test("init") {
1515
var vector = Vector()
16-
var _1: CInt = 1
1716
//There seems to be an issue when importing this method, where the const_ref is mapped with
1817
//the correct typealias to be able to pass immutable values. related to: https://github.com/apple/swift/pull/41611
19-
vector.push_back(&_1)
18+
vector.push_back(1)
2019
//ideally we should call vector.begin(), however we need to prevent a copy of self before vector.begin() is invoked
2120
//current workaround is to use beginMutating()
2221
let it = vector.beginMutating()
@@ -25,10 +24,9 @@ StdIteratorTestSuite.test("init") {
2524

2625
StdIteratorTestSuite.test("advance") {
2726
var vector = Vector()
28-
var _1: CInt = 1, _2: CInt = 2, _3: CInt = 3
29-
vector.push_back(&_1)
30-
vector.push_back(&_2)
31-
vector.push_back(&_3)
27+
vector.push_back(1)
28+
vector.push_back(2)
29+
vector.push_back(3)
3230
var it = vector.beginMutating()
3331
std.__1.advance(&it, 2)
3432
expectEqual(it[0], 3)

test/Interop/Cxx/stdlib/use-std-vector.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ StdVectorTestSuite.test("init") {
2424

2525
StdVectorTestSuite.test("push back") {
2626
var v = Vector()
27-
var _42: CInt = 42
28-
v.push_back(&_42)
27+
v.push_back(42)
2928
expectEqual(v.size(), 1)
3029
expectFalse(v.empty())
3130
expectEqual(v[0], 42)
3231
}
3332

3433
func fill(vector v: inout Vector) {
35-
var _1: CInt = 1, _2: CInt = 2, _3: CInt = 3
36-
v.push_back(&_1)
37-
v.push_back(&_2)
38-
v.push_back(&_3)
34+
v.push_back(1)
35+
v.push_back(2)
36+
v.push_back(3)
3937
}
4038

4139
// TODO: in some configurations the stdlib emits a "initializeWithCopy" where the arguments

0 commit comments

Comments
 (0)