Skip to content

Commit e96642c

Browse files
authored
Merge pull request #72106 from meg-gupta/lifetimedepdiag
Loosen some lifetime dependence diagnostics
2 parents 44c2c07 + 6f40fc8 commit e96642c

File tree

14 files changed

+81
-17
lines changed

14 files changed

+81
-17
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ option(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS
657657
"Enable experimental NoncopyableGenerics"
658658
FALSE)
659659

660+
option(SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES
661+
"Enable experimental NonescapableTypes"
662+
FALSE)
663+
660664
option(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING
661665
"Enable experimental string processing"
662666
FALSE)
@@ -1253,6 +1257,7 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
12531257
message(STATUS "Concurrency Support: ${SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY}")
12541258
message(STATUS "Distributed Support: ${SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED}")
12551259
message(STATUS "NoncopyableGenerics Support: ${SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS}")
1260+
message(STATUS "NonEscapableTypes Support: ${SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES}")
12561261
message(STATUS "String Processing Support: ${SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING}")
12571262
message(STATUS "Backtracing Support: ${SWIFT_ENABLE_BACKTRACING}")
12581263
message(STATUS "Unicode Support: ${SWIFT_STDLIB_ENABLE_UNICODE_DATA}")

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7653,8 +7653,6 @@ NOTE(note_non_bitwise_copyable_type_indirect_enum_element,none,
76537653
"indirect case is here", ())
76547654
ERROR(non_bitwise_copyable_type_noncopyable,none,
76557655
"noncopyable type cannot conform to 'BitwiseCopyable'", ())
7656-
ERROR(non_bitwise_copyable_type_nonescapable,none,
7657-
"nonescapable type cannot conform to 'BitwiseCopyable'", ())
76587656
ERROR(non_bitwise_copyable_type_cxx_nontrivial,none,
76597657
"non-trivial C++ type cannot conform to 'BitwiseCopyable'", ())
76607658
ERROR(non_bitwise_copyable_c_type_nontrivial,none,

lib/Sema/LifetimeDependence.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,20 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType,
178178
}
179179
}
180180

181-
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
182-
auto *bitwiseCopyableProtocol =
183-
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
184-
if (bitwiseCopyableProtocol && mod->checkConformance(type, bitwiseCopyableProtocol)) {
185-
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
186-
return true;
181+
// Diagnose when we have lifetime dependence on a type that is
182+
// BitwiseCopyable & Escapable.
183+
// ~Escapable types are non-trivial in SIL and we should not raise this
184+
// error.
185+
// TODO: Diagnose ~Escapable types are always non-trivial in SIL.
186+
if (type->isEscapable()) {
187+
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
188+
auto *bitwiseCopyableProtocol =
189+
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
190+
if (bitwiseCopyableProtocol &&
191+
mod->checkConformance(type, bitwiseCopyableProtocol)) {
192+
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
193+
return true;
194+
}
187195
}
188196
}
189197

@@ -379,7 +387,7 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd, Type resultType) {
379387
// TODO: rdar://123555720: Remove this check after another round of
380388
// surveying builtins
381389
if (auto *fd = dyn_cast<FuncDecl>(afd)) {
382-
if (fd->isImplicit()) {
390+
if (fd->isImplicit() && fd->getModuleContext()->isBuiltinModule()) {
383391
return std::nullopt;
384392
}
385393
}

lib/Sema/TypeCheckBitwise.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,6 @@ static bool checkBitwiseCopyableInstanceStorage(NominalTypeDecl *nominal,
239239
return true;
240240
}
241241

242-
if (!dc->mapTypeIntoContext(nominal->getDeclaredInterfaceType())->isEscapable()) {
243-
if (!isImplicit(check)) {
244-
nominal->diagnose(diag::non_bitwise_copyable_type_nonescapable);
245-
}
246-
return true;
247-
}
248-
249242
if (isa<ClassDecl>(nominal)) {
250243
if (!isImplicit(check)) {
251244
nominal->diagnose(diag::non_bitwise_copyable_type_class);

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,10 @@ function(_compile_swift_files
617617
list(APPEND swift_flags "-Xfrontend" "-enable-experimental-associated-type-inference")
618618
endif()
619619

620+
if(SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES)
621+
list(APPEND swift_flags "-enable-experimental-feature" "NonescapableTypes")
622+
endif()
623+
620624
if (SWIFT_STDLIB_ENABLE_STRICT_CONCURRENCY_COMPLETE)
621625
list(APPEND swift_flags "-strict-concurrency=complete")
622626
endif()

stdlib/public/core/Misc.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,8 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
173173

174174
@_marker public protocol Escapable {}
175175

176-
@_marker public protocol _BitwiseCopyable {}
176+
#if $NoncopyableGenerics && $NonescapableTypes
177+
@_marker public protocol _BitwiseCopyable: ~Escapable { }
178+
#else
179+
@_marker public protocol _BitwiseCopyable { }
180+
#endif

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING)
192192
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY)
193193
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED)
194194
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS)
195+
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES)
195196
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
196197
normalize_boolean_spelling(SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION)
197198
normalize_boolean_spelling(SWIFT_ENABLE_MACCATALYST)
@@ -404,6 +405,10 @@ foreach(SDK ${SWIFT_SDKS})
404405
list(APPEND LIT_ARGS "--param" "noncopyable_generics")
405406
endif()
406407

