Change:
<ins> // Test that certain unevaluated constant expression arguments that are // deemed too verbose or complex and that may leak private or // `doc(hidden)` struct fields are not displayed in the documentation. // // Read the documentation of `rustdoc::clean::utils::print_const_expr` // for further details. #![feature(const_trait_impl, generic_const_exprs)] #![allow(incomplete_features)] // @has hide_complex_unevaluated_const_arguments/trait.Stage.html pub trait Stage { // A helper constant that prevents const expressions containing it // from getting fully evaluated since it doesn't have a body and // thus is non-reducible. This allows us to specifically test the // pretty-printing of *unevaluated* consts. const ABSTRACT: usize; // Currently considered "overly complex" by the `generic_const_exprs` // feature. If / once this expression kind gets supported, this // unevaluated const expression could leak the private struct field. // // FIXME: Once the line below compiles, make this a test that //        ensures that the private field is not printed. // //const ARRAY0: [u8; Struct { private: () } + Self::ABSTRACT]; // This assoc. const could leak the private assoc. function `Struct::new`. // Ensure that this does not happen. // // @has - '//*[@id="associatedconstant.ARRAY1"]'  //        'const ARRAY1: [u8; { _ }]' const ARRAY1: [u8; Struct::new(/* ... */) + Self::ABSTRACT * 1_000]; // @has - '//*[@id="associatedconstant.VERBOSE"]'  //        'const VERBOSE: [u16; { _ }]' const VERBOSE: [u16; compute("thing", 9 + 9) * Self::ABSTRACT]; // Check that we do not leak the private struct field contained within // the path. The output could definitely be improved upon // (e.g. printing sth. akin to `<Self as Helper<{ _ }>>::OUT`) but // right now “safe is safe”. // // @has - '//*[@id="associatedconstant.PATH"]'  //        'const PATH: usize = _' const PATH: usize = <Self as Helper<{ Struct { private: () } }>>::OUT; } const fn compute(input: &str, extra: usize) -> usize { input.len() + extra } pub trait Helper<const S: Struct> { const OUT: usize; } impl<const S: Struct, St: Stage + ?Sized> Helper<S> for St { const OUT: usize = St::ABSTRACT; } // Currently in rustdoc, const arguments are not evaluated in this position // and therefore they fall under the realm of `print_const_expr`. // If rustdoc gets patched to evaluate const arguments, it is fine to replace // this test as long as one can ensure that private fields are not leaked! // // @has hide_complex_unevaluated_const_arguments/trait.Sub.html  //      '//*[@class="rust trait"]'  //      'pub trait Sub: Sup<{ _ }, { _ }> { }' pub trait Sub: Sup<{ 90 * 20 * 4 }, { Struct { private: () } }> {} pub trait Sup<const N: usize, const S: Struct> {} pub struct Struct { private: () } impl Struct { const fn new() -> Self { Self { private: () } } } impl const std::ops::Add<usize> for Struct { type Output = usize; fn add(self, _: usize) -> usize { 0 } } </ins>

Discussion:
<br<img width="300" src="https://user-"<br<img width="300" src="https://user-"<brI used the following script to bisect: It bisects to nightly-2022-04-14 and specifically , of which seems the most relevant because it involves associated constants in rustdoc. <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
is indeed the cause. More specifically the following line: If we “fail” to evaluate (“failure” also includes “the constant is a struct”) the const value of a provided associated const with , we fall back to which prints the constant in raw uncensored form including structs with their private fields. I assume in the non-associated const version (shown below) the call to does not happen and if fails in such a case, neither the value nor the is being printed. . I am not sure how to proceed This is another instance of constant-printing for provided assoc consts being problematic. Previous incident: Performance ().
Maybe we could update the method so that it can only show the public items to fulfill our need?
The ideal solution (non-hotfix) would be to generally improve the printing of (unevaluated) constants (as returned by . . Not sure how easy it is to implement though.
Seems like we want something similar. I don't think it's very difficult but might involve a lot of code. Want to give it a try?
I might as well try. If successful, my PR needs backporting. claim
Error: Parsing assign command in failed: ...'bot assign' ''... Please let know if you're having trouble with this bot.
I'm not convinced that displaying the value of (provided associated) constants in rustdoc is ever worthwhile and indeed it could be harmful. Users should not need to know what value a constant holds, only what it represents; indeed they should only ever be using the constant itself to represent that value when it's required—that is to say, the value assigned is an implementation detail and nobody should be relying upon it.
I strongly disagree. Associated constants are meant to be used by externals users, otherwise they'd not be created.

They are definitely not an implementation detail and they are part of the public API so everyone should rely on them. There are a lot of examples so I'll just take one: all  associated constants in the crate.
I'm not sure I follow. Why do I need to know that is ? Surely I will write code that switches on and the compiler handles the rest?
I agree with They are not an implementation detail, at least not those that are of a type that exposes structural equality (deriving and ) since downstream users may depend (as in “compilation may fail if the implementation changes”) on the concrete value of a constant if they use it in a pattern [exhaustiveness checking] or inside of a const argument [type checking] (which may currently require if the type is not numeric). ( I even vaguely remember seeing a Zulip discussion about how public constants always leak their implementation / concrete value, not just those of a type, but I am not sure anymore )
Isn't that just relying on an implementation detail in order to work-around current limitations in the language? If/when lands, the constant should be used in place of its value?
What I am saying is that changing the value of a public constant is a breaking change since it is part of the public API. As a simplified example, consider the case where a crate called exports two constants of type , named and . Crate depending on crate contains the following code: Crate also depending on : In version A of , constant is not equal to and the dependent crates and compile just fine. Now, in the succeeding version B of , and are set equal to each other. and no longer compile with the new version. The inequality was part of the public contract but it was broken. If we wanted to follow SemVer, version B would need to be incompatible with A.
Ah, understood! Sorry for being slow on the uptake :). That all makes sense now.
I understand your argument to some extent, but changing a function's body can also be a SemVer breaking change. Why should constants — which also hide a complex expression and present a simple API, like functions — be treated differently?
I'm a bit lost. We only show public parts of the API unless using and and I are supporting the idea to only show the public parts of "complex" associated constants' value to make it treated like the other items.

Or did I completely miss your comment?
My point is that we never show function bodies (even with ), and IMO constants are a subset of () functions, so we also should not show constant bodies. We do show the bodies of free (non-associated) constants already, but I think I'd rather stop doing that instead of also displaying associated constants' values.
I see your point. I disagree though. If you update a constant value, it's the same as changing a function's header: it's a breaking change. And it's also super useful to be able to see the value of a constant (associated or not) and I think it's definitely a big plus.
Well, I at least think we shouldn't show the raw expression from the constant's body; we should only show the fully-computed value.
Yes, we should prefer printing the evaluated const. I am working on this. It's not as trivial and I am going to do it incrementally. The biggest issue is the missing support for const substitutions as you might remember. Yesterday I almost had a working PR done, in some sense a hotfix for this issue, that “censors” complex unevaluated expressions like struct literals and match expressions by replacing them with (over time this procedure can be fine-tuned). However, I ran into some nasty corner cases (certain qualified paths that contain const arguments) and regressions. I will try to simplify the patch, add tests and open it as a PR today or tomorrow (hopefully). I have further plans for improving const support but the current representation of constant expressions and the lack of const substitutions in rustdoc is giving me headaches. I have already prepared a more sophisticated PR that adds hyperlinks to const exprs and understands the concept of private and fields when pretty-printing but it's not quite ready yet.
Yeah, the current state of the code is quite unfortunate :( I've tried to improve it, but as you've seen it's very hard to understand and change.
I don't like this. <img width="1440" alt="image" src="https://user-"
PR coming up within the next hour. I just need to write some more tests.