Skip to content

Make error more descriptive when non-sink argument is used instead of expected sink argument #2102

Open

Description

@Andrew15-5

The issue #2101 was created due to the next error message:

error: unexpected argument
   ┌─ /file.typ:42:21
   │
42 │   heading(numbering: it => it, name, ..args)
   │                      ^^

It shows that the argument is unexpected, but in reality it is of course expected, and instead of a normal argument, it should be a sink argument:

42 │   heading(numbering: (..it) => it, name, ..args)

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.

Activity

  1. added
    scripting
    diagnostics
    on Sep 11, 2023
  2. tingerrr commented on Oct 31, 2023

    @tingerrr
    Contributor

    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.

  3. added
    feature request
    on Nov 14, 2023
  4. added
    good first issue
    good contribution
    on Jul 16, 2024
  5. Yoric commented on May 7, 2025

    @Yoric

    Is this issue still valid? If so, I'd be happy to take a look at it.

  6. laurmaedje commented on May 7, 2025

    @laurmaedje
    Member

    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:

    self.call_impl(engine, context, args.into_args(self.span))

    The error is then produced here:

    args.finish()?;

    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.

  7. Yoric commented on May 7, 2025

    @Yoric

    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

    heading(numbering: it => it, name, ..args)

    does this mean that the callee is heading itself and that the span is this entire expression?

    To reproduce the error, would it be sufficient to add e.g.

    --- arguments-not-really-unexpected ---
    // Error: This is the error message I'd really like to see
    #heading(level: 2, numbering: it => it)[]
    

    to tests/foundations/arguments.typ?

  8. laurmaedje commented on May 7, 2025

    @laurmaedje
    Member

    The check is performed at the call-site of it => it, which is within the heading show rule (indirectly through the numbering code):

    let numbering = Counter::of(HeadingElem::elem())
    .display_at_loc(engine, location, styles, numbering)?
    .spanned(span);

    numbering.apply(engine, context, &self.0)

    Self::Func(func) => func.call(engine, context, numbers.iter().copied())?,

    self.call_impl(engine, context, args.into_args(self.span))

    To reproduce the error, would it be sufficient to add e.g.

    Yes

  9. Yoric commented on May 7, 2025

    @Yoric

    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?

  10. Yoric commented on May 7, 2025

    @Yoric

    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?

  11. Andrew15-5 commented on May 7, 2025

    @Andrew15-5
    ContributorAuthor

    Something like that. Plus a hint that says that you gotta try using something like (..n).

  12. laurmaedje commented on May 8, 2025

    @laurmaedje
    Member

    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.

  13. Andrew15-5 commented on May 8, 2025

    @Andrew15-5
    ContributorAuthor

    The term "sink argument" isn't really a thing.

    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.

    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.

  14. tingerrr commented on May 9, 2025

    @tingerrr
    Contributor

    The term "sink argument" isn't really a thing.

    It's literally a thing: https://typst.app/docs/reference/foundations/arguments/#argument-sinks.

    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.

  15. reknih commented on May 9, 2025

    @reknih
    Member

    @Yoric should I assign you to the issue so that everyone knows you're working on a PR?

  16. Andrew15-5 commented on May 9, 2025

    @Andrew15-5
    ContributorAuthor

    all of which would suffer UX wise if the message was hand tailored to assume it's a numbering function.

    Tailored means knowing the situation, not assuming things that are not true.

  17. Yoric commented on May 9, 2025

    @Yoric

    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

    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.

    Is this correct?

  18. Yoric commented on Jun 3, 2025

    @Yoric
  19. tingerrr commented on Jun 3, 2025

    @tingerrr
    Contributor

    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.

  20. Andrew15-5 commented on Jun 4, 2025

    @Andrew15-5
    ContributorAuthor
    bail!(arg.span, "wrong number number of arguments, {n} arguments were not consumed", n=self.items.len())

    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.

  21. Yoric commented on Jun 4, 2025

    @Yoric

    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.

  22. laurmaedje commented on Jun 4, 2025

    @laurmaedje
    Member

    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.

  23. added a commit that references this issue on Feb 28, 2026
    fb3d3d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsfeature requestgood contributiongood first issuescripting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions