Skip to content

Commit 29c0b0d

Browse files
authored
Merge pull request #66896 from zoecarver/fix-ambiguous-math-func
[cxx-interop] Pull over fix from 8e7766b and apply to strstr, sin, an…
2 parents 7a46466 + 6233a65 commit 29c0b0d

File tree

4 files changed

+74
-29
lines changed

4 files changed

+74
-29
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,19 +3202,30 @@ namespace {
32023202
// presence in the C++ standard library will cause overloading
32033203
// ambiguities or other type checking errors in Swift.
32043204
auto isAlternativeCStdlibFunctionFromTextualHeader =
3205-
[](const clang::FunctionDecl *d) -> bool {
3205+
[this](const clang::FunctionDecl *d) -> bool {
32063206
// stdlib.h might be a textual header in libc++'s module map.
32073207
// in this case, check for known ambiguous functions by their name
32083208
// instead of checking if they come from the `std` module.
32093209
if (!d->getDeclName().isIdentifier())
32103210
return false;
3211-
return d->getName() == "abs" || d->getName() == "div";
3211+
if (d->getName() == "abs" || d->getName() == "div")
3212+
return true;
3213+
if (Impl.SwiftContext.LangOpts.Target.isOSDarwin())
3214+
return d->getName() == "strstr" || d->getName() == "sin" ||
3215+
d->getName() == "cos" || d->getName() == "exit";
3216+
return false;
32123217
};
3213-
if (decl->getOwningModule() &&
3214-
(decl->getOwningModule()
3215-
->getTopLevelModule()
3216-
->getFullModuleName() == "std" ||
3217-
isAlternativeCStdlibFunctionFromTextualHeader(decl))) {
3218+
auto topLevelModuleEq =
3219+
[](const clang::FunctionDecl *d, StringRef n) -> bool {
3220+
return d->getOwningModule() &&
3221+
d->getOwningModule()
3222+
->getTopLevelModule()
3223+
->getFullModuleName() == n;
3224+
};
3225+
if (topLevelModuleEq(decl, "std")) {
3226+
if (isAlternativeCStdlibFunctionFromTextualHeader(decl)) {
3227+
return nullptr;
3228+
}
32183229
auto filename =
32193230
Impl.getClangPreprocessor().getSourceManager().getFilename(
32203231
decl->getLocation());
@@ -3223,6 +3234,13 @@ namespace {
32233234
return nullptr;
32243235
}
32253236
}
3237+
// Use the exit function from _SwiftConcurrency.h as it is platform
3238+
// agnostic.
3239+
if ((topLevelModuleEq(decl, "Darwin") ||
3240+
topLevelModuleEq(decl, "SwiftGlibc")) &&
3241+
decl->getDeclName().isIdentifier() && decl->getName() == "exit") {
3242+
return nullptr;
3243+
}
32263244
}
32273245

32283246
auto dc =

test/Interop/Cxx/stdlib/avoid-import-cxx-math.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
import CxxStdlib
66

77
func test() {
8-
let x: Float = 1.0
8+
let x: Double = 1.0
99
let y: Double = 2.0
1010

11-
// Note: we dispatch `pow(Float,Double)`
12-
// to ensure we don't pick up the
13-
// C++ stdlib `pow` function template.
14-
// The `pow` function is still reexported
15-
// from Darwin via CxxStdlib, so there are
16-
// matching overloads that can be found still.
17-
// Note: the error is different on Glibc instead
18-
// of Darwin, so do not check the exact error.
19-
let _ = CxxStdlib.pow(x, y) // expected-error {{}}
11+
let _ = pow(x, y)
2012

21-
let _ = CxxStdlib.abs(x) // expected-error {{module 'CxxStdlib' has no member named 'abs'}}
22-
let _ = CxxStdlib.div(x) // expected-error {{module 'CxxStdlib' has no member named 'div'}}
13+
let _ = abs(x)
14+
// https://github.com/apple/swift/issues/67006
15+
// let _ = div(42, 2)
16+
let _ = sin(x)
17+
let _ = cos(x)
18+
let _ = strstr("a", "aaa")
19+
20+
exit(0)
2321
}

test/Interop/Cxx/stdlib/print-swiftconcurrencyshims-interface.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library)
2+
//
3+
// REQUIRES: executable_test
4+
// REQUIRES: concurrency
5+
// REQUIRES: libdispatch
6+
// REQUIRES: concurrency_runtime
7+
8+
import StdlibUnittest
9+
10+
import CxxStdlib
11+
import Cxx
12+
13+
import _Concurrency
14+
import Dispatch
15+
16+
@main struct Main {
17+
static func main() async {
18+
var ConcurrencyTestSuite = TestSuite("Concurrency")
19+
20+
ConcurrencyTestSuite.test("Task.sleep") {
21+
let start = DispatchTime.now()
22+
await Task.sleep(100_000_000)
23+
let stop = DispatchTime.now()
24+
expectTrue(stop >= (start + .nanoseconds(100_000_000)))
25+
}
26+
27+
ConcurrencyTestSuite.test("Task.sleep (non-blocking)") {
28+
let task = detach {
29+
std.string("Hello, Swift!")
30+
}
31+
32+
await Task.sleep(100_000_000)
33+
expectEqual(await task.get(), "Hello, Swift!")
34+
}
35+
36+
await runAllTestsAsync()
37+
}
38+
}
39+

0 commit comments

Comments
 (0)