rust-lang / rust-lang/rust

`unreachable_patterns` not emitted on macros generated by another crate's macro

未關閉
#158,366 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

A-diagnostics A-lints A-patterns C-bug L-false-negative L-unreachable_patterns needs-triage T-compiler
主要語言
Rust
星號
119k
分支
16.1k
PR 合併指標
PR 指標待擷取

描述

I tried this code:

crate_a:

#[macro_export]
macro_rules! create_pats {
    () => {
        macro_rules! pat_a {
            () => {
                _
            };
        }
        macro_rules! pat_b {
            () => {
                _
            };
        }
    };
}

crate_b:

use crate_a::create_pats;

create_pats!();

#[deny(unreachable_patterns)]
fn demo(value: u8) -> i32 {
    match value {
        pat_a!() => 42,
        pat_b!() => 24,
    }
}

I expected to see this happen: unreachable_pattern error

error: unreachable pattern
  --> crate_a/src/lib.rs:11:17
   |
 6 |                 _
   |                 - matches any value
...
11 |                 _
   |                 ^ no value can reach this

  --> crate_b/src/main.rs:9:17
 9 |         pat_b!() => 24,
   |         -------- in this macro invocation
   |
note: the lint level is defined here
  --> src/main.rs:5:8
   |
 5 | #[deny(unreachable_patterns)]
   |        ^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `pat_b` (in Nightly builds, run with -Z macro-backtrace for more info)

Instead, this happened: successful compilation

When the create_pats! macro is defined in crate_b, the lint is emitted as expected.

Meta

rustc --version --verbose:

rustc 1.98.0-nightly (b30f3df3b 2026-06-11)
binary: rustc
commit-hash: b30f3df3ba3c4c9de2f58f1a75dd9500b79b3f8d
commit-date: 2026-06-11
host: x86_64-unknown-linux-gnu
release: 1.98.0-nightly
LLVM version: 22.1.6

@rustbot label +A-diagnostics +A-lints +A-patterns +L-unreachable_patterns +T-compiler +L-false-negative

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先使用 crate_a/src/lib.rs 和 crate_b/src/main.rs 重現兩個 crate 的範例,然後將其與 create_pats! 定義在 crate_b 中的情況進行比較。追蹤跨 crate 產生的巨集如何處理 unreachable_patterns lint。當拒絕該 lint 時,pat_b!() 分支被診斷為不可達,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
rust
領域
compilers
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
50/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。