Show position info in string concatenation / addition errors

This commit is contained in:
Eelco Dolstra 2014-04-04 22:19:33 +02:00
parent 8160f794e7
commit bd9b1d97b4
7 changed files with 63 additions and 45 deletions

View file

@ -275,6 +275,16 @@ LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2))
throw EvalError(format(s) % s2); throw EvalError(format(s) % s2);
} }
LocalNoInlineNoReturn(void throwEvalError(const char * s, const Pos & pos))
{
throw EvalError(format(s) % pos);
}
LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const Pos & pos))
{
throw EvalError(format(s) % s2 % pos);
}
LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3)) LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3))
{ {
throw EvalError(format(s) % s2 % s3); throw EvalError(format(s) % s2 % s3);
@ -295,6 +305,11 @@ LocalNoInlineNoReturn(void throwTypeError(const char * s))
throw TypeError(s); throw TypeError(s);
} }
LocalNoInlineNoReturn(void throwTypeError(const char * s, const Pos & pos))
{
throw TypeError(format(s) % pos);
}
LocalNoInlineNoReturn(void throwTypeError(const char * s, const string & s1)) LocalNoInlineNoReturn(void throwTypeError(const char * s, const string & s1))
{ {
throw TypeError(format(s) % s1); throw TypeError(format(s) % s1);
@ -648,7 +663,7 @@ void ExprAttrs::eval(EvalState & state, Env & env, Value & v)
foreach (AttrDefs::iterator, i, attrs) foreach (AttrDefs::iterator, i, attrs)
v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos)); v.attrs->push_back(Attr(i->first, i->second.e->maybeThunk(state, env), &i->second.pos));
/* dynamic attrs apply *after* rec and __overrides */ /* Dynamic attrs apply *after* rec and __overrides. */
foreach (DynamicAttrDefs::iterator, i, dynamicAttrs) { foreach (DynamicAttrDefs::iterator, i, dynamicAttrs) {
Value nameVal; Value nameVal;
if (i->nameExpr->es->size() == 1) { if (i->nameExpr->es->size() == 1) {
@ -1113,17 +1128,17 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
if (firstType == tInt) { if (firstType == tInt) {
if (vTmp.type != tInt) if (vTmp.type != tInt)
throwEvalError("cannot add %1% to an integer", showType(vTmp)); throwEvalError("cannot add %1% to an integer, at %2%", showType(vTmp), pos);
n += vTmp.integer; n += vTmp.integer;
} else } else
s << state.coerceToString(vTmp, context, false, firstType == tString); s << state.coerceToString(pos, vTmp, context, false, firstType == tString);
} }
if (firstType == tInt) if (firstType == tInt)
mkInt(v, n); mkInt(v, n);
else if (firstType == tPath) { else if (firstType == tPath) {
if (!context.empty()) if (!context.empty())
throwEvalError("a string that refers to a store path cannot be appended to a path, in `%1%'", s.str()); throwEvalError("a string that refers to a store path cannot be appended to a path, at %1%", pos);
mkPath(v, s.str().c_str()); mkPath(v, s.str().c_str());
} else } else
mkString(v, s.str(), context); mkString(v, s.str(), context);
@ -1233,7 +1248,7 @@ bool EvalState::isDerivation(Value & v)
} }
string EvalState::coerceToString(Value & v, PathSet & context, string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
bool coerceMore, bool copyToStore) bool coerceMore, bool copyToStore)
{ {
forceValue(v); forceValue(v);
@ -1252,8 +1267,8 @@ string EvalState::coerceToString(Value & v, PathSet & context,
if (v.type == tAttrs) { if (v.type == tAttrs) {
Bindings::iterator i = v.attrs->find(sOutPath); Bindings::iterator i = v.attrs->find(sOutPath);
if (i == v.attrs->end()) throwTypeError("cannot coerce a set to a string"); if (i == v.attrs->end()) throwTypeError("cannot coerce a set to a string, at %1%", pos);
return coerceToString(*i->value, context, coerceMore, copyToStore); return coerceToString(pos, *i->value, context, coerceMore, copyToStore);
} }
if (coerceMore) { if (coerceMore) {
@ -1268,7 +1283,7 @@ string EvalState::coerceToString(Value & v, PathSet & context,
if (v.type == tList) { if (v.type == tList) {
string result; string result;
for (unsigned int n = 0; n < v.list.length; ++n) { for (unsigned int n = 0; n < v.list.length; ++n) {
result += coerceToString(*v.list.elems[n], result += coerceToString(pos, *v.list.elems[n],
context, coerceMore, copyToStore); context, coerceMore, copyToStore);
if (n < v.list.length - 1 if (n < v.list.length - 1
/* !!! not quite correct */ /* !!! not quite correct */
@ -1279,7 +1294,7 @@ string EvalState::coerceToString(Value & v, PathSet & context,
} }
} }
throwTypeError("cannot coerce %1% to a string", v); throwTypeError("cannot coerce %1% to a string, at %2%", v, pos);
} }
@ -1305,11 +1320,11 @@ string EvalState::copyPathToStore(PathSet & context, const Path & path)
} }
Path EvalState::coerceToPath(Value & v, PathSet & context) Path EvalState::coerceToPath(const Pos & pos, Value & v, PathSet & context)
{ {
string path = coerceToString(v, context, false, false); string path = coerceToString(pos, v, context, false, false);
if (path == "" || path[0] != '/') if (path == "" || path[0] != '/')
throwEvalError("string `%1%' doesn't represent an absolute path", path); throwEvalError("string `%1%' doesn't represent an absolute path, at %1%", path, pos);
return path; return path;
} }

View file

@ -177,7 +177,7 @@ public:
string. If `coerceMore' is set, also converts nulls, integers, string. If `coerceMore' is set, also converts nulls, integers,
booleans and lists to a string. If `copyToStore' is set, booleans and lists to a string. If `copyToStore' is set,
referenced paths are copied to the Nix store as a side effect. */ referenced paths are copied to the Nix store as a side effect. */
string coerceToString(Value & v, PathSet & context, string coerceToString(const Pos & pos, Value & v, PathSet & context,
bool coerceMore = false, bool copyToStore = true); bool coerceMore = false, bool copyToStore = true);
string copyPathToStore(PathSet & context, const Path & path); string copyPathToStore(PathSet & context, const Path & path);
@ -185,7 +185,7 @@ public:
/* Path coercion. Converts strings, paths and derivations to a /* Path coercion. Converts strings, paths and derivations to a
path. The result is guaranteed to be a canonicalised, absolute path. The result is guaranteed to be a canonicalised, absolute
path. Nothing is copied to the store. */ path. Nothing is copied to the store. */
Path coerceToPath(Value & v, PathSet & context); Path coerceToPath(const Pos & pos, Value & v, PathSet & context);
public: public:

View file

@ -13,7 +13,7 @@ string DrvInfo::queryDrvPath()
if (drvPath == "" && attrs) { if (drvPath == "" && attrs) {
Bindings::iterator i = attrs->find(state->sDrvPath); Bindings::iterator i = attrs->find(state->sDrvPath);
PathSet context; PathSet context;
drvPath = i != attrs->end() ? state->coerceToPath(*i->value, context) : ""; drvPath = i != attrs->end() ? state->coerceToPath(*i->pos, *i->value, context) : "";
} }
return drvPath; return drvPath;
} }
@ -24,7 +24,7 @@ string DrvInfo::queryOutPath()
if (outPath == "" && attrs) { if (outPath == "" && attrs) {
Bindings::iterator i = attrs->find(state->sOutPath); Bindings::iterator i = attrs->find(state->sOutPath);
PathSet context; PathSet context;
outPath = i != attrs->end() ? state->coerceToPath(*i->value, context) : ""; outPath = i != attrs->end() ? state->coerceToPath(*i->pos, *i->value, context) : "";
} }
return outPath; return outPath;
} }
@ -50,7 +50,7 @@ DrvInfo::Outputs DrvInfo::queryOutputs()
Bindings::iterator outPath = out->value->attrs->find(state->sOutPath); Bindings::iterator outPath = out->value->attrs->find(state->sOutPath);
if (outPath == out->value->attrs->end()) continue; // FIXME: throw error? if (outPath == out->value->attrs->end()) continue; // FIXME: throw error?
PathSet context; PathSet context;
outputs[name] = state->coerceToPath(*outPath->value, context); outputs[name] = state->coerceToPath(*outPath->pos, *outPath->value, context);
} }
} else } else
outputs["out"] = queryOutPath(); outputs["out"] = queryOutPath();

