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: simplify clean by removing FnRetTy #112113

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ fn clean_fn_decl_with_args<'tcx>(
args: Arguments,
) -> FnDecl {
let output = match decl.output {
hir::FnRetTy::Return(typ) => Return(clean_ty(typ, cx)),
hir::FnRetTy::DefaultReturn(..) => DefaultReturn,
hir::FnRetTy::Return(typ) => clean_ty(typ, cx),
hir::FnRetTy::DefaultReturn(..) => Type::Tuple(Vec::new()),
};
FnDecl { inputs: args, output, c_variadic: decl.c_variadic }
}
Expand All @@ -1126,10 +1126,7 @@ fn clean_fn_decl_from_did_and_sig<'tcx>(

// We assume all empty tuples are default return type. This theoretically can discard `-> ()`,
// but shouldn't change any code meaning.
let output = match clean_middle_ty(sig.output(), cx, None) {
Type::Tuple(inner) if inner.is_empty() => DefaultReturn,
ty => Return(ty),
};
let output = clean_middle_ty(sig.output(), cx, None);

FnDecl {
output,
Expand Down
44 changes: 15 additions & 29 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use crate::formats::item_type::ItemType;
use crate::html::render::Context;
use crate::passes::collect_intra_doc_links::UrlFragment;

pub(crate) use self::FnRetTy::*;
pub(crate) use self::ItemKind::*;
pub(crate) use self::SelfTy::*;
pub(crate) use self::Type::{
Expand Down Expand Up @@ -1353,7 +1352,7 @@ pub(crate) struct Function {
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) struct FnDecl {
pub(crate) inputs: Arguments,
pub(crate) output: FnRetTy,
pub(crate) output: Type,
pub(crate) c_variadic: bool,
}

Expand All @@ -1371,18 +1370,16 @@ impl FnDecl {
///
/// This function will panic if the return type does not match the expected sugaring for async
/// functions.
pub(crate) fn sugared_async_return_type(&self) -> FnRetTy {
match &self.output {
FnRetTy::Return(Type::ImplTrait(bounds)) => match &bounds[0] {
GenericBound::TraitBound(PolyTrait { trait_, .. }, ..) => {
let bindings = trait_.bindings().unwrap();
let ret_ty = bindings[0].term();
let ty = ret_ty.ty().expect("Unexpected constant return term");
FnRetTy::Return(ty.clone())
}
_ => panic!("unexpected desugaring of async function"),
},
_ => panic!("unexpected desugaring of async function"),
pub(crate) fn sugared_async_return_type(&self) -> Type {
if let Type::ImplTrait(v) = &self.output &&
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
{
let bindings = trait_.bindings().unwrap();
let ret_ty = bindings[0].term();
let ty = ret_ty.ty().expect("Unexpected constant return term");
ty.clone()
} else {
panic!("unexpected desugaring of async function")
}
}
}
Expand Down Expand Up @@ -1425,21 +1422,6 @@ impl Argument {
}
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) enum FnRetTy {
Return(Type),
DefaultReturn,
}

impl FnRetTy {
pub(crate) fn as_return(&self) -> Option<&Type> {
match self {
Return(ret) => Some(ret),
DefaultReturn => None,
}
}
}

#[derive(Clone, Debug)]
pub(crate) struct Trait {
pub(crate) def_id: DefId,
Expand Down Expand Up @@ -1641,6 +1623,10 @@ impl Type {
matches!(self, Type::ImplTrait(_))
}

pub(crate) fn is_unit(&self) -> bool {
matches!(self, Type::Tuple(v) if v.is_empty())
}

pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
if let QPath(box QPathData { self_type, trait_, assoc, .. }) = self {
Some((self_type, trait_.as_ref()?.def_id(), assoc.clone()))
Expand Down
39 changes: 18 additions & 21 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,9 +1257,9 @@ impl clean::Impl {
};
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), "#trait-implementations-1", cx)?;
// Write output.
if let clean::FnRetTy::Return(ty) = &bare_fn.decl.output {
if !bare_fn.decl.output.is_unit() {
write!(f, " -> ")?;
fmt_type(ty, f, use_absolute, cx)?;
fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
}
} else if let Some(ty) = self.kind.as_blanket_ty() {
fmt_type(ty, f, use_absolute, cx)?;
Expand Down Expand Up @@ -1296,22 +1296,6 @@ impl clean::Arguments {
}
}

impl clean::FnRetTy {
pub(crate) fn print<'a, 'tcx: 'a>(
&'a self,
cx: &'a Context<'tcx>,
) -> impl fmt::Display + 'a + Captures<'tcx> {
display_fn(move |f| match self {
clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
clean::Return(ty) if f.alternate() => {
write!(f, " -> {:#}", ty.print(cx))
}
clean::Return(ty) => write!(f, " -&gt; {}", ty.print(cx)),
clean::DefaultReturn => Ok(()),
})
}
}

impl clean::BareFunctionDecl {
fn print_hrtb_with_space<'a, 'tcx: 'a>(
&'a self,
Expand Down Expand Up @@ -1366,15 +1350,15 @@ impl clean::FnDecl {
"({args:#}{ellipsis}){arrow:#}",
args = self.inputs.print(cx),
ellipsis = ellipsis,
arrow = self.output.print(cx)
arrow = self.print_output(cx)
)
} else {
write!(
f,
"({args}{ellipsis}){arrow}",
args = self.inputs.print(cx),
ellipsis = ellipsis,
arrow = self.output.print(cx)
arrow = self.print_output(cx)
)
}
})
Expand Down Expand Up @@ -1464,9 +1448,22 @@ impl clean::FnDecl {
Some(n) => write!(f, "\n{})", Indent(n))?,
};

