Skip to content

Commit b75e631

Browse files
bors[bot]taiki-e
andauthored
Merge #71
71: Do not display UnpinStruct in the document by default r=taiki-e a=taiki-e There are some problems as mentioned in #70 This causes the problem that by default the actual Unpin bounds cannot be known from the document if the original struct/enum itself is public (see #53 (comment) and rust-lang/rust#63281 for more). This can be enabled using `--cfg pin_project_show_unpin_struct` in RUSTFLAGS. ```toml # in Cargo.toml [package.metadata.docs.rs] rustdoc-args = ["--cfg", "pin_project_show_unpin_struct"] ``` cc @Aaron1011 @seanmonstar @LucioFranco Co-authored-by: Taiki Endo <te316e89@gmail.com>
2 parents 09cb58b + 60751fb commit b75e631

File tree

9 files changed

+32
-53
lines changed

9 files changed

+32
-53
lines changed

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
toolchain: nightly
5050
- script: |
5151
cargo clean
52-
RUSTFLAGS='--cfg compiletest' cargo test -p pin-project --all-features --test compiletest
52+
RUSTFLAGS='--cfg compiletest --cfg pin_project_show_unpin_struct' cargo test -p pin-project --all-features --test compiletest
5353
displayName: compiletest
5454
5555
- job: clippy

pin-project-internal/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ lazy_static = { version = "1.3", optional = true }
3535

3636
[dev-dependencies]
3737
pin-project = { version = "0.4.0-alpha.8", path = ".." }
38-
39-
[build-dependencies]
40-
rustc_version = "0.2.3"

pin-project-internal/build.rs

+14-31
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
// Based on https://stackoverflow.com/a/49250753/1290530
2-
31
use std::env;
42

5-
use rustc_version::{version_meta, Channel};
6-
73
fn main() {
8-
// Set cfg flags depending on release channel
9-
match version_meta().unwrap().channel {
10-
// Enable our feature on nightly, or when using a
11-
// locally build rustc.
12-
//
13-
// This is intended to avoid the issue that cannot know the actual
14-
// trait implementation bounds of the `Unpin` implementation from the
15-
// document of generated code.
16-
// See [taiki-e/pin-project#53] and [rust-lang/rust#63281] for more details.
17-
//
18-
// [taiki-e/pin-project#53]: https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867
19-
// [rust-lang/rust#63281]: https://github.com/rust-lang/rust/issues/63281
20-
//
21-
// You can opt-out of this in one of the followg ways:
22-
// * Use `--cfg pin_project_stable_docs` in RUSTFLAGS.
23-
// ```toml
24-
// # in Cargo.toml
25-
// [package.metadata.docs.rs]
26-
// rustdoc-args = ["--cfg", "pin_project_stable_docs"]
27-
// ```
28-
// * Use `-Zallow-features` in RUSTFLAGS to disallow unstable features.
29-
Channel::Nightly | Channel::Dev
30-
if feature_allowed("proc_macro_def_site") && !cfg!(pin_project_stable_docs) =>
31-
{
32-
println!("cargo:rustc-cfg=proc_macro_def_site");
33-
}
34-
_ => {}
4+
// While this crate supports stable Rust, it currently requires
5+
// nightly Rust in order for rustdoc to correctly document auto-generated
6+
// `Unpin` impls. This does not affect the runtime functionality of this crate,
7+
// nor does it affect the safety of the api provided by this crate.
8+
//
9+
// This is disabled by default and can be enabled using
10+
// `--cfg pin_project_show_unpin_struct` in RUSTFLAGS.
11+
//
12+
// Refs:
13+
// * https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867
14+
// * https://github.com/taiki-e/pin-project/pull/70
15+
// * https://github.com/rust-lang/rust/issues/63281
16+
if cfg!(pin_project_show_unpin_struct) && feature_allowed("proc_macro_def_site") {
17+
println!("cargo:rustc-cfg=proc_macro_def_site");
3518
}
3619
}
3720

pin-project-internal/src/pin_project/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@ impl Context {
192192
{
193193
proc_macro::Span::def_site().into()
194194
}
195-
196195
#[cfg(not(proc_macro_def_site))]
197196
{
198197
Span::call_site()
199198
}
200199
};
201200

202-
let struct_ident = format_ident!("__UnpinStruct{}", orig_ident, span = make_span());
201+
let struct_ident = if cfg!(proc_macro_def_site) {
202+
format_ident!("UnpinStruct{}", orig_ident, span = make_span())
203+
} else {
204+
format_ident!("__UnpinStruct{}", orig_ident)
205+
};
203206
let always_unpin_ident = format_ident!("AlwaysUnpin{}", orig_ident, span = make_span());
204207

205208
// Generate a field in our new struct for every
@@ -292,6 +295,7 @@ impl Context {
292295
///
293296
/// [taiki-e/pin-project#53]: https://github.com/taiki-e/pin-project/pull/53#issuecomment-525906867
294297
/// [rust-lang/rust#63281]: https://github.com/rust-lang/rust/issues/63281
298+
#[allow(dead_code)]
295299
#vis struct #struct_ident #full_generics #where_clause {
296300
__pin_project_use_generics: #always_unpin_ident <(#(#type_params),*)>,
297301

src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
//! * [`pinned_drop`] - An attribute for annotating a function that implements `Drop`.
77
//! * [`project`] - An attribute to support pattern matching.
88
//!
9-
//! NOTE: While this crate supports stable Rust, it currently requires
10-
//! nightly Rust in order for rustdoc to correctly document auto-generated
11-
//! `Unpin` impls. This does not affect the runtime functionality of this crate,
12-
//! nor does it affect the safety of the api provided by this crate.
13-
//!
14-
//!
159
//! ## Examples
1610
//!
1711
//! [`pin_project`] attribute creates a projection struct covering all the fields.

tests/compiletest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg(compiletest)]
2+
#![cfg(pin_project_show_unpin_struct)]
23
#![cfg(feature = "project_attr")]
34
#![warn(rust_2018_idioms, single_use_lifetimes)]
45

tests/ui/pin_project/proper_unpin.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0277]: the trait bound `T: std::marker::Unpin` is not satisfied in `__UnpinStructFoo<T, U>`
1+
error[E0277]: the trait bound `T: std::marker::Unpin` is not satisfied in `UnpinStructFoo<T, U>`
22
--> $DIR/proper_unpin.rs:20:5
33
|
44
17 | fn is_unpin<T: Unpin>() {}
55
| ----------------------- required by `is_unpin`
66
...
77
20 | is_unpin::<Foo<T, U>>(); //~ ERROR E0277
8-
| ^^^^^^^^^^^^^^^^^^^^^ within `__UnpinStructFoo<T, U>`, the trait `std::marker::Unpin` is not implemented for `T`
8+
| ^^^^^^^^^^^^^^^^^^^^^ within `UnpinStructFoo<T, U>`, the trait `std::marker::Unpin` is not implemented for `T`
99
|
1010
= help: consider adding a `where T: std::marker::Unpin` bound
1111
= note: required because it appears within the type `Inner<T>`
12-
= note: required because it appears within the type `__UnpinStructFoo<T, U>`
12+
= note: required because it appears within the type `UnpinStructFoo<T, U>`
1313
= note: required because of the requirements on the impl of `std::marker::Unpin` for `Foo<T, U>`
1414

1515
error: aborting due to previous error

tests/ui/pin_project/unpin_sneaky.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ struct Foo {
88
inner: u8,
99
}
1010

11-
impl Unpin for __UnpinStructFoo {} //~ ERROR E0412,E0321
11+
impl Unpin for UnpinStructFoo {} //~ ERROR E0412,E0321
1212

1313
fn main() {}

tests/ui/pin_project/unpin_sneaky.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error[E0412]: cannot find type `__UnpinStructFoo` in this scope
1+
error[E0412]: cannot find type `UnpinStructFoo` in this scope
22
--> $DIR/unpin_sneaky.rs:11:16
33
|
4-
11 | impl Unpin for __UnpinStructFoo {} //~ ERROR E0412,E0321
5-
| ^^^^^^^^^^^^^^^^ not found in this scope
4+
11 | impl Unpin for UnpinStructFoo {} //~ ERROR E0412,E0321
5+
| ^^^^^^^^^^^^^^ not found in this scope
66
help: possible candidate is found in another module, you can import it into scope
77
|
8-
3 | use crate::__UnpinStructFoo;
8+
3 | use crate::UnpinStructFoo;
99
|
1010

1111
error[E0321]: cross-crate traits with a default impl, like `std::marker::Unpin`, can only be implemented for a struct/enum type, not `[type error]`
1212
--> $DIR/unpin_sneaky.rs:11:1
1313
|
14-
11 | impl Unpin for __UnpinStructFoo {} //~ ERROR E0412,E0321
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
14+
11 | impl Unpin for UnpinStructFoo {} //~ ERROR E0412,E0321
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
1616

1717
error: aborting due to 2 previous errors
1818

0 commit comments

Comments
 (0)