You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make error more descriptive when non-sink argument is used instead of expected sink argument #2102
The error did throw me off, but after an answer and some "digging" through the documentation, the error was resolved. In any case, the error message should be more descriptive and user-friendly.
So, naively, this would be applied to the result of Numbering::{apply_vm,apply_vt} if any of the contained errors was of this type, but errors currently don't carry any strongly typed kind.
I was actually thinking of adding these when I wrote #2385, but refrained as it was out of scope and had no immediate benefit.
Personally, I think adding an error code to diagnostics would make attaching hints to diagnostics much easier, as we wouldn't need to rely on string comparisons to find out if we have a specific diagnostic category here.
Yes, it is! The confusing situation always arises when the "unexpected argument" error (which is supposed to be annotated where the user typed argument) is instead annotated at the parameter because the argument is compiler generated and has no source location (= span). In this case, the argument's span defaults to the function's span:
You could look into detecting that the argument's span matches the function's span (args.span == func.span()) and producing a better error message in this case. This would be a simple way to detect the situation. There might be a way to make it cleaner by not assigning the default span in this way at all, but I'm not sure it would work out easily.
Ah, the check is performed at the call-site of the anonymous function passed as numbering, but the span points to argument it of said anonymous function, is that it?
So ideally, what kind of error would we want to display? Something like this function receives a sink argument but expects a non-sink argument?
I think that error message isn't too clear, to be honest. The term "sink argument" isn't really a thing. If anything, it would be spreaded arguments. But we don't know whether arguments were spreaded at the point where I proposed the check. We only know that there are multiple arguments and that our anonymous function was not prepared to handle all of them.
Plus a hint that says that you gotta try using something like (..n).
We also don't know whether an argument sink is the best solution. The function could just always be passed two arguments, but have just one parameter.
What the error message should express, in my opinion, is that the function was called with a different number of arguments than it has parameters and that one should either adjust the number of parameters or consider using an argument sink.
Spread operator is when you pass something in, not when you receive something.
We also don't know whether an argument sink is the best solution.
It is the most idiomatic solution with regards to numbering. It is the solution that 100% will work, because it captures any arguments. Any other solution are more specific and hint can't cover them all, it's just a hint to fix the error.
The function could just always be passed two arguments, but have just one parameter.
#let f(one) = {};#f(1, 2) produces an error, so this statement is incorrect. I don't understand it at all.
What the error message should express, in my opinion, is that the function was called with a different number of arguments than it has parameters and that one should either adjust the number of parameters or consider using an argument sink.
If it can't detect the callee function/field, then sure, but if it can, then it's better to tailor the explanation to fit better to the situation.
Not to be pedantic, but it technically isn't as that link refers to argument sinks, not sink arguments, this is because a sink may receive many arguments.
Spread operator is when you pass something in, not when you receive something.
We also don't know whether an argument sink is the best solution.
It is the most idiomatic solution with regards to numbering. It is the solution that 100% will work, because it captures any arguments. Any other solution are more specific and hint can't cover them all, it's just a hint to fix the error.
The problem is that from within Typst, it's not immediately clear if this is related to numbering at all. This kind of error can be emitted anywhere where an unexpected number of arguments is passed to some anonymous function.
The function could just always be passed two arguments, but have just one parameter.
#let f(one) = {};#f(1, 2) produces an error, so this statement is incorrect. I don't understand it at all.
He's saying that this same error would be emitted in this exact case or similar cases.
What the error message should express, in my opinion, is that the function was called with a different number of arguments than it has parameters and that one should either adjust the number of parameters or consider using an argument sink.
If it can't detect the callee function/field, then sure, but if it can, then it's better to tailor the explanation to fit better to the situation.
This once again assumes that numbering is the titular situation, but it is only one situation where this happens, plenty of packages use callbacks with specific parameter counts, all of which would suffer UX wise if the message was hand tailored to assume it's a numbering function.
If I read things correctly, an easy (but probably incomplete) fix would be to simply replace
bail!(arg.span,"unexpected argument")
with something along the lines of
bail!(arg.span,"wrong number number of arguments, {n} arguments were not consumed", n=self.items.len())
without attempting to be smart (i.e. still just two cases). Is this correct?
Now, if I read things correctly, to do things better, we'd need the ability to detect why an argument was not consumed, as it could be because we're passing/expecting a spread, because this would help us customize the error message? At this case, my understanding of the goal is insufficient. If we want to head in this direction, I'd need to have a few examples of the various cases in which we want to improve the error messages.
I think what you wrote is already an improvement over what we have now. And yes, We need more information to be able to tell the user why an argument may be unexpected. I've briefly looked at this in the past, but at least with my understanding at that time it looked like a non-trivial refactor of the argument handling code.
I think adding number to the error is great. Though I think it's better to say how many arguments were given and used, because this is information that you probably have, not the difference between the two.
I don't know if infrastructure allows detecting numbering case or how hard it will be to make it work. But that would be ideal.
I've been trying to add some more information to Args to track the initial number of arguments, but this breaks test_value_size by making Args too large.
My hope was that the following error message would make sense:
if list.len() < n {bail!(self.span,"wrong number number of arguments: {} arguments were given, but we're trying to consume at least {} arguments",self.initial_len,self.items.len() + n,)}
How should I approach this? I suppose that I could box Args in Value, but I expect that this will have a performance/memory cost.
I think this is derailing a bit. I wouldn't want to replace the "unexpected argument" error everywhere. The message above is a bit verbose and technical in my opinion.
I think we should go back to what I mentioned in #2102 (comment) and only have a more specific message for the cases where both spans match, e.g. something in the direction of function received more arguments than it expected.
Activity
tingerrr commentedon Oct 31, 2023
So, naively, this would be applied to the result of
Numbering::{apply_vm,apply_vt}if any of the contained errors was of this type, but errors currently don't carry any strongly typed kind.I was actually thinking of adding these when I wrote #2385, but refrained as it was out of scope and had no immediate benefit.
Personally, I think adding an error code to diagnostics would make attaching hints to diagnostics much easier, as we wouldn't need to rely on string comparisons to find out if we have a specific diagnostic category here.
Yoric commentedon May 7, 2025
Is this issue still valid? If so, I'd be happy to take a look at it.
laurmaedje commentedon May 7, 2025
Yes, it is! The confusing situation always arises when the "unexpected argument" error (which is supposed to be annotated where the user typed argument) is instead annotated at the parameter because the argument is compiler generated and has no source location (= span). In this case, the argument's span defaults to the function's span:
typst/crates/typst-library/src/foundations/func.rs
Line 297 in 9b09146
The error is then produced here:
typst/crates/typst-eval/src/call.rs
Line 250 in 9b09146
You could look into detecting that the argument's span matches the function's span (
args.span == func.span()) and producing a better error message in this case. This would be a simple way to detect the situation. There might be a way to make it cleaner by not assigning the default span in this way at all, but I'm not sure it would work out easily.Yoric commentedon May 7, 2025
Thanks for the reply, @laurmaedje. Trying to parse this. Please bear with me, this is my first attempt at contributing to typst :)
If I understand correctly your links, the check is performed at the call-site. In the case of
does this mean that the callee is
headingitself and that the span is this entire expression?To reproduce the error, would it be sufficient to add e.g.
to
tests/foundations/arguments.typ?laurmaedje commentedon May 7, 2025
The check is performed at the call-site of
it => it, which is within the heading show rule (indirectly through the numbering code):typst/crates/typst-library/src/model/heading.rs
Lines 236 to 238 in 9b09146
typst/crates/typst-library/src/introspection/counter.rs
Line 671 in 9b09146
typst/crates/typst-library/src/model/numbering.rs
Line 112 in 9b09146
typst/crates/typst-library/src/foundations/func.rs
Line 297 in 9b09146
Yes
Yoric commentedon May 7, 2025
Ah, the check is performed at the call-site of the anonymous function passed as
numbering, but the span points to argumentitof said anonymous function, is that it?Yoric commentedon May 7, 2025
So ideally, what kind of error would we want to display? Something like
this function receives a sink argument but expects a non-sink argument?Andrew15-5 commentedon May 7, 2025
Something like that. Plus a hint that says that you gotta try using something like
(..n).laurmaedje commentedon May 8, 2025
I think that error message isn't too clear, to be honest. The term "sink argument" isn't really a thing. If anything, it would be spreaded arguments. But we don't know whether arguments were spreaded at the point where I proposed the check. We only know that there are multiple arguments and that our anonymous function was not prepared to handle all of them.
We also don't know whether an argument sink is the best solution. The function could just always be passed two arguments, but have just one parameter.
What the error message should express, in my opinion, is that the function was called with a different number of arguments than it has parameters and that one should either adjust the number of parameters or consider using an argument sink.
Andrew15-5 commentedon May 8, 2025
It's literally a thing: https://typst.app/docs/reference/foundations/arguments/#argument-sinks.
Spread operator is when you pass something in, not when you receive something.
It is the most idiomatic solution with regards to numbering. It is the solution that 100% will work, because it captures any arguments. Any other solution are more specific and hint can't cover them all, it's just a hint to fix the error.
#let f(one) = {};#f(1, 2)produces an error, so this statement is incorrect. I don't understand it at all.If it can't detect the callee function/field, then sure, but if it can, then it's better to tailor the explanation to fit better to the situation.
tingerrr commentedon May 9, 2025
Not to be pedantic, but it technically isn't as that link refers to argument sinks, not sink arguments, this is because a sink may receive many arguments.
The problem is that from within Typst, it's not immediately clear if this is related to numbering at all. This kind of error can be emitted anywhere where an unexpected number of arguments is passed to some anonymous function.
He's saying that this same error would be emitted in this exact case or similar cases.
This once again assumes that numbering is the titular situation, but it is only one situation where this happens, plenty of packages use callbacks with specific parameter counts, all of which would suffer UX wise if the message was hand tailored to assume it's a numbering function.
reknih commentedon May 9, 2025
@Yoric should I assign you to the issue so that everyone knows you're working on a PR?
Andrew15-5 commentedon May 9, 2025
Tailored means knowing the situation, not assuming things that are not true.
Yoric commentedon May 9, 2025
I think I'm getting lost in this conversation :)
If I read things correctly, an easy (but probably incomplete) fix would be to simply replace
with something along the lines of
without attempting to be smart (i.e. still just two cases). Is this correct?
Now, if I read things correctly, to do things better, we'd need the ability to detect why an argument was not consumed, as it could be because we're passing/expecting a spread, because this would help us customize the error message? At this case, my understanding of the goal is insufficient. If we want to head in this direction, I'd need to have a few examples of the various cases in which we want to improve the error messages.
Is this correct?
Yoric commentedon Jun 3, 2025
ping @Andrew15-5 @reknih @tingerrr ?
tingerrr commentedon Jun 3, 2025
I think what you wrote is already an improvement over what we have now. And yes, We need more information to be able to tell the user why an argument may be unexpected. I've briefly looked at this in the past, but at least with my understanding at that time it looked like a non-trivial refactor of the argument handling code.
Andrew15-5 commentedon Jun 4, 2025
number number
I think adding number to the error is great. Though I think it's better to say how many arguments were given and used, because this is information that you probably have, not the difference between the two.
I don't know if infrastructure allows detecting numbering case or how hard it will be to make it work. But that would be ideal.
Yoric commentedon Jun 4, 2025
I've been trying to add some more information to
Argsto track the initial number of arguments, but this breakstest_value_sizeby makingArgstoo large.My hope was that the following error message would make sense:
How should I approach this? I suppose that I could box
ArgsinValue, but I expect that this will have a performance/memory cost.laurmaedje commentedon Jun 4, 2025
I think this is derailing a bit. I wouldn't want to replace the "unexpected argument" error everywhere. The message above is a bit verbose and technical in my opinion.
I think we should go back to what I mentioned in #2102 (comment) and only have a more specific message for the cases where both spans match, e.g. something in the direction of
function received more arguments than it expected.Improve error message when function receives too many compiler-genera…
eval: report descriptive error when a function receives excess compil…