Skip to content

Commit

Permalink
add pos to errorinfo, remove from hints
Browse files Browse the repository at this point in the history
  • Loading branch information
bburdette committed May 9, 2020
1 parent 1b801ce commit 55eb717
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 193 deletions.
17 changes: 14 additions & 3 deletions src/error-demo/error-demo.cc
Expand Up @@ -126,17 +126,28 @@ int main()
// Error with previous and next lines of code.
logError(
ErrorInfo { .name = "error name",
.description = "error description",
.description = "error with code lines",
.hint = hintfmt("this hint has %1% templated %2%!!",
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::optional("previous line of code"),
.prevLineOfCode = "previous line of code",
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::optional("next line of code"),
.nextLineOfCode = "next line of code",
}});


// Error without lines of code.
logError(
ErrorInfo { .name = "error name",
.description = "error without any code lines.",
.hint = hintfmt("this hint has %1% templated %2%!!",
"yellow",
"values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13)
}});

return 0;
}
7 changes: 6 additions & 1 deletion src/libexpr/attr-set.hh
Expand Up @@ -76,7 +76,12 @@ public:
{
auto a = get(name);
if (!a)
throw Error("attribute '%s' missing, at %s", name, pos);
throw Error(
ErrorInfo {
.hint = hintfmt("attribute '%s' missing", name),
.nixCode = NixCode { .errPos = pos }
});

return *a;
}

Expand Down
18 changes: 13 additions & 5 deletions src/libexpr/eval-inline.hh
Expand Up @@ -9,7 +9,11 @@ namespace nix {

LocalNoInlineNoReturn(void throwEvalError(const char * s, const Pos & pos))
{
throw EvalError(s, pos);
throw EvalError(
ErrorInfo {
.hint = hintfmt(s),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwTypeError(const char * s, const Value & v))
Expand All @@ -20,7 +24,11 @@ LocalNoInlineNoReturn(void throwTypeError(const char * s, const Value & v))

LocalNoInlineNoReturn(void throwTypeError(const char * s, const Value & v, const Pos & pos))
{
throw TypeError(s, showType(v), pos);
throw TypeError(
ErrorInfo {
.hint = hintfmt(s, showType(v)),
.nixCode = NixCode { .errPos = pos }
});
}


Expand All @@ -43,7 +51,7 @@ void EvalState::forceValue(Value & v, const Pos & pos)
else if (v.type == tApp)
callFunction(*v.app.left, *v.app.right, v, noPos);
else if (v.type == tBlackhole)
throwEvalError("infinite recursion encountered, at %1%", pos);
throwEvalError("infinite recursion encountered", pos);
}


Expand All @@ -59,7 +67,7 @@ inline void EvalState::forceAttrs(Value & v, const Pos & pos)
{
forceValue(v);
if (v.type != tAttrs)
throwTypeError("value is %1% while a set was expected, at %2%", v, pos);
throwTypeError("value is %1% while a set was expected", v, pos);
}


Expand All @@ -75,7 +83,7 @@ inline void EvalState::forceList(Value & v, const Pos & pos)
{
forceValue(v);
if (!v.isList())
throwTypeError("value is %1% while a list was expected, at %2%", v, pos);
throwTypeError("value is %1% while a list was expected", v, pos);
}

/* Note: Various places expect the allocated memory to be zeroed. */
Expand Down
76 changes: 52 additions & 24 deletions src/libexpr/eval.cc
Expand Up @@ -498,7 +498,11 @@ LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2))

LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const Pos & pos))
{
throw EvalError(s, s2, pos);
throw EvalError(
ErrorInfo {
.hint = hintfmt(s, s2),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3))
Expand All @@ -508,7 +512,11 @@ LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, con

LocalNoInlineNoReturn(void throwEvalError(const char * s, const string & s2, const string & s3, const Pos & pos))
{
throw EvalError(s, s2, s3, pos);
throw EvalError(
ErrorInfo {
.hint = hintfmt(s, s2, s3),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwEvalError(const char * s, const Symbol & sym, const Pos & p1, const Pos & p2))
Expand All @@ -518,7 +526,11 @@ LocalNoInlineNoReturn(void throwEvalError(const char * s, const Symbol & sym, co

LocalNoInlineNoReturn(void throwTypeError(const char * s, const Pos & pos))
{
throw TypeError(s, pos);
throw TypeError(
ErrorInfo {
.hint = hintfmt(s),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwTypeError(const char * s, const string & s1))
Expand All @@ -528,17 +540,29 @@ LocalNoInlineNoReturn(void throwTypeError(const char * s, const string & s1))

LocalNoInlineNoReturn(void throwTypeError(const char * s, const ExprLambda & fun, const Symbol & s2, const Pos & pos))
{
throw TypeError(s, fun.showNamePos(), s2, pos);
throw TypeError(
ErrorInfo {
.hint = hintfmt(s, fun.showNamePos(), s2),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwAssertionError(const char * s, const string & s1, const Pos & pos))
{
throw AssertionError(s, s1, pos);
throw AssertionError(
ErrorInfo {
.hint = hintfmt(s, s1),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInlineNoReturn(void throwUndefinedVarError(const char * s, const string & s1, const Pos & pos))
{
throw UndefinedVarError(s, s1, pos);
throw UndefinedVarError(
ErrorInfo {
.hint = hintfmt(s, s1),
.nixCode = NixCode { .errPos = pos }
});
}

LocalNoInline(void addErrorPrefix(Error & e, const char * s, const string & s2))
Expand Down Expand Up @@ -804,7 +828,7 @@ inline bool EvalState::evalBool(Env & env, Expr * e, const Pos & pos)
Value v;
e->eval(*this, env, v);
if (v.type != tBool)
throwTypeError("value is %1% while a Boolean was expected, at %2%", v, pos);
throwTypeError("value is %1% while a Boolean was expected", v, pos);
return v.boolean;
}

Expand Down Expand Up @@ -1006,7 +1030,7 @@ void ExprSelect::eval(EvalState & state, Env & env, Value & v)
} else {
state.forceAttrs(*vAttrs, pos);
if ((j = vAttrs->attrs->find(name)) == vAttrs->attrs->end())
throwEvalError("attribute '%1%' missing, at %2%", name, pos);
throwEvalError("attribute '%1%' missing", name, pos);
}
vAttrs = j->value;
pos2 = j->pos;
Expand Down Expand Up @@ -1132,7 +1156,7 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po
}

if (fun.type != tLambda)
throwTypeError("attempt to call something which is not a function but %1%, at %2%", fun, pos);
throwTypeError("attempt to call something which is not a function but %1%", fun, pos);

ExprLambda & lambda(*fun.lambda.fun);

Expand Down Expand Up @@ -1160,7 +1184,7 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po
for (auto & i : lambda.formals->formals) {
Bindings::iterator j = arg.attrs->find(i.name);
if (j == arg.attrs->end()) {
if (!i.def) throwTypeError("%1% called without required argument '%2%', at %3%",
if (!i.def) throwTypeError("%1% called without required argument '%2%'",
lambda, i.name, pos);
env2.values[displ++] = i.def->maybeThunk(*this, env2);
} else {
Expand All @@ -1176,7 +1200,7 @@ void EvalState::callFunction(Value & fun, Value & arg, Value & v, const Pos & po
user. */
for (auto & i : *arg.attrs)
if (lambda.formals->argNames.find(i.name) == lambda.formals->argNames.end())
throwTypeError("%1% called with unexpected argument '%2%', at %3%", lambda, i.name, pos);
throwTypeError("%1% called with unexpected argument '%2%'", lambda, i.name, pos);
abort(); // can't happen
}
}
Expand Down Expand Up @@ -1417,14 +1441,14 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
nf = n;
nf += vTmp.fpoint;
} else
throwEvalError("cannot add %1% to an integer, at %2%", showType(vTmp), pos);
throwEvalError("cannot add %1% to an integer", showType(vTmp), pos);
} else if (firstType == tFloat) {
if (vTmp.type == tInt) {
nf += vTmp.integer;
} else if (vTmp.type == tFloat) {
nf += vTmp.fpoint;
} else
throwEvalError("cannot add %1% to a float, at %2%", showType(vTmp), pos);
throwEvalError("cannot add %1% to a float", showType(vTmp), pos);
} else
s << state.coerceToString(pos, vTmp, context, false, firstType == tString);
}
Expand All @@ -1435,7 +1459,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
mkFloat(v, nf);
else if (firstType == tPath) {
if (!context.empty())
throwEvalError("a string that refers to a store path cannot be appended to a path, at %1%", pos);
throwEvalError("a string that refers to a store path cannot be appended to a path", pos);
auto path = canonPath(s.str());
mkPath(v, path.c_str());
} else
Expand Down Expand Up @@ -1484,7 +1508,7 @@ NixInt EvalState::forceInt(Value & v, const Pos & pos)
{
forceValue(v, pos);
if (v.type != tInt)
throwTypeError("value is %1% while an integer was expected, at %2%", v, pos);
throwTypeError("value is %1% while an integer was expected", v, pos);
return v.integer;
}

Expand All @@ -1495,7 +1519,7 @@ NixFloat EvalState::forceFloat(Value & v, const Pos & pos)
if (v.type == tInt)
return v.integer;
else if (v.type != tFloat)
throwTypeError("value is %1% while a float was expected, at %2%", v, pos);
throwTypeError("value is %1% while a float was expected", v, pos);
return v.fpoint;
}

Expand All @@ -1504,7 +1528,7 @@ bool EvalState::forceBool(Value & v, const Pos & pos)
{
forceValue(v);
if (v.type != tBool)
throwTypeError("value is %1% while a Boolean was expected, at %2%", v, pos);
throwTypeError("value is %1% while a Boolean was expected", v, pos);
return v.boolean;
}

Expand All @@ -1519,7 +1543,7 @@ void EvalState::forceFunction(Value & v, const Pos & pos)
{
forceValue(v);
if (v.type != tLambda && v.type != tPrimOp && v.type != tPrimOpApp && !isFunctor(v))
throwTypeError("value is %1% while a function was expected, at %2%", v, pos);
throwTypeError("value is %1% while a function was expected", v, pos);
}


Expand All @@ -1528,7 +1552,7 @@ string EvalState::forceString(Value & v, const Pos & pos)
forceValue(v, pos);
if (v.type != tString) {
if (pos)
throwTypeError("value is %1% while a string was expected, at %2%", v, pos);
throwTypeError("value is %1% while a string was expected", v, pos);
else
throwTypeError("value is %1% while a string was expected", v);
}
Expand Down Expand Up @@ -1557,7 +1581,7 @@ string EvalState::forceStringNoCtx(Value & v, const Pos & pos)
string s = forceString(v, pos);
if (v.string.context) {
if (pos)
throwEvalError("the string '%1%' is not allowed to refer to a store path (such as '%2%'), at %3%",
throwEvalError("the string '%1%' is not allowed to refer to a store path (such as '%2%')",
v.string.s, v.string.context[0], pos);
else
throwEvalError("the string '%1%' is not allowed to refer to a store path (such as '%2%')",
Expand Down Expand Up @@ -1614,7 +1638,7 @@ string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
return *maybeString;
}
auto i = v.attrs->find(sOutPath);
if (i == v.attrs->end()) throwTypeError("cannot coerce a set to a string, at %1%", pos);
if (i == v.attrs->end()) throwTypeError("cannot coerce a set to a string", pos);
return coerceToString(pos, *i->value, context, coerceMore, copyToStore);
}

Expand Down Expand Up @@ -1645,7 +1669,7 @@ string EvalState::coerceToString(const Pos & pos, Value & v, PathSet & context,
}
}

throwTypeError("cannot coerce %1% to a string, at %2%", v, pos);
throwTypeError("cannot coerce %1% to a string", v, pos);
}


Expand Down Expand Up @@ -1676,7 +1700,7 @@ Path EvalState::coerceToPath(const Pos & pos, Value & v, PathSet & context)
{
string path = coerceToString(pos, v, context, false, false);
if (path == "" || path[0] != '/')
throwEvalError("string '%1%' doesn't represent an absolute path, at %2%", path, pos);
throwEvalError("string '%1%' doesn't represent an absolute path", path, pos);
return path;
}

Expand Down Expand Up @@ -1883,7 +1907,11 @@ void EvalState::printStats()

string ExternalValueBase::coerceToString(const Pos & pos, PathSet & context, bool copyMore, bool copyToStore) const
{
throw TypeError("cannot coerce %1% to a string, at %2%", showType(), pos);
throw TypeError(
ErrorInfo {
.hint = hintfmt("cannot coerce %1% to a string", showType()),
.nixCode = NixCode { .errPos = pos }
});
}


Expand Down
17 changes: 12 additions & 5 deletions src/libexpr/parser.y
Expand Up @@ -404,7 +404,12 @@ expr_simple
| URI {
static bool noURLLiterals = settings.isExperimentalFeatureEnabled("no-url-literals");
if (noURLLiterals)
throw ParseError("URL literals are disabled, at %s", CUR_POS);
throw ParseError(
ErrorInfo {
.hint = hintfmt("URL literals are disabled"),
.nixCode = NixCode { .errPos = CUR_POS }
});

$$ = new ExprString(data->symbols.create($1));
}
| '(' expr ')' { $$ = $2; }
Expand Down Expand Up @@ -669,10 +674,12 @@ Path EvalState::findFile(SearchPath & searchPath, const string & path, const Pos
Path res = r.second + suffix;
if (pathExists(res)) return canonPath(res);
}
string f =
"file '%1%' was not found in the Nix search path (add it using $NIX_PATH or -I)"
+ string(pos ? ", at %2%" : "");
throw ThrownError(f, path, pos);

throw ThrownError(
ErrorInfo {
.hint = hintfmt("file '%1%' was not found in the Nix search path (add it using $NIX_PATH or -I)", path),
.nixCode = NixCode { .errPos = pos }
});
}


Expand Down

0 comments on commit 55eb717

Please sign in to comment.