Skip to content

Commit dd7ec7f

Browse files
authored
Merge pull request #77532 from DougGregor/experimental-flag-for-span-usage
Temporarily put uses of "Span" and "RawSpan" behind an experimental feature flag
2 parents 27bb00e + adc73a8 commit dd7ec7f

File tree

11 files changed

+65
-8
lines changed

11 files changed

+65
-8
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7912,6 +7912,10 @@ ERROR(noncopyable_cannot_have_read_set_accessor,none,
79127912
ERROR(nonescapable_types_attr_disabled,none,
79137913
"attribute requires '-enable-experimental-feature NonescapableTypes'", ())
79147914

7915+
ERROR(span_requires_feature_flag,none,
7916+
"'%0' requires -enable-experimental-feature Span",
7917+
(StringRef))
7918+
79157919
//------------------------------------------------------------------------------
79167920
// MARK: Init accessors
79177921
//------------------------------------------------------------------------------

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ EXPERIMENTAL_FEATURE(MacrosOnImports, true)
234234
EXPERIMENTAL_FEATURE(TupleConformances, false)
235235
EXPERIMENTAL_FEATURE(FullTypedThrows, false)
236236
EXPERIMENTAL_FEATURE(SameElementRequirements, false)
237+
EXPERIMENTAL_FEATURE(Span, true)
237238

238239
// Whether to enable @_used and @_section attributes
239240
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ UNINTERESTING_FEATURE(GroupActorErrors)
197197
UNINTERESTING_FEATURE(SameElementRequirements)
198198
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
199199
UNINTERESTING_FEATURE(GenerateForceToMainActorThunks)
200+
UNINTERESTING_FEATURE(Span)
200201

201202
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
202203
auto isFunctionTypeWithSending = [](Type type) {

lib/Sema/TypeCheckType.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,32 @@ static void diagnoseGenericArgumentsOnSelf(const TypeResolution &resolution,
17191719
}
17201720
}
17211721

1722+
/// Diagnose when this is one of the Span types, which currently requires
1723+
/// an experimental feature to use.
1724+
static void diagnoseSpanType(TypeDecl *typeDecl, SourceLoc loc,
1725+
const DeclContext *dc) {
1726+
if (loc.isInvalid())
1727+
return;
1728+
1729+
if (!typeDecl->isStdlibDecl())
1730+
return;
1731+
1732+
ASTContext &ctx = typeDecl->getASTContext();
1733+
if (ctx.LangOpts.hasFeature(Feature::Span))
1734+
return;
1735+
1736+
auto nameString = typeDecl->getName().str();
1737+
if (nameString != "Span" && nameString != "RawSpan")
1738+
return;
1739+
1740+
// Don't require this in the standard library or _Concurrency library.
1741+
auto module = dc->getParentModule();
1742+
if (module->isStdlibModule() || module->getName().str() == "_Concurrency")
1743+
return;
1744+
1745+
ctx.Diags.diagnose(loc, diag::span_requires_feature_flag, nameString);
1746+
}
1747+
17221748
/// Resolve the given identifier type representation as an unqualified type,
17231749
/// returning the type it references.
17241750
/// \param silContext Used to look up generic parameters in SIL mode.
@@ -1854,6 +1880,9 @@ resolveUnqualifiedIdentTypeRepr(const TypeResolution &resolution,
18541880
repr->setInvalid();
18551881
return ErrorType::get(ctx);
18561882
}
1883+
1884+
diagnoseSpanType(currentDecl, repr->getLoc(), DC);
1885+
18571886
repr->setValue(currentDecl, currentDC);
18581887
return current;
18591888
}
@@ -2074,6 +2103,8 @@ static Type resolveQualifiedIdentTypeRepr(const TypeResolution &resolution,
20742103
member = memberTypes.back().Member;
20752104
inferredAssocType = memberTypes.back().InferredAssociatedType;
20762105
repr->setValue(member, nullptr);
2106+
2107+
diagnoseSpanType(member, repr->getLoc(), DC);
20772108
}
20782109

20792110
return maybeDiagnoseBadMemberType(member, memberType, inferredAssocType);

test/Macros/PointerBounds/CountedBy/MutableSpan.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3-
// XFAIL: OS=windows-msvc
4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
3+
// REQUIRES: swift_feature_Span
4+
5+
// RUN: not %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span > %t.log 2>&1
6+
// RUN: %FileCheck --match-full-lines %s < %t.log
57

68
@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
79
func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {

test/Macros/PointerBounds/CountedBy/SimpleSpan.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3+
// REQUIRES: swift_feature_Span
34

4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
5+
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span 2>&1 | %FileCheck --match-full-lines %s
56

67
@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
78
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) {

test/Macros/PointerBounds/CountedBy/SimpleSpanWithReturn.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3+
// REQUIRES: swift_feature_Span
34

4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
5+
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span 2>&1 | %FileCheck --match-full-lines %s
56

67
@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
78
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) -> CInt {

test/Macros/PointerBounds/SizedBy/MutableRawSpan.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3-
// XFAIL: OS=windows-msvc
4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
3+
// REQUIRES: swift_feature_Span
4+
5+
// RUN: not %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span > %t.log 2>&1
6+
// RUN: %FileCheck --match-full-lines %s < %t.log
57

68
@PointerBounds(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1))
79
func myFunc(_ ptr: UnsafeMutableRawPointer, _ size: CInt) {

test/Macros/PointerBounds/SizedBy/SimpleRawSpan.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3+
// REQUIRES: swift_feature_Span
34

4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
5+
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span 2>&1 | %FileCheck --match-full-lines %s
56

67
@PointerBounds(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1))
78
func myFunc(_ ptr: UnsafeRawPointer, _ size: CInt) {

test/Macros/PointerBounds/SizedBy/SimpleRawSpanWithReturn.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: swift_swift_parser
22
// REQUIRES: pointer_bounds
3+
// REQUIRES: swift_feature_Span
34

4-
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s
5+
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span 2>&1 | %FileCheck --match-full-lines %s
56

67
@PointerBounds(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1))
78
func myFunc(_ ptr: UnsafeRawPointer, _ size: CInt) -> CInt {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -verify-additional-prefix missing-
2+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature Span
3+
// REQUIRES: swift_feature_Span
4+
5+
@available(SwiftStdlib 6.1, *)
6+
func f(_: Span<Int>) { }
7+
// expected-missing-error@-1{{'Span' requires -enable-experimental-feature Span}}
8+
9+
10+
@available(SwiftStdlib 6.1, *)
11+
func g(_: RawSpan) { }
12+
// expected-missing-error@-1{{'RawSpan' requires -enable-experimental-feature Span}}

0 commit comments

Comments
 (0)