Skip to content
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

fix: Add missing context to builtins.{match,split} results #12500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ namespace nix {
* Miscellaneous
*************************************************************/

static inline Value * mkString(EvalState & state, const std::csub_match & match)
static inline Value * mkString(EvalState & state, const std::csub_match & match, const NixStringContext & context)
{
Value * v = state.allocValue();
v->mkString({match.first, match.second});
v->mkString({match.first, match.second}, context);
return v;
}

Expand Down Expand Up @@ -4318,7 +4318,7 @@ void prim_match(EvalState & state, const PosIdx pos, Value * * args, Value & v)
if (!match[i + 1].matched)
v2 = &state.vNull;
else
v2 = mkString(state, match[i + 1]);
v2 = mkString(state, match[i + 1], context);
v.mkList(list);

} catch (std::regex_error & e) {
Expand Down Expand Up @@ -4402,7 +4402,7 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
const auto & match = *i;

// Add a string for non-matched characters.
list[idx++] = mkString(state, match.prefix());
list[idx++] = mkString(state, match.prefix(), context);

// Add a list for matched substrings.
const size_t slen = match.size() - 1;
Expand All @@ -4413,14 +4413,14 @@ void prim_split(EvalState & state, const PosIdx pos, Value * * args, Value & v)
if (!match[si + 1].matched)
v2 = &state.vNull;
else
v2 = mkString(state, match[si + 1]);
v2 = mkString(state, match[si + 1], context);
}

(list[idx++] = state.allocValue())->mkList(list2);

// Add a string for non-matched suffix characters.
if (idx == 2 * len)
list[idx++] = mkString(state, match.suffix());
list[idx++] = mkString(state, match.suffix(), context);
}

assert(idx == 2 * len + 1);
Expand Down
1 change: 1 addition & 0 deletions tests/functional/lang/eval-okay-match-context.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ true true true ]
20 changes: 20 additions & 0 deletions tests/functional/lang/eval-okay-match-context.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let

s = "${builtins.derivation {
name = "test";
builder = "/bin/sh";
system = "x86_64-linux";
}}";

c = builtins.getContext s;

matchRes = builtins.match ".*(-).*" s;

splitRes = builtins.split "(-)" s;

in
[
(c == builtins.getContext (builtins.head matchRes))
(c == builtins.getContext (builtins.head splitRes))
(c == builtins.getContext (builtins.head (builtins.elemAt splitRes 1)))
]
Loading