Skip to content

fix(macros): 带 #[cfg] 的方法只在包含它的构建里检查(#541) - #591

Merged
eric8810 merged 2 commits into
mainfrom
fix/issue-541-cfg-methods
Oct 24, 2026
Merged

eric8810 merged 2 commits into
mainfrom
fix/issue-541-cfg-methods

Conversation

@eric8810

Copy link
Copy Markdown
Contributor

Refs #541(第 2 项)。第 1 项与第 4 项的一部分已由 #555 完成;第 3 项(build.rs 生成器的 marked() 不按 impl cfg 过滤)、第 4 项的回调形态检查与跨块 camelCase 重名按排期推到 0.16,本 PR 不涉及。

问题

#[rutis::service] 对所有公开方法做绑定校验,不看方法自己的 #[cfg]:#[cfg(windows)] pub fn f(&mut self) 在 Linux 上也报 “cannot be called from other languages”,尽管这个方法不在构建里。方法上的 cfg 会原样到达宏,但宏求不了 cfg 的值。

修复

宏不再当场报带 #[cfg](含 cfg_attr(.., cfg(..)))的方法的错误,而是把错误生成为带同样 cfg(以及 impl 的 cfg)的 compile_error!,span 仍在那个方法上。没有这个方法的构建里,错误随方法一起被 cfg 去掉;有这个方法的构建里,照样是指到该方法、提示 #[service(skip)] 的编译错误(R5)。接收者、绑定不了的参数/返回值、同名方法这几类错误都走这条路径;不带 cfg 的方法不变。

测试(修复前都失败)

  • crates/rutis-bridge/tests/marked_services.rs:Greeter 加了 #[cfg(any())] pub fn reset(&mut self) 和 cfg_attr 去掉的 pub fn make<T: Default>(&self) -> T。修复前这个测试文件编译失败(两个错误),修复后能编译,现有用例全部通过。
  • rutis-macros 单元测试 a_cfg_methods_error_is_under_its_cfg:错误带方法与 impl 的 cfg、指到方法所在的行。
  • rutis/src/service.rs 的文档测试:#[cfg(all())] 的 &mut self 方法仍然编译失败(compile_fail),#[cfg(any())] 的能编译。

文档(中英)

设计 R5、Rust 宿主指南“对外的方法”、承诺表 bridge/marked-services。

本地 macOS:cargo fmt、clippy -p rutis-macros -p rutis -p rutis-bridge --all-targets --all-features -D warnings、cargo test -p rutis-macros、cargo test -p rutis --doc service、cargo test -p rutis-bridge --all-features --test marked_services。

…hods

# Conflicts:
#	crates/rutis-macros/src/lib.rs
#	docs/guarantees.en.md
#	docs/guarantees.md
#	docs/guide/rust-host.en.md
#	docs/guide/rust-host.md
@eric8810
eric8810 merged commit 2b8efd5 into main Oct 24, 2026
26 checks passed
@eric8810 eric8810 mentioned this pull request Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant