Hide = _
as associated constant value inside impl blocks by dtolnay · Pull Request #134321 · rust-lang/rust (original) (raw)
Yeah I gave this some thought before going with the current approach. From what I figured out (I haven't worked in this code before):
Before this PR, AssocConstItem
and TyAssocConstItem
are separate variants because they must store different contents. The variant's data is either Constant
(which is Generics
+ ConstantKind
+ Type
) or just Generics
+ Type
.
In order to merge ProvidedAssocConstItem
and RequiredAssocConstItem
into a single TraitAssocConstItem
variant, either:
Constant
needs to containOption<ConstantKind>
, which has a big blast radius because non-associated constants and paths (dyn Foo<K = 0>
) use this sameConstant
type in a context where not having a value doesn't make sense.- or we need
TraitAssocConstItem
to store notConstant
but some otherConstantWithOptionalValue
type — which ends up being not less duplication than splittingProvidedAssocConstItem
vsRequiredAssocConstItem
.
There is a more promising approach, which I gave up on but is still potentially salvageable. Notice this parent
argument:
fn render_assoc_item( |
---|
w: &mut Buffer, |
item: &clean::Item, |
link: AssocItemLink<'_>, |
parent: ItemType, |
clean::AssocConstItem(ci) => assoc_const( |
---|
w, |
item, |
&ci.generics, |
&ci.type_, |
Some(&ci.kind), |
link, |
if parent == ItemType::Trait { 4 } else { 0 }, |
cx, |
), |
When printing, associated constants are supposed to know whether they are inside of a ItemType::Trait
as opposed to a ItemType::Impl
. This would be perfect for deciding whether = _
needs to be shown (only when parent is trait, never when parent is impl).
But this doesn't work because parent
ends up being misused under the assumption that it's only relevant for indentation (?). See this call site, which is inside a function called item_trait
and is responsible for printing a trait and its contents. And yet it is passing ItemType::Impl
as the value of parent
, which does not make sense.
render_assoc_item( |
---|
w, |
m, |
AssocItemLink::Anchor(Some(&id)), |
ItemType::Impl, |
cx, |
RenderMode::Normal, |
); |