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.2k
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 摘要。