Files
Ed Page 958efb1ff4 Vendor latest annotate-snippets (#27033)
## Summary

This is a first step towards #20411. After this, we would work to either
upstream changes or adjust things within Ruff.

Performance improvements have already been upstreamed
(https://github.com/rust-lang/annotate-snippets-rs/pull/437,
https://github.com/rust-lang/annotate-snippets-rs/pull/440)

Re-implemented the following from the fork:
- Hidden snippets
(https://github.com/rust-lang/annotate-snippets-rs/issues/448)
- Renderer-level disabling of hyperlinks
- Jupyter cell indices
(https://github.com/rust-lang/annotate-snippets-rs/issues/449)
- Fixable mark
(https://github.com/rust-lang/annotate-snippets-rs/issues/450)
- `lineno_offset` (maybe not needed if #20648 is taken care of)
- anonymized lines affects origin
(https://github.com/rust-lang/annotate-snippets-rs/issues/451)
- cut indicator (maybe not needed if we can switch to unicode renderer)

Additional hacks to minimize behavior change include
- Adjust upstream's hidden severity behavior to match Ruff's
(https://github.com/rust-lang/annotate-snippets-rs/issues/452)
- How newlines are annotated
(https://github.com/rust-lang/annotate-snippets-rs/issues/454)
- To not indent for line numbers when none will be present
(https://github.com/rust-lang/annotate-snippets-rs/issues/453)
- Enabling `simd` by default

All of the above represents work that needs to be resolved to address
#20411.

`lib.name` hack is used to simplify maintenance with the assumption that
we'll continue working towards #20411.

Behavior changes that were included
- Tweak when implicit padding lines are present
- Fix Origin's line/column is now derived from the primary annotation
- Make consistent the rendering of primary/context annotations
- Fix snippets to show up for empty source (but may degrade #24528)
- Fix handling of term width
- Fix rendering of annotations between newline and first character
(granted, maybe the caller is wrong)
- Tweak multiline annotation markers

New or upcoming features annotate-snippets features that we will be able
to leverage by moving this forward
- `AnnotationKind::Visible` to improve the quality of built-in
code-folding if Ruff were to switch to it
- `DecorStyle::Unicode`
- *(upcoming)* No-graphiscs mode for screen readers (may also be useful
for AIs)

Deferred:
- Moving related `Group`s into the same `Report`
- Using `secondary_title` for any `Group` after the first in a `Report`
- Using a `Snippet`, instead of titles, for "virtual code" like
https://github.com/astral-sh/ruff/pull/27033#discussion_r3622727236
- Switching hidden snippets to `Origin`

## Test Plan

Within cpython, I ran
```console
$ hyperfine -i "../../ruff/target-baseline/release/ruff check" "../../ruff/target-update/release/ruff check" -N -m20 --output=null
Benchmark 1: ../../ruff/target-baseline/release/ruff check
  Time (mean ± σ):      27.5 ms ±   1.1 ms    [User: 33.0 ms, System: 108.4 ms]
  Range (min … max):    25.0 ms …  31.7 ms    94 runs

  Warning: Ignoring non-zero exit code.

Benchmark 2: ../../ruff/target-update/release/ruff check
  Time (mean ± σ):      28.1 ms ±   2.0 ms    [User: 33.4 ms, System: 105.7 ms]
  Range (min … max):    24.8 ms …  38.9 ms    102 runs

  Warning: Ignoring non-zero exit code.
  Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use t
he '--warmup' or '--prepare' options.

Summary
  ../../ruff/target-baseline/release/ruff check ran
    1.03 ± 0.08 times faster than ../../ruff/target-update/release/ruff check
```

Before:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/2d94dabf-b28d-48c1-b17c-52aac59fd57e"
/>

After:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/b388ada3-e3d7-4ba0-b90b-1330eb81f4be"
/>
2026-07-28 11:57:09 -05:00

2.4 KiB

Missing argument diagnostics

If a non-union callable is called with a required parameter missing, we add a subdiagnostic showing where the parameter was defined. We don't do this for unions as we currently emit a separate diagnostic for each element of the union; having a sub-diagnostic for each element would probably be too verbose for it to be worth it.

module.py:

def f(a, b=42): ...
def g(a, b): ...

class Foo:
    def method(self, a): ...

main.py:

from module import f, g, Foo

f()  # snapshot

def coinflip() -> bool:
    return True

h = f if coinflip() else g

# snapshot: missing-argument
# snapshot: missing-argument
h(b=56)

Foo().method()  # snapshot: missing-argument

from typing import Protocol

class P(Protocol):
    def method(self, value: int) -> None: ...

def check_protocol(p: P) -> None:
    p.method()  # snapshot: missing-argument
error[missing-argument]: No argument provided for required parameter `a` of function `f`
 --> src/main.py:3:1
  |
3 | f()  # snapshot
  | ^^^
info: Parameter declared here
 --> src/module.py:1:7
  |
1 | def f(a, b=42): ...
  |       ^


error[missing-argument]: No argument provided for required parameter `a` of function `f`
  --> src/main.py:12:1
   |
12 | h(b=56)
   | ^^^^^^^
info: Union variant `def f(a, b=42) -> Unknown` is incompatible with this call site
info: Attempted to call union type `(def f(a, b=42) -> Unknown) | (def g(a, b) -> Unknown)`


error[missing-argument]: No argument provided for required parameter `a` of function `g`
  --> src/main.py:12:1
   |
12 | h(b=56)
   | ^^^^^^^
info: Union variant `def g(a, b) -> Unknown` is incompatible with this call site
info: Attempted to call union type `(def f(a, b=42) -> Unknown) | (def g(a, b) -> Unknown)`


error[missing-argument]: No argument provided for required parameter `a` of bound method `Foo.method`
  --> src/main.py:14:1
   |
14 | Foo().method()  # snapshot: missing-argument
   | ^^^^^^^^^^^^^^
info: Parameter declared here
 --> src/module.py:5:22
  |
5 |     def method(self, a): ...
  |                      ^


error[missing-argument]: No argument provided for required parameter `value` of bound method `P.method`
  --> src/main.py:22:5
   |
22 |     p.method()  # snapshot: missing-argument
   |     ^^^^^^^^^^
info: Parameter declared here
  --> src/main.py:19:22
   |
19 |     def method(self, value: int) -> None: ...
   |                      ^^^^^^^^^^