Skip to content

Assign Diagnostics with Identifiers #4217

Open

Description

@Myriad-Dreamin

Description

The diagnostics from Typst should be able to be identified or referred via a precise way, e.g. by assigning identifiers, MSVC Error LNK2019.

Use Case

Here are four possible use cases:

Activity

  1. changed the title [-]Assigning Diagnostics with Identifiers[/-] [+]Assign Diagnostics with Identifiers[/+] on May 22, 2024
  2. PgBiel commented on May 22, 2024

    @PgBiel
    Contributor

    Hi, this is something we intend to work on, in particular to make features such as warning suppression easier to implement. I have been considering something like Rust's slugs (see https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html), which would make a reference to a warning immediately readable (for example, unnecessary_import_rename), but we could also use numeric codes for compiler warnings (e.g. W0123). This can also be expanded to errors. What do you think?

  3. Myriad-Dreamin commented on May 22, 2024

    @Myriad-Dreamin
    ContributorAuthor

    IMHO, it is not necessary to enforce diagnostic IDs to be immediately readable, as the IDs usually come with the diagnostics messages or some documentation from editors, which explain the meaning of IDs. This is also not quite useful as the English string is not quite readable for non-English users, which means we will have to show them by localization in the end.

    But string IDs do have an advantage that I like, that we can have a namespace on IDs, e.g. @preview/some_package:some_warning_id, so we can maintain and operate warnings emitted by packages easily.

  4. Myriad-Dreamin commented on May 22, 2024

    @Myriad-Dreamin
    ContributorAuthor

    But it is expensive to have string IDs, and it is okay to let all package warnings share the same diagnostic ID. Overall, numeric codes looks better than string IDs.

  5. PgBiel commented on May 23, 2024

    @PgBiel
    Contributor

    The idea of using names/slugs is mostly for warnings so you can suppress them, but can be optionally extended to errors as well. While we can use numeric IDs, they would only make sense for the compiler's internal warnings and errors imo - if/when we get package warnings, we don't want to force package authors to keep a consistent list of numeric IDs for their diagnostics.

    For instance, I think this (using hypothetical warning suppression syntax)

    #let f = {
        @bikeshed::allow(unused_param)
        (a, b, c) => a + b
    }

    is better / more syntactically coherent and readable than

    #let f = {
        @bikeshed::allow(W0932)
        (a, b, c) => a + b
    }

    Of course, it is fair to think of non-English speakers, but this would generally only be used within code (where all syntax is also in English); the warning messages themselves wouldn't use those slugs, and can be localized in the future.

  6. Myriad-Dreamin commented on May 23, 2024

    @Myriad-Dreamin
    ContributorAuthor

    you can parse the syntax names into numeric codes, which is not a problem. These numeric codes are exactly used by compiler internals and packages may not be able to allocate a code at script level. For example:

    #let f = {
        @bikeshed::allow(@preview/modern-thesis, overflow-box)
        (a, b, c) => a + b
    }

    Then it is mapped to 10086 meaning a general overflow errno for packages.
    I don't mean it necessary to use numeric code, but the diagnostics IDs are frequently read by machines but not humans so it is important to keep the efficient memory usage and cost to compare imo.

    If your're really worry about readability, you can use string id, but please still limit the length of these ids so that machines can compare them quickly.

  7. laurmaedje commented on May 23, 2024

    @laurmaedje
    Member

    The performance/comparison problem can be solved with interning (i.e. PicoStr).

  8. Myriad-Dreamin commented on May 23, 2024

    @Myriad-Dreamin
    ContributorAuthor

    The performance/comparison problem can be solved with interning (i.e. PicoStr).

    The interning technique is only useful for rust programs that use typst crates.

  9. laurmaedje commented on May 23, 2024

    @laurmaedje
    Member

    Is the performance with strings really such a big concern? I am not really convinced that this is a bottleneck.

  10. PgBiel commented on May 23, 2024

    @PgBiel
    Contributor

    While there isn't a problem in translating the compiler's slugs to IDs internally (and thus for World consumers to use as well), that probably can't be done for packages' warnings unfortunately (would be hard to enforce consistent IDs in principle).

    To better gauge how we can tackle this, could you share the use case you have in mind in which the string identifiers could be a problem (and/or how you plan to use the identifiers)? Worth noting that warn suppression, at least, would be handled by the compiler itself, so I assume there is some other requirement in play.

  11. Myriad-Dreamin commented on May 23, 2024

    @Myriad-Dreamin
    ContributorAuthor

    To better gauge how we can tackle this, could you share the use case you have in mind in which the string identifiers could be a problem (and/or how you plan to use the identifiers)?

    I'm not saying identifiers are necessary to be numeric, but I was required to compare numeric identifiers and string identifiers. My opinions are summarized as follows:

    • My case is already in listed in the first message. In the use cases, the diagnostic IDs are read by language service servers and clients for identification and filtering, but not directly for users, so it is worth keeping efficiency than readability.
    • for warning suppression or other scenarios that require readability, we can parse the source code and map readable slugs into diagnostic IDs or map IDs back.
    • even if we uses string IDs, package writers may not be allowed to allocate diagnostic IDs by themselves, and we may have some government on it.
  12. Myriad-Dreamin commented on May 23, 2024

    @Myriad-Dreamin
    ContributorAuthor

    Is the performance with strings really such a big concern?

    For the performance issue on compiler's side, I did not quite think of it in practice, but I can construct some cases that we need to intern and compare frequently, that is raise warnings on arithmetics.

    @bikeshed::warn(bad-arithmetics)
    #let mul(a, b) => a * b
    #for I in range (10000) { mul(int(1e10), int(1e10)) }

    Then the warnings will be raised by thousands of times. This should be optimized carefully if we use string ids but is probably not a thing if we use numeric ids.

  13. PgBiel commented on May 23, 2024

    @PgBiel
    Contributor

    Well, we gotta be pragmatic here. Realistically, your document shouldn't be emitting thousands of warnings. In your particular case, you'd only have a single warning as well in the end, since it'd have the same message and span (diagnostics are deduplicated). Overall, I don't see how this can be that large of a problem; the typical document will have 0-5 warnings, maybe 10-20 suppressed.

    With that said, I'm in favor of limiting the length of the slugs. Maybe we can limit it to 32 or 64 bytes just to be sure.

  14. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    diagnostics are deduplicated

    That's happening on compile's side, so there are thousands of diagnostics along with their ids to be duplicated by compiler. That means, after adding diag ids, you'll probably deduplicate them by span, id, and message. I don't see a problem of my expression. There is even some problem I have met is that I ever opened pdf as typst source file, typst was slow to compile as expected and then my editor was flooded with more than 19,999 errors and crashed. Just unsure whether deduplicating diagnostics played a role in slow compilation.

    I don't see how this can be that large of a problem; the typical document will have 0-5 warnings, maybe 10-20 suppressed.

    Agree. But the warnings to suppress may be more than imagined, 100-1000 is also frequently to have per document, when you suppress some warnings like overflow box.

    With that said, I'm in favor of limiting the length of the slugs. Maybe we can limit it to 32 or 64 bytes just to be sure.

    That's what I'm happy to see, that restrict the length of strings. 32 bytes of limit is still a bit excessive for id-like data. If you make use of EcoString's feature, 13~14 bytes is a good limit so that no data is allocated on heap.

  15. Enter-tainer commented on May 24, 2024

    @Enter-tainer
    Contributor

    Is the performance with strings really such a big concern? I am not really convinced that this is a bottleneck.

    +1. iirc clangd, clippy use string id and there doesnt seems to be such a limit. they have very long error id strings.

  16. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    Is the performance with strings really such a big concern? I am not really convinced that this is a bottleneck.

    +1. iirc clangd, clippy use string id and there doesnt seems to be such a limit. they have very long error id strings.

    I don't know whether you've read these repositories, but IIRC both rustc and clang compiler use numeric ids. clang strictly assigns all linting with unique numeric ids. rustc assigns all linting with a same numeric id.

    The string ids you see may be the visual name of these diagnostics, i.e. a part of the message field.

  17. Enter-tainer commented on May 24, 2024

    @Enter-tainer
    Contributor

    Seems that i've misread. I agree with

    These numeric codes are exactly used by compiler internals

    I'm not sure with this

    In the use cases, the diagnostic IDs are read by language service servers and clients for identification and filtering

    IIRC to make a ls, you need embed typst-* crate. i think if numeric codes are exactly used by compiler internals holds, this will hold, too. Am i right?

    FWIW, r-a always pass a string id to ls client. https://github.com/rust-lang/rust-analyzer/blob/f6fc109fcb2bed2291320985fe75c6901839f4ba/crates/rust-analyzer/src/diagnostics/to_proto.rs#L427. I think clangd/clang-tidy should do the same thing.

  18. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    IIRC to make a ls, you need embed typst-* crate. i think if numeric codes are exactly used by compiler internals holds, this will hold, too. Am i right?

    For a server (rust) it is, for a client (lua, javascript, etc.) it isn't.

    FWIW, r-a always pass a string id to ls client.

    However, rustc assigns all linting with a same numeric id, so r-a will probably only passes a same string id here.

  19. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    I think I've been a bit tired on discussing whether to use numeric codes or string codes. I think it is not a big problem to always uses string codes for identifying. Currently, it is not a problem to have a slightly slower implementation but a problem to have no idea on comparing them precisely. I'm just usually concerned about how to implement a fast compiler, and throw some points about what I have read and learned from projects. From perspective of performance, I may follow what rustc does, which

    • uses numeric codes solution.
    • provides a way to filter linting level diagnostics by configuration or macros, and doesn't provide a way to filter them at client side.
  20. Enter-tainer commented on May 24, 2024

    @Enter-tainer
    Contributor

    rustc assigns all linting with a same numeric id, so r-a will probably only passes a same string id here.

    I did some test and the result is interesting. Seems that for compiler errors, the error code looks like E\d+, for warnings, it is a descriptive string

                "code": "unused_variables",
                "source": "rustc",
                "message": "unused variable: `test`\n`#[warn(unused_variables)]` on by default",
    
    

    For compiler design, i think using a numeric id(maybe using a enum) is beneficial. I totally agree with Currently, it is not a problem to have a slightly slower implementation but a problem to have no idea on comparing them precisely .

    For language server design, in the case of "too many diagnostics", i think the format/length of the code is not the bottleneck. Because when there is a lot of errors, like 10000 of them, the language client/server need to ser/de a big json array and send it to another process using stdio. This can easily takes more time comparing to filtering string ids. I remember I've seen "too many errors, stopping" or similar thing in clangd/r-a. Maybe it is their way to prevent "too many errors" problem.

    provides a way to filter linting level diagnostics by configuration or macros, and doesn't provide a way to filter them at client side.

    I don't know if i get it clearly. When using vscode, i can always filter diagnostics using the panel. (Actually, i never use this functionality). I don't think you can prevent users do this.

  21. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    After reading the thread again, I think there was a mystery divergence, which would lead the discussion to become ineffective. First, a diagnostic message may be assigned with different forms of IDs at different stages. We seem to be on different channels when discussing :). Specifically, it might have three stages:

    • compilation stage
    • rust tools and language servers
    • languages clients

    In fact, I start to be aware of that after I revisit the rustc's design on diagnostics.

    Then, in compiler and rust tools, it seems like a third approach other than numeric or string ids, having a pointer ID for comparison, is even better than the string IDs with interning. This is because you don't even have any cost on interning the IDs when creating diagnostics. It has the same flexibility as assigning string IDs, and it keeps the same performance as clangd which strictly assigns numeric codes to all specific diagnostics.

    Lastly, at language client level, besides efficiency, rustc uses short and limited amount of string codes to ensure steady user interface to language clients as well as editor users. They don't want to break users' diagnostics (problem) matchers by unstable diagnostic IDs.

  22. Enter-tainer commented on May 24, 2024

    @Enter-tainer
    Contributor

    Pointer ID/Type ID sounds cool. What does it looks like?

  23. Myriad-Dreamin commented on May 24, 2024

    @Myriad-Dreamin
    ContributorAuthor

    I deleted the type id to avoid confusing in scope of typst, as we might also want to allow assigning IDs for package warnings where we cannot compare by type id. The clippy has a macro that creates a global lint object at runtime:

    declare_clippy_lint! {
        /// ### What it does
        /// Checks for type parameters in generics that are never used anywhere else.
        ///
        /// ### Why is this bad?
        /// Functions cannot infer the value of unused type parameters; therefore, calling them
        /// requires using a turbofish, which serves no purpose but to satisfy the compiler.
        ///
        /// ### Example
        /// ```no_run
        /// fn unused_ty<T>(x: u8) {
        ///     // ..
        /// }
        /// ```
        /// Use instead:
        /// ```no_run
        /// fn no_unused_ty(x: u8) {
        ///     // ..
        /// }
        /// ```
        #[clippy::version = "1.69.0"]
        pub EXTRA_UNUSED_TYPE_PARAMETERS,
        complexity,
        "unused type parameters in function definitions"
    }

    https://github.com/rust-lang/rust-clippy/blob/76856ffb576fb2dff472481a6389d42ca836db6f/clippy_lints/src/extra_unused_type_parameters.rs#L17-L41

  24. added
    diagnostics
    on May 24, 2024
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 request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions