Skip to content

Commit 675454a

Browse files
authored
Rollup merge of rust-lang#42286 - ollie27:rustdoc_assoc_const, r=GuillaumeGomez
rustdoc: Cleanup associated const value rendering Rather than (ab)using Debug for outputting the type in plain text use the alternate format parameter which already does exactly that. This fixes type parameters for example which would output raw HTML. Also cleans up adding parens around references to trait objects.
2 parents 90cbfa3 + 86ea93e commit 675454a

File tree

4 files changed

+74
-156
lines changed

4 files changed

+74
-156
lines changed

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ pub struct PolyTrait {
14871487
/// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original
14881488
/// type out of the AST/TyCtxt given one of these, if more information is needed. Most importantly
14891489
/// it does not preserve mutability or boxes.
1490-
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq)]
1490+
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Debug)]
14911491
pub enum Type {
14921492
/// structs/enums/traits (most that'd be an hir::TyPath)
14931493
ResolvedPath {

src/librustdoc/html/format.rs

+53-153
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
106106
}
107107
}
108108

109-
impl<'a, T: fmt::Debug> fmt::Debug for CommaSep<'a, T> {
110-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111-
for (i, item) in self.0.iter().enumerate() {
112-
if i != 0 { write!(f, ", ")?; }
113-
fmt::Debug::fmt(item, f)?;
114-
}
115-
Ok(())
116-
}
117-
}
118-
119109
impl<'a> fmt::Display for TyParamBounds<'a> {
120110
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121111
let &TyParamBounds(bounds) = self;
@@ -469,8 +459,7 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
469459
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
470460
/// rendering function with the necessary arguments for linking to a local path.
471461
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
472-
print_all: bool, use_absolute: bool, is_not_debug: bool,
473-
need_paren: bool) -> fmt::Result {
462+
print_all: bool, use_absolute: bool) -> fmt::Result {
474463
let empty = clean::PathSegment {
475464
name: String::new(),
476465
params: clean::PathParameters::Parenthesized {
@@ -499,13 +488,9 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
499488
} else {
500489
root.push_str(&seg.name);
501490
root.push_str("/");
502-
if is_not_debug {
503-
write!(w, "<a class=\"mod\" href=\"{}index.html\">{}</a>::",
504-
root,
505-
seg.name)?;
506-
} else {
507-
write!(w, "{}::", seg.name)?;
508-
}
491+
write!(w, "<a class=\"mod\" href=\"{}index.html\">{}</a>::",
492+
root,
493+
seg.name)?;
509494
}
510495
}
511496
}
@@ -517,39 +502,21 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
517502
}
518503
}
519504
if w.alternate() {
520-
if is_not_debug {
521-
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
522-
} else {
523-
write!(w, "{:?}{}", HRef::new(did, &last.name), last.params)?;
524-
}
505+
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
525506
} else {
526-
if is_not_debug {
527-
let path = if use_absolute {
528-
match href(did) {
529-
Some((_, _, fqp)) => format!("{}::{}",
530-
fqp[..fqp.len()-1].join("::"),
531-
HRef::new(did, fqp.last()
532-
.unwrap_or(&String::new()))),
533-
None => format!("{}", HRef::new(did, &last.name)),
507+
let path = if use_absolute {
508+
match href(did) {
509+
Some((_, _, fqp)) => {
510+
format!("{}::{}",
511+
fqp[..fqp.len() - 1].join("::"),
512+
HRef::new(did, fqp.last().unwrap_or(&String::new())))
534513
}
535-
} else {
536-
format!("{}", HRef::new(did, &last.name))
537-
};
538-
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
514+
None => format!("{}", HRef::new(did, &last.name)),
515+
}
539516
} else {
540-
let path = if use_absolute {
541-
match href(did) {
542-
Some((_, _, fqp)) => format!("{:?}::{:?}",
543-
fqp[..fqp.len()-1].join("::"),
544-
HRef::new(did, fqp.last()
545-
.unwrap_or(&String::new()))),
546-
None => format!("{:?}", HRef::new(did, &last.name)),
547-
}
548-
} else {
549-
format!("{:?}", HRef::new(did, &last.name))
550-
};
551-
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
552-
}
517+
format!("{}", HRef::new(did, &last.name))
518+
};
519+
write!(w, "{}{}", path, last.params)?;
553520
}
554521
Ok(())
555522
}
@@ -600,17 +567,13 @@ fn primitive_link(f: &mut fmt::Formatter,
600567

601568
/// Helper to render type parameters
602569
fn tybounds(w: &mut fmt::Formatter,
603-
typarams: &Option<Vec<clean::TyParamBound>>,
604-
need_paren: bool) -> fmt::Result {
570+
typarams: &Option<Vec<clean::TyParamBound>>) -> fmt::Result {
605571
match *typarams {
606572
Some(ref params) => {
607573
for param in params {
608574
write!(w, " + ")?;
609575
fmt::Display::fmt(param, w)?;
610576
}
611-
if need_paren {
612-
write!(w, ")")?;
613-
}
614577
Ok(())
615578
}
616579
None => Ok(())
@@ -637,30 +600,18 @@ impl<'a> fmt::Display for HRef<'a> {
637600
}
638601
}
639602

640-
impl<'a> fmt::Debug for HRef<'a> {
641-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
642-
write!(f, "{}", self.text)
643-
}
644-
}
645-
646-
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
647-
is_not_debug: bool, is_ref: bool) -> fmt::Result {
603+
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
648604
match *t {
649605
clean::Generic(ref name) => {
650606
f.write_str(name)
651607
}
652608
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
653609
// Paths like T::Output and Self::Output should be rendered with all segments
654-
let need_paren = match *typarams {
655-
Some(ref v) => !v.is_empty(),
656-
_ => false,
657-
} && is_ref;
658-
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug, need_paren)?;
659-
tybounds(f, typarams, need_paren)
610+
resolved_path(f, did, path, is_generic, use_absolute)?;
611+
tybounds(f, typarams)
660612
}
661613
clean::Infer => write!(f, "_"),
662-
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
663-
clean::Primitive(prim) => write!(f, "{}", prim.as_str()),
614+
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
664615
clean::BareFunction(ref decl) => {
665616
if f.alternate() {
666617
write!(f, "{}{}fn{:#}{:#}",
@@ -678,30 +629,26 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
678629
}
679630
clean::Tuple(ref typs) => {
680631
match &typs[..] {
681-
&[] if is_not_debug => primitive_link(f, PrimitiveType::Tuple, "()"),
682-
&[] => write!(f, "()"),
683-
&[ref one] if is_not_debug => {
632+
&[] => primitive_link(f, PrimitiveType::Tuple, "()"),
633+
&[ref one] => {
684634
primitive_link(f, PrimitiveType::Tuple, "(")?;
685635
//carry f.alternate() into this display w/o branching manually
686636
fmt::Display::fmt(one, f)?;
687637
primitive_link(f, PrimitiveType::Tuple, ",)")
688638
}
689-
&[ref one] => write!(f, "({:?},)", one),
690-
many if is_not_debug => {
639+
many => {
691640
primitive_link(f, PrimitiveType::Tuple, "(")?;
692641
fmt::Display::fmt(&CommaSep(&many), f)?;
693642
primitive_link(f, PrimitiveType::Tuple, ")")
694643
}
695-
many => write!(f, "({:?})", &CommaSep(&many)),
696644
}
697645
}
698-
clean::Vector(ref t) if is_not_debug => {
646+
clean::Vector(ref t) => {
699647
primitive_link(f, PrimitiveType::Slice, "[")?;
700648
fmt::Display::fmt(t, f)?;
701649
primitive_link(f, PrimitiveType::Slice, "]")
702650
}
703-
clean::Vector(ref t) => write!(f, "[{:?}]", t),
704-
clean::FixedVector(ref t, ref s) if is_not_debug => {
651+
clean::FixedVector(ref t, ref s) => {
705652
primitive_link(f, PrimitiveType::Array, "[")?;
706653
fmt::Display::fmt(t, f)?;
707654
if f.alternate() {
@@ -712,17 +659,10 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
712659
&format!("; {}]", Escape(s)))
713660
}
714661
}
715-
clean::FixedVector(ref t, ref s) => {
716-
if f.alternate() {
717-
write!(f, "[{:?}; {}]", t, s)
718-
} else {
719-
write!(f, "[{:?}; {}]", t, Escape(s))
720-
}
721-
}
722662
clean::Never => f.write_str("!"),
723663
clean::RawPointer(m, ref t) => {
724664
match **t {
725-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} if is_not_debug => {
665+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
726666
if f.alternate() {
727667
primitive_link(f, clean::PrimitiveType::RawPointer,
728668
&format!("*{}{:#}", RawMutableSpace(m), t))
@@ -731,21 +671,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
731671
&format!("*{}{}", RawMutableSpace(m), t))
732672
}
733673
}
734-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
735-
if f.alternate() {
736-
write!(f, "*{}{:#?}", RawMutableSpace(m), t)
737-
} else {
738-
write!(f, "*{}{:?}", RawMutableSpace(m), t)
739-
}
740-
}
741-
_ if is_not_debug => {
674+
_ => {
742675
primitive_link(f, clean::PrimitiveType::RawPointer,
743676
&format!("*{}", RawMutableSpace(m)))?;
744677
fmt::Display::fmt(t, f)
745678
}
746-
_ => {
747-
write!(f, "*{}{:?}", RawMutableSpace(m), t)
748-
}
749679
}
750680
}
751681
clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
@@ -757,7 +687,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
757687
match **ty {
758688
clean::Vector(ref bt) => { // BorrowedRef{ ... Vector(T) } is &[T]
759689
match **bt {
760-
clean::Generic(_) if is_not_debug => {
690+
clean::Generic(_) => {
761691
if f.alternate() {
762692
primitive_link(f, PrimitiveType::Slice,
763693
&format!("&{}{}[{:#}]", lt, m, **bt))
@@ -766,14 +696,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
766696
&format!("&amp;{}{}[{}]", lt, m, **bt))
767697
}
768698
}
769-
clean::Generic(_) => {
770-
if f.alternate() {
771-
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
772-
} else {
773-
write!(f, "&{}{}[{:?}]", lt, m, **bt)
774-
}
775-
}
776-
_ if is_not_debug => {
699+
_ => {
777700
if f.alternate() {
778701
primitive_link(f, PrimitiveType::Slice,
779702
&format!("&{}{}[", lt, m))?;
@@ -785,26 +708,25 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
785708
}
786709
primitive_link(f, PrimitiveType::Slice, "]")
787710
}
788-
_ => {
789-
if f.alternate() {
790-
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
791-
} else {
792-
write!(f, "&{}{}[{:?}]", lt, m, **bt)
793-
}
794-
}
795711
}
796712
}
713+
clean::ResolvedPath { typarams: Some(ref v), .. } if !v.is_empty() => {
714+
if f.alternate() {
715+
write!(f, "&{}{}", lt, m)?;
716+
} else {
717+
write!(f, "&amp;{}{}", lt, m)?;
718+
}
719+
write!(f, "(")?;
720+
fmt_type(&ty, f, use_absolute)?;
721+
write!(f, ")")
722+
}
797723
_ => {
798724
if f.alternate() {
799725
write!(f, "&{}{}", lt, m)?;
800-
fmt_type(&ty, f, use_absolute, is_not_debug, true)
726+
fmt_type(&ty, f, use_absolute)
801727
} else {
802-
if is_not_debug {
803-
write!(f, "&amp;{}{}", lt, m)?;
804-
} else {
805-
write!(f, "&{}{}", lt, m)?;
806-
}
807-
fmt_type(&ty, f, use_absolute, is_not_debug, true)
728+
write!(f, "&amp;{}{}", lt, m)?;
729+
fmt_type(&ty, f, use_absolute)
808730
}
809731
}
810732
}
@@ -833,32 +755,16 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
833755
_ => true,
834756
};
835757
if f.alternate() {
836-
if is_not_debug {
837-
if should_show_cast {
838-
write!(f, "<{:#} as {:#}>::", self_type, trait_)?
839-
} else {
840-
write!(f, "{:#}::", self_type)?
841-
}
758+
if should_show_cast {
759+
write!(f, "<{:#} as {:#}>::", self_type, trait_)?
842760
} else {
843-
if should_show_cast {
844-
write!(f, "<{:#?} as {:#?}>::", self_type, trait_)?
845-
} else {
846-
write!(f, "{:#?}::", self_type)?
847-
}
761+
write!(f, "{:#}::", self_type)?
848762
}
849763
} else {
850-
if is_not_debug {
851-
if should_show_cast {
852-
write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
853-
} else {
854-
write!(f, "{}::", self_type)?
855-
}
764+
if should_show_cast {
765+
write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
856766
} else {
857-
if should_show_cast {
858-
write!(f, "<{:?} as {:?}>::", self_type, trait_)?
859-
} else {
860-
write!(f, "{:?}::", self_type)?
861-
}
767+
write!(f, "{}::", self_type)?
862768
}
863769
};
864770
match *trait_ {
@@ -874,7 +780,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
874780
// look at).
875781
box clean::ResolvedPath { did, ref typarams, .. } => {
876782
let path = clean::Path::singleton(name.clone());
877-
resolved_path(f, did, &path, true, use_absolute, is_not_debug, false)?;
783+
resolved_path(f, did, &path, true, use_absolute)?;
878784

879785
// FIXME: `typarams` are not rendered, and this seems bad?
880786
drop(typarams);
@@ -893,13 +799,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
893799

894800
impl fmt::Display for clean::Type {
895801
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
896-
fmt_type(self, f, false, true, false)
897-
}
898-
}
899-
900-
impl fmt::Debug for clean::Type {
901-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
902-
fmt_type(self, f, false, false, false)
802+
fmt_type(self, f, false)
903803
}
904804
}
905805

@@ -933,7 +833,7 @@ fn fmt_impl(i: &clean::Impl,
933833
write!(f, " for ")?;
934834
}
935835

936-
fmt_type(&i.for_, f, use_absolute, true, false)?;
836+
fmt_type(&i.for_, f, use_absolute)?;
937837

938838
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
939839
Ok(())
@@ -1139,7 +1039,7 @@ impl fmt::Display for clean::Import {
11391039
impl fmt::Display for clean::ImportSource {
11401040
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11411041
match self.did {
1142-
Some(did) => resolved_path(f, did, &self.path, true, false, true, false),
1042+
Some(did) => resolved_path(f, did, &self.path, true, false),
11431043
_ => {
11441044
for (i, seg) in self.path.segments.iter().enumerate() {
11451045
if i > 0 {

src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1662,9 +1662,9 @@ fn md_render_assoc_item(item: &clean::Item) -> String {
16621662
match item.inner {
16631663
clean::AssociatedConstItem(ref ty, ref default) => {
16641664
if let Some(default) = default.as_ref() {
1665-
format!("```\n{}: {:?} = {}\n```\n\n", item.name.as_ref().unwrap(), ty, default)
1665+
format!("```\n{}: {:#} = {}\n```\n\n", item.name.as_ref().unwrap(), ty, default)
16661666
} else {
1667-
format!("```\n{}: {:?}\n```\n\n", item.name.as_ref().unwrap(), ty)
1667+
format!("```\n{}: {:#}\n```\n\n", item.name.as_ref().unwrap(), ty)
16681668
}
16691669
}
16701670
_ => String::new(),

0 commit comments

Comments
 (0)