rust/crates
bors 2a57b01980 Auto merge of #14122 - lowr:patch/abort-macro-expansion-on-overflow, r=Veykril
fix: Don't expand macros in the same expansion tree after overflow

This patch fixes 2 bugs:

- In `Expander::enter_expand_id()` (and in code paths it's called), we never check whether we've reached the recursion limit. Although it hasn't been reported as far as I'm aware, this may cause hangs or stack overflows if some malformed attribute macro is used on associated items.
- We keep expansion even when recursion limit is reached. Take the following for example:

  ```rust
  macro_rules! foo { () => {{ foo!(); foo!(); }} }
  fn main() { foo!(); }
  ```

  We keep expanding the first `foo!()` in each expansion and would reach the limit at some point, *after which* we would try expanding the second `foo!()` in each expansion until it hits the limit again. This will (by default) lead to ~2^128 expansions.

  This is essentially what's happening in #14074. Unlike rustc, we don't just stop expanding macros when we fail as long as it produces some tokens so that we can provide completions and other services in incomplete macro calls.

This patch provides a method that takes care of recursion depths (`Expander::within_limit()`) and stops macro expansions in the whole macro expansion tree once it detects recursion depth overflow. To be honest, I'm not really satisfied with this fix because it can still be used in unintended ways to bypass overflow checks, and I'm still seeking ways such that misuses are caught by the compiler by leveraging types or something.

Fixes #14074
2023-02-14 07:37:53 +00:00
..
base-db ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
cfg ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
flycheck ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
hir ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
hir-def Auto merge of #14122 - lowr:patch/abort-macro-expansion-on-overflow, r=Veykril 2023-02-14 07:37:53 +00:00
hir-expand Auto merge of #14122 - lowr:patch/abort-macro-expansion-on-overflow, r=Veykril 2023-02-14 07:37:53 +00:00
hir-ty ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide-assists ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide-completion ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide-db ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide-diagnostics ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
ide-ssr ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
intern ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
limit ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
mbe ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
parser ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
paths ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
proc-macro-api ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
proc-macro-srv ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
proc-macro-srv-cli ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
proc-macro-test ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
profile ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
project-model ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
rust-analyzer ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
sourcegen ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
stdx ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
syntax ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
test-utils ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
text-edit ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
toolchain ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
tt ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
vfs ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00
vfs-notify ⬆️ rust-analyzer 2023-02-13 13:55:14 +02:00