Skip to content

Add support for _resultDependsOn #70263

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 2 commits into from
Dec 7, 2023
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
1 change: 1 addition & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : size_t {
BridgedAttributedTypeSpecifierLegacyOwned,
BridgedAttributedTypeSpecifierConst,
BridgedAttributedTypeSpecifierIsolated,
BridgedAttributedTypeSpecifierResultDependsOn,
};

SWIFT_NAME("BridgedSimpleIdentTypeRepr.createParsed(_:loc:name:)")
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ SIMPLE_DECL_ATTR(_noExistentials, NoExistentials,
SIMPLE_DECL_ATTR(_noObjCBridging, NoObjCBridging,
OnAbstractFunction | OnSubscript | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
155)

CONTEXTUAL_SIMPLE_DECL_ATTR(_resultDependsOn, ResultDependsOn,
OnParam | DeclModifier | UserInaccessible | ABIBreakingToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
156)
#undef TYPE_ATTR
#undef DECL_ATTR_ALIAS
#undef CONTEXTUAL_DECL_ATTR_ALIAS
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6424,6 +6424,9 @@ class ParamDecl : public VarDecl {

/// Whether or not this parameter is 'isolated'.
IsIsolated = 1 << 2,

/// Whether or not this paramater is '_resultDependsOn'
IsResultDependsOn = 1 << 3,
};

/// The default value, if any, along with flags.
Expand Down Expand Up @@ -6637,6 +6640,16 @@ class ParamDecl : public VarDecl {
ArgumentNameAndFlags.setInt(flags);
}

bool hasResultDependsOn() const {
return DefaultValueAndFlags.getInt().contains(Flags::IsResultDependsOn);
}

void setResultDependsOn(bool value = true) {
auto flags = DefaultValueAndFlags.getInt();
DefaultValueAndFlags.setInt(value ? flags | Flags::IsResultDependsOn
: flags - Flags::IsResultDependsOn);
}

/// Does this parameter reject temporary pointer conversions?
bool isNonEphemeral() const;

Expand Down
16 changes: 16 additions & 0 deletions include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,21 @@ class CompileTimeConstTypeRepr : public SpecifierTypeRepr {
static bool classof(const CompileTimeConstTypeRepr *T) { return true; }
};

/// A lifetime dependent type.
/// \code
/// x : _resultDependsOn Int
/// \endcode
class ResultDependsOnTypeRepr : public SpecifierTypeRepr {
public:
ResultDependsOnTypeRepr(TypeRepr *Base, SourceLoc InOutLoc)
: SpecifierTypeRepr(TypeReprKind::ResultDependsOn, Base, InOutLoc) {}

static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::ResultDependsOn;
}
static bool classof(const ResultDependsOnTypeRepr *T) { return true; }
};

/// A TypeRepr for a known, fixed type.
///
/// Fixed type representations should be used sparingly, in places
Expand Down Expand Up @@ -1522,6 +1537,7 @@ inline bool TypeRepr::isSimple() const {
case TypeReprKind::Isolated:
case TypeReprKind::Placeholder:
case TypeReprKind::CompileTimeConst:
case TypeReprKind::ResultDependsOn:
return true;
}
llvm_unreachable("bad TypeRepr kind");
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/TypeReprNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ABSTRACT_TYPEREPR(Specifier, TypeRepr)
SPECIFIER_TYPEREPR(Ownership, SpecifierTypeRepr)
SPECIFIER_TYPEREPR(Isolated, SpecifierTypeRepr)
SPECIFIER_TYPEREPR(CompileTimeConst, SpecifierTypeRepr)
SPECIFIER_TYPEREPR(ResultDependsOn, SpecifierTypeRepr)
TYPEREPR(Fixed, TypeRepr)
TYPEREPR(SILBox, TypeRepr)
TYPEREPR(Self, TypeRepr)
Expand Down
16 changes: 9 additions & 7 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2228,23 +2228,24 @@ enum class ParamSpecifier : uint8_t {
/// `__shared`, a legacy spelling of `borrowing`.
LegacyShared = 4,
/// `__owned`, a legacy spelling of `consuming`.
LegacyOwned = 5,
LegacyOwned = 5
};

/// Provide parameter type relevant flags, i.e. variadic, autoclosure, and
/// escaping.
class ParameterTypeFlags {
enum ParameterFlags : uint16_t {
None = 0,
Variadic = 1 << 0,
AutoClosure = 1 << 1,
None = 0,
Variadic = 1 << 0,
AutoClosure = 1 << 1,
NonEphemeral = 1 << 2,
SpecifierShift = 3,
Specifier = 7 << SpecifierShift,
Specifier = 7 << SpecifierShift,
NoDerivative = 1 << 6,
Isolated = 1 << 7,
Isolated = 1 << 7,
CompileTimeConst = 1 << 8,
NumBits = 9
ResultDependsOn = 1 << 9,
NumBits = 10
};
OptionSet<ParameterFlags> value;
static_assert(NumBits <= 8*sizeof(OptionSet<ParameterFlags>), "overflowed");
Expand Down Expand Up @@ -2284,6 +2285,7 @@ class ParameterTypeFlags {
bool isIsolated() const { return value.contains(Isolated); }
bool isCompileTimeConst() const { return value.contains(CompileTimeConst); }
bool isNoDerivative() const { return value.contains(NoDerivative); }
bool hasResultDependsOn() const { return value.contains(ResultDependsOn); }

/// Get the spelling of the parameter specifier used on the parameter.
ParamSpecifier getOwnershipSpecifier() const {
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1310,4 +1310,9 @@ def platform_c_calling_convention :
def platform_c_calling_convention_EQ :
Joined<["-"], "experimental-platform-c-calling-convention=">,
Alias<platform_c_calling_convention>;

def disable_experimental_parser_round_trip : Flag<["-"],
"disable-experimental-parser-round-trip">,
HelpText<"Disable round trip through the new swift parser">;

} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
13 changes: 10 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ class Parser {
SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc,
SourceLoc &ConstLoc,
SourceLoc &ResultDependsOnLoc,
TypeAttributes &Attributes) {
if (Tok.isAny(tok::at_sign, tok::kw_inout) ||
(canHaveParameterSpecifierContextualKeyword() &&
Expand All @@ -1205,16 +1206,19 @@ class Parser {
Tok.getRawText().equals("consuming") ||
Tok.getRawText().equals("borrowing") ||
Tok.isContextualKeyword("isolated") ||
Tok.isContextualKeyword("_const"))))
return parseTypeAttributeListPresent(
Specifier, SpecifierLoc, IsolatedLoc, ConstLoc, Attributes);
Tok.isContextualKeyword("_const") ||
Tok.getRawText().equals("_resultDependsOn"))))
return parseTypeAttributeListPresent(Specifier, SpecifierLoc, IsolatedLoc,
ConstLoc, ResultDependsOnLoc,
Attributes);
return makeParserSuccess();
}

ParserStatus parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc,
SourceLoc &ConstLoc,
SourceLoc &ResultDependsOnLoc,
TypeAttributes &Attributes);

