Skip to content

Commit bec666c

Browse files
authored
Merge pull request #73061 from meg-gupta/lifetimediagfixes
Diagnostic fixes to LifetimeDependence
2 parents bc4eeb8 + cbe582f commit bec666c

File tree

7 files changed

+53
-35
lines changed

7 files changed

+53
-35
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7900,14 +7900,14 @@ ERROR(lifetime_dependence_invalid_return_type, none,
79007900
ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
79017901
"cannot infer lifetime dependence %0, multiple parameters qualifiy as a candidate", (StringRef))
79027902
ERROR(lifetime_dependence_cannot_infer_no_candidates, none,
7903-
"cannot infer lifetime dependence %0, no parameters found that are either "
7903+
"cannot infer lifetime dependence%0, no parameters found that are either "
79047904
"~Escapable or Escapable with a borrowing ownership", (StringRef))
79057905
ERROR(lifetime_dependence_ctor_non_self_or_nil_return, none,
79067906
"expected nil or self as return values in an initializer with "
79077907
"lifetime dependent specifiers",
79087908
())
79097909
ERROR(lifetime_dependence_on_bitwise_copyable, none,
7910-
"invalid lifetime dependence on bitwise copyable type", ())
7910+
"invalid lifetime dependence on BitwiseCopyable type", ())
79117911
ERROR(lifetime_dependence_cannot_be_applied_to_tuple_elt, none,
79127912
"lifetime dependence specifiers cannot be applied to tuple elements", ())
79137913
ERROR(lifetime_dependence_method_escapable_bitwisecopyable_self, none,

include/swift/AST/LifetimeDependence.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_AST_LIFETIMEDEPENDENCE_H
1818
#define SWIFT_AST_LIFETIMEDEPENDENCE_H
1919

20+
#include "swift/AST/DeclContext.h"
2021
#include "swift/AST/Identifier.h"
2122
#include "swift/AST/IndexSubset.h"
2223
#include "swift/AST/Ownership.h"

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ add_swift_host_library(swiftAST STATIC
6161
IndexSubset.cpp
6262
InlinableText.cpp
6363
LayoutConstraint.cpp
64+
LifetimeDependence.cpp
6465
Module.cpp
6566
ModuleDependencies.cpp
6667
ModuleLoader.cpp

lib/Sema/LifetimeDependence.cpp renamed to lib/AST/LifetimeDependence.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "TypeChecker.h"
14-
13+
#include "swift/AST/LifetimeDependence.h"
1514
#include "swift/AST/ASTContext.h"
1615
#include "swift/AST/Decl.h"
17-
#include "swift/AST/LifetimeDependence.h"
16+
#include "swift/AST/DiagnosticsSema.h"
17+
#include "swift/AST/Module.h"
1818
#include "swift/AST/ParameterList.h"
1919
#include "swift/AST/Type.h"
2020
#include "swift/AST/TypeRepr.h"
@@ -165,13 +165,25 @@ static LifetimeDependenceKind getLifetimeDependenceKindFromDecl(
165165
return LifetimeDependenceKind::Scope;
166166
}
167167
if (parsedLifetimeDependenceKind == ParsedLifetimeDependenceKind::Inherit) {
168-
// TODO: assert that this can happen only on deserialized decls
168+
// TODO: assert that this can happen in SIL tests
169169
return LifetimeDependenceKind::Inherit;
170170
}
171171
return paramType->isEscapable() ? LifetimeDependenceKind::Scope
172172
: LifetimeDependenceKind::Inherit;
173173
}
174174

175+
static bool isBitwiseCopyable(Type type, ModuleDecl *mod, ASTContext &ctx) {
176+
if (!ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
177+
return false;
178+
}
179+
auto *bitwiseCopyableProtocol =
180+
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
181+
if (!bitwiseCopyableProtocol) {
182+
return false;
183+
}
184+
return (bool)(mod->checkConformance(type, bitwiseCopyableProtocol));
185+
}
186+
175187
std::optional<LifetimeDependenceInfo>
176188
LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
177189
auto *dc = afd->getDeclContext();
@@ -205,14 +217,9 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
205217
// error.
206218
// TODO: Diagnose ~Escapable types are always non-trivial in SIL.
207219
if (paramType->isEscapable()) {
208-
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
209-
auto *bitwiseCopyableProtocol =
210-
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
211-
if (bitwiseCopyableProtocol &&
212-
mod->checkConformance(paramType, bitwiseCopyableProtocol)) {
213-
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
214-
return true;
215-
}
220+
if (isBitwiseCopyable(paramType, mod, ctx)) {
221+
diags.diagnose(loc, diag::lifetime_dependence_on_bitwise_copyable);
222+
return true;
216223
}
217224
}
218225

@@ -277,7 +284,7 @@ LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd) {
277284
}
278285
case LifetimeDependenceSpecifier::SpecifierKind::Ordered: {
279286
auto index = specifier.getIndex();
280-
if (index > afd->getParameters()->size()) {
287+
if (index >= afd->getParameters()->size()) {
281288
diags.diagnose(specifier.getLoc(),
282289
diag::lifetime_dependence_invalid_param_index, index);
283290
return std::nullopt;
@@ -426,16 +433,11 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
426433
if (!cd && afd->hasImplicitSelfDecl()) {
427434
Type selfTypeInContext = dc->getSelfTypeInContext();
428435
if (selfTypeInContext->isEscapable()) {
429-
if (ctx.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
430-
auto *bitwiseCopyableProtocol =
431-
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
432-
if (bitwiseCopyableProtocol &&
433-
mod->checkConformance(selfTypeInContext, bitwiseCopyableProtocol)) {
436+
if (isBitwiseCopyable(selfTypeInContext, mod, ctx)) {
434437
diags.diagnose(
435438
returnLoc,
436439
diag::lifetime_dependence_method_escapable_bitwisecopyable_self);
437440
return std::nullopt;
438-
}
439441
}
440442
}
441443
auto kind = getLifetimeDependenceKindFromType(selfTypeInContext);
@@ -455,23 +457,26 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
455457
unsigned paramIndex = 0;
456458
bool hasParamError = false;
457459
for (auto *param : *afd->getParameters()) {
458-
SWIFT_DEFER {
459-
paramIndex++;
460-
};
460+
SWIFT_DEFER { paramIndex++; };
461461
Type paramTypeInContext =
462462
afd->mapTypeIntoContext(param->getInterfaceType());
463463
if (paramTypeInContext->hasError()) {
464464
hasParamError = true;
465465
continue;
466466
}
467467
auto paramOwnership = param->getValueOwnership();
468-
if (paramTypeInContext->isEscapable() && paramOwnership == ValueOwnership::Default) {
469-
continue;
468+
if (paramTypeInContext->isEscapable()) {
469+
if (isBitwiseCopyable(paramTypeInContext, mod, ctx)) {
470+
continue;
471+
}
472+
if (paramOwnership == ValueOwnership::Default) {
473+
continue;
474+
}
470475
}
471476

472477
auto lifetimeKind = getLifetimeDependenceKindFromType(paramTypeInContext);
473-
if (!isLifetimeDependenceCompatibleWithOwnership(lifetimeKind, paramOwnership,
474-
afd)) {
478+
if (!isLifetimeDependenceCompatibleWithOwnership(lifetimeKind,
479+
paramOwnership, afd)) {
475480
continue;
476481
}
477482
if (candidateParam) {
@@ -496,12 +501,11 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
496501
if (cd && afd->isImplicit()) {
497502
diags.diagnose(returnLoc,
498503
diag::lifetime_dependence_cannot_infer_no_candidates,
499-
"on implicit initializer");
504+
" on implicit initializer");
500505
return std::nullopt;
501506
}
502507
diags.diagnose(returnLoc,
503-
diag::lifetime_dependence_cannot_infer_no_candidates,
504-
"");
508+
diag::lifetime_dependence_cannot_infer_no_candidates, "");
505509
return std::nullopt;
506510
}
507511
return lifetimeDependenceInfo;

lib/Sema/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ add_swift_host_library(swiftSema STATIC
3535
DerivedConformances.cpp
3636
ImportResolution.cpp
3737
InstrumenterSupport.cpp
38-
LifetimeDependence.cpp
3938
LookupVisibleDecls.cpp
4039
MiscDiagnostics.cpp
4140
PCMacro.cpp

test/Sema/explicit_lifetime_dependence_specifiers2.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ struct AnotherBufferView : ~Escapable, _BitwiseCopyable {
1212

1313
struct BufferView : ~Escapable {
1414
let ptr: UnsafeRawBufferPointer
15-
init(_ bv: borrowing AnotherBufferView) -> _borrow(bv) Self {
15+
init(_ bv: borrowing AnotherBufferView) -> dependsOn(bv) Self {
1616
self.ptr = bv.ptr
1717
return self
1818
}
1919
}
2020

21+
struct NonescapableType: ~Escapable {}
22+
23+
func f<T: ~Escapable & _BitwiseCopyable>(arg: T) -> NonescapableType {
24+
NonescapableType()
25+
}

test/Sema/implicit_lifetime_dependence.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NonescapableTypes -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature BitwiseCopyable
22
// REQUIRES: asserts
33

44
struct BufferView : ~Escapable, ~Copyable {
@@ -24,7 +24,15 @@ struct ImplicitInit3 : ~Escapable, ~Copyable { // expected-error{{cannot infer l
2424
let mbv2: BufferView
2525
}
2626

27-
func foo() -> BufferView { // expected-error{{cannot infer lifetime dependence , no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
27+
func foo1() -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
28+
return BufferView(nil, 0)
29+
}
30+
31+
func foo2(_ i: borrowing Int) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
32+
return BufferView(nil, 0)
33+
}
34+
35+
func foo3<T: _BitwiseCopyable>(arg: borrowing T) -> BufferView { // expected-error{{cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership}}
2836
return BufferView(nil, 0)
2937
}
3038

0 commit comments

Comments
 (0)