Skip to content

Initial language support for lifetime dependence #71069

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 5 commits into from
Jan 25, 2024
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
14 changes: 14 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2166,5 +2166,19 @@ ERROR(requires_experimental_feature, none,
"%2 is enabled",
(StringRef, bool, StringRef))

ERROR(expected_lparen_after_lifetime_dependence, PointsToFirstBadToken,
"expected '(' after lifetime dependence specifier", ())

ERROR(expected_identifier_or_index_or_self_after_lifetime_dependence,
PointsToFirstBadToken,
"expected identifier or index or self in lifetime dependence specifier",
())

ERROR(expected_rparen_after_lifetime_dependence, PointsToFirstBadToken,
"expected ')' after param list in lifetime dependence specifier", ())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"expected ')' after param list in lifetime dependence specifier", ())
"expected ')' after parameter list in lifetime dependence specifier", ())

Also: I don’ t think we have a test case for this diagnostic.


ERROR(expected_param_index_lifetime_dependence, PointsToFirstBadToken,
"expected unsigned parameter index in lifetime dependence specifier", ())

#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
27 changes: 27 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7765,5 +7765,32 @@ ERROR(result_depends_on_no_result,none,
ERROR(pack_iteration_where_clause_not_supported, none,
"'where' clause in pack iteration is not supported", ())


//------------------------------------------------------------------------------
// MARK: Lifetime Dependence Diagnostics
//------------------------------------------------------------------------------

ERROR(lifetime_dependence_invalid_param_name, none,
"invalid parameter name specified %0", (Identifier))
ERROR(lifetime_dependence_invalid_param_index, none,
"invalid parameter index specified %0", (unsigned))
ERROR(lifetime_dependence_invalid_self, none,
"invalid lifetime dependence specifier, self is valid in non-static "
"methods only", ())
ERROR(lifetime_dependence_duplicate_param_id, none,
"duplicate lifetime dependence specifier", ())
ERROR(lifetime_dependence_cannot_use_kind, none,
"invalid use of %0 lifetime dependence for %1 ownership",
(StringRef, StringRef))
ERROR(lifetime_dependence_only_on_function_method_init_result, none,
"lifetime dependence specifiers may only be used on result of "
"functions, methods, initializers",
())
ERROR(lifetime_dependence_invalid_return_type, none,
"lifetime dependence specifiers can only be specifier on ~Escapable "
"results", ())
ERROR(lifetime_dependence_missing_ownership_modifier, none,
"lifetime dependence can only be specified on parameters with ownership "
"modifiers (borrowing, consuming, inout)", ())
#define UNDEFINE_DIAGNOSTIC_MACROS
#include "DefineDiagnosticMacros.h"
112 changes: 80 additions & 32 deletions include/swift/AST/ExtInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ class ClangTypeInfo {
void dump(llvm::raw_ostream &os, const clang::ASTContext &ctx) const;
};

class LifetimeDependenceInfo {
IndexSubset *copyLifetimeParamIndices;
IndexSubset *borrowLifetimeParamIndices;

public:
LifetimeDependenceInfo()
: copyLifetimeParamIndices(nullptr), borrowLifetimeParamIndices(nullptr) {
}
LifetimeDependenceInfo(IndexSubset *copyLifetimeParamIndices,
IndexSubset *borrowLifetimeParamIndices)
: copyLifetimeParamIndices(copyLifetimeParamIndices),
borrowLifetimeParamIndices(borrowLifetimeParamIndices) {}

operator bool() const { return empty(); }

bool empty() const {
return copyLifetimeParamIndices == nullptr &&
borrowLifetimeParamIndices == nullptr;
}
};

// MARK: - UnexpectedClangTypeError
/// Potential errors when trying to store a Clang type in an ExtInfo.
struct UnexpectedClangTypeError {
Expand Down Expand Up @@ -369,42 +390,47 @@ class ASTExtInfoBuilder {
unsigned bits; // Naturally sized for speed.

ClangTypeInfo clangTypeInfo;

Type globalActor;
Type thrownError;

LifetimeDependenceInfo lifetimeDependenceInfo;

using Representation = FunctionTypeRepresentation;

ASTExtInfoBuilder(
unsigned bits, ClangTypeInfo clangTypeInfo, Type globalActor,
Type thrownError
) : bits(bits), clangTypeInfo(clangTypeInfo), globalActor(globalActor),
thrownError(thrownError) {}
ASTExtInfoBuilder(unsigned bits, ClangTypeInfo clangTypeInfo,
Type globalActor, Type thrownError,
LifetimeDependenceInfo lifetimeDependenceInfo)
: bits(bits), clangTypeInfo(clangTypeInfo), globalActor(globalActor),
thrownError(thrownError),
lifetimeDependenceInfo(lifetimeDependenceInfo) {}

public:
/// An ExtInfoBuilder for a typical Swift function: @convention(swift),
/// @escaping, non-throwing, non-differentiable.
ASTExtInfoBuilder()
: ASTExtInfoBuilder(Representation::Swift, false, false, Type(),
DifferentiabilityKind::NonDifferentiable, nullptr,
Type()) {}
Type(), LifetimeDependenceInfo()) {}

// Constructor for polymorphic type.
ASTExtInfoBuilder(Representation rep, bool throws, Type thrownError)
: ASTExtInfoBuilder(rep, false, throws, thrownError,
DifferentiabilityKind::NonDifferentiable, nullptr,
Type()) {}
Type(), LifetimeDependenceInfo()) {}

// Constructor with no defaults.
ASTExtInfoBuilder(Representation rep, bool isNoEscape, bool throws,
Type thrownError,
DifferentiabilityKind diffKind, const clang::Type *type,
Type globalActor)
Type thrownError, DifferentiabilityKind diffKind,
const clang::Type *type, Type globalActor,
LifetimeDependenceInfo lifetimeDependenceInfo)
: ASTExtInfoBuilder(
((unsigned)rep) | (isNoEscape ? NoEscapeMask : 0) |
(throws ? ThrowsMask : 0) |
(((unsigned)diffKind << DifferentiabilityMaskOffset) &
DifferentiabilityMask),
ClangTypeInfo(type), globalActor, thrownError) {}
ClangTypeInfo(type), globalActor, thrownError,
lifetimeDependenceInfo) {}

