Skip to content

[cxx-interop] Do not require the LifetimeDependence feature in _SwiftifyImport #79911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ class SourceManager {
/// translated.
SourceLoc getLocForForeignLoc(SourceLoc otherLoc, SourceManager &otherMgr);

/// Returns true when the location is in a buffer generated by the
/// \p _SwiftifyImport macro.
bool isImportMacroGeneratedLoc(SourceLoc loc);

private:
int getLineOffset(SourceLoc Loc) const {
if (auto VFile = getVirtualFile(Loc))
Expand Down
4 changes: 3 additions & 1 deletion lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/SourceManager.h"

namespace swift {

Expand Down Expand Up @@ -576,7 +577,8 @@ LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
}
}

if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence)) {
if (!ctx.LangOpts.hasFeature(Feature::LifetimeDependence) &&
!ctx.SourceMgr.isImportMacroGeneratedLoc(returnLoc)) {
diags.diagnose(returnLoc, diag::lifetime_dependence_feature_required);
return std::nullopt;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Basic/SourceLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,15 @@ bool SourceManager::encloses(SourceRange enclosing, SourceRange inner) const {
return containsLoc(enclosing, inner.Start) &&
isAtOrBefore(inner.End, enclosing.End);
}

bool SourceManager::isImportMacroGeneratedLoc(SourceLoc loc) {
if (loc.isInvalid())
return false;

auto buffer = findBufferContainingLoc(loc);
auto genInfo = getGeneratedSourceInfo(buffer);
if (genInfo && genInfo->macroName == "_SwiftifyImport")
return true;

return false;
}
4 changes: 3 additions & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "swift/AST/SourceFile.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Bridging/ASTGen.h"
Expand Down Expand Up @@ -2556,7 +2557,8 @@ parseLifetimeDescriptor(Parser &P,
ParserResult<LifetimeAttr> Parser::parseLifetimeAttribute(SourceLoc atLoc,
SourceLoc loc) {
ParserStatus status;
if (!Context.LangOpts.hasFeature(Feature::LifetimeDependence)) {
if (!Context.LangOpts.hasFeature(Feature::LifetimeDependence) &&
!Context.SourceMgr.isImportMacroGeneratedLoc(atLoc)) {
diagnose(loc, diag::requires_experimental_feature, "@lifetime", false,
getFeatureName(Feature::LifetimeDependence));
status.setIsParseError();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

// REQUIRES: swift_swift_parser
// REQUIRES: swift_feature_LifetimeDependence

// 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
// 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

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