fmt::Display::fmt(&self.output.print(cx), f)?;
fmt::Display::fmt(&self.print_output(cx), f)?;
Ok(())
}

pub(crate) fn print_output<'a, 'tcx: 'a>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn print_output<'a, 'tcx: 'a>(
fn print_output<'a, 'tcx: 'a>(

unless I'm being blind this isn't being used anywhere else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Wanna send a fix? :)

&'a self,
cx: &'a Context<'tcx>,
) -> impl fmt::Display + 'a + Captures<'tcx> {
display_fn(move |f| match &self.output {
clean::Tuple(tys) if tys.is_empty() => Ok(()),
ty if f.alternate() => {
write!(f, " -> {:#}", ty.print(cx))
}
ty => write!(f, " -&gt; {}", ty.print(cx)),
})
}
}

pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ fn assoc_method(
+ name.as_str().len()
+ generics_len;

let notable_traits = d.output.as_return().and_then(|output| notable_traits_button(output, cx));
let notable_traits = notable_traits_button(&d.output, cx);

let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
header_len += 4;
Expand Down Expand Up @@ -1282,6 +1282,11 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> Option<String> {
let mut has_notable_trait = false;

if ty.is_unit() {
// Very common fast path.
return None;
}

let did = ty.def_id(cx.cache())?;

// Box has pass-through impls for Read, Write, Iterator, and Future when the
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
+ name.as_str().len()
+ generics_len;

let notable_traits =
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));
let notable_traits = notable_traits_button(&f.decl.output, cx);

wrap_item(w, |w| {
w.reserve(header_len);
Expand Down
23 changes: 5 additions & 18 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_span::symbol::Symbol;
use serde::ser::{Serialize, SerializeStruct, Serializer};

use crate::clean;
use crate::clean::types::{FnRetTy, Function, Generics, ItemId, Type, WherePredicate};
use crate::clean::types::{Function, Generics, ItemId, Type, WherePredicate};
use crate::formats::cache::{Cache, OrphanImplItem};
use crate::formats::item_type::ItemType;
use crate::html::format::join_with_double_colon;
Expand Down Expand Up @@ -656,22 +656,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
}

let mut ret_types = Vec::new();
match decl.output {
FnRetTy::Return(ref return_type) => {
add_generics_and_bounds_as_types(
self_,
generics,
return_type,
tcx,
0,
&mut ret_types,
cache,
);
if ret_types.is_empty() {
ret_types.push(get_index_type(return_type, vec![]));
}
}
_ => {}
};
add_generics_and_bounds_as_types(self_, generics, &decl.output, tcx, 0, &mut ret_types, cache);
if ret_types.is_empty() {
ret_types.push(get_index_type(&decl.output, vec![]));
}
(all_types, ret_types)
}
5 changes: 1 addition & 4 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,7 @@ impl FromWithTcx<clean::FnDecl> for FnDecl {
.into_iter()
.map(|arg| (arg.name.to_string(), arg.type_.into_tcx(tcx)))
.collect(),
output: match output {
clean::FnRetTy::Return(t) => Some(t.into_tcx(tcx)),
clean::FnRetTy::DefaultReturn => None,
},
output: if output.is_unit() { None } else { Some(output.into_tcx(tcx)) },
c_variadic,
}
}
Expand Down