Skip to content

Commit 5a46664

Browse files
authored
Merge pull request #84359 from DougGregor/diagnose-untyped-throws-in-embedded
2 parents 197bcc2 + 7088022 commit 5a46664

File tree

9 files changed

+52
-1
lines changed

9 files changed

+52
-1
lines changed

lib/Frontend/CompilerInvocation.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,21 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
25252525
return false;
25262526
}
25272527

2528+
/// Determine whether the given argument list enables Embedded Swift.
2529+
static bool isEmbedded(ArgList &args) {
2530+
using namespace swift::options;
2531+
2532+
for (const Arg *arg : args.filtered_reverse(
2533+
OPT_enable_experimental_feature, OPT_disable_experimental_feature)) {
2534+
if (llvm::StringRef(arg->getValue()) != "Embedded")
2535+
continue;
2536+
2537+
return arg->getOption().matches(OPT_enable_experimental_feature);
2538+
}
2539+
2540+
return false;
2541+
}
2542+
25282543
static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
25292544
DiagnosticEngine &Diags) {
25302545
// NOTE: This executes at the beginning of parsing the command line and cannot
@@ -2592,6 +2607,14 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
25922607
}
25932608
}
25942609

2610+
// If the "embedded" flag was provided, enable the EmbeddedRestrictions
2611+
// warning group. This group is opt-in in non-Embedded builds.
2612+
if (isEmbedded(Args)) {
2613+
Opts.WarningsAsErrorsRules.push_back(
2614+
WarningAsErrorRule(WarningAsErrorRule::Action::Disable,
2615+
"EmbeddedRestrictions"));
2616+
}
2617+
25952618
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
25962619
Opts.SuppressRemarks |= Args.hasArg(OPT_suppress_remarks);
25972620
for (const Arg *arg : Args.filtered(OPT_warning_treating_Group)) {

stdlib/public/core/Array.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,7 @@ extension Array {
15091509
}
15101510

15111511
extension Array {
1512+
#if !$Embedded
15121513
/// Implementation preserved (for ABI reasons) for:
15131514
/// Array(unsafeUninitializedCapacity:initializingWith:)
15141515
/// and ContiguousArray(unsafeUninitializedCapacity:initializingWith:)
@@ -1525,6 +1526,7 @@ extension Array {
15251526
initializingWithTypedThrowsInitializer: initializer
15261527
)
15271528
}
1529+
#endif
15281530

15291531
/// Implementation for:
15301532
/// Array(unsafeUninitializedCapacity:initializingWith:)

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ where Indices == Range<Int> {
7676
/// Returns a `_SliceBuffer` containing the elements in `bounds`.
7777
subscript(bounds: Range<Int>) -> _SliceBuffer<Element> { get }
7878

79+
#if !$Embedded
7980
// Superseded by the typed-throws version of this function, but retained
8081
// for ABI reasons.
8182
func withUnsafeBufferPointer<R>(
8283
_ body: (UnsafeBufferPointer<Element>) throws -> R
8384
) rethrows -> R
85+
#endif
8486

8587
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
8688
/// underlying contiguous storage. If no such storage exists, it is
@@ -90,11 +92,13 @@ where Indices == Range<Int> {
9092
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
9193
) throws(E) -> R
9294

95+
#if !$Embedded
9396
// Superseded by the typed-throws version of this function, but retained
9497
// for ABI reasons.
9598
mutating func withUnsafeMutableBufferPointer<R>(
9699
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
97100
) rethrows -> R
101+
#endif
98102

99103
/// Call `body(p)`, where `p` is an `UnsafeMutableBufferPointer`
100104
/// over the underlying contiguous storage.

stdlib/public/core/Collection.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@ extension Collection {
12131213
return Array(result)
12141214
}
12151215

1216+
#if !$Embedded
12161217
// ABI-only entrypoint for the rethrows version of map, which has been
12171218
// superseded by the typed-throws version. Expressed as "throws", which is
12181219
// ABI-compatible with "rethrows".
@@ -1224,6 +1225,7 @@ extension Collection {
12241225
) throws -> [T] {
12251226
try map(transform)
12261227
}
1228+
#endif
12271229

12281230
/// Returns a subsequence containing all but the given number of initial
12291231
/// elements.

stdlib/public/core/ExistentialCollection.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ extension AnySequence {
13341334
}
13351335
}
13361336

1337+
#if !$Embedded
13371338
// ABI-only entrypoint for the rethrows version of map, which has been
13381339
// superseded by the typed-throws version. Expressed as "throws", which is
13391340
// ABI-compatible with "rethrows".
@@ -1345,6 +1346,7 @@ extension AnySequence {
13451346
) throws -> [T] {
13461347
try map(transform)
13471348
}
1349+
#endif
13481350

13491351
@inlinable
13501352
public __consuming func filter(
@@ -1438,6 +1440,7 @@ extension AnyCollection {
14381440
}
14391441
}
14401442

1443+
#if !$Embedded
14411444
// ABI-only entrypoint for the rethrows version of map, which has been
14421445
// superseded by the typed-throws version. Expressed as "throws", which is
14431446
// ABI-compatible with "rethrows".
@@ -1449,6 +1452,7 @@ extension AnyCollection {
14491452
) throws -> [T] {
14501453
try map(transform)
14511454
}
1455+
#endif
14521456

14531457
@inlinable
14541458
public __consuming func filter(
@@ -1548,6 +1552,7 @@ extension AnyBidirectionalCollection {
15481552
}
15491553
}
15501554

1555+
#if !$Embedded
15511556
// ABI-only entrypoint for the rethrows version of map, which has been
15521557
// superseded by the typed-throws version. Expressed as "throws", which is
15531558
// ABI-compatible with "rethrows".
@@ -1559,6 +1564,7 @@ extension AnyBidirectionalCollection {
15591564
) throws -> [T] {
15601565
try map(transform)
15611566
}
1567+
#endif
15621568

15631569
@inlinable
15641570
public __consuming func filter(
@@ -1660,6 +1666,7 @@ extension AnyRandomAccessCollection {
16601666
}
16611667
}
16621668

1669+
#if !$Embedded
16631670
// ABI-only entrypoint for the rethrows version of map, which has been
16641671
// superseded by the typed-throws version. Expressed as "throws", which is
16651672
// ABI-compatible with "rethrows".
@@ -1671,6 +1678,7 @@ extension AnyRandomAccessCollection {
16711678
) throws -> [T] {
16721679
try map(transform)
16731680
}
1681+
#endif
16741682

16751683
@inlinable
16761684
public __consuming func filter(

stdlib/public/core/LifetimeManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public func withUnsafePointer<T: ~Copyable, E: Error, Result: ~Copyable>(
184184
return try unsafe body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
185185
}
186186

187+
#if !$Embedded
187188
/// ABI: Historical withUnsafePointer(to:_:) rethrows, expressed as "throws",
188189
/// which is ABI-compatible with "rethrows".
189190
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@@ -196,6 +197,7 @@ internal func __abi_withUnsafePointer<T, Result>(
196197
{
197198
return try unsafe body(UnsafePointer<T>(Builtin.addressOfBorrow(value)))
198199
}
200+
#endif
199201

200202
/// Invokes the given closure with a pointer to the given argument.
201203
///
@@ -229,6 +231,7 @@ public func withUnsafePointer<T: ~Copyable, E: Error, Result: ~Copyable>(
229231
try unsafe body(UnsafePointer<T>(Builtin.addressof(&value)))
230232
}
231233

234+
#if !$Embedded
232235
/// ABI: Historical withUnsafePointer(to:_:) rethrows,
233236
/// expressed as "throws", which is ABI-compatible with "rethrows".
234237
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@@ -240,6 +243,7 @@ internal func __abi_se0413_withUnsafePointer<T, Result>(
240243
) throws -> Result {
241244
return try unsafe body(UnsafePointer<T>(Builtin.addressof(&value)))
242245
}
246+
#endif
243247

244248
/// Invokes the given closure with a pointer to the given argument.
245249
///

stdlib/public/core/Sequence.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ extension Sequence {
705705
return Array(result)
706706
}
707707

708+
#if !$Embedded
708709
// ABI-only entrypoint for the rethrows version of map, which has been
709710
// superseded by the typed-throws version. Expressed as "throws", which is
710711
// ABI-compatible with "rethrows".
@@ -716,6 +717,7 @@ extension Sequence {
716717
) throws -> [T] {
717718
try map(transform)
718719
}
720+
#endif
719721

720722
/// Returns an array containing, in order, the elements of the sequence
721723
/// that satisfy the given predicate.

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ public func withUnsafeMutableBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
12781278
return try unsafe body(unsafe .init(start: pointer, count: MemoryLayout<T>.size))
12791279
}
12801280

1281+
#if !$Embedded
12811282
/// ABI: Historical withUnsafeMutableBytes(of:_:) rethrows,
12821283
/// expressed as "throws", which is ABI-compatible with "rethrows".
12831284
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@@ -1292,6 +1293,7 @@ func __abi_se0413_withUnsafeMutableBytes<T, Result>(
12921293
start: $0, count: MemoryLayout<T>.size))
12931294
}
12941295
}
1296+
#endif
12951297

12961298
/// Invokes the given closure with a buffer pointer covering the raw bytes of
12971299
/// the given argument.
@@ -1345,6 +1347,7 @@ public func withUnsafeBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
13451347
return try unsafe body(unsafe .init(start: address, count: MemoryLayout<T>.size))
13461348
}
13471349

1350+
#if !$Embedded
13481351
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
13491352
/// expressed as "throws", which is ABI-compatible with "rethrows".
13501353
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@@ -1358,6 +1361,7 @@ func __abi_se0413_withUnsafeBytes<T, Result>(
13581361
try unsafe body(unsafe UnsafeRawBufferPointer(start: $0, count: MemoryLayout<T>.size))
13591362
}
13601363
}
1364+
#endif
13611365

13621366
/// Invokes the given closure with a buffer pointer covering the raw bytes of
13631367
/// the given argument.
@@ -1409,6 +1413,7 @@ public func withUnsafeBytes<
14091413
return try unsafe body(unsafe .init(start: addr, count: MemoryLayout<T>.size))
14101414
}
14111415

1416+
#if !$Embedded
14121417
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
14131418
/// expressed as "throws", which is ABI-compatible with "rethrows".
14141419
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
@@ -1422,6 +1427,7 @@ func __abi_se0413_withUnsafeBytes<T, Result>(
14221427
let buffer = unsafe UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
14231428
return try unsafe body(buffer)
14241429
}
1430+
#endif
14251431

14261432
/// Invokes the given closure with a buffer pointer covering the raw bytes of
14271433
/// the given argument.

test/embedded/restrictions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -Wwarning EmbeddedRestrictions -verify-additional-prefix nonembedded-
2-
// RUN: %target-typecheck-verify-swift -Wwarning EmbeddedRestrictions -enable-experimental-feature Embedded -verify-additional-prefix embedded-
2+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Embedded -verify-additional-prefix embedded-
33

44
// REQUIRES: swift_in_compiler
55
// REQUIRES: swift_feature_Embedded

0 commit comments

Comments
 (0)