View file

@ -320,10 +320,11 @@ MakeBinOp(OpConcatLists, "++")
struct ExprConcatStrings : Expr struct ExprConcatStrings : Expr
{ {
Pos pos;
bool forceString; bool forceString;
vector<Expr *> * es; vector<Expr *> * es;
ExprConcatStrings(bool forceString, vector<Expr *> * es) ExprConcatStrings(const Pos & pos, bool forceString, vector<Expr *> * es)
: forceString(forceString), es(es) { }; : pos(pos), forceString(forceString), es(es) { };
COMMON_METHODS COMMON_METHODS
}; };

View file

@ -130,7 +130,7 @@ static void addFormal(const Pos & pos, Formals * formals, const Formal & formal)
} }
static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es) static Expr * stripIndentation(const Pos & pos, SymbolTable & symbols, vector<Expr *> & es)
{ {
if (es.empty()) return new ExprString(symbols.create("")); if (es.empty()) return new ExprString(symbols.create(""));
@ -216,7 +216,7 @@ static Expr * stripIndentation(SymbolTable & symbols, vector<Expr *> & es)
} }
/* If this is a single string, then don't do a concatenation. */ /* If this is a single string, then don't do a concatenation. */
return es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0]) ? (*es2)[0] : new ExprConcatStrings(true, es2); return es2->size() == 1 && dynamic_cast<ExprString *>((*es2)[0]) ? (*es2)[0] : new ExprConcatStrings(pos, true, es2);
} }
@ -344,7 +344,7 @@ expr_op
{ vector<Expr *> * l = new vector<Expr *>; { vector<Expr *> * l = new vector<Expr *>;
l->push_back($1); l->push_back($1);
l->push_back($3); l->push_back($3);
$$ = new ExprConcatStrings(false, l); $$ = new ExprConcatStrings(CUR_POS, false, l);
} }
| expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprBuiltin(data->symbols.create("sub")), $1), $3); } | expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprBuiltin(data->symbols.create("sub")), $1), $3); }
| expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprBuiltin(data->symbols.create("mul")), $1), $3); } | expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprBuiltin(data->symbols.create("mul")), $1), $3); }
@ -381,7 +381,7 @@ expr_simple
| INT { $$ = new ExprInt($1); } | INT { $$ = new ExprInt($1); }
| '"' string_parts '"' { $$ = $2; } | '"' string_parts '"' { $$ = $2; }
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE { | IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
$$ = stripIndentation(data->symbols, *$2); $$ = stripIndentation(CUR_POS, data->symbols, *$2);
} }
| PATH { $$ = new ExprPath(absPath($1, data->basePath)); } | PATH { $$ = new ExprPath(absPath($1, data->basePath)); }
| SPATH { | SPATH {
@ -413,7 +413,7 @@ expr_simple
string_parts string_parts
: STR : STR
| string_parts_interpolated { $$ = new ExprConcatStrings(true, $1); } | string_parts_interpolated { $$ = new ExprConcatStrings(CUR_POS, true, $1); }
| { $$ = new ExprString(data->symbols.create("")); } | { $$ = new ExprString(data->symbols.create("")); }
; ;
@ -509,7 +509,7 @@ attr
string_attr string_attr
: '"' string_parts '"' { $$ = $2; } : '"' string_parts '"' { $$ = $2; }
| DOLLAR_CURLY expr '}' { $$ = new ExprConcatStrings(true, new vector<Expr*>(1, $2)); } | DOLLAR_CURLY expr '}' { $$ = new ExprConcatStrings(CUR_POS, true, new vector<Expr*>(1, $2)); }
; ;
expr_list expr_list

View file

@ -42,7 +42,7 @@ std::pair<string, string> decodeContext(const string & s)
static void prim_import(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_import(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[0], context); Path path = state.coerceToPath(pos, *args[0], context);
foreach (PathSet::iterator, i, context) { foreach (PathSet::iterator, i, context) {
Path ctx = decodeContext(*i).first; Path ctx = decodeContext(*i).first;
@ -255,14 +255,14 @@ static void prim_abort(EvalState & state, const Pos & pos, Value * * args, Value
{ {
PathSet context; PathSet context;
throw Abort(format("evaluation aborted with the following error message: `%1%'") % throw Abort(format("evaluation aborted with the following error message: `%1%'") %
state.coerceToString(*args[0], context)); state.coerceToString(pos, *args[0], context));
} }
static void prim_throw(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_throw(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
throw ThrownError(format("%1%") % state.coerceToString(*args[0], context)); throw ThrownError(format("%1%") % state.coerceToString(pos, *args[0], context));
} }
@ -273,7 +273,7 @@ static void prim_addErrorContext(EvalState & state, const Pos & pos, Value * * a
v = *args[1]; v = *args[1];
} catch (Error & e) { } catch (Error & e) {
PathSet context; PathSet context;
e.addPrefix(format("%1%\n") % state.coerceToString(*args[0], context)); e.addPrefix(format("%1%\n") % state.coerceToString(pos, *args[0], context));
throw; throw;
} }
} }
@ -383,7 +383,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
if (key == "args") { if (key == "args") {
state.forceList(*i->value, pos); state.forceList(*i->value, pos);
for (unsigned int n = 0; n < i->value->list.length; ++n) { for (unsigned int n = 0; n < i->value->list.length; ++n) {
string s = state.coerceToString(*i->value->list.elems[n], context, true); string s = state.coerceToString(posDrvName, *i->value->list.elems[n], context, true);
drv.args.push_back(s); drv.args.push_back(s);
} }
} }
@ -391,7 +391,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
/* All other attributes are passed to the builder through /* All other attributes are passed to the builder through
the environment. */ the environment. */
else { else {
string s = state.coerceToString(*i->value, context, true); string s = state.coerceToString(posDrvName, *i->value, context, true);
drv.env[key] = s; drv.env[key] = s;
if (key == "builder") drv.builder = s; if (key == "builder") drv.builder = s;
else if (i->name == state.sSystem) drv.platform = s; else if (i->name == state.sSystem) drv.platform = s;
@ -558,7 +558,7 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
static void prim_toPath(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_toPath(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[0], context); Path path = state.coerceToPath(pos, *args[0], context);
mkString(v, canonPath(path), context); mkString(v, canonPath(path), context);
} }
@ -574,7 +574,7 @@ static void prim_toPath(EvalState & state, const Pos & pos, Value * * args, Valu
static void prim_storePath(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_storePath(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[0], context); Path path = state.coerceToPath(pos, *args[0], context);
/* Resolve symlinks in path, unless path itself is a symlink /* Resolve symlinks in path, unless path itself is a symlink
directly in the store. The latter condition is necessary so directly in the store. The latter condition is necessary so
e.g. nix-push does the right thing. */ e.g. nix-push does the right thing. */
@ -592,7 +592,7 @@ static void prim_storePath(EvalState & state, const Pos & pos, Value * * args, V
static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[0], context); Path path = state.coerceToPath(pos, *args[0], context);
if (!context.empty()) if (!context.empty())
throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos); throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos);
mkBool(v, pathExists(path)); mkBool(v, pathExists(path));
@ -604,7 +604,7 @@ static void prim_pathExists(EvalState & state, const Pos & pos, Value * * args,
static void prim_baseNameOf(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_baseNameOf(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
mkString(v, baseNameOf(state.coerceToString(*args[0], context)), context); mkString(v, baseNameOf(state.coerceToString(pos, *args[0], context)), context);
} }
@ -614,7 +614,7 @@ static void prim_baseNameOf(EvalState & state, const Pos & pos, Value * * args,
static void prim_dirOf(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_dirOf(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path dir = dirOf(state.coerceToPath(*args[0], context)); Path dir = dirOf(state.coerceToPath(pos, *args[0], context));
if (args[0]->type == tPath) mkPath(v, dir.c_str()); else mkString(v, dir, context); if (args[0]->type == tPath) mkPath(v, dir.c_str()); else mkString(v, dir, context);
} }
@ -623,7 +623,7 @@ static void prim_dirOf(EvalState & state, const Pos & pos, Value * * args, Value
static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[0], context); Path path = state.coerceToPath(pos, *args[0], context);
if (!context.empty()) if (!context.empty())
throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos); throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos);
mkString(v, readFile(path).c_str()); mkString(v, readFile(path).c_str());
@ -731,7 +731,7 @@ struct FilterFromExpr : PathFilter
static void prim_filterSource(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_filterSource(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
Path path = state.coerceToPath(*args[1], context); Path path = state.coerceToPath(pos, *args[1], context);
if (!context.empty()) if (!context.empty())
throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos); throw EvalError(format("string `%1%' cannot refer to other paths, at %2%") % path % pos);
@ -1105,7 +1105,7 @@ static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Va
static void prim_toString(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_toString(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
string s = state.coerceToString(*args[0], context, true, false); string s = state.coerceToString(pos, *args[0], context, true, false);
mkString(v, s, context); mkString(v, s, context);
} }
@ -1119,7 +1119,7 @@ static void prim_substring(EvalState & state, const Pos & pos, Value * * args, V
int start = state.forceInt(*args[0], pos); int start = state.forceInt(*args[0], pos);
int len = state.forceInt(*args[1], pos); int len = state.forceInt(*args[1], pos);
PathSet context; PathSet context;
string s = state.coerceToString(*args[2], context); string s = state.coerceToString(pos, *args[2], context);
if (start < 0) throw EvalError(format("negative start position in `substring', at %1%") % pos); if (start < 0) throw EvalError(format("negative start position in `substring', at %1%") % pos);
@ -1130,7 +1130,7 @@ static void prim_substring(EvalState & state, const Pos & pos, Value * * args, V
static void prim_stringLength(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_stringLength(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
string s = state.coerceToString(*args[0], context); string s = state.coerceToString(pos, *args[0], context);
mkInt(v, s.size()); mkInt(v, s.size());
} }
@ -1138,7 +1138,7 @@ static void prim_stringLength(EvalState & state, const Pos & pos, Value * * args
static void prim_unsafeDiscardStringContext(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_unsafeDiscardStringContext(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
string s = state.coerceToString(*args[0], context); string s = state.coerceToString(pos, *args[0], context);
mkString(v, s, PathSet()); mkString(v, s, PathSet());
} }
@ -1152,7 +1152,7 @@ static void prim_unsafeDiscardStringContext(EvalState & state, const Pos & pos,
static void prim_unsafeDiscardOutputDependency(EvalState & state, const Pos & pos, Value * * args, Value & v) static void prim_unsafeDiscardOutputDependency(EvalState & state, const Pos & pos, Value * * args, Value & v)
{ {
PathSet context; PathSet context;
string s = state.coerceToString(*args[0], context); string s = state.coerceToString(pos, *args[0], context);
PathSet context2; PathSet context2;
foreach (PathSet::iterator, i, context) { foreach (PathSet::iterator, i, context) {

View file

@ -121,8 +121,10 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
debug("evaluating user environment builder"); debug("evaluating user environment builder");
state.forceValue(topLevel); state.forceValue(topLevel);
PathSet context; PathSet context;
Path topLevelDrv = state.coerceToPath(*topLevel.attrs->find(state.sDrvPath)->value, context); Attr & aDrvPath(*topLevel.attrs->find(state.sDrvPath));
Path topLevelOut = state.coerceToPath(*topLevel.attrs->find(state.sOutPath)->value, context); Path topLevelDrv = state.coerceToPath(aDrvPath.pos ? *(aDrvPath.pos) : noPos, *(aDrvPath.value), context);
Attr & aOutPath(*topLevel.attrs->find(state.sOutPath));
Path topLevelOut = state.coerceToPath(aOutPath.pos ? *(aOutPath.pos) : noPos, *(aOutPath.value), context);
/* Realise the resulting store expression. */ /* Realise the resulting store expression. */
debug("building user environment"); debug("building user environment");