-
Notifications
You must be signed in to change notification settings - Fork 89
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(failed): show function #586
base: master
Are you sure you want to change the base?
Conversation
@@ -61,7 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed { | |||
self.is_parsed = true; | |||
return Ok(()); | |||
} else { | |||
return error!(meta, tok, "Failed expression must be followed by a block or statement") | |||
return error!(meta, tok, format!("The function '{}' requires a 'failed' block or statement to handle errors", meta.get_token_at(meta.get_index() - 4).unwrap().word)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good rule of thumb: suspect any code with magic numbers, like "4" here; there will probably be some edge case not covered by it. For example:
hwalters@Ghostwheel ~/git/amber-lang (210)
$ cat untrusted.ab
$ echo 'Hello world' $
hwalters@Ghostwheel ~/git/amber-lang (210)
$ amber untrusted.ab
thread 'main' panicked at src/modules/condition/failed.rs:64:171:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I'll see if I can find a better way of doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wasn't sure too.
Maybe a check if there are parenthesis because probably with built-in doesn't work too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, the error message "The function '{}' requires..." implies that you expect the $...$
block to be run inside a function; but as my example shows, this is not always the case. I think you need to reword the error message so it doesn't reference a function name at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which is exactly what we did before. So, based on the above comment, I would question whether we need this PR at all?
One way we could solve this is by including an If this sounds convoluted, I'll open my PR tomorrow to show what I mean |
@@ -61,7 +61,7 @@ impl SyntaxModule<ParserMetadata> for Failed { | |||
self.is_parsed = true; | |||
return Ok(()); | |||
} else { | |||
return error!(meta, tok, "Failed expression must be followed by a block or statement") | |||
return error!(meta, tok, format!("The function '{}' requires a 'failed' block or statement to handle errors", meta.get_token_at(meta.get_index() - 4).unwrap().word)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This module cannot move back to get the function name. The function invocation should provide this module with the name
You know better than anyone else so I think that your proposal is good. |
On my daily check if I can resolve some bugs in Amber I tried with #210
I am not sure about
meta.get_index() - 4
to get the function with the issue.Using the amberr code:
The
meta
is:So I think that the actual index is always the next symbol where it should be the
failed
, in this case a}
, followed by the closing parenthesis of the function, the parameters, the open parenthesis and finally the function name.Right now amber doesn't share in the context what is the function or the line is processing, but in this way with
- 4
we start from the end that it is more easy to be right so we can ignore theecho
token.Anyway I am not so skilled in the internals but let me know if needs some changes internally before to be able to close that ticket.