From ae4a00057671486c161ee630315f5d214f75b61b Mon Sep 17 00:00:00 2001 From: piegames Date: Sun, 1 Feb 2026 12:38:11 +0100 Subject: [PATCH] deprecated-features/broken-string-escape: Improve warning message Explicitly catch common errors (trying to escape line breaks, badly escaping interpolations) to provide better messages and user guidance for these cases. Change-Id: I3dd1b2ad3bca33be393e65be5e72f4fb9544a46a --- lix/libexpr/parser/parser-impl1.inc.cc | 4 +- lix/libexpr/parser/state.hh | 58 ++++++++++++++++--- .../depr-warning-escapes.out.exp | 1 - .../lang/string-indented/depr-warning.out.exp | 1 - .../eval-okay-backslash-newline.err.exp | 5 ++ ...apes.err.exp => eval-okay-escapes.err.exp} | 16 ++++- .../string-indented/eval-okay-escapes.out.exp | 2 +- ...depr-warning.err.exp => eval-okay.err.exp} | 0 .../lang/string-indented/in-escapes.nix | 4 ++ .../lang/string-indented/test.toml | 7 --- .../eval-okay-backslash-newline.err.exp | 5 ++ .../lang/string/eval-okay-escapes.err.exp | 2 +- .../functional2/lang/string/eval-okay.err.exp | 6 ++ .../functional2/lang/string/eval-okay.out.exp | 2 +- tests/functional2/lang/string/in.nix | 1 + tests/functional2/lang/string/test.toml | 9 --- 16 files changed, 91 insertions(+), 32 deletions(-) delete mode 100644 tests/functional2/lang/string-indented/depr-warning-escapes.out.exp delete mode 100644 tests/functional2/lang/string-indented/depr-warning.out.exp create mode 100644 tests/functional2/lang/string-indented/eval-okay-backslash-newline.err.exp rename tests/functional2/lang/string-indented/{depr-warning-escapes.err.exp => eval-okay-escapes.err.exp} (65%) rename tests/functional2/lang/string-indented/{depr-warning.err.exp => eval-okay.err.exp} (100%) create mode 100644 tests/functional2/lang/string/eval-okay-backslash-newline.err.exp create mode 100644 tests/functional2/lang/string/eval-okay.err.exp delete mode 100644 tests/functional2/lang/string/test.toml diff --git a/lix/libexpr/parser/parser-impl1.inc.cc b/lix/libexpr/parser/parser-impl1.inc.cc index e35320ffc..2df6872c1 100644 --- a/lix/libexpr/parser/parser-impl1.inc.cc +++ b/lix/libexpr/parser/parser-impl1.inc.cc @@ -698,7 +698,7 @@ template<> struct BuildAST { if (!ps.featureSettings.isEnabled(Dep::BrokenStringEscape) && c != '\\' && c != '$' && c != '"' && c != 'r' && c != 'n' && c != 't') { - ps.badEscapeFound(ps.at(in), c, "\\"); + ps.badEscapeFound(ps.at(in), c, false); } s.append(ps.at(in), "\\"); // FIXME compat with old parser s.append(ps.at(in), in.string_view()); @@ -769,7 +769,7 @@ template<> struct BuildAST { KJ_FALLTHROUGH; default: if (!ps.featureSettings.isEnabled(Dep::BrokenStringEscape)) { - ps.badEscapeFound(ps.at(in), c, "''\\"); + ps.badEscapeFound(ps.at(in), c, true); } s.lines.back().parts.emplace_back(ps.at(in), in.string_view()); diff --git a/lix/libexpr/parser/state.hh b/lix/libexpr/parser/state.hh index 4556c4e6e..f3d1ec2e7 100644 --- a/lix/libexpr/parser/state.hh +++ b/lix/libexpr/parser/state.hh @@ -38,7 +38,7 @@ struct State void badLineEndingFound(const PosIdx pos, bool warnOnly); void badFirstLineIndStringFound(const PosIdx pos); void badSingleLineIndStringFound(const PosIdx pos); - void badEscapeFound(const PosIdx pos, char found, std::string escape); + void badEscapeFound(const PosIdx pos, char found, bool isIndented); void nulFound(const PosIdx pos); void recSetMergeFound(const AttrPath & attrPath, const PosIdx pos); void recSetDynamicAttrFound(const PosIdx pos); @@ -129,17 +129,61 @@ inline void State::badFirstLineIndStringFound(const PosIdx pos) }); } // Added 2024-12-12, equally used in the wild. -inline void State::badEscapeFound(const PosIdx pos, char found, std::string escape) +inline void State::badEscapeFound(const PosIdx pos, char found, bool isIndented) { - logWarning({ - .msg = HintFmt( - "%s is an ill-defined escape. You can drop the %s and simply write %s instead. Use %s " - "to silence this warning.", + auto escape = std::string(isIndented ? "''\\" : "\\"); + auto interpolEscape = std::string(isIndented ? "''${" : "\\${"); + HintFmt msg = HintFmt( + "%s is an ill-defined escape. You can drop the %s and simply write %s instead. " + "Use %s to silence this warning.", + escape + found, + escape, + found, + "--extra-deprecated-features broken-string-escape" + ); + /* Special case some common escapes to provide better messages */ + if (found == '$' || found == '{') { + /* Someone possibly tried to escape an interpolation but used the wrong sequence. + * We don't have the full context within this function to know for sure (extracting that information + * would require changing some parser rules), but we can at least add a hint about this to the default + * message. + */ + msg = HintFmt( + "%s is an ill-defined escape. You can drop the %s and simply write %s instead. " + "If you meant to escape an interpolation, write %s instead. " + "Use %s to silence this warning.", escape + found, escape, found, + interpolEscape, "--extra-deprecated-features broken-string-escape" - ), + ); + } else if (found == '\r' || found == '\n') { + /* Someone tried to escape a line break */ + msg = HintFmt( + "%s at the end of a line is an ill-defined escape. Escaping line endings has no effect. " + "You can either drop the trailing %s, or use an explicit string concatenation instead. " + "Use %s to silence this warning.", + escape, + escape, + "--extra-deprecated-features broken-string-escape" + ); + } else if (!isprint(found) || isspace(found)) { + /* Generic error message for all non-printable escape characters */ + msg = HintFmt( + "Found an ill-defined escape. You can drop the %s, as it has no effect. %s" + "Use %s to silence this warning.", + escape, + isIndented + ? "(Note that an escaped space at the beginning of a string line may influence the string's " + "indentation, however deliberately using this in strings is not supported.) " + : "", + "--extra-deprecated-features broken-string-escape" + ); + } + + logWarning({ + .msg = msg, .pos = positions[pos], }); } diff --git a/tests/functional2/lang/string-indented/depr-warning-escapes.out.exp b/tests/functional2/lang/string-indented/depr-warning-escapes.out.exp deleted file mode 100644 index c1c79abde..000000000 --- a/tests/functional2/lang/string-indented/depr-warning-escapes.out.exp +++ /dev/null @@ -1 +0,0 @@ -"a\n\nf{$\"'\rv\\\t\n'\${\"x\"}\n" diff --git a/tests/functional2/lang/string-indented/depr-warning.out.exp b/tests/functional2/lang/string-indented/depr-warning.out.exp deleted file mode 100644 index 726cbc0bf..000000000 --- a/tests/functional2/lang/string-indented/depr-warning.out.exp +++ /dev/null @@ -1 +0,0 @@ -[ "This is an indented multi-line string\nliteral. An amount of whitespace at\nthe start of each line matching the minimum\nindentation of all lines in the string\nliteral together will be removed. Thus,\nin this case four spaces will be\nstripped from each line, even though\n THIS LINE is indented six spaces.\n\nAlso, empty lines don't count in the\ndetermination of the indentation level (the\nprevious empty line has indentation 0, but\nit doesn't matter).\n" "If the string starts with whitespace\n followed by a newline, it's stripped, but\n that's not the case here. Two spaces are\n stripped because of the \" \" at the start. \n" "This line is indented\na bit further.\n" "Anti-quotations, like so, are\nalso allowed.\n" " The \\ is not special here.\n' can be followed by any character except another ', e.g. 'x'.\nLikewise for $, e.g. $$ or $varName.\nBut ' followed by ' is special, as is $ followed by {.\nIf you want them, use anti-quotations: '', \${.\n" " Tabs are not interpreted as whitespace (since we can't guess\n what tab settings are intended), so don't use them.\n\tThis line starts with a space and a tab, so only one\n space will be stripped from each line.\n" "Also note that if the last line (just before the closing ' ')\nconsists only of whitespace, it's ignored. But here there is\nsome non-whitespace stuff, so the line isn't removed. " "\nThis shows a hacky way to preserve an empty line after the start.\nBut there's no reason to do so: you could just repeat the empty\nline.\n" " Similarly you can force an indentation level,\n in this case to 2 spaces. This works because the anti-quote\n is significant (not whitespace).\n" "" "" "" "start on network-interfaces\n\nstart script\n\n rm -f /var/run/opengl-driver\n ln -sf 123 /var/run/opengl-driver\n\n rm -f /var/log/slim.log\n \nend script\n\nenv SLIM_CFGFILE=abc\nenv SLIM_THEMESDIR=def\nenv FONTCONFIG_FILE=/etc/fonts/fonts.conf \t\t\t\t# !!! cleanup\nenv XKB_BINDIR=foo/bin \t\t\t\t# Needed for the Xkb extension.\nenv LD_LIBRARY_PATH=libX11/lib:libXext/lib:/usr/lib/ # related to xorg-sys-opengl - needed to load libglx for (AI)GLX support (for compiz)\n\nenv XORG_DRI_DRIVER_PATH=nvidiaDrivers/X11R6/lib/modules/drivers/ \n\nexec slim/bin/slim\n" "Escaping of ' followed by ': ''\nEscaping of $ followed by {: \${\nAnd finally to interpret \\n etc. as in a string: \n, \r, \t.\n" "foo\n'bla'\nbar\n" "cut -d $'\\t' -f 1\n" "ending dollar $$\n" " Lines without any indentation effectively disable the indentation\n stripping for the entire string:\n\n cat >$out/foo/data <