Skip to content

Commit 22e2276

Browse files
author
Gabor Horvath
committed
[cxx-interop] Do not require the LifetimeDependence feature in _SwiftifyImport
We use experimental features to let people know that the construct is subject to change and users should not rely on this unless they are willing to rewrite the uses of this feature later. However, in compiler generated code everything should be fair game, we will update the compiler when these features change. This is a requirement to be able to turn safe wrapper generation on by default.
1 parent 5a92bc5 commit 22e2276

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,10 @@ class SourceManager {
550550
/// translated.
551551
SourceLoc getLocForForeignLoc(SourceLoc otherLoc, SourceManager &otherMgr);
552552

553+
/// Returns true when the location is in a buffer generated by the
554+
/// \p _SwiftifyImport macro.
555+
bool isImportMacroGeneratedLoc(SourceLoc loc);
556+
553557
private:
554558
int getLineOffset(SourceLoc Loc) const {
555559
if (auto VFile = getVirtualFile(Loc))

lib/AST/LifetimeDependence.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Basic/Assertions.h"
2424
#include "swift/Basic/Defer.h"
2525
#include "swift/Basic/Range.h"
26+
#include "swift/Basic/SourceManager.h"
2627

2728
namespace swift {
2829

@@ -576,7 +577,8 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
576577
}
577578
}
578579

579-
if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence)) {
580+
if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence) &&
581+
!ctx.SourceMgr.isImportMacroGeneratedLoc(returnLoc)) {
580582
diags.diagnose(returnLoc, diag::lifetime_dependence_feature_required);
581583
return std::nullopt;
582584
}

lib/Basic/SourceLoc.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,3 +865,15 @@ bool SourceManager::encloses(SourceRange enclosing, SourceRange inner) const {
865865
return containsLoc(enclosing, inner.Start) &&
866866
isAtOrBefore(inner.End, enclosing.End);
867867
}
868+
869+
bool SourceManager::isImportMacroGeneratedLoc(SourceLoc loc) {
870+
if (loc.isInvalid())
871+
return false;
872+
873+
auto buffer = findBufferContainingLoc(loc);
874+
auto genInfo = getGeneratedSourceInfo(buffer);
875+
if (genInfo && genInfo->macroName == "_SwiftifyImport")
876+
return true;
877+
878+
return false;
879+
}

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/AST/SourceFile.h"
3232
#include "swift/Basic/Assertions.h"
3333
#include "swift/Basic/Defer.h"
34+
#include "swift/Basic/SourceManager.h"
3435
#include "swift/Basic/Statistic.h"
3536
#include "swift/Basic/StringExtras.h"
3637
#include "swift/Bridging/ASTGen.h"
@@ -2556,7 +2557,8 @@ parseLifetimeDescriptor(Parser &P,
25562557
ParserResult<LifetimeAttr> Parser::parseLifetimeAttribute(SourceLoc atLoc,
25572558
SourceLoc loc) {
25582559
ParserStatus status;
2559-
if (!Context.LangOpts.hasFeature(Feature::LifetimeDependence)) {
2560+
if (!Context.LangOpts.hasFeature(Feature::LifetimeDependence) &&
2561+
!Context.SourceMgr.isImportMacroGeneratedLoc(atLoc)) {
25602562
diagnose(loc, diag::requires_experimental_feature, "@lifetime", false,
25612563
getFeatureName(Feature::LifetimeDependence));
25622564
status.setIsParseError();

test/Macros/SwiftifyImport/CxxSpan/LifetimeboundSpan.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
// REQUIRES: swift_swift_parser
3-
// REQUIRES: swift_feature_LifetimeDependence
43

5-
// RUN: %target-swift-frontend %s -enable-experimental-cxx-interop -I %S/Inputs -Xcc -std=c++20 -swift-version 5 -module-name main -disable-availability-checking -typecheck -enable-experimental-feature LifetimeDependence -plugin-path %swift-plugin-dir -dump-macro-expansions -verify -strict-memory-safety 2>&1 | %FileCheck --match-full-lines %s
4+
// RUN: %target-swift-frontend %s -enable-experimental-cxx-interop -I %S/Inputs -Xcc -std=c++20 -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -verify -strict-memory-safety 2>&1 | %FileCheck --match-full-lines %s
65

76
// FIXME swift-ci linux tests do not support std::span
87
// UNSUPPORTED: OS=linux-gnu

0 commit comments

Comments
 (0)