Skip to content

Don't show two different types as the same thing in a single function #122673

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

Open
scottmcm opened this issue Mar 18, 2024 · 10 comments
Open

Don't show two different types as the same thing in a single function #122673

scottmcm opened this issue Mar 18, 2024 · 10 comments
Assignees
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Mar 18, 2024

I noticed this in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/traits/consts/trait.ConstMethods.html#tymethod.scalar_to_backend today:

image

Those two Scalars are actually different types, however.

It would be nice to detect things like this and show some distinguisher. Maybe value::Scalar and rustc_abi::Scalar -- including enough module context to distinguish -- or something? Or maybe show the crate name?

(Crate name might be good, since a single crate can fix multiple types with the same name, if needed.)

@scottmcm scottmcm added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-ui Area: Rustdoc UI (generated HTML) labels Mar 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 18, 2024
@krtab
Copy link
Contributor

krtab commented Mar 18, 2024

@rustbot claim

(cc @GuillaumeGomez )

@jieyouxu jieyouxu added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 18, 2024
@fmease
Copy link
Member

fmease commented Mar 22, 2024

Prior art: trimmed_def_paths.

@krtab
Copy link
Contributor

krtab commented Mar 22, 2024

I don't think trimmed_def_paths does exactly what we want, because in our case, we should trim based on what has already been displayed.

MRE

pub mod Foo {
    pub enum E {}
}

pub mod Bar {
    pub enum E {}
}


pub fn f(foo: Foo::E, bar: Bar::E) {}

gives in doc

pub fn f(foo: E, bar: E)

Now the question is, should it be:

pub fn f(foo: E, bar: Foo::E)
pub fn f(foo: Bar::E, bar: E)

or

pub fn f(foo: Bar::E, bar: Foo::E)

@fmease
Copy link
Member

fmease commented Mar 22, 2024

I think the latter (or was that a rhetorical question lol?)

@GuillaumeGomez
Copy link
Member

I also prefer the latter (whether it's a rhetorical question or not).

krtab added a commit to krtab/rust that referenced this issue Mar 22, 2024
@krtab
Copy link
Contributor

krtab commented Mar 22, 2024

I prefer it as well, but as discussed in #122872 this means that there must be two passes to disambiguate: the first one identifies the non ambiguous suffix of the symbol path, and the second one does the actual rendering. Choosing pub fn f(foo: E, bar: Foo::E) allows us to do it in one pass (a sort of "streaming" disambiguation), which makes it easier to generalize: printing functions must now simply take a context and reset it when appropriate. This context can easily be passed along when we match on the constructs.

If we do the two passes solution, the matching/deconstructing code has to be duplicated (or we go for a full visitor pattern?)

@GuillaumeGomez
Copy link
Member

What information is missing to force it to be done in two passes? Once you have all names in the same context (ie, function arguments, struct fields, etc), you can check that a same type has the same name or not and there disambiguate. If you don't have a context when displaying, then try generating the name ahead of time if needed in the clean pass?

@wackbyte
Copy link
Contributor

Here's some notes I have on how I think disambiguation should work:

  • The least possible context should be used to disambiguate.
    • i.e., a::b::c::T and a::d::c::T disambiguate as b::c::T and d::c::T.
    • What about types from the crate root? Perhaps $crate::T?
      • What if there were extern crate foo and mod foo? ::foo::T and foo::T? foo::T and $crate::foo::T?
    • self, super, and paths to re-exports should be preserved for disambiguation.
      • i.e., paths should not necessarily be canonicalized.
  • The name of an item should be prioritized on its own page.
    • i.e., struct Foo(Foo, other::Foo) should preserve the first Foo in Foo's page.
      • This would not occur in the "Aliased type" section of the page of a type alias using Foo, though, because it would not be Foo's page.
    • The same should occur in the implementations on a trait's page.
  • Type parameters should be accounted for.
    • i.e., <T = a::T> should not render as <T = T>.
  • Disambiguation on item pages should work in one of the following ways:
    1. Local to the item definition, each implementation, and each associated item.
      • i.e., a::T and b::T in different methods in the same implementation can coexist as T.
    2. Local to the item definition and each implementation.
      • i.e., a::T and b::T in different implementations on the same page can coexist as T.
      • I prefer this one.
    3. Global to the entire page.
      • i.e., any types with the same name referenced on a page will always be disambiguated.
      • This one is the most consistent, but perhaps unnecessarily so?

@scottmcm
Copy link
Member Author

scottmcm commented Dec 2, 2024

Another example:
Image

The type is mir::Const, but the thing in the first variant is a ty::Const instead.

@GuillaumeGomez
Copy link
Member

Still in my TODO list. ^^'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants