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`.
This commit is contained in:
Alex Crichton 2021-06-03 18:56:31 -07:00
parent e4a6032706
commit aba85ff820
2 changed files with 12 additions and 1 deletions

View file

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

View file

@ -0,0 +1,7 @@
// check-pass
#![feature(wasm_target_feature)]
#[cfg(any(target_arch = "wasm32", doc))]
#[target_feature(enable = "simd128")]
pub fn foo() {}