diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index c20e0c359..5ff7d5314 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1045,16 +1045,12 @@ static RegisterPrimOp primop_trace({ static void prim_warn(EvalState & state, const PosIdx pos, Value * * args, Value & v) { - state.forceValue(*args[0], pos); + // We only accept a string argument for now. The use case for pretty printing a value is covered by `trace`. + // By rejecting non-strings we allow future versions to add more features without breaking existing code. + auto msgStr = state.forceString(*args[0], pos, "while evaluating the first argument; the message passed to builtins.warn"); { - BaseError msg(args[0]->type() == nString - ? std::string(args[0]->string_view()) - : ({ - std::stringstream s; - s << ValuePrinter(state, *args[0]); - s.str(); - })); + BaseError msg(std::string{msgStr}); msg.atPos(state.positions[pos]); auto info = msg.info(); info.level = lvlWarn; diff --git a/tests/functional/lang.sh b/tests/functional/lang.sh index 843ff7cfa..569e7082e 100755 --- a/tests/functional/lang.sh +++ b/tests/functional/lang.sh @@ -38,8 +38,10 @@ nix-instantiate --eval -E 'let x = { repeating = x; tracing = builtins.trace x t nix-instantiate --eval -E 'builtins.warn "Hello" 123' 2>&1 | grepQuiet 'warning: Hello' nix-instantiate --eval -E 'builtins.addErrorContext "while doing ${"something"} interesting" (builtins.warn "Hello" 123)' 2>/dev/null | grepQuiet 123 -nix-instantiate --eval -E 'let x = builtins.warn { x = x; } true; in x' \ - 2>&1 | grepQuiet -E 'warning: { x = «potential infinite recursion»; }' + +# warn does not accept non-strings for now +expectStderr 1 nix-instantiate --eval -E 'let x = builtins.warn { x = x; } true; in x' \ + | grepQuiet "expected a string but found a set" expectStderr 1 nix-instantiate --eval --abort-on-warn -E 'builtins.warn "Hello" 123' | grepQuiet Hello NIX_ABORT_ON_WARN=1 expectStderr 1 nix-instantiate --eval -E 'builtins.addErrorContext "while doing ${"something"} interesting" (builtins.warn "Hello" 123)' | grepQuiet "while doing something interesting"