Skip to content

fix(macros): guard the generated entry with the impl cfg - #555

Merged
eric8810 merged 4 commits into
mainfrom
fix/issue-541-macros-cfg
Oct 22, 2026
Merged

eric8810 merged 4 commits into
mainfrom
fix/issue-541-macros-cfg

Conversation

@eric8810

@eric8810 eric8810 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Part of #541 (not all of it yet).

  • The generated const _/entry/registration now carry the impl block's #[cfg]/#[cfg_attr(..., cfg(...))]. The entry is under the same cfg as its block.
  • A type parameter that is not used as a callback parameter (e.g. pub fn make<T: Default>(&self) -> T) is now rejected at the method, as R5 requires.
  • &'static T parameters are rejected at the method: decoding owns the value and cannot produce a static borrow.
  • self: &Self is accepted as a shared receiver by both the macro and the build.rs declaration generator.

Still open in #541: cfg-disabled methods are still validated; marked() in the generator does not filter impl cfg like marked_types() does; callback return types are not checked as BoxFuture<'static, Result<..>>; a camelCase collision across two blocks is not detected.

End-to-end (crates/rutis-bridge/tests/marked_services.rs): a marked block under #[cfg(any())] and one removed by #[cfg_attr(not(any()), cfg(any()))], each naming a function that does not exist, build and serve nothing; a self: &Self method is served. The generator's test checks such a method is declared with no warning.

Note: rustc removes a cfg-disabled item before it expands the attribute macro on it, so the end-to-end case also builds without the guard; the guard keeps the generated entry under the same cfg as its block regardless.

Tests: cargo test -p rutis-bridge --test marked_services, cargo test -p rutis-macros, cargo test -p rutis-bridge --features exports --lib build::exports, cargo check -p rutis -p rutis-loader, cargo fmt --check.

@eric8810

Copy link
Copy Markdown
Contributor Author

审核结论:需要修改。

impl 上的 #[cfg] / #[cfg_attr(.., cfg(..))] 带到生成的 const _ 上(lib.rs:280,365)正确;self: &Self 能服务;新的编译期拒绝(只用于返回类型的类型参数、&'static 参数)也对。与 #534/#531/#522 不冲突。

  1. build.rs 声明生成器没跟上 self: &Self(crates/rutis-bridge/src/build/exports.rs:1171):仍要求 receiver.reference.is_some(),而 syn 只在 &self 简写时设置它。结果这种方法运行时可调用,但 TS 声明里没有,还会给出误导的构建警告。修法:与 shared_self 接受同样的接收者,或从本 PR 去掉这部分。
  2. 正文写 “Fixes 0.15 review(macros): impl 级 cfg 未带到生成代码,若干绑定错误不在方法处 #541”,合并会关掉 0.15 review(macros): impl 级 cfg 未带到生成代码,若干绑定错误不在方法处 #541,但只修了第 1 项和第 4 项的一部分。仍未做:#[cfg] 掉的方法仍被检查;exports.rs:1011 的 marked() 仍忽略 impl cfg;回调返回类型未检查为 BoxFuture<'static, Result<..>>;两个块的 camelCase 名字冲突未发现。改为 “Part of 0.15 review(macros): impl 级 cfg 未带到生成代码,若干绑定错误不在方法处 #541”。
  3. 可选:现有测试只检查属性存在;可在 crates/rutis-bridge/tests/marked_services.rs 加一个 #[cfg(any())] 的标注 impl 做端到端检查。

@eric8810
eric8810 force-pushed the fix/issue-541-macros-cfg branch from e2c1c4d to 8948361 Compare October 22, 2026 00:08
…rked blocks (#541)

marked_services: a marked block under #[cfg(any())] and one removed by a
cfg_attr build and serve nothing; a self: &Self method is served. The
declaration generator's test checks such a method gives no warning.
@eric8810 eric8810 added this to the 0.15 milestone Oct 22, 2026
@eric8810

Copy link
Copy Markdown
Contributor Author

接手完成剩余部分(06f62ffa):

@eric8810
eric8810 merged commit eb7da0f into main Oct 22, 2026
50 of 52 checks passed
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