From d758ccbe1c99b31f51e23863c96ee0c5b313d647 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Mon, 30 Oct 2023 18:11:12 -0400 Subject: [PATCH] remove support for in the interpolated expression sequence by popular demaand --- compiler/src/dmd/astbase.d | 4 ++-- compiler/src/dmd/expression.d | 5 ----- compiler/src/dmd/expression.h | 6 +----- compiler/src/dmd/lexer.d | 20 ------------------- compiler/src/dmd/tokens.d | 2 +- compiler/src/dmd/tokens.h | 2 +- .../interpolatedexpressionsequence_postfix.d | 6 +++--- .../runnable/interpolatedexpressionsequence.d | 10 ++++++---- compiler/test/unit/lexer/location_offset.d | 1 + druntime/src/core/interpolation.d | 10 ++++------ 10 files changed, 19 insertions(+), 47 deletions(-) diff --git a/compiler/src/dmd/astbase.d b/compiler/src/dmd/astbase.d index c4c8489b1eda..0c847276d28f 100644 --- a/compiler/src/dmd/astbase.d +++ b/compiler/src/dmd/astbase.d @@ -4910,12 +4910,12 @@ struct ASTBase extern (C++) final class InterpExp : Expression { - InterpolatedSet interpolatedSet; + InterpolatedSet* interpolatedSet; char postfix = 0; // 'c', 'w', 'd' extern (D) this(const ref Loc loc, InterpolatedSet* interpolatedSet, char postfix = 0) { - super(loc, EXP.interpolatedSet, __traits(classInstanceSize, InterpExp)); + super(loc, EXP.interpolated, __traits(classInstanceSize, InterpExp)); this.interpolatedSet = interpolatedSet; this.postfix = postfix; } diff --git a/compiler/src/dmd/expression.d b/compiler/src/dmd/expression.d index 16881ed59c75..6e074fff8c64 100644 --- a/compiler/src/dmd/expression.d +++ b/compiler/src/dmd/expression.d @@ -1900,11 +1900,6 @@ extern (C++) final class InterpExp : Expression this.postfix = postfix; } - static InterpExp create(const ref Loc loc, InterpolatedSet* set) - { - return new InterpExp(loc, set); - } - override void accept(Visitor v) { v.visit(this); diff --git a/compiler/src/dmd/expression.h b/compiler/src/dmd/expression.h index cc1deddb42e8..f2fd31edbcfe 100644 --- a/compiler/src/dmd/expression.h +++ b/compiler/src/dmd/expression.h @@ -379,12 +379,8 @@ class InterpExp final : public Expression OwnedBy ownedByCtfe; void* interpolatedSet; - static InterpExp *create(const Loc &loc, void* set); - void accept(Visitor* v) override { v->visit(this); } -} - - +}; // Tuple diff --git a/compiler/src/dmd/lexer.d b/compiler/src/dmd/lexer.d index e808758f1baa..dbd11ab912c0 100644 --- a/compiler/src/dmd/lexer.d +++ b/compiler/src/dmd/lexer.d @@ -1846,28 +1846,8 @@ class Lexer // otherwise returns false, indicating to treat it as just part of a normal string private bool handleInterpolatedSegment(Token* token, Loc start) { - bool wasSpecial = true; switch(*p) { - case '_': - case 'a': .. case 'z': - case 'A': .. case 'Z': - - // always put the string part in first - token.appendInterpolatedPart(stringbuffer); - stringbuffer.setsize(0); - - // identifier, scan it with the lexer to follow all rules - auto pstart = p; - Token tok; - scan(&tok); - - // then put the interpolated string segment - token.appendInterpolatedPart(pstart[0 .. p - pstart]); - - stringbuffer.setsize(0); - - return true; case '(': // expression, at this level we need to scan until the closing ')' diff --git a/compiler/src/dmd/tokens.d b/compiler/src/dmd/tokens.d index ba97d5ee2edd..50c6251cb7c1 100644 --- a/compiler/src/dmd/tokens.d +++ b/compiler/src/dmd/tokens.d @@ -947,7 +947,7 @@ nothrow: */ void setString(const(char)* ptr, size_t length) { - assert(value == TOK.string_); + value = TOK.string_; auto s = cast(char*)mem.xmalloc_noscan(length + 1); memcpy(s, ptr, length); s[length] = 0; diff --git a/compiler/src/dmd/tokens.h b/compiler/src/dmd/tokens.h index 98d632460468..5f9aadb899e0 100644 --- a/compiler/src/dmd/tokens.h +++ b/compiler/src/dmd/tokens.h @@ -468,7 +468,7 @@ struct Token { utf8_t *ustring; // UTF8 string void *interpolatedSet; - } + }; unsigned len; unsigned char postfix; // 'c', 'w', 'd' }; diff --git a/compiler/test/fail_compilation/interpolatedexpressionsequence_postfix.d b/compiler/test/fail_compilation/interpolatedexpressionsequence_postfix.d index 859667f6852e..c915c446b499 100644 --- a/compiler/test/fail_compilation/interpolatedexpressionsequence_postfix.d +++ b/compiler/test/fail_compilation/interpolatedexpressionsequence_postfix.d @@ -1,8 +1,8 @@ /* TEST_OUTPUT: --- -interpolatedexpressionsequence_postfix.d(10): Error: String postfixes on interpolated expression sequences are not allowed. -interpolatedexpressionsequence_postfix.d(11): Error: String postfixes on interpolated expression sequences are not allowed. -interpolatedexpressionsequence_postfix.d(12): Error: String postfixes on interpolated expression sequences are not allowed. +fail_compilation/interpolatedexpressionsequence_postfix.d(10): Error: String postfixes on interpolated expression sequences are not allowed. +fail_compilation/interpolatedexpressionsequence_postfix.d(11): Error: String postfixes on interpolated expression sequences are not allowed. +fail_compilation/interpolatedexpressionsequence_postfix.d(12): Error: String postfixes on interpolated expression sequences are not allowed. --- */ void main() { diff --git a/compiler/test/runnable/interpolatedexpressionsequence.d b/compiler/test/runnable/interpolatedexpressionsequence.d index 9f43f4848b1f..831150792f9e 100644 --- a/compiler/test/runnable/interpolatedexpressionsequence.d +++ b/compiler/test/runnable/interpolatedexpressionsequence.d @@ -23,7 +23,7 @@ void main() { string b = "one"; // parser won't permit alias = i".." directly; i"..." is meant to // be used as a function/template parameter at this time. - alias expr = AliasSeq!i"$a $(b)"; + alias expr = AliasSeq!i"$(a) $(b)"; // elements from the source code are available at compile time, so // we static assert those, but the values, of course, are different static assert(expr[0] == InterpolationHeader()); @@ -38,12 +38,14 @@ void main() { // it does currently allow `auto` to be used, it creates a value tuple // you can embed any D expressions inside the parenthesis, and the // token is not ended until you get the *outer* ) and ". - auto thing = i"$b $("$" ~ ')' ~ `"`)"; + auto thing = i"$(b) $("$" ~ ')' ~ `"`)"; assert(simpleToString(thing) == "one $)\""); + assert(simpleToString(i"$b") == "$b"); // support for $ident removed by popular demand + // i`` and iq{} should also work - assert(simpleToString(i` $b is $(b)!`) == " one is one!"); - assert(simpleToString(iq{ $b is $(b)!}) == " one is one!"); + assert(simpleToString(i` $(b) is $(b)!`) == " one is one!"); + assert(simpleToString(iq{ $(b) is $(b)!}) == " one is one!"); assert(simpleToString(i`\$('$')`) == "\\$"); // no \ escape there assert(simpleToString(iq{{$('$')}}) == "{$}"); // {} needs to work } diff --git a/compiler/test/unit/lexer/location_offset.d b/compiler/test/unit/lexer/location_offset.d index b87321468878..21266276d2c3 100644 --- a/compiler/test/unit/lexer/location_offset.d +++ b/compiler/test/unit/lexer/location_offset.d @@ -515,6 +515,7 @@ enum ignoreTokens showCtfeContext, objcClassReference, vectorArray, + interpolated, wchar_tLiteral, endOfLine, diff --git a/druntime/src/core/interpolation.d b/druntime/src/core/interpolation.d index d09b96c16baf..a2f2a09638c8 100644 --- a/druntime/src/core/interpolation.d +++ b/druntime/src/core/interpolation.d @@ -9,7 +9,7 @@ int num; // the compiler uses this module to implement the // i"..." literal used here. - auto a = i"$str has $num items."; + auto a = i"$(str) has $(num) items."; --- The variable `a` is a sequence of expressions: @@ -33,7 +33,7 @@ In the example: --- - auto a = i"$(str) has $num items."; + auto a = i"$(str) has $(num) items."; --- We will find: @@ -50,11 +50,9 @@ --- You can see the correspondence with the original - input: when you write `$(expression)`, or the shorthand - equivalent when you only want an expression consisting of - a single identifier, `$identifier`, the string of the + input: when you write `$(expression)`, the string of the code is passed as `InterpolatedExpression!ThatString`, - (excluding any parenthesis around the expression, if present), + (excluding any parenthesis around the expression), and everything else is passed as `InterpolatedLiteral!str`, in the same sequence as they appeared in the source.