void checkInvariants() const;

Expand Down Expand Up @@ -444,6 +470,10 @@ class ASTExtInfoBuilder {
Type getGlobalActor() const { return globalActor; }
Type getThrownError() const { return thrownError; }

LifetimeDependenceInfo getLifetimeDependenceInfo() const {
return lifetimeDependenceInfo;
}

constexpr bool hasSelfParam() const {
switch (getSILRepresentation()) {
case SILFunctionTypeRepresentation::Thick:
Expand Down Expand Up @@ -477,31 +507,31 @@ class ASTExtInfoBuilder {
return ASTExtInfoBuilder((bits & ~RepresentationMask) | (unsigned)rep,
shouldStoreClangType(rep) ? clangTypeInfo
: ClangTypeInfo(),
globalActor, thrownError);
globalActor, thrownError, lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withNoEscape(bool noEscape = true) const {
return ASTExtInfoBuilder(noEscape ? (bits | NoEscapeMask)
: (bits & ~NoEscapeMask),
clangTypeInfo, globalActor, thrownError);
return ASTExtInfoBuilder(
noEscape ? (bits | NoEscapeMask) : (bits & ~NoEscapeMask),
clangTypeInfo, globalActor, thrownError, lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withConcurrent(bool concurrent = true) const {
return ASTExtInfoBuilder(concurrent ? (bits | SendableMask)
: (bits & ~SendableMask),
clangTypeInfo, globalActor, thrownError);
return ASTExtInfoBuilder(
concurrent ? (bits | SendableMask) : (bits & ~SendableMask),
clangTypeInfo, globalActor, thrownError, lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withAsync(bool async = true) const {
return ASTExtInfoBuilder(async ? (bits | AsyncMask)
: (bits & ~AsyncMask),
clangTypeInfo, globalActor, thrownError);
return ASTExtInfoBuilder(async ? (bits | AsyncMask) : (bits & ~AsyncMask),
clangTypeInfo, globalActor, thrownError,
lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withThrows(bool throws, Type thrownError) const {
return ASTExtInfoBuilder(
throws ? (bits | ThrowsMask) : (bits & ~ThrowsMask), clangTypeInfo,
globalActor, thrownError);
globalActor, thrownError, lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withThrows() const {
Expand All @@ -513,12 +543,12 @@ class ASTExtInfoBuilder {
return ASTExtInfoBuilder(
(bits & ~DifferentiabilityMask) |
((unsigned)differentiability << DifferentiabilityMaskOffset),
clangTypeInfo, globalActor, thrownError);
clangTypeInfo, globalActor, thrownError, lifetimeDependenceInfo);
}
[[nodiscard]]
ASTExtInfoBuilder withClangFunctionType(const clang::Type *type) const {
return ASTExtInfoBuilder(
bits, ClangTypeInfo(type), globalActor, thrownError);
return ASTExtInfoBuilder(bits, ClangTypeInfo(type), globalActor,
thrownError, lifetimeDependenceInfo);
}

/// Put a SIL representation in the ExtInfo.
Expand All @@ -532,19 +562,27 @@ class ASTExtInfoBuilder {
return ASTExtInfoBuilder((bits & ~RepresentationMask) | (unsigned)rep,
shouldStoreClangType(rep) ? clangTypeInfo
: ClangTypeInfo(),
globalActor, thrownError);
globalActor, thrownError, lifetimeDependenceInfo);
}

[[nodiscard]]
ASTExtInfoBuilder withGlobalActor(Type globalActor) const {
return ASTExtInfoBuilder(bits, clangTypeInfo, globalActor, thrownError);
return ASTExtInfoBuilder(bits, clangTypeInfo, globalActor, thrownError,
lifetimeDependenceInfo);
}

[[nodiscard]] ASTExtInfoBuilder withLifetimeDependenceInfo(
LifetimeDependenceInfo lifetimeDependenceInfo) const {
return ASTExtInfoBuilder(bits, clangTypeInfo, globalActor, thrownError,
lifetimeDependenceInfo);
}

bool isEqualTo(ASTExtInfoBuilder other, bool useClangTypes) const {
return bits == other.bits &&
(useClangTypes ? (clangTypeInfo == other.clangTypeInfo) : true) &&
globalActor.getPointer() == other.globalActor.getPointer() &&
thrownError.getPointer() == other.thrownError.getPointer();
(useClangTypes ? (clangTypeInfo == other.clangTypeInfo) : true) &&
globalActor.getPointer() == other.globalActor.getPointer() &&
thrownError.getPointer() == other.thrownError.getPointer() &&
lifetimeDependenceInfo == other.lifetimeDependenceInfo;
}

constexpr std::tuple<unsigned, const void *, const void *, const void *>
Expand Down Expand Up @@ -573,8 +611,9 @@ class ASTExtInfo {
ASTExtInfo(ASTExtInfoBuilder builder) : builder(builder) {}

ASTExtInfo(unsigned bits, ClangTypeInfo clangTypeInfo, Type globalActor,
Type thrownError)
: builder(bits, clangTypeInfo, globalActor, thrownError) {
Type thrownError, LifetimeDependenceInfo lifetimeDependenceInfo)
: builder(bits, clangTypeInfo, globalActor, thrownError,
lifetimeDependenceInfo) {
builder.checkInvariants();
};

Expand Down Expand Up @@ -621,6 +660,10 @@ class ASTExtInfo {
Type getGlobalActor() const { return builder.getGlobalActor(); }
Type getThrownError() const { return builder.getThrownError(); }

LifetimeDependenceInfo getLifetimeDependenceInfo() const {
return builder.getLifetimeDependenceInfo();
}

/// Helper method for changing the representation.
///
/// Prefer using \c ASTExtInfoBuilder::withRepresentation for chaining.
Expand Down Expand Up @@ -674,6 +717,11 @@ class ASTExtInfo {
return builder.withGlobalActor(globalActor).build();
}

[[nodiscard]] ASTExtInfo withLifetimeDependenceInfo(
LifetimeDependenceInfo lifetimeDependenceInfo) const {
return builder.withLifetimeDependenceInfo(lifetimeDependenceInfo).build();
}

bool isEqualTo(ASTExtInfo other, bool useClangTypes) const {
return builder.isEqualTo(other.builder, useClangTypes);
}
Expand Down
110 changes: 110 additions & 0 deletions include/swift/AST/LifetimeDependence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//===--- LifetimeDependenceSpecifiers.h ------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines types and utilities related to Lifetime Dependence
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_AST_LIFETIMEDEPENDENCE_H
#define SWIFT_AST_LIFETIMEDEPENDENCE_H

#include "swift/AST/Decl.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/OptionSet.h"
#include "llvm/Support/TrailingObjects.h"

namespace swift {

enum class LifetimeDependenceKind : uint8_t {
Copy = 0,
Consume,
Borrow,
Mutate
};

class LifetimeDependenceSpecifier {
public:
enum class SpecifierKind { Named, Ordered, Self };

private:
SourceLoc loc;
SpecifierKind specifierKind;
LifetimeDependenceKind lifetimeDependenceKind;
union Value {
struct {
Identifier name;
} Named;
struct {
unsigned index;
} Ordered;
struct {
} self;
Value(Identifier name) : Named({name}) {}
Value(unsigned index) : Ordered({index}) {}
Value() {}
} value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this could we take advantage of inline bit fields here? We'll get a NamedSpecifier, OrderedSpecifier or SelfSpecifier sublcasses and share trailing storage to store name/id similar to #70998?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I do this as a follow on commit ?


LifetimeDependenceSpecifier(SourceLoc loc, SpecifierKind specifierKind,
LifetimeDependenceKind lifetimeDependenceKind,
Value value)
: loc(loc), specifierKind(specifierKind),
lifetimeDependenceKind(lifetimeDependenceKind), value(value) {}

public:
static LifetimeDependenceSpecifier getNamedLifetimeDependenceSpecifier(
SourceLoc loc, LifetimeDependenceKind kind, Identifier name) {
return {loc, SpecifierKind::Named, kind, name};
}

static LifetimeDependenceSpecifier getOrderedLifetimeDependenceSpecifier(
SourceLoc loc, LifetimeDependenceKind kind, unsigned index) {
return {loc, SpecifierKind::Ordered, kind, index};
}

static LifetimeDependenceSpecifier
getSelfLifetimeDependenceSpecifier(SourceLoc loc,
LifetimeDependenceKind kind) {
return {loc, SpecifierKind::Self, kind, {}};
}

SourceLoc getLoc() const { return loc; }

SpecifierKind getSpecifierKind() const { return specifierKind; }

LifetimeDependenceKind getLifetimeDependenceKind() const {
return lifetimeDependenceKind;
}

Identifier getName() const {
assert(specifierKind == SpecifierKind::Named);
return value.Named.name;
}

unsigned getIndex() const {
assert(specifierKind == SpecifierKind::Ordered);
return value.Ordered.index;
}
std::string getParamString() const {
switch (specifierKind) {
case SpecifierKind::Named:
return value.Named.name.str().str();
case SpecifierKind::Self:
return "self";
case SpecifierKind::Ordered:
return std::to_string(value.Ordered.index);
}
llvm_unreachable("Invalid LifetimeDependenceSpecifier::SpecifierKind");
}
};
} // namespace swift

#endif
Loading