Skip to content

Commit

Permalink
Make not found impl trait not fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jul 7, 2024
1 parent b7cae43 commit 3d25f36
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 69 deletions.
79 changes: 17 additions & 62 deletions src/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,17 @@ fn module_page<'context>(
name: Option<&'context str>,
) -> Option<Result<(&'context Item, Option<&'context str>)>> {
if !id.0.starts_with("0:") {
warn!("ignoring for now `pub use`: {:?}", id);
warn!("ignoring `pub use` of {:?} ({:?})", &name, id);
return None;
}

let item = global_context
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))
.with_context(|| {
format!("unable to find the item {:?} from module - fatal", id)
})
.ok()?;

match &item.inner {
Expand Down Expand Up @@ -871,11 +873,9 @@ fn trait_page<'context>(
.items
.iter()
.map(|id| {
let item = global_context
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;
let item = global_context.krate.index.get(id).with_context(|| {
format!("unable to find the item {:?} - from trait page - fatal", id)
})?;

Ok((
item,
Expand Down Expand Up @@ -937,29 +937,7 @@ fn trait_page<'context>(
}
}

let mut impls = trait_
.implementations
.iter()
.map(|id| {
let item = global_context
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;

let impl_ = match &item.inner {
ItemEnum::Impl(impl_) => impl_,
_ => {
return Err(anyhow::anyhow!(
"impl id is not impl in struct_union_content"
))
}
};

Ok((item, impl_, name_of(impl_)?))
})
.collect::<Result<Vec<_>>>()?;
impls.sort_by(|(_, _, x_name), (_, _, y_name)| x_name.cmp(y_name));
let impls = fetch_impls(global_context, &trait_.implementations)?;

for (item, impl_, _name) in &impls {
let (toc, who) = match type_id(&impl_.for_) {
Expand Down Expand Up @@ -1024,28 +1002,7 @@ fn struct_union_enum_content<'context, 'krate>(
variants: &[Id],
impls: &[Id],
) -> Result<(Vec<TocSection<'context>>, impl markup::Render + 'context)> {
let mut impls = impls
.iter()
.map(|id| {
let item = global_context
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;

let impl_ = match &item.inner {
ItemEnum::Impl(impl_) => impl_,
_ => {
return Err(anyhow::anyhow!(
"impl id is not impl in struct_union_content"
))
}
};

Ok((item, impl_, name_of(impl_)?))
})
.collect::<Result<Vec<_>>>()?;
impls.sort_by(|(_, _, x_name), (_, _, y_name)| x_name.cmp(y_name));
let impls = fetch_impls(global_context, &impls)?;

let mut toc_variants = TocSection {
name: VARIANTS,
Expand Down Expand Up @@ -1093,7 +1050,7 @@ fn struct_union_enum_content<'context, 'krate>(
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;
.with_context(|| format!("unable to find variant {:?} -- fatal", id))?;

VariantEnchantedWithExtras::from_variant(
global_context,
Expand Down Expand Up @@ -1128,7 +1085,7 @@ fn struct_union_enum_content<'context, 'krate>(
|(item, impl_, _)| match (&impl_.trait_, &impl_.blanket_impl) {
(Some(rustdoc_types::Path { id, .. }), None) => {
match is_auto_trait(global_context.krate, id) {
Ok((false, _)) => Some(CodeEnchantedWithExtras::from_items(
Ok(Some((false, _))) => Some(CodeEnchantedWithExtras::from_items(
global_context,
page_context,
TocSupplier::Top(&mut toc_traits),
Expand All @@ -1150,7 +1107,7 @@ fn struct_union_enum_content<'context, 'krate>(
|(item, impl_, _)| match (&impl_.trait_, &impl_.blanket_impl) {
(Some(rustdoc_types::Path { id, .. }), None) => {
match is_auto_trait(global_context.krate, id) {
Ok((true, _)) => Some(CodeEnchantedWithExtras::from_items(
Ok(Some((true, _))) => Some(CodeEnchantedWithExtras::from_items(
global_context,
page_context,
TocSupplier::Top(&mut toc_auto_traits),
Expand Down Expand Up @@ -1423,11 +1380,9 @@ impl<'context, 'krate>
.items
.iter()
.map(|id| {
let item = global_context
.krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;
let item = global_context.krate.index.get(id).with_context(|| {
format!("unable to find the impl item {:?} -- fatal", id)
})?;

CodeEnchanted::from_item(
global_context,
Expand Down Expand Up @@ -1545,7 +1500,7 @@ impl<'context, 'krate>
.map(|id| {
let item =
global_context.krate.index.get(id).with_context(|| {
format!("Unable to find the item {:?}", id)
format!("unable to find struct field {:?}", id)
})?;

VariantEnchanted::from_item(
Expand All @@ -1564,7 +1519,7 @@ impl<'context, 'krate>
.map(|id| {
let item =
global_context.krate.index.get(id).with_context(|| {
format!("Unable to find the item {:?}", id)
format!("unable to find tuple field {:?}", id)
})?;

VariantEnchanted::from_item(
Expand Down
48 changes: 41 additions & 7 deletions src/html/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Collections of utilities functions for the html generation
use anyhow::{anyhow, Context as _, Result};
use log::{debug, trace};
use log::{debug, trace, warn};
use rustdoc_types::*;
use std::borrow::Cow;
use std::path::{Path as StdPath, PathBuf};
Expand All @@ -10,6 +10,34 @@ use super::id::Id as HtmlId;
use super::render::{GlobalContext, PageContext};
use crate::pp;

pub(crate) fn fetch_impls<'context, 'krate>(
global_context: &'context GlobalContext<'krate>,
impls_ids: &[Id],
) -> Result<Vec<(&'krate Item, &'krate Impl, String)>> {
let mut impls = Vec::with_capacity(impls_ids.len());

for id in impls_ids {
let Some(item) = global_context.krate.index.get(id) else {
warn!("unable to find impl {:?} -- skipping", id);
continue;
};

let impl_ = match &item.inner {
ItemEnum::Impl(impl_) => impl_,
_ => {
return Err(anyhow::anyhow!(
"impl id is not impl in struct_union_content"
))
}
};

impls.push((item, impl_, name_of(impl_)?))
}

impls.sort_by(|(_, _, x_name), (_, _, y_name)| x_name.cmp(y_name));
Ok(impls)
}

pub(crate) fn prefix_item_kind(kind: &ItemKind) -> Option<(&'static str, bool)> {
Some(match kind {
ItemKind::Module => ("mod", true),
Expand Down Expand Up @@ -80,14 +108,20 @@ pub(crate) fn type_id(type_: &Type) -> Result<&Id, Option<ItemKind>> {
}

/// Determine if an [`Item`] is auto-trait and also return the crate id
pub(crate) fn is_auto_trait<'krate>(krate: &'krate Crate, id: &'krate Id) -> Result<(bool, u32)> {
let item = krate
.index
.get(id)
.with_context(|| format!("Unable to find the item {:?}", id))?;
pub(crate) fn is_auto_trait<'krate>(
krate: &'krate Crate,
id: &'krate Id,
) -> Result<Option<(bool, u32)>> {
let Some(item) = krate.index.get(id) else {
warn!(
"unable to find impl (for auto-trait checking) {:?} -- skipping",
id
);
return Ok(None);
};

Ok(match &item.inner {
ItemEnum::Trait(trait_) => (trait_.is_auto, item.crate_id),
ItemEnum::Trait(trait_) => Some((trait_.is_auto, item.crate_id)),
_ => return Err(anyhow!("is_auto_trait: error not an trait")),
})
}
Expand Down

0 comments on commit 3d25f36

Please sign in to comment.