Skip to content
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 does not substitute const parameters #82852

Open
fmease opened this issue Mar 7, 2021 · 9 comments
Open

Rustdoc does not substitute const parameters #82852

fmease opened this issue Mar 7, 2021 · 9 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Mar 7, 2021

I ran cargo doc on this code (src/lib.rs):

// #![feature(min_const_generics)] // stable by now

type Array<T, const N: usize> = [T; N];

pub type A0 = [i32; 3];           // printed correctly
pub type A1 = Array<i32, 3>;      // printed INCORRECTLY as `[i32; N]` (should print `[i32; 3]`)
pub struct B0(pub [i32; 3]);      // printed correctly
pub struct B1(pub Array<i32, 3>); // printed INCORRECTLY as `B1(pub [i32; N])` (should print `[i32; 3]`)

I expected to see the definitions type A1 = [i32; 3]; and pub struct B1(pub [i32; 3]); in the generated documentation.
Instead, I found type A1 = [i32; N]; and pub struct B1(pub [i32; N]);.

That is, the definitions wrongly contain a stray identifier N that should have been substituted with the literal 3 just like the T was successfully substituted with i32.

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (51748a8fc 2021-03-05)
binary: rustc
commit-hash: 51748a8fc77283914d4135f31b5966a407208187
commit-date: 2021-03-05
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 12.0.0

@rustbot modify labels: +T-rustdoc +A-const-generics +F-min_const_generics +C-bug

@fmease fmease added the C-bug Category: This is a bug. label Mar 7, 2021
@rustbot rustbot added A-const-generics Area: const generics (parameters and arguments) F-min_const_generics T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 7, 2021
@fmease
Copy link
Member Author

fmease commented Mar 7, 2021

I hope this is not a duplicate or an already known issue that is part of some bigger tracking issue or some agenda. I thoroughly searched beforehand but couldn't find anything related.

@jyn514
Copy link
Member

jyn514 commented Mar 7, 2021

This is a duplicate of #14072 I think.

@fmease
Copy link
Member Author

fmease commented Mar 8, 2021

Hmm, maybe it is. Hard to tell as that old issue is really vague and updates to it are scattered.

In my eyes, this simple case of const parameters (literals only) looks manageable to implement. Not so sure about the general case which requires const evaluation.

@ jyn514, do you think writing the code for the substitution of const parameters is a matter of "copying"/mirroring the one for the substitution of type parameters (which in some parts has to exist as seen by T -> i32)? Thinking about a potential first-time contribution.

@jyn514
Copy link
Member

jyn514 commented Mar 8, 2021

In my eyes, this simple case of const parameters (literals only) looks manageable to implement. Not so sure about the general case which requires const evaluation.

You can evaluate constants with print_evaluated_const:

value: print_evaluated_const(cx, def_id),

@ jyn514, do you think writing the code for the substitution of const parameters is a matter of "copying"/mirroring the one for the substitution of type parameters (which in some parts has to exist as seen by T -> i32)? Thinking about a potential first-time contribution.

I'm not sure how this part of the code works, sorry. Happy to look into it if you get stuck though :)

@fmease
Copy link
Member Author

fmease commented Mar 9, 2021

Happy to look into it if you get stuck though :)

Thank you :) I appreciate that. I opened a Zulip stream to discuss the work on the implementation.

@clarfonthey
Copy link
Contributor

Provided another example in a (now closed) duplicate issue:

pub trait Test {}
pub struct Array<T, const N: usize>([T; N]);

impl Test for [u8; 1 + 1] {}
impl Test for Array<u8, {1 + 1}> {}

screenshot of docs

@fmease

This comment was marked as outdated.

@camelid
Copy link
Member

camelid commented Oct 31, 2021

I think the issue here is that DocContext.ct_substs is actually never read from. I'm looking into this a bit.

@fmease
Copy link
Member Author

fmease commented Oct 31, 2021

@camelid Yes, I've made the same observation and noted down some more findings in the Zulip stream linked above (although some information there might be outdated by now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. 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.

6 participants