bool parseConventionAttributeInternal(bool justChecking,
Expand Down Expand Up @@ -1510,6 +1514,9 @@ class Parser {
/// The location of the '_const' keyword, if present.
SourceLoc CompileConstLoc;

/// The location of the '_resultDependsOn' keyword, if present.
SourceLoc ResultDependsOnLoc;

/// The type following the ':'.
TypeRepr *Type = nullptr;

Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,9 @@ BridgedTypeRepr BridgedSpecifierTypeRepr_createParsed(
case BridgedAttributedTypeSpecifierIsolated: {
return new (context) IsolatedTypeRepr(baseType, loc);
}
case BridgedAttributedTypeSpecifierResultDependsOn: {
return new (context) ResultDependsOnTypeRepr(baseType, loc);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3385,6 +3385,13 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, StringRef>,
printFoot();
}

void visitResultDependsOnTypeRepr(ResultDependsOnTypeRepr *T,
StringRef label) {
printCommon("_resultDependsOn", label);
printRec(T->getBase());
printFoot();
}

void visitOptionalTypeRepr(OptionalTypeRepr *T, StringRef label) {
printCommon("type_optional", label);
printRec(T->getBase());
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3605,6 +3605,10 @@ static bool usesFeatureNonEscapableTypes(Decl *decl) {
if (fd && fd->getAttrs().getAttribute(DAK_ResultDependsOnSelf)) {
return true;
}
auto *pd = dyn_cast<ParamDecl>(decl);
if (pd && pd->hasResultDependsOn()) {
return true;
}
return false;
}

Expand Down Expand Up @@ -4360,6 +4364,9 @@ static void printParameterFlags(ASTPrinter &printer,
if (flags.isIsolated())
printer.printKeyword("isolated", options, " ");

if (flags.hasResultDependsOn())
printer.printKeyword("resultDependsOn", options, " ");

if (!options.excludeAttrKind(TAK_escaping) && escaping)
printer.printKeyword("@escaping", options, " ");

Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,10 @@ bool Traversal::visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T) {
return doIt(T->getBase());
}

bool Traversal::visitResultDependsOnTypeRepr(ResultDependsOnTypeRepr *T) {
return doIt(T->getBase());
}

bool Traversal::visitOpaqueReturnTypeRepr(OpaqueReturnTypeRepr *T) {
return doIt(T->getConstraint());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7671,7 +7671,7 @@ void ParamDecl::setSpecifier(Specifier specifier) {
VarDecl::Introducer introducer;
switch (specifier) {
// Unannotated or `borrowing` parameters are locally immutable.
// So are parameters using the legacy `__shared` or `__owned` modifiers.
// So are parameters using the legacy `__shared` or `__owned` modifiers.
case ParamSpecifier::Default:
case ParamSpecifier::Borrowing:
case ParamSpecifier::LegacyShared:
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3022,12 +3022,11 @@ directReferencesForTypeRepr(Evaluator &evaluator,
case TypeReprKind::SILBox:
case TypeReprKind::Placeholder:
case TypeReprKind::Pack:
return { };

case TypeReprKind::OpaqueReturn:
case TypeReprKind::NamedOpaqueReturn:
case TypeReprKind::Existential:
case TypeReprKind::Inverse:
case TypeReprKind::ResultDependsOn:
return { };

case TypeReprKind::Fixed:
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ void SpecifierTypeRepr::printImpl(ASTPrinter &Printer,
case TypeReprKind::CompileTimeConst:
Printer.printKeyword("_const", Opts, " ");
break;
case TypeReprKind::ResultDependsOn:
Printer.printKeyword("_resultDependsOn", Opts, " ");
break;
}
printTypeRepr(Base, Printer, Opts);
}
Expand Down
1 change: 1 addition & 0 deletions lib/ASTGen/Sources/ASTGen/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ extension BridgedAttributedTypeSpecifier {
case .keyword(.__owned): self = .legacyOwned
case .keyword(._const): self = .const
case .keyword(.isolated): self = .isolated
case .keyword(._resultDependsOn): self = .resultDependsOn
default: return nil
}
}
Expand Down
4 changes: 0 additions & 4 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ using namespace swift;

LangOptions::LangOptions() {
// Note: Introduce default-on language options here.
#ifndef NDEBUG
Features.insert(Feature::ParserRoundTrip);
Features.insert(Feature::ParserValidation);
#endif
// Enable any playground options that are enabled by default.
#define PLAYGROUND_OPTION(OptionName, Description, DefaultOn, HighPerfOn) \
if (DefaultOn) \
Expand Down
10 changes: 10 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,16 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
}
}

#ifndef NDEBUG
/// Enable round trip parsing via the new swift parser unless it is disabled
/// explicitly. The new Swift parser can have mismatches with C++ parser -
/// rdar://118013482 Use this flag to disable round trip through the new
/// Swift parser for such cases.
if (!Args.hasArg(OPT_disable_experimental_parser_round_trip)) {
Opts.Features.insert(Feature::ParserRoundTrip);
Opts.Features.insert(Feature::ParserValidation);
}
#endif
return HadError || UnsupportedOS || UnsupportedArch;
}

Expand Down
36 changes: 22 additions & 14 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5128,22 +5128,21 @@ ParserStatus Parser::parseDeclModifierList(DeclAttributes &Attributes,
/// '@' attribute
/// '@' attribute attribute-list-clause
/// \endverbatim
ParserStatus
Parser::parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc,
SourceLoc &ConstLoc,
TypeAttributes &Attributes) {
ParserStatus Parser::parseTypeAttributeListPresent(
ParamDecl::Specifier &Specifier, SourceLoc &SpecifierLoc,
SourceLoc &IsolatedLoc, SourceLoc &ConstLoc, SourceLoc &ResultDependsOnLoc,
TypeAttributes &Attributes) {
PatternBindingInitializer *initContext = nullptr;
Specifier = ParamDecl::Specifier::Default;
while (Tok.is(tok::kw_inout)
|| (canHaveParameterSpecifierContextualKeyword()
&& (Tok.isContextualKeyword("__shared")
|| Tok.isContextualKeyword("__owned")
|| Tok.isContextualKeyword("isolated")
|| Tok.isContextualKeyword("consuming")
|| Tok.isContextualKeyword("borrowing")
|| Tok.isContextualKeyword("_const")))) {
while (Tok.is(tok::kw_inout) ||
(canHaveParameterSpecifierContextualKeyword() &&
(Tok.isContextualKeyword("__shared") ||
Tok.isContextualKeyword("__owned") ||
Tok.isContextualKeyword("isolated") ||
Tok.isContextualKeyword("consuming") ||
Tok.isContextualKeyword("borrowing") ||
Tok.isContextualKeyword("_const") ||
Tok.isContextualKeyword("_resultDependsOn")))) {

if (Tok.isContextualKeyword("isolated")) {
if (IsolatedLoc.isValid()) {
Expand All @@ -5160,6 +5159,15 @@ Parser::parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
continue;
}

if (Tok.isContextualKeyword("_resultDependsOn")) {
if (!Context.LangOpts.hasFeature(Feature::NonEscapableTypes)) {
diagnose(Tok, diag::requires_non_escapable_types, "resultDependsOn",
false);
}
ResultDependsOnLoc = consumeToken();
continue;
}

if (SpecifierLoc.isValid()) {
diagnose(Tok, diag::parameter_specifier_repeated)
.fixItRemove(SpecifierLoc);
Expand Down
Loading