Skip to content

Commit aba85ff

Browse files
committedJun 8, 2021
Enable rustdoc to document safe wasm intrinsics
This commit fixes an issue not found during #84988 where rustdoc is used to document cross-platform intrinsics but it was requiring that functions which use `#[target_feature]` are `unsafe` erroneously, even if they're WebAssembly specific. Rustdoc today, for example, already has a special case where it enables annotations like `#[target_feature(enable = "simd128")]` on platforms other than WebAssembly. The purpose of this commit is to relax the "require all `#[target_feature]` functions are `unsafe`" requirement for all targets whenever rustdoc is running, enabling all targets to fully document other targets, such as WebAssembly, where intrinsics functions aren't always `unsafe`.
1 parent e4a6032 commit aba85ff

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed
 

‎compiler/rustc_typeck/src/collect.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2771,7 +2771,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27712771
}
27722772
} else if tcx.sess.check_name(attr, sym::target_feature) {
27732773
if !tcx.is_closure(id) && tcx.fn_sig(id).unsafety() == hir::Unsafety::Normal {
2774-
if tcx.sess.target.is_like_wasm {
2774+
if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
27752775
// The `#[target_feature]` attribute is allowed on
27762776
// WebAssembly targets on all functions, including safe
27772777
// ones. Other targets require that `#[target_feature]` is
@@ -2785,6 +2785,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27852785
// deterministic trap. There is no undefined behavior when
27862786
// executing WebAssembly so `#[target_feature]` is allowed
27872787
// on safe functions (but again, only for WebAssembly)
2788+
//
2789+
// Note that this is also allowed if `actually_rustdoc` so
2790+
// if a target is documenting some wasm-specific code then
2791+
// it's not spuriously denied.
27882792
} else if !tcx.features().target_feature_11 {
27892793
let mut err = feature_err(
27902794
&tcx.sess.parse_sess,

‎src/test/rustdoc-ui/wasm-safe.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
#![feature(wasm_target_feature)]
4+
5+
#[cfg(any(target_arch = "wasm32", doc))]
6+
#[target_feature(enable = "simd128")]
7+
pub fn foo() {}

0 commit comments

Comments
 (0)
Please sign in to comment.