# 错误[E0706]：线程局部变量无法跨越 yield 点借用

- **ID:** `rust/e0706-thread-local-cannot-be-borrowed-across-yield`
- **领域:** rust
- **类别:** type_error
- **错误码:** `E0706`
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

在异步代码中，对线程局部变量（例如 `thread_local!`）的引用跨越了 `.await` 点，这可能导致线程切换，因此不安全。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.75.0 | active | — | — |
| 1.77.0 | active | — | — |
| 1.79.0 | active | — | — |

## 解决方案

1. ```
   在 await 点之前克隆线程局部值：`let val = THREAD_LOCAL.with(|v| v.clone());` 然后在 await 后使用 `val`。
   ```
2. ```
   重构代码以避免跨 await 持有借用：使用 `let result = { THREAD_LOCAL.with(|v| v.do_something()) }; result.await`。
   ```

## 无效尝试

- **Wrapping the thread-local in `Rc` to extend lifetime** — `Rc` is not `Send`, so cannot be used across await points in async contexts. (85% 失败率)
- **Using `unsafe` to extend the borrow lifetime manually** — Undefined behavior; compiler may still reject or cause data races. (90% 失败率)
