Stream: rustdoc

Topic: json: weird output for impl in function


view this post on Zulip Urgau (Aug 03 2021 at 17:19):

I tried the rustdoc json output to this function:

pub fn generics<T>(a: T, b: impl Iterator<Item = bool>) -> ! {}

and the output was:

[src/main.rs:52] item = Item {
    id: Id(
        "0:28",
    ),
    crate_id: 0,
    name: Some(
        "generics",
    ),
    span: Some(
        Span {
            filename: "lib.rs",
            begin: (
                37,
                0,
            ),
            end: (
                37,
                63,
            ),
        },
    ),
    visibility: Public,
    docs: None,
    links: {},
    attrs: [],
    deprecation: None,
    inner: Function(
        Function {
            decl: FnDecl {
                inputs: [
                    (
                        "a",
                        Generic(
                            "T",
                        ),
                    ),
                    (
                        "b",
                        ImplTrait(
                            [
                                TraitBound {
                                    trait_: ResolvedPath {
                                        name: "Iterator",
                                        id: Id(
                                            "2:7435",
                                        ),
                                        args: Some(
                                            AngleBracketed {
                                                args: [],
                                                bindings: [
                                                    TypeBinding {
                                                        name: "Item",
                                                        binding: Equality(
                                                            Primitive(
                                                                "bool",
                                                            ),
                                                        ),
                                                    },
                                                ],
                                            },
                                        ),
                                        param_names: [],
                                    },
                                    generic_params: [],
                                    modifier: None,
                                },
                            ],
                        ),
                    ),
                ],
                output: Some(
                    Never,
                ),
                c_variadic: false,
            },
            generics: Generics {
                params: [
                    GenericParamDef {
                        name: "T",
                        kind: Type {
                            bounds: [],
                            default: None,
                        },
                    },
                    GenericParamDef {
                        name: "impl Iterator<Item = bool>",
                        kind: Type {
                            bounds: [
                                TraitBound {
                                    trait_: ResolvedPath {
                                        name: "Iterator",
                                        id: Id(
                                            "2:7435",
                                        ),
                                        args: Some(
                                            AngleBracketed {
                                                args: [],
                                                bindings: [
                                                    TypeBinding {
                                                        name: "Item",
                                                        binding: Equality(
                                                            Primitive(
                                                                "bool",
                                                            ),
                                                        ),
                                                    },
                                                ],
                                            },
                                        ),
                                        param_names: [],
                                    },
                                    generic_params: [],
                                    modifier: None,
                                },
                            ],
                            default: None,
                        },
                    },
                ],
                where_predicates: [],
            },
            header: {},
            abi: "\"Rust\"",
        },
    ),
}

The thing that I find weird is that the impl Iterator<Item = bool> is in both function.inputs and function.generics.
Is that expected ? If yes, how do I distinguishes them ?

view this post on Zulip Urgau (Aug 04 2021 at 14:23):

I hope it's ok to ping you. If it's not, let me know. cc @Joseph Ryan @CraftSpider

view this post on Zulip GuillaumeGomez (Aug 04 2021 at 15:51):

Well, that seems normal to me. It's a generic argument after all

view this post on Zulip Urgau (Aug 04 2021 at 15:59):

That's what I thought; Would you know a way to differentiate them? In my use case I want to print the function like you would write it.

view this post on Zulip Joseph Ryan (Aug 04 2021 at 15:59):

Yeah impl Trait is the only time you end up with that duplication I think? since usually inputs will contain args "as written" like a: T and then generics will have the where clause or generic param info about the actual bounds on T. It'd be weird to not include impl Trait in either place so it gets duplicated, i'm not sure what you mean by "distinguishing them" since they should be the same?

view this post on Zulip Joseph Ryan (Aug 04 2021 at 15:59):

also pinging me is fine, thanks for asking

view this post on Zulip Joseph Ryan (Aug 04 2021 at 16:02):

for the purpose of printing a function signature I think you can just ignore any impl Traits in generics since they cant show up in the generic params or where clause

view this post on Zulip Joseph Ryan (Aug 04 2021 at 16:04):

it'd go something like [visibility] fn [name]<[generics.params]>([inputs]) -> [output] where [generics.where_predicates] if that makes sense

view this post on Zulip Urgau (Aug 04 2021 at 16:08):

Joseph Ryan said:

Yeah impl Trait is the only time you end up with that duplication I think? since usually inputs will contain args "as written" like a: T and then generics will have the where clause or generic param info about the actual bounds on T. It'd be weird to not include impl Trait in either place so it gets duplicated, i'm not sure what you mean by "distinguishing them" since they should be the same?

What I meant was, how do you know which one is "right"?

Joseph Ryan said:

for the purpose of printing a function signature I think you can just ignore any impl Traits in generics since they cant show up in the generic params or where clause

So, I'm just gonna ignore every generic param who name's start with impl, right ?

Joseph Ryan said:

it'd go something like [visibility] fn [name]<[generics.params]>([inputs]) -> [output] where [generics.where_predicates] if that makes sense

Exactly, that's what I had also deduced. Thanks for the confirmation.

view this post on Zulip Urgau (Aug 04 2021 at 16:35):

@Joseph Ryan If you're interested this was the output of the printing for the function above: pub fn generics<T, impl Iterator<Item = bool>: Iterator<Item = bool>>(a: T, b: impl Iterator<Item = bool>) -> !;

view this post on Zulip Joshua Nelson (Aug 04 2021 at 17:47):

It seems wrong that generics has the existential type, it should only be in the arguments IMO

view this post on Zulip Urgau (Aug 04 2021 at 17:58):

Another thing that's also weird is that in generics the name is directly impl Iterator<Item = bool> in addition to having TraitBound which "re-describes" the structure of the impl bound.


Last updated: Oct 11 2021 at 22:34 UTC