-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rustdoc doesn't handle impl section of type definitions #32077
Comments
Yes, they're currently ignored. I believe @alexcrichton was the one who made this change? |
This was never really "added" or "changed", it's just a bug in the implementation. |
@srinivasreddy No I'm not, but I'd be happy to see this fixed |
@rtbo Thanks for confirmation. I am on it. |
What is the expectation here? To only show direct impls on the typedef ( |
related #19381 |
@mitaa my expectation would be to have on the rustdoc page of the |
I'd say methods from the original type that apply should be shown here, just like methods from |
Wonder whether it's still considered a known/ignored rustdoc bug? It's a bit of a PITA for crates that defined most types as specialisations, which is a quite common part in FFI-heavy code (this and the fact that typedefs are eagerly expanded everywhere even when it doesn't make much sense). |
+1 on this, https://docs.rs/xi-rope/0.2.0/xi_rope/rope/type.Rope.html is missing all the impl Rope methods, which is where so much of the good stuff is. |
I am looking into this, as it makes My initial prototype renders this code: pub trait Trait1 {
/// From Trait1
fn trait_func1();
}
pub trait Trait2 {
/// From Trait2
fn trait_func2();
}
/// This is a MyStruct.
pub struct MyStruct;
impl MyStruct {
/// do_stuff() with MyStruct
pub fn do_stuff(&self) {}
}
impl Trait1 for MyStruct {
fn trait_func1() {}
}
/// This is a MyAlias
pub type MyAlias = MyStruct;
impl MyAlias {
/// do_more_stuff() with MyAlias
pub fn do_more_stuff(&self) {}
}
impl Trait2 for MyAlias {
fn trait_func2() {}
} ... like this: I haven't played around with |
I'd missed that there was a PR (#25892) referenced from related issue #19381, which took a similar approach to the one I was preparing. That PR was closed as @alexcrichton pointed out:
You can see this in the following example: /// A generic struct.
pub struct GenericStruct<T> {
i: T
}
impl<T> GenericStruct<T> {
/// A method on TypedefStruct.
pub fn generic_impl_method(arg: T) {}
}
/// A typedef for GenericStruct, without any generics.
pub type TypedefStruct = GenericStruct<u8>;
impl TypedefStruct {
/// A method on TypedefStruct.
pub fn typedef_impl_method() {}
} ... which renders as: In the I had a look at coming up with a solution for #14072, but it's very involved and I think it's beyond my abilities. I'll continue to take a look, but I am not optimistic! From the closed PR #25892:
It is worth pointing out, that in the case of Would it be an improvement if the page for |
This is also badly needed in nalgebra, which suffers both from otherwise-invisible |
Ah, I hadn't even considered the need to recursively follow type aliases, which would be necessary for nalgebra: type Matrix2<N> = MatrixN<N, U2>;
type MatrixN<N, D> = MatrixNM<N, D, D>;
type MatrixNM<N, R, C> = Matrix<N, R, C, MatrixArray<N, R, C>>;
pub struct Matrix<N: Scalar, R: Dim, C: Dim, S> { .. }
// Ideally this would be visible in the documentation for Matrix2<N>,
// with the correct types substituted throughout the impl:
impl<N, R: Dim, C: Dim, S> Matrix<N, R, C, S>
where N: Scalar + ClosedNeg,
S: StorageMut<N, R, C> { .. }
I've created a pull request (#42027) for this simple improvement. |
@mjkillough Just to add, in my case it looks like this: trait ID {}
struct Object<T: ID> {}
impl<T: ID> Object<T: ID> { /* shared methods */ }
struct FileID {}
impl ID for FileID {}
pub type File = Object<FileID>;
impl File { /* file-specific methods */ }
struct GroupID {}
impl ID for GroupID {}
pub type Group = Object<GroupID>;
impl Group { /* group-specific methods */ }
trait ContainerID : ID {}
impl ContainerID for FileID {}
impl ContainerID for GroupID {}
impl<T: ContainerID> Object<T> { /* container methods */ } This way, help for |
Document direct implementations on type aliases. This improves #32077, but is not a complete fix. For a type alias `type NewType = AliasedType`, it will include any `impl NewType` and `impl Trait for NewType` blocks in the documentation for `NewType`. A complete fix would include the implementations from the aliased type in the type alias' documentation, so that users have a complete picture of methods that are available on the alias. However, to do this properly would require a fix for #14072, as the alias may affect the type parameters of the type alias, making the documentation difficult to understand. (That is, for `type Result = std::result::Result<(), ()>` we would ideally show documentation for `impl Result<(), ()>`, rather than generic documentation for `impl<T, E> Result<T, E>`). I think this improvement is worthwhile, as it exposes implementations which are not currently documented by rustdoc. The documentation for the implementations on the aliased type are still accessible by clicking through to the docs for that type. (Although perhaps it's now less obvious to the user that they should click-through to get there).
(e.g. this example and this one, may also involve #14072 to some extent...) |
MCVE: pub struct Event {}
pub type KeyPressEvent = Event;
impl Event {
pub fn detail(&self) {}
} As of 1.48 stable, |
Note that a workaround is to use |
This would be extremely useful for all svd2rust-generated crates (e.g. the whole embedded ecosystem). Currently, svd2rust generates a bunch of typedefs for each registers, like this: https://docs.rs/nrf51-hal/0.12.1/nrf51_hal/pac/uart0/pseltxd/type.W.html The problem is, it can be very hard to know what function is available on this typedef, as the generic This makes life quite miserable. My expectation is that typedefs would list all impls that apply to them, filtering out all impls that don't apply to that instantiation of the type (e.g. if we have |
@GuillaumeGomez One of the comments above sums up the type alias issues I mentioned: #32077 (comment) Ideally, the documentation of the alias should include whatever method is applicable to that type (taking into account potential trait bounds). One example in |
Noted. I'll try to fix this issue in the next weeks. Thanks for the ping! |
…otriddle,lcnr [rustdoc] List matching impls on type aliases Fixes rust-lang#32077. Thanks a lot to `@lcnr` who helped me a lot with this fix! cc `@notriddle` r? `@lcnr`
This reverts commit 6f552c8.
Rollup merge of rust-lang#115201 - notriddle:notriddle/type-alias-impl-list, r=GuillaumeGomez rustdoc: list matching impls on type aliases Fixes rust-lang#32077 Fixes rust-lang#99952 Remake of rust-lang#112429 Partially reverts rust-lang#112543, but keeps the test case. This version of the PR avoids the infinite loop by structurally matching types instead of using full unification. This version does not support type alias trait bounds, but the compiler does not enforce those anyway (rust-lang#21903). r? `@GuillaumeGomez` CC `@lcnr`
In rust-xcb I have the following code:
However as one can see here, the impl section is ignored.
The text was updated successfully, but these errors were encountered: