Skip to content

Commit

Permalink
Rollup merge of #98577 - GuillaumeGomez:associated-items, r=notriddle
Browse files Browse the repository at this point in the history
Fix "kind" for associated types in trait implementations in rustdoc JSON

Fixes #81340.

Contrary to what is suggested in the issue, I really think we should distinguish between associated items and "normal" constants and types.

cc `@CraftSpider` `@SimonSapin`
r? `@notriddle`
  • Loading branch information
matthiaskrgr authored Jun 27, 2022
2 parents 9509348 + 9277f95 commit d0ae6eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,11 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
bounds: b.into_iter().map(|x| x.into_tcx(tcx)).collect(),
default: None,
},
// FIXME: do not map to Typedef but to a custom variant
AssocTypeItem(t, _) => ItemEnum::Typedef(t.into_tcx(tcx)),
AssocTypeItem(t, b) => ItemEnum::AssocType {
generics: t.generics.into_tcx(tcx),
bounds: b.into_iter().map(|x| x.into_tcx(tcx)).collect(),
default: t.item_type.map(|ty| ty.into_tcx(tcx)),
},
// `convert_item` early returns `None` for striped items and keywords.
StrippedItem(_) | KeywordItem(_) => unreachable!(),
ExternCrateItem { ref src } => ItemEnum::ExternCrate {
Expand Down
29 changes: 29 additions & 0 deletions src/test/rustdoc-json/assoc_items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![no_std]

// @has assoc_items.json

pub struct Simple;

impl Simple {
// @has - "$.index[*][?(@.name=='CONSTANT')].kind" \"assoc_const\"
pub const CONSTANT: usize = 0;
}

pub trait EasyToImpl {
// @has - "$.index[*][?(@.name=='ToDeclare')].kind" \"assoc_type\"
// @has - "$.index[*][?(@.name=='ToDeclare')].inner.default" null
type ToDeclare;
// @has - "$.index[*][?(@.name=='AN_ATTRIBUTE')].kind" \"assoc_const\"
// @has - "$.index[*][?(@.name=='AN_ATTRIBUTE')].inner.default" null
const AN_ATTRIBUTE: usize;
}

impl EasyToImpl for Simple {
// @has - "$.index[*][?(@.name=='ToDeclare')].inner.default.kind" \"primitive\"
// @has - "$.index[*][?(@.name=='ToDeclare')].inner.default.inner" \"usize\"
type ToDeclare = usize;
// @has - "$.index[*][?(@.name=='AN_ATTRIBUTE')].inner.type.kind" \"primitive\"
// @has - "$.index[*][?(@.name=='AN_ATTRIBUTE')].inner.type.inner" \"usize\"
// @has - "$.index[*][?(@.name=='AN_ATTRIBUTE')].inner.default" \"12\"
const AN_ATTRIBUTE: usize = 12;
}

0 comments on commit d0ae6eb

Please sign in to comment.