Skip to content

Error on function literals in math - #7866

Merged
laurmaedje merged 3 commits into
typst:math-syntaxfrom
isuffix:error-on-func-literal
Oct 29, 2026
Merged

laurmaedje merged 3 commits into
typst:math-syntaxfrom
isuffix:error-on-func-literal

Conversation

@isuffix

@isuffix isuffix commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

Part of the Math Syntax Refactor
This PR is part of the exhaustive math syntax refactor. It should be considered exploratory, and not necessarily a final decision, even if merged to the math-syntax branch. Non-specific feedback for this change should go in one of its related issues.


This PR causes function literals placed in math to produce errors instead of rendering the function name as monospace text. Function literals in markup mode still render as before.

This depends on PR #7865. Only the last commit is new.

This is in part a preparation for resolving The Math Mode Problem (cf. #7084), but that will come later.

Here is one of the test cases showing the new error and hint. I am not fully satisfied with the wording, so feel free to give suggestions :)

--- math-func-literal-parens eval ---
// Error: 3-6 this does not call the `mat` function
// Hint: 3-6 to call the `mat` function, write `mat()`
$ mat () $

This is implemented by breaking out the Eval impls of MathIdent and FieldAccess into free functions so that when called with Eval::eval(), they produce an error on function literals in math, but are still able to be evaluated in MathCall::eval() without producing an error.

I'm not quite happy with the amount of complication required for this approach to work with field accesses, and an alternative I considered was to encode the difference between MathIdent and Ident into a more generic MathIdentWrapper which could directly contain both Ident and FieldAccess nodes when used in math. I didn't go with that for now because it requires a lot of code churn, but it does feel a bit conceptually cleaner. I also believe it could simpify #7407, although have not tried it.

@isuffix
isuffix force-pushed the error-on-func-literal branch from b8e556c to 6eecef6 Compare February 13, 2026 23:11
@MDLC01

MDLC01 commented Feb 14, 2026

Copy link
Copy Markdown
Collaborator

For the hint, I think it should detect whether parentheses are opened after some whitespace and suggest "to call the mat function, remove any whitespace before the opening parenthesis". Right now, it's weird because if the user specified arguments inside parentheses but added a whitespace, the hint is just wrong by suggesting to use () with no arguments.

If no parentheses are present, I think the current hint could be rephrased to "to call the mat function, specify arguments in parentheses: mat()". This provides a little more information.

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Feb 16, 2026
@isuffix isuffix added math Related to math category, with its syntax, layout, etc. syntax About syntax, parsing, etc. labels Feb 16, 2026
@laurmaedje

Copy link
Copy Markdown
Member

I'm not quite happy with the amount of complication required for this approach to work with field accesses, and an alternative I considered was to encode the difference between MathIdent and Ident into a more generic MathIdentWrapper which could directly contain both Ident and FieldAccess nodes when used in math. I didn't go with that for now because it requires a lot of code churn, but it does feel a bit conceptually cleaner.

I took a look to understand the current approach and am happy to learn about the alternative. Let's discuss this later.

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Feb 19, 2026
@isuffix
isuffix force-pushed the error-on-func-literal branch 3 times, most recently from d5d9094 to 5c638d2 Compare February 26, 2026 21:58
@laurmaedje laurmaedje added the blocked This PR is blocked by something. label Mar 2, 2026
@isuffix
isuffix force-pushed the error-on-func-literal branch from 5c638d2 to 57ab738 Compare March 4, 2026 03:23
@isuffix isuffix mentioned this pull request Mar 4, 2026
@isuffix
isuffix force-pushed the error-on-func-literal branch from 57ab738 to eb63506 Compare March 4, 2026 05:27
@isuffix

isuffix commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator Author

I have now implemented the alternative design for MathAccessWrapper, and this PR is again just one extra commit ahead of #7865. However, this is not the end of the PR. I have some draft changes locally that should help give us higher fidelity errors, but I still need to work on them for a bit before they're ready. However, I've uploaded this commit because it's relatively self-contained and should be reviewable for now.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed blocked This PR is blocked by something. labels Apr 2, 2026
@isuffix
isuffix marked this pull request as draft April 14, 2026 00:35
@isuffix
isuffix force-pushed the error-on-func-literal branch 2 times, most recently from 599a4bd to 68c9b56 Compare June 15, 2026 00:34
@isuffix
isuffix force-pushed the error-on-func-literal branch 3 times, most recently from 1439a16 to be8b45e Compare June 19, 2026 23:25
@isuffix

isuffix commented Jun 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

I've been working on other things for a while, but now it's time to get back to improving math syntax!

For this PR, I realized that there was a nice simplification if we just return a MathError enum from the eval_display method. This centralizes the check function literal displays for MathIdent and MathFieldAccess and doesn't require adding any new eval functions or traits! This also handles edge cases if an embedded code expression would render as a function literal (now erroring) or if a function literal would be passed as an argument to another function in math (now not erroring), neither of which I was handling before 😅. And of course, I've added a bunch of tests to cover all the new cases I could think up.

This PR also has a new second commit that adjusts the error range when we're hinting for delimiters to use a sub-range of the overall Math node that spans from the function literal to the delimiters, which should make the error more actionable. For example, in the math-func-literal-parens test, the main error range is now 3-9 for mat () instead of 3-6 for just mat.

Unfortunately, I still do not have a fix that can add hints when a function literal is next to parens but is inside an operator, like in math-func-literal-frac for $ 1 / frac (1,2) $. However I think this PR should be good enough to merge on its own, and I will plan to try building those hints in a different PR, perhaps by sharing some infrastructure with other math syntax updates.

I would love to hear if anyone has feedback on any of the new error messages or test cases!

@isuffix
isuffix marked this pull request as ready for review June 20, 2026 00:07
Comment thread tests/suite/math/call.typ Outdated
) $

--- math-func-literal-basic eval ---
// Error: 3-6 this does not call the `mat` function

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel like this doesn't fit the style of an error message. Plus, it's kind of redundant with the hint.
The error message should more closely stick to what exactly went wrong, so perhaps something like "cannot use function without call"?

@cady-b cady-b Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree that this feels more like a hint than an error, and with the others it's generally redundant; below, the "expected content, found function" is more useful as an error to me, but perhaps also difficult to understand for newcomers. Not entirely sure what I'd want it to be (the idea of functions having a sort of debug representation isn't the most straightforward), but perhaps something along the lines of "function type does not represent mathematical content"?

Though wording aside, this is a pretty sweet! A welcome change with really nice hints that'll probably prevent quite a few headaches 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about "cannot display function" and add the current message as a hint?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've updated to that

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Jun 29, 2026
Comment thread crates/typst-eval/src/math.rs Resolved
Comment thread crates/typst-eval/src/math.rs Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Jul 3, 2026
@isuffix
isuffix force-pushed the error-on-func-literal branch from be8b45e to 789c501 Compare July 10, 2026 15:51
@isuffix
isuffix force-pushed the error-on-func-literal branch 2 times, most recently from 07f8891 to 04de0fa Compare October 26, 2026 22:39
@isuffix
isuffix force-pushed the error-on-func-literal branch from 04de0fa to 413da78 Compare October 27, 2026 16:49
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Oct 28, 2026
@laurmaedje
laurmaedje merged commit 413da78 into typst:math-syntax Oct 29, 2026
9 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Looks good now! :)

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Oct 29, 2026
@Jollywatt

Jollywatt commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

I know I'm late, but what was the rationale behind making this an error? Since functions can be displayed in markup mode as monospace names, it seems a bit asymmetric to forbid it in math mode.

Was there another motivation related to the larger math parsing refactor?

@isuffix

isuffix commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Yes, we've settled on the syntax refactor making $f_pi(x)$ parse like $f_pi (x)$ even for actual functions like in $f_abs(x)$. But we will now error instead of just rendering incorrectly for the function case, which you might not notice in a long document migrated from the previous version.

@isuffix
isuffix deleted the error-on-func-literal branch October 30, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

math Related to math category, with its syntax, layout, etc. syntax About syntax, parsing, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants