Skip to content

Commit ef9b2e8

Browse files
committed
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.
1 parent 41e74a2 commit ef9b2e8

File tree

4 files changed

+53
-145
lines changed

4 files changed

+53
-145
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

+41-142
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,7 +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) -> fmt::Result {
462+
print_all: bool, use_absolute: bool) -> fmt::Result {
473463
let empty = clean::PathSegment {
474464
name: String::new(),
475465
params: clean::PathParameters::Parenthesized {
@@ -498,13 +488,9 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
498488
} else {
499489
root.push_str(&seg.name);
500490
root.push_str("/");
501-
if is_not_debug {
502-
write!(w, "<a class=\"mod\" href=\"{}index.html\">{}</a>::",
503-
root,
504-
seg.name)?;
505-
} else {
506-
write!(w, "{}::", seg.name)?;
507-
}
491+
write!(w, "<a class=\"mod\" href=\"{}index.html\">{}</a>::",
492+
root,
493+
seg.name)?;
508494
}
509495
}
510496
}
@@ -516,39 +502,21 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
516502
}
517503
}
518504
if w.alternate() {
519-
if is_not_debug {
520-
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
521-
} else {
522-
write!(w, "{:?}{}", HRef::new(did, &last.name), last.params)?;
523-
}
505+
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
524506
} else {
525-
if is_not_debug {
526-
let path = if use_absolute {
527-
match href(did) {
528-
Some((_, _, fqp)) => format!("{}::{}",
529-
fqp[..fqp.len()-1].join("::"),
530-
HRef::new(did, fqp.last()
531-
.unwrap_or(&String::new()))),
532-
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())))
533513
}
534-
} else {
535-
format!("{}", HRef::new(did, &last.name))
536-
};
537-
write!(w, "{}{}", path, last.params)?;
514+
None => format!("{}", HRef::new(did, &last.name)),
515+
}
538516
} else {
539-
let path = if use_absolute {
540-
match href(did) {
541-
Some((_, _, fqp)) => format!("{:?}::{:?}",
542-
fqp[..fqp.len()-1].join("::"),
543-
HRef::new(did, fqp.last()
544-
.unwrap_or(&String::new()))),
545-
None => format!("{:?}", HRef::new(did, &last.name)),
546-
}
547-
} else {
548-
format!("{:?}", HRef::new(did, &last.name))
549-
};
550-
write!(w, "{}{}", path, last.params)?;
551-
}
517+
format!("{}", HRef::new(did, &last.name))
518+
};
519+
write!(w, "{}{}", path, last.params)?;
552520
}
553521
Ok(())
554522
}
@@ -632,26 +600,18 @@ impl<'a> fmt::Display for HRef<'a> {
632600
}
633601
}
634602

635-
impl<'a> fmt::Debug for HRef<'a> {
636-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
637-
write!(f, "{}", self.text)
638-
}
639-
}
640-
641-
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
642-
is_not_debug: bool) -> fmt::Result {
603+
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
643604
match *t {
644605
clean::Generic(ref name) => {
645606
f.write_str(name)
646607
}
647608
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
648609
// Paths like T::Output and Self::Output should be rendered with all segments
649-
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug)?;
610+
resolved_path(f, did, path, is_generic, use_absolute)?;
650611
tybounds(f, typarams)
651612
}
652613
clean::Infer => write!(f, "_"),
653-
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
654-
clean::Primitive(prim) => write!(f, "{}", prim.as_str()),
614+
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
655615
clean::BareFunction(ref decl) => {
656616
if f.alternate() {
657617
write!(f, "{}{}fn{:#}{:#}",
@@ -669,30 +629,26 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
669629
}
670630
clean::Tuple(ref typs) => {
671631
match &typs[..] {
672-
&[] if is_not_debug => primitive_link(f, PrimitiveType::Tuple, "()"),
673-
&[] => write!(f, "()"),
674-
&[ref one] if is_not_debug => {
632+
&[] => primitive_link(f, PrimitiveType::Tuple, "()"),
633+
&[ref one] => {
675634
primitive_link(f, PrimitiveType::Tuple, "(")?;
676635
//carry f.alternate() into this display w/o branching manually
677636
fmt::Display::fmt(one, f)?;
678637
primitive_link(f, PrimitiveType::Tuple, ",)")
679638
}
680-
&[ref one] => write!(f, "({:?},)", one),
681-
many if is_not_debug => {
639+
many => {
682640
primitive_link(f, PrimitiveType::Tuple, "(")?;
683641
fmt::Display::fmt(&CommaSep(&many), f)?;
684642
primitive_link(f, PrimitiveType::Tuple, ")")
685643
}
686-
many => write!(f, "({:?})", &CommaSep(&many)),
687644
}
688645
}
689-
clean::Vector(ref t) if is_not_debug => {
646+
clean::Vector(ref t) => {
690647
primitive_link(f, PrimitiveType::Slice, "[")?;
691648
fmt::Display::fmt(t, f)?;
692649
primitive_link(f, PrimitiveType::Slice, "]")
693650
}
694-
clean::Vector(ref t) => write!(f, "[{:?}]", t),
695-
clean::FixedVector(ref t, ref s) if is_not_debug => {
651+
clean::FixedVector(ref t, ref s) => {
696652
primitive_link(f, PrimitiveType::Array, "[")?;
697653
fmt::Display::fmt(t, f)?;
698654
if f.alternate() {
@@ -703,17 +659,10 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
703659
&format!("; {}]", Escape(s)))
704660
}
705661
}
706-
clean::FixedVector(ref t, ref s) => {
707-
if f.alternate() {
708-
write!(f, "[{:?}; {}]", t, s)
709-
} else {
710-
write!(f, "[{:?}; {}]", t, Escape(s))
711-
}
712-
}
713662
clean::Never => f.write_str("!"),
714663
clean::RawPointer(m, ref t) => {
715664
match **t {
716-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} if is_not_debug => {
665+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
717666
if f.alternate() {
718667
primitive_link(f, clean::PrimitiveType::RawPointer,
719668
&format!("*{}{:#}", RawMutableSpace(m), t))
@@ -722,21 +671,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
722671
&format!("*{}{}", RawMutableSpace(m), t))
723672
}
724673
}
725-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
726-
if f.alternate() {
727-
write!(f, "*{}{:#?}", RawMutableSpace(m), t)
728-
} else {
729-
write!(f, "*{}{:?}", RawMutableSpace(m), t)
730-
}
731-
}
732-
_ if is_not_debug => {
674+
_ => {
733675
primitive_link(f, clean::PrimitiveType::RawPointer,
734676
&format!("*{}", RawMutableSpace(m)))?;
735677
fmt::Display::fmt(t, f)
736678
}
737-
_ => {
738-
write!(f, "*{}{:?}", RawMutableSpace(m), t)
739-
}
740679
}
741680
}
742681
clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
@@ -748,7 +687,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
748687
match **ty {
749688
clean::Vector(ref bt) => { // BorrowedRef{ ... Vector(T) } is &[T]
750689
match **bt {
751-
clean::Generic(_) if is_not_debug => {
690+
clean::Generic(_) => {
752691
if f.alternate() {
753692
primitive_link(f, PrimitiveType::Slice,
754693
&format!("&{}{}[{:#}]", lt, m, **bt))
@@ -757,14 +696,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
757696
&format!("&amp;{}{}[{}]", lt, m, **bt))
758697
}
759698
}
760-
clean::Generic(_) => {
761-
if f.alternate() {
762-
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
763-
} else {
764-
write!(f, "&{}{}[{:?}]", lt, m, **bt)
765-
}
766-
}
767-
_ if is_not_debug => {
699+
_ => {
768700
if f.alternate() {
769701
primitive_link(f, PrimitiveType::Slice,
770702
&format!("&{}{}[", lt, m))?;
@@ -776,26 +708,15 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
776708
}
777709
primitive_link(f, PrimitiveType::Slice, "]")
778710
}
779-
_ => {
780-
if f.alternate() {
781-
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
782-
} else {
783-
write!(f, "&{}{}[{:?}]", lt, m, **bt)
784-
}
785-
}
786711
}
787712
}
788713
_ => {
789714
if f.alternate() {
790715
write!(f, "&{}{}", lt, m)?;
791-
fmt_type(&ty, f, use_absolute, is_not_debug)
716+
fmt_type(&ty, f, use_absolute)
792717
} else {
793-
if is_not_debug {
794-
write!(f, "&amp;{}{}", lt, m)?;
795-
} else {
796-
write!(f, "&{}{}", lt, m)?;
797-
}
798-
fmt_type(&ty, f, use_absolute, is_not_debug)
718+
write!(f, "&amp;{}{}", lt, m)?;
719+
fmt_type(&ty, f, use_absolute)
799720
}
800721
}
801722
}
@@ -824,32 +745,16 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
824745
_ => true,
825746
};
826747
if f.alternate() {
827-
if is_not_debug {
828-
if should_show_cast {
829-
write!(f, "<{:#} as {:#}>::", self_type, trait_)?
830-
} else {
831-
write!(f, "{:#}::", self_type)?
832-
}
748+
if should_show_cast {
749+
write!(f, "<{:#} as {:#}>::", self_type, trait_)?
833750
} else {
834-
if should_show_cast {
835-
write!(f, "<{:#?} as {:#?}>::", self_type, trait_)?
836-
} else {
837-
write!(f, "{:#?}::", self_type)?
838-
}
751+
write!(f, "{:#}::", self_type)?
839752
}
840753
} else {
841-
if is_not_debug {
842-
if should_show_cast {
843-
write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
844-
} else {
845-
write!(f, "{}::", self_type)?
846-
}
754+
if should_show_cast {
755+
write!(f, "&lt;{} as {}&gt;::", self_type, trait_)?
847756
} else {
848-
if should_show_cast {
849-
write!(f, "<{:?} as {:?}>::", self_type, trait_)?
850-
} else {
851-
write!(f, "{:?}::", self_type)?
852-
}
757+
write!(f, "{}::", self_type)?
853758
}
854759
};
855760
match *trait_ {
@@ -865,7 +770,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
865770
// look at).
866771
box clean::ResolvedPath { did, ref typarams, .. } => {
867772
let path = clean::Path::singleton(name.clone());
868-
resolved_path(f, did, &path, true, use_absolute, is_not_debug)?;
773+
resolved_path(f, did, &path, true, use_absolute)?;
869774

870775
// FIXME: `typarams` are not rendered, and this seems bad?
871776
drop(typarams);
@@ -884,13 +789,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
884789

885790
impl fmt::Display for clean::Type {
886791
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
887-
fmt_type(self, f, false, true)
888-
}
889-
}
890-
891-
impl fmt::Debug for clean::Type {
892-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
893-
fmt_type(self, f, false, false)
792+
fmt_type(self, f, false)
894793
}
895794
}
896795

@@ -924,7 +823,7 @@ fn fmt_impl(i: &clean::Impl,
924823
write!(f, " for ")?;
925824
}
926825

927-
fmt_type(&i.for_, f, use_absolute, true)?;
826+
fmt_type(&i.for_, f, use_absolute)?;
928827

929828
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
930829
Ok(())
@@ -1130,7 +1029,7 @@ impl fmt::Display for clean::Import {
11301029
impl fmt::Display for clean::ImportSource {
11311030
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11321031
match self.did {
1133-
Some(did) => resolved_path(f, did, &self.path, true, false, true),
1032+
Some(did) => resolved_path(f, did, &self.path, true, false),
11341033
_ => {
11351034
for (i, seg) in self.path.segments.iter().enumerate() {
11361035
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(),

src/test/rustdoc/assoc-consts.rs

+9
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ impl Bar {
2626
// @has - '//*[@class="docblock"]' 'BAR: usize = 3'
2727
pub const BAR: usize = 3;
2828
}
29+
30+
pub struct Baz<'a, U: 'a, T>(T, &'a [U]);
31+
32+
impl Bar {
33+
// @has assoc_consts/struct.Bar.html '//*[@id="associatedconstant.BAZ"]' \
34+
// "const BAZ: Baz<'static, u8, u32>"
35+
// @has - '//*[@class="docblock"]' "BAZ: Baz<'static, u8, u32> = Baz(321, &[1, 2, 3])"
36+
const BAZ: Baz<'static, u8, u32> = Baz(321, &[1, 2, 3]);
37+
}

0 commit comments

Comments
 (0)