408+
if(SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES)
409+
list(APPEND LIT_ARGS "--param" "nonescapable_types")
410+
endif()
411+
407412
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
408413
list(APPEND LIT_ARGS "--param" "string_processing")
409414
endif()

test/Sema/explicit_lifetime_dependence_specifiers.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes -disable-experimental-parser-round-trip -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature BitwiseCopyable
22
// REQUIRES: asserts
33
// REQUIRES: noncopyable_generics
4+
// REQUIRES: nonescapable_types
45

56
struct Container {
67
let ptr: UnsafeRawBufferPointer
78
}
89

10+
struct AnotherBufferView : ~Escapable, _BitwiseCopyable {
11+
let ptr: UnsafeRawBufferPointer
12+
@_unsafeNonescapableResult
13+
init(_ ptr: UnsafeRawBufferPointer) {
14+
self.ptr = ptr
15+
}
16+
}
17+
918
struct BufferView : ~Escapable {
1019
let ptr: UnsafeRawBufferPointer
1120
@_unsafeNonescapableResult
@@ -16,6 +25,10 @@ struct BufferView : ~Escapable {
1625
self.ptr = c.ptr
1726
return self
1827
}
28+
init(_ bv: borrowing AnotherBufferView) -> _borrow(bv) Self {
29+
self.ptr = bv.ptr
30+
return self
31+
}
1932
}
2033

2134
struct MutableBufferView : ~Escapable, ~Copyable {

test/lit.site.cfg.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ if "@SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED@" == "TRUE":
145145
config.available_features.add('distributed')
146146
if "@SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS@" == "TRUE":
147147
config.available_features.add('noncopyable_generics')
148+
if "@SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES@" == "TRUE":
149+
config.available_features.add('nonescapable_types')
148150
if "@SWIFT_STDLIB_STATIC_PRINT@" == "TRUE":
149151
config.available_features.add('stdlib_static_print')
150152
if "@SWIFT_STDLIB_ENABLE_UNICODE_DATA" == "TRUE":

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,10 @@ def create_argument_parser():
13511351
default=False,
13521352
help='Enable experimental NoncopyableGenerics.')
13531353

1354+
option('--enable-experimental-nonescapable-types', toggle_true,
1355+
default=False,
1356+
help='Enable experimental NonescapableTypes.')
1357+
13541358
option('--enable-experimental-string-processing', toggle_true,
13551359
default=True,
13561360
help='Enable experimental Swift string processing.')

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
'enable_cxx_interop_swift_bridging_header': True,
170170
'enable_experimental_distributed': True,
171171
'enable_experimental_noncopyable_generics': False,
172+
'enable_experimental_nonescapable_types': False,
172173
'enable_experimental_string_processing': True,
173174
'enable_experimental_observation': True,
174175
'swift_enable_backtracing': True,
@@ -593,6 +594,7 @@ class BuildScriptImplOption(_BaseOption):
593594
EnableOption('--enable-cxx-interop-swift-bridging-header'),
594595
EnableOption('--enable-experimental-distributed'),
595596
EnableOption('--enable-experimental-noncopyable-generics'),
597+
EnableOption('--enable-experimental-nonescapable-types'),
596598
EnableOption('--enable-experimental-string-processing'),
597599
EnableOption('--enable-experimental-observation'),
598600
EnableOption('--enable-lsan'),

