Skip to content

Commit 77bbfbc

Browse files
authored
Rollup merge of #112113 - notriddle:notriddle/rm-fnretty, r=GuillaumeGomez
rustdoc: simplify `clean` by removing `FnRetTy` The default fn ret ty is always unit. Just use that. Looking back at the time when `FnRetTy` (then called `FunctionRetTy`) was first added to rustdoc, it seems to originally be there because `-> !` was a special form: the never type didn't exist back then. eb01b17#diff-384affc1b4190940f114f3fcebbf969e7e18657a71ef9001da6b223a036687d9L921-L924 `DefaultReturn` was later added to rustdoc to mirror a change in HIR, which added a variant for DefaultReturn because it makes `Span` management easier. This isn't needed in rustdoc, since it doesn't carry spans. 3f0cc80#diff-384affc1b4190940f114f3fcebbf969e7e18657a71ef9001da6b223a036687d9R1144
2 parents 1d643e1 + 1862fcb commit 77bbfbc

File tree

7 files changed

+49
-81
lines changed

7 files changed

+49
-81
lines changed

Diff for: src/librustdoc/clean/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,8 @@ fn clean_fn_decl_with_args<'tcx>(
11111111
args: Arguments,
11121112
) -> FnDecl {
11131113
let output = match decl.output {
1114-
hir::FnRetTy::Return(typ) => Return(clean_ty(typ, cx)),
1115-
hir::FnRetTy::DefaultReturn(..) => DefaultReturn,
1114+
hir::FnRetTy::Return(typ) => clean_ty(typ, cx),
1115+
hir::FnRetTy::DefaultReturn(..) => Type::Tuple(Vec::new()),
11161116
};
11171117
FnDecl { inputs: args, output, c_variadic: decl.c_variadic }
11181118
}
@@ -1126,10 +1126,7 @@ fn clean_fn_decl_from_did_and_sig<'tcx>(
11261126

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

11341131
FnDecl {
11351132
output,

Diff for: src/librustdoc/clean/types.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use crate::formats::item_type::ItemType;
4242
use crate::html::render::Context;
4343
use crate::passes::collect_intra_doc_links::UrlFragment;
4444

45-
pub(crate) use self::FnRetTy::*;
4645
pub(crate) use self::ItemKind::*;
4746
pub(crate) use self::SelfTy::*;
4847
pub(crate) use self::Type::{
@@ -1353,7 +1352,7 @@ pub(crate) struct Function {
13531352
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
13541353
pub(crate) struct FnDecl {
13551354
pub(crate) inputs: Arguments,
1356-
pub(crate) output: FnRetTy,
1355+
pub(crate) output: Type,
13571356
pub(crate) c_variadic: bool,
13581357
}
13591358

@@ -1371,18 +1370,16 @@ impl FnDecl {
13711370
///
13721371
/// This function will panic if the return type does not match the expected sugaring for async
13731372
/// functions.
1374-
pub(crate) fn sugared_async_return_type(&self) -> FnRetTy {
1375-
match &self.output {
1376-
FnRetTy::Return(Type::ImplTrait(bounds)) => match &bounds[0] {
1377-
GenericBound::TraitBound(PolyTrait { trait_, .. }, ..) => {
1378-
let bindings = trait_.bindings().unwrap();
1379-
let ret_ty = bindings[0].term();
1380-
let ty = ret_ty.ty().expect("Unexpected constant return term");
1381-
FnRetTy::Return(ty.clone())
1382-
}
1383-
_ => panic!("unexpected desugaring of async function"),
1384-
},
1385-
_ => panic!("unexpected desugaring of async function"),
1373+
pub(crate) fn sugared_async_return_type(&self) -> Type {
1374+
if let Type::ImplTrait(v) = &self.output &&
1375+
let [GenericBound::TraitBound(PolyTrait { trait_, .. }, _ )] = &v[..]
1376+
{
1377+
let bindings = trait_.bindings().unwrap();
1378+
let ret_ty = bindings[0].term();
1379+
let ty = ret_ty.ty().expect("Unexpected constant return term");
1380+
ty.clone()
1381+
} else {
1382+
panic!("unexpected desugaring of async function")
13861383
}
13871384
}
13881385
}
@@ -1425,21 +1422,6 @@ impl Argument {
14251422
}
14261423
}
14271424

1428-
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1429-
pub(crate) enum FnRetTy {
1430-
Return(Type),
1431-
DefaultReturn,
1432-
}
1433-
1434-
impl FnRetTy {
1435-
pub(crate) fn as_return(&self) -> Option<&Type> {
1436-
match self {
1437-
Return(ret) => Some(ret),
1438-
DefaultReturn => None,
1439-
}
1440-
}
1441-
}
1442-
14431425
#[derive(Clone, Debug)]
14441426
pub(crate) struct Trait {
14451427
pub(crate) def_id: DefId,
@@ -1641,6 +1623,10 @@ impl Type {
16411623
matches!(self, Type::ImplTrait(_))
16421624
}
16431625

1626+
pub(crate) fn is_unit(&self) -> bool {
1627+
matches!(self, Type::Tuple(v) if v.is_empty())
1628+
}
1629+
16441630
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
16451631
if let QPath(box QPathData { self_type, trait_, assoc, .. }) = self {
16461632
Some((self_type, trait_.as_ref()?.def_id(), assoc.clone()))

Diff for: src/librustdoc/html/format.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,9 @@ impl clean::Impl {
12571257
};
12581258
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), "#trait-implementations-1", cx)?;
12591259
// Write output.
1260-
if let clean::FnRetTy::Return(ty) = &bare_fn.decl.output {
1260+
if !bare_fn.decl.output.is_unit() {
12611261
write!(f, " -> ")?;
1262-
fmt_type(ty, f, use_absolute, cx)?;
1262+
fmt_type(&bare_fn.decl.output, f, use_absolute, cx)?;
12631263
}
12641264
} else if let Some(ty) = self.kind.as_blanket_ty() {
12651265
fmt_type(ty, f, use_absolute, cx)?;
@@ -1296,22 +1296,6 @@ impl clean::Arguments {
12961296
}
12971297
}
12981298

1299-
impl clean::FnRetTy {
1300-
pub(crate) fn print<'a, 'tcx: 'a>(
1301-
&'a self,
1302-
cx: &'a Context<'tcx>,
1303-
) -> impl fmt::Display + 'a + Captures<'tcx> {
1304-
display_fn(move |f| match self {
1305-
clean::Return(clean::Tuple(tys)) if tys.is_empty() => Ok(()),
1306-
clean::Return(ty) if f.alternate() => {
1307-
write!(f, " -> {:#}", ty.print(cx))
1308-
}
1309-
clean::Return(ty) => write!(f, " -&gt; {}", ty.print(cx)),
1310-
clean::DefaultReturn => Ok(()),
1311-
})
1312-
}
1313-
}
1314-
13151299
impl clean::BareFunctionDecl {
13161300
fn print_hrtb_with_space<'a, 'tcx: 'a>(
13171301
&'a self,
@@ -1366,15 +1350,15 @@ impl clean::FnDecl {
13661350
"({args:#}{ellipsis}){arrow:#}",
13671351
args = self.inputs.print(cx),
13681352
ellipsis = ellipsis,
1369-
arrow = self.output.print(cx)
1353+
arrow = self.print_output(cx)
13701354
)
13711355
} else {
13721356
write!(
13731357
f,
13741358
"({args}{ellipsis}){arrow}",
13751359
args = self.inputs.print(cx),
13761360
ellipsis = ellipsis,
1377-
arrow = self.output.print(cx)
1361+
arrow = self.print_output(cx)
13781362
)
13791363
}
13801364
})
@@ -1464,9 +1448,22 @@ impl clean::FnDecl {
14641448
Some(n) => write!(f, "\n{})", Indent(n))?,
14651449
};
14661450

1467-
fmt::Display::fmt(&self.output.print(cx), f)?;
1451+
fmt::Display::fmt(&self.print_output(cx), f)?;
14681452
Ok(())
14691453
}
1454+
1455+
pub(crate) fn print_output<'a, 'tcx: 'a>(
1456+
&'a self,
1457+
cx: &'a Context<'tcx>,
1458+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1459+
display_fn(move |f| match &self.output {
1460+
clean::Tuple(tys) if tys.is_empty() => Ok(()),
1461+
ty if f.alternate() => {
1462+
write!(f, " -> {:#}", ty.print(cx))
1463+
}
1464+
ty => write!(f, " -&gt; {}", ty.print(cx)),
1465+
})
1466+
}
14701467
}
14711468

14721469
pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(

Diff for: src/librustdoc/html/render/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ fn assoc_method(
844844
+ name.as_str().len()
845845
+ generics_len;
846846

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

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

1285+
if ty.is_unit() {
1286+
// Very common fast path.
1287+
return None;
1288+
}
1289+
12851290
let did = ty.def_id(cx.cache())?;
12861291

12871292
// Box has pass-through impls for Read, Write, Iterator, and Future when the

Diff for: src/librustdoc/html/render/print_item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
587587
+ name.as_str().len()
588588
+ generics_len;
589589

590-
let notable_traits =
591-
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));
590+
let notable_traits = notable_traits_button(&f.decl.output, cx);
592591

593592
wrap_item(w, |w| {
594593
w.reserve(header_len);

Diff for: src/librustdoc/html/render/search_index.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::symbol::Symbol;
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
88

99
use crate::clean;
10-
use crate::clean::types::{FnRetTy, Function, Generics, ItemId, Type, WherePredicate};
10+
use crate::clean::types::{Function, Generics, ItemId, Type, WherePredicate};
1111
use crate::formats::cache::{Cache, OrphanImplItem};
1212
use crate::formats::item_type::ItemType;
1313
use crate::html::format::join_with_double_colon;
@@ -656,22 +656,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
656656
}
657657

658658
let mut ret_types = Vec::new();
659-
match decl.output {
660-
FnRetTy::Return(ref return_type) => {
661-
add_generics_and_bounds_as_types(
662-
self_,
663-
generics,
664-
return_type,
665-
tcx,
666-
0,
667-
&mut ret_types,
668-
cache,
669-
);
670-
if ret_types.is_empty() {
671-
ret_types.push(get_index_type(return_type, vec![]));
672-
}
673-
}
674-
_ => {}
675-
};
659+
add_generics_and_bounds_as_types(self_, generics, &decl.output, tcx, 0, &mut ret_types, cache);
660+
if ret_types.is_empty() {
661+
ret_types.push(get_index_type(&decl.output, vec![]));
662+
}
676663
(all_types, ret_types)
677664
}

Diff for: src/librustdoc/json/conversions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,7 @@ impl FromWithTcx<clean::FnDecl> for FnDecl {
624624
.into_iter()
625625
.map(|arg| (arg.name.to_string(), arg.type_.into_tcx(tcx)))
626626
.collect(),
627-
output: match output {
628-
clean::FnRetTy::Return(t) => Some(t.into_tcx(tcx)),
629-
clean::FnRetTy::DefaultReturn => None,
630-
},
627+
output: if output.is_unit() { None } else { Some(output.into_tcx(tcx)) },
631628
c_variadic,
632629
}
633630
}

0 commit comments

Comments
 (0)