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

Better stringify list error #1764

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ def _ValueToPartValue(val, quoted, part_loc):
return part_value.Array(val.d.values())

# Cases added for YSH
# value_e.List is also here - we use val_ops.stringify()s err message
elif case(value_e.Null, value_e.Bool, value_e.Int, value_e.Float,
value_e.Eggex):
value_e.Eggex, value_e.List):
s = val_ops.Stringify(val, loc.Missing)
return part_value.String(s, quoted, not quoted)

Expand Down
5 changes: 5 additions & 0 deletions ysh/val_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def Stringify(val, blame_loc, prefix=''):
val = cast(value.Eggex, UP_val)
s = regex_translate.AsPosixEre(val) # lazily converts to ERE

elif case(value_e.List):
raise error.TypeErrVerbose(
"%got a List, which can't be Stringified! Use @ instead of $ or join()" % prefix,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo -- should be %s and not %g

I would say

"Maybe use @ to splice, instead of $"

The problem is that this error can actually appear in many places

We have noticed in the past that overly specific error messages can become misleading -- i.e. trying to be helpful doesn't always work:

https://oilshell.zulipchat.com/#narrow/stream/384942-language-design/topic/Problem.20with.20our.20static.20checks


It would also help to add another function to test/ysh-runtime-errors.sh, with all the examples that are intended to be changed by this commit

Then we can run it and see how they look.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility to check the AST if we're at a $ ?
The "dream" scenario IMO would be something like:

ysh ysh-0.19.0$ write $[:|a b c|]                             write $[:|a b c|]
        ^~ Use @ instead of $ to slice
[ interactive ]:1: fatal: expected Null, Bool, Int, Float, Eggex, got List. Can't stringify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible in theory ... we have all the info necessary

But I don't think we have any examples of it. We would have to enumerate all the cases and make sure that the error makes sense.

I think this actually DOES exist in test/ysh-runtime-errors.sh, but I suppose it's not organized well enough to actually find them! I guess we could grep the output and find them.

Whenever I write error messages, I put examples in that file, and eyeball them to see if they make sense.


As mentioned it's easy to construct an error for a specific situations, and then find it doesn't apply to other situations that hit the same code path

I think it's OK to say "Maybe" or "Hint" in those cases

blame_loc)

else:
raise error.TypeErr(
val, "%sexpected Null, Bool, Int, Float, Eggex" % prefix,
Expand Down
Loading