Skip to content

Revert "Regex literal runtime plumbing." #40506

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 0 additions & 3 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,6 @@ class ASTContext final {
ConcreteDeclRef getBuiltinInitDecl(NominalTypeDecl *decl,
KnownProtocolKind builtinProtocol,
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const;

/// Retrieve _StringProcessing.Regex.init(_regexString: String).
ConcreteDeclRef getRegexInitDecl(Type regexType) const;

/// Retrieve the declaration of Swift.<(Int, Int) -> Bool.
FuncDecl *getLessThanIntDecl() const;
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ WARNING(emit_reference_dependencies_without_primary_file,none,

WARNING(warn_implicit_concurrency_import_failed,none,
"unable to perform implicit import of \"_Concurrency\" module: no such module found", ())
WARNING(warn_implicit_string_processing_import_failed,none,
"unable to perform implicit import of \"_StringProcessing\" module: no such module found", ())

ERROR(error_module_name_required,none, "-module-name is required", ())
ERROR(error_bad_module_name,none,
Expand Down
11 changes: 3 additions & 8 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,9 @@ ERROR(builtin_string_literal_broken_proto,none,
ERROR(string_literal_broken_proto,none,
"protocol 'ExpressibleByStringLiteral' is broken", ())

ERROR(regex_decl_broken,none,
"cannot find 'Regex' type in scope", ())

// Array literals
ERROR(should_use_dictionary_literal,none,
"dictionary of type %0 cannot be %select{used|initialized}1 "
Expand Down Expand Up @@ -4732,14 +4735,6 @@ ERROR(async_unavailable_decl,none,
"%0 %1 is unavailable from asynchronous contexts%select{|; %3}2",
(DescriptiveDeclKind, DeclBaseName, bool, StringRef))

//------------------------------------------------------------------------------
// MARK: String Processing
//------------------------------------------------------------------------------

ERROR(string_processing_lib_missing,none,
"missing '%0' declaration, probably because the '_StringProcessing' "
"module was not imported properly", (StringRef))

//------------------------------------------------------------------------------
// MARK: Type Check Types
//------------------------------------------------------------------------------
Expand Down
14 changes: 11 additions & 3 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -966,18 +966,26 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
class RegexLiteralExpr : public LiteralExpr {
SourceLoc Loc;
StringRef RegexText;
Expr *SemanticExpr;

RegexLiteralExpr(SourceLoc loc, StringRef regexText, bool isImplicit)
RegexLiteralExpr(SourceLoc loc, StringRef regexText, Expr *semanticExpr,
bool isImplicit)
: LiteralExpr(ExprKind::RegexLiteral, isImplicit), Loc(loc),
RegexText(regexText) {}
RegexText(regexText), SemanticExpr(semanticExpr) {}

public:
static RegexLiteralExpr *createParsed(ASTContext &ctx, SourceLoc loc,
StringRef regexText);
StringRef regexText,
Expr *semanticExpr);

/// Retrieve the raw regex text.
StringRef getRegexText() const { return RegexText; }

/// Retrieve the semantic expression that the regex will be type-checked and
/// emitted as.
Expr *getSemanticExpr() const { return SemanticExpr; }
void setSemanticExpr(Expr *expr) { SemanticExpr = expr; }

SourceRange getSourceRange() const { return Loc; }

static bool classof(const Expr *E) {
Expand Down
5 changes: 2 additions & 3 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,9 @@ IDENTIFIER(pullback)
IDENTIFIER(TangentVector)
IDENTIFIER(zero)

// String processing
// Regex literals
IDENTIFIER(Regex)
IDENTIFIER_(regexString)
IDENTIFIER_(StringProcessing)
IDENTIFIER(_regexString)

// Distributed actors
IDENTIFIER(transport)
Expand Down
4 changes: 0 additions & 4 deletions include/swift/AST/KnownSDKTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,4 @@ KNOWN_SDK_TYPE_DECL(Distributed, DistributedActor, ProtocolDecl, 0)
KNOWN_SDK_TYPE_DECL(Distributed, ActorIdentity, ProtocolDecl, 0)
KNOWN_SDK_TYPE_DECL(Distributed, AnyActorIdentity, StructDecl, 0)

// String processing
KNOWN_SDK_TYPE_DECL(StringProcessing, Regex, StructDecl, 1)
KNOWN_SDK_TYPE_DECL(StringProcessing, DynamicCaptures, EnumDecl, 0)

#undef KNOWN_SDK_TYPE_DECL
12 changes: 0 additions & 12 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,6 @@ class CompilerInvocation {
/// imported.
bool shouldImportSwiftConcurrency() const;

/// Whether the Swift String Processing support library should be implicitly
/// imported.
bool shouldImportSwiftStringProcessing() const;

/// Performs input setup common to these tools:
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
/// Return value includes the buffer so caller can keep it alive.
Expand Down Expand Up @@ -537,14 +533,6 @@ class CompilerInstance {
/// i.e. if it can be found.
bool canImportSwiftConcurrency() const;

/// Verify that if an implicit import of the `StringProcessing` module if
/// expected, it can actually be imported. Emit a warning, otherwise.
void verifyImplicitStringProcessingImport();

/// Whether the Swift String Processing support library can be imported
/// i.e. if it can be found.
bool canImportSwiftStringProcessing() const;

/// Gets the SourceFile which is the primary input for this CompilerInstance.
/// \returns the primary SourceFile, or nullptr if there is no primary input;
/// if there are _multiple_ primary inputs, fails with an assertion.
Expand Down
2 changes: 0 additions & 2 deletions include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ constexpr static const StringLiteral SWIFT_ONONE_SUPPORT = "SwiftOnoneSupport";
constexpr static const StringLiteral SWIFT_CONCURRENCY_NAME = "_Concurrency";
/// The name of the Distributed module, which supports that extension.
constexpr static const StringLiteral SWIFT_DISTRIBUTED_NAME = "_Distributed";
/// The name of the StringProcessing module, which supports that extension.
constexpr static const StringLiteral SWIFT_STRING_PROCESSING_NAME = "_StringProcessing";
/// The name of the SwiftShims module, which contains private stdlib decls.
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
/// The name of the Builtin module, which contains Builtin functions.
Expand Down
17 changes: 1 addition & 16 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,23 +1217,8 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
return witness;
}

ConcreteDeclRef ASTContext::getRegexInitDecl(Type regexType) const {
auto *spModule = getLoadedModule(Id_StringProcessing);
DeclName name(*const_cast<ASTContext *>(this),
DeclBaseName::createConstructor(),
{Id_regexString});
SmallVector<ValueDecl *, 1> results;
spModule->lookupQualified(getRegexType(), DeclNameRef(name),
NL_IncludeUsableFromInline, results);
assert(results.size() == 1);
auto *foundDecl = cast<ConstructorDecl>(results[0]);
auto subs = regexType->getMemberSubstitutionMap(spModule, foundDecl);
return ConcreteDeclRef(foundDecl, subs);
}

static
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op,
FuncDecl *&cached) {
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op, FuncDecl *&cached) {
if (cached)
return cached;

Expand Down
5 changes: 1 addition & 4 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,10 +1909,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
}
void visitRegexLiteralExpr(RegexLiteralExpr *E) {
printCommon(E, "regex_literal_expr");
PrintWithColorRAII(OS, LiteralValueColor)
<< " text=" << QuotedString(E->getRegexText())
<< " initializer=";
E->getInitializer().dump(PrintWithColorRAII(OS, LiteralValueColor).getOS());
printRec(E->getSemanticExpr());
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

Expand Down
5 changes: 5 additions & 0 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,11 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

Expr *visitRegexLiteralExpr(RegexLiteralExpr *E) {
if (auto *newExpr = doIt(E->getSemanticExpr())) {
E->setSemanticExpr(newExpr);
} else {
return nullptr;
}
return E;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2212,8 +2212,9 @@ SourceLoc TapExpr::getEndLoc() const {

RegexLiteralExpr *
RegexLiteralExpr::createParsed(ASTContext &ctx, SourceLoc loc,
StringRef regexText) {
return new (ctx) RegexLiteralExpr(loc, regexText, /*implicit*/ false);
StringRef regexText, Expr *semanticExpr) {
return new (ctx) RegexLiteralExpr(loc, regexText, semanticExpr,
/*implicit*/ false);
}

void swift::simple_display(llvm::raw_ostream &out, const ClosureExpr *CE) {
Expand Down
32 changes: 0 additions & 32 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,6 @@ bool CompilerInvocation::shouldImportSwiftConcurrency() const {
FrontendOptions::ParseInputMode::SwiftModuleInterface;
}

bool CompilerInvocation::shouldImportSwiftStringProcessing() const {
return getLangOptions().EnableExperimentalStringProcessing;
}

/// Implicitly import the SwiftOnoneSupport module in non-optimized
/// builds. This allows for use of popular specialized functions
/// from the standard library, which makes the non-optimized builds
Expand Down Expand Up @@ -837,20 +833,6 @@ bool CompilerInstance::canImportSwiftConcurrency() const {
{getASTContext().getIdentifier(SWIFT_CONCURRENCY_NAME), SourceLoc()});
}

void CompilerInstance::verifyImplicitStringProcessingImport() {
if (Invocation.shouldImportSwiftStringProcessing() &&
!canImportSwiftStringProcessing()) {
Diagnostics.diagnose(SourceLoc(),
diag::warn_implicit_string_processing_import_failed);
}
}

bool CompilerInstance::canImportSwiftStringProcessing() const {
return getASTContext().canImportModule(
{getASTContext().getIdentifier(SWIFT_STRING_PROCESSING_NAME),
SourceLoc()});
}

ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
auto &frontendOpts = Invocation.getFrontendOptions();

Expand Down Expand Up @@ -891,19 +873,6 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
}
}

if (Invocation.shouldImportSwiftStringProcessing()) {
switch (imports.StdlibKind) {
case ImplicitStdlibKind::Builtin:
case ImplicitStdlibKind::None:
break;

case ImplicitStdlibKind::Stdlib:
if (canImportSwiftStringProcessing())
pushImport(SWIFT_STRING_PROCESSING_NAME);
break;
}
}

imports.ShouldImportUnderlyingModule = frontendOpts.ImportUnderlyingModule;
imports.BridgingHeaderPath = frontendOpts.ImplicitObjCHeaderPath;
return imports;
Expand Down Expand Up @@ -1115,7 +1084,6 @@ bool CompilerInstance::loadStdlibIfNeeded() {
}

verifyImplicitConcurrencyImport();
verifyImplicitStringProcessingImport();

// If we failed to load, we should have already diagnosed.
if (M->failedToLoad()) {
Expand Down
26 changes: 25 additions & 1 deletion lib/Parse/ParseRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ ParserResult<Expr> Parser::parseExprRegexLiteral() {
diagnose(Tok, diag::regex_literal_parsing_error, errorStr);

auto loc = consumeToken();

// Create an implicit .init(_regexString: "<regex text>") call to serve as the
// semantic expression for the regex. The type-checker will provide it with
// the correct contextual type. We delay the contextual type for a couple of
// reasons:
// 1. We need to delay type lookup until after parsing.
// 2. Even if the AST synthesis were done lazily in e.g a request, we don't
// currently have great support for implicit TypeExprs for unopened generic
// types, as we want to phase out the use of UnboundGenericType. The Regex
// type isn't currently generic, but might be in the future.
auto *regexStringExpr =
new (Context) StringLiteralExpr(Context.AllocateCopy(regexText), loc);
regexStringExpr->setImplicit();

DeclName initName(Context, DeclBaseName::createConstructor(),
{Context.Id__regexString});
DeclNameRef initNameRef(initName);
auto *dotInit = new (Context) UnresolvedMemberExpr(
/*dotLoc*/ loc, DeclNameLoc(loc), initNameRef, /*implicit*/ true);

auto *args =
ArgumentList::forImplicitCallTo(initNameRef, {regexStringExpr}, Context);
auto *call = CallExpr::createImplicit(Context, dotInit, args);

return makeParserResult(
RegexLiteralExpr::createParsed(Context, loc, regexText));
RegexLiteralExpr::createParsed(Context, loc, regexText, call));
}
21 changes: 0 additions & 21 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1896,25 +1896,6 @@ buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
return builtinLiteralArgs;
}

static inline PreparedArguments
buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
RegexLiteralExpr *expr) {
auto &ctx = SGF.getASTContext();
// %0 = string_literal <regex text>
auto strLiteralArgs = emitStringLiteral(SGF, expr, expr->getRegexText(), C,
StringLiteralExpr::Encoding::UTF8);
// %1 = function_ref String.init(
// _builtinStringLiteral:utf8CodeUnitCount:isASCII:)
// %2 = apply %1(%0, ..., ...) -> $String
auto strInitDecl = ctx.getStringBuiltinInitDecl(ctx.getStringDecl());
RValue string = SGF.emitApplyAllocatingInitializer(
expr, strInitDecl, std::move(strLiteralArgs),
/*overriddenSelfType*/ Type(), SGFContext());
PreparedArguments args({AnyFunctionType::Param(ctx.getStringType())});
args.add(expr, std::move(string));
return args;
}

static inline PreparedArguments
buildBuiltinLiteralArgs(SILGenFunction &SGF, SGFContext C,
MagicIdentifierLiteralExpr *magicLiteral) {
Expand Down Expand Up @@ -1980,8 +1961,6 @@ static inline PreparedArguments buildBuiltinLiteralArgs(SILGenFunction &SGF,
return buildBuiltinLiteralArgs(SGF, C, integerLiteral);
} else if (auto floatLiteral = dyn_cast<FloatLiteralExpr>(literal)) {
return buildBuiltinLiteralArgs(SGF, C, floatLiteral);
} else if (auto regexLiteral = dyn_cast<RegexLiteralExpr>(literal)) {
return buildBuiltinLiteralArgs(SGF, C, regexLiteral);
} else {
return buildBuiltinLiteralArgs(
SGF, C, cast<MagicIdentifierLiteralExpr>(literal));
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *E,
}

RValue RValueEmitter::visitRegexLiteralExpr(RegexLiteralExpr *E, SGFContext C) {
return SGF.emitLiteral(E, C);
return SGF.emitRValue(E->getSemanticExpr());
}

RValue RValueEmitter::
Expand Down
5 changes: 1 addition & 4 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2792,10 +2792,7 @@ namespace {
}

Expr *visitRegexLiteralExpr(RegexLiteralExpr *expr) {
simplifyExprType(expr);
expr->setInitializer(
cs.getASTContext().getRegexInitDecl(cs.getType(expr)));
return expr;
return simplifyExprType(expr);
}

Expr *visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) {
Expand Down
Loading