utils/swift_build_support/swift_build_support/products/swift.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def __init__(self, args, toolchain, source_dir, build_dir):
6262
# Add experimental NoncopyableGenerics flag.
6363
self.cmake_options.extend(self._enable_experimental_noncopyable_generics)
6464

65+
# Add experimental NonescapableTypes flag.
66+
self.cmake_options.extend(self._enable_experimental_nonescapable_types)
67+
6568
# Add backtracing flag.
6669
self.cmake_options.extend(self._enable_backtracing)
6770

@@ -198,6 +201,11 @@ def _enable_experimental_noncopyable_generics(self):
198201
return [('SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS:BOOL',
199202
self.args.enable_experimental_noncopyable_generics)]
200203

204+
@property
205+
def _enable_experimental_nonescapable_types(self):
206+
return [('SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES:BOOL',
207+
self.args.enable_experimental_nonescapable_types)]
208+
201209
@property
202210
def _enable_backtracing(self):
203211
return [('SWIFT_ENABLE_BACKTRACING:BOOL',

utils/swift_build_support/tests/products/test_swift.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def setUp(self):
5959
enable_cxx_interop_swift_bridging_header=False,
6060
enable_experimental_distributed=False,
6161
enable_experimental_noncopyable_generics=False,
62+
enable_experimental_nonescapable_types=False,
6263
enable_experimental_observation=False,
6364
swift_enable_backtracing=False,
6465
enable_synchronization=False,
@@ -104,6 +105,7 @@ def test_by_default_no_cmake_options(self):
104105
'-DSWIFT_ENABLE_CXX_INTEROP_SWIFT_BRIDGING_HEADER:BOOL=FALSE',
105106
'-DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED:BOOL=FALSE',
106107
'-DSWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS:BOOL=FALSE',
108+
'-DSWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES:BOOL=FALSE',
107109
'-DSWIFT_ENABLE_EXPERIMENTAL_OBSERVATION:BOOL=FALSE',
108110
'-DSWIFT_ENABLE_BACKTRACING:BOOL=FALSE',
109111
'-DSWIFT_ENABLE_SYNCHRONIZATION:BOOL=FALSE',
@@ -133,6 +135,7 @@ def test_swift_runtime_tsan(self):
133135
'-DSWIFT_ENABLE_CXX_INTEROP_SWIFT_BRIDGING_HEADER:BOOL=FALSE',
134136
'-DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED:BOOL=FALSE',
135137
'-DSWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS:BOOL=FALSE',
138+
'-DSWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES:BOOL=FALSE',
136139
'-DSWIFT_ENABLE_EXPERIMENTAL_OBSERVATION:BOOL=FALSE',
137140
'-DSWIFT_ENABLE_BACKTRACING:BOOL=FALSE',
138141
'-DSWIFT_ENABLE_SYNCHRONIZATION:BOOL=FALSE',
@@ -417,6 +420,19 @@ def test_experimental_noncopyable_generics_flags(self):
417420
[x for x in swift.cmake_options
418421
if 'DSWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS' in x])
419422

423+
def test_experimental_nonescapable_types_flags(self):
424+
self.args.enable_experimental_nonescapable_types = True
425+
swift = Swift(
426+
args=self.args,
427+
toolchain=self.toolchain,
428+
source_dir='/path/to/src',
429+
build_dir='/path/to/build')
430+
self.assertEqual(
431+
['-DSWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES:BOOL='
432+
'TRUE'],
433+
[x for x in swift.cmake_options
434+
if 'DSWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES' in x])
435+
420436
def test_experimental_observation_flags(self):
421437
self.args.enable_experimental_observation = True
422438
swift = Swift(

validation-test/lit.site.cfg.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ if "@SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED@" == "TRUE":
132132
config.available_features.add('distributed')
133133
if "@SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS@" == "TRUE":
134134
config.available_features.add('noncopyable_generics')
135+
if "@SWIFT_ENABLE_EXPERIMENTAL_NONESCAPABLE_TYPES@" == "TRUE":
136+
config.available_features.add('nonescapable_types')
135137
if "@SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING@" == "TRUE":
136138
config.available_features.add('string_processing')
137139
if "@SWIFT_STDLIB_ENABLE_DEBUG_PRECONDITIONS_IN_RELEASE@" == "TRUE":

0 commit comments

Comments
 (0)