Skip to content

Commit fda3f98

Browse files
authored
Rollup merge of #39944 - GuillaumeGomez:associated-consts, r=frewsxcv
Improve associated constant rendering in rustdoc Before: <img width="1440" alt="screen shot 2017-02-19 at 00 30 51" src="https://cloud.githubusercontent.com/assets/3050060/23097697/caeed80e-f63a-11e6-98c2-5d27e4efd76d.png"> After: <img width="1440" alt="screen shot 2017-02-19 at 00 30 39" src="https://cloud.githubusercontent.com/assets/3050060/23097698/cfb4874e-f63a-11e6-80cf-ffbf5c5c6162.png"> cc @SergioBenitez r? @rust-lang/docs
2 parents 43df65f + bd704ba commit fda3f98

File tree

9 files changed

+176
-63
lines changed

9 files changed

+176
-63
lines changed

src/librustdoc/clean/mod.rs

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

src/librustdoc/html/format.rs

+91-25
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ impl<'a, T: fmt::Display> fmt::Display for CommaSep<'a, T> {
9090
}
9191
}
9292

93+
impl<'a, T: fmt::Debug> fmt::Debug for CommaSep<'a, T> {
94+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95+
for (i, item) in self.0.iter().enumerate() {
96+
if i != 0 { write!(f, ", ")?; }
97+
fmt::Debug::fmt(item, f)?;
98+
}
99+
Ok(())
100+
}
101+
}
102+
93103
impl<'a> fmt::Display for TyParamBounds<'a> {
94104
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95105
let &TyParamBounds(bounds) = self;
@@ -165,7 +175,7 @@ impl<'a> fmt::Display for WhereClause<'a> {
165175
if f.alternate() {
166176
clause.push_str(" where ");
167177
} else {
168-
clause.push_str(" <span class='where fmt-newline'>where ");
178+
clause.push_str(" <span class=\"where fmt-newline\">where ");
169179
}
170180
for (i, pred) in gens.where_predicates.iter().enumerate() {
171181
if i > 0 {
@@ -449,8 +459,8 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
449459
} else {
450460
root.push_str(&seg.name);
451461
root.push_str("/");
452-
write!(w, "<a class='mod'
453-
href='{}index.html'>{}</a>::",
462+
write!(w, "<a class=\"mod\"
463+
href=\"{}index.html\">{}</a>::",
454464
root,
455465
seg.name)?;
456466
}
@@ -491,7 +501,7 @@ fn primitive_link(f: &mut fmt::Formatter,
491501
Some(&def_id) if def_id.is_local() => {
492502
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
493503
let len = if len == 0 {0} else {len - 1};
494-
write!(f, "<a class='primitive' href='{}primitive.{}.html'>",
504+
write!(f, "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
495505
repeat("../").take(len).collect::<String>(),
496506
prim.to_url_str())?;
497507
needs_termination = true;
@@ -508,7 +518,7 @@ fn primitive_link(f: &mut fmt::Formatter,
508518
(.., render::Unknown) => None,
509519
};
510520
if let Some((cname, root)) = loc {
511-
write!(f, "<a class='primitive' href='{}{}/primitive.{}.html'>",
521+
write!(f, "<a class=\"primitive\" href=\"{}{}/primitive.{}.html\">",
512522
root,
513523
cname,
514524
prim.to_url_str())?;
@@ -550,7 +560,7 @@ impl<'a> fmt::Display for HRef<'a> {
550560
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
551561
match href(self.did) {
552562
Some((url, shortty, fqp)) => if !f.alternate() {
553-
write!(f, "<a class='{}' href='{}' title='{} {}'>{}</a>",
563+
write!(f, "<a class=\"{}\" href=\"{}\" title=\"{} {}\">{}</a>",
554564
shortty, url, shortty, fqp.join("::"), self.text)
555565
} else {
556566
write!(f, "{}", self.text)
@@ -560,7 +570,8 @@ impl<'a> fmt::Display for HRef<'a> {
560570
}
561571
}
562572

563-
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
573+
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
574+
is_not_debug: bool) -> fmt::Result {
564575
match *t {
565576
clean::Generic(ref name) => {
566577
f.write_str(name)
@@ -571,7 +582,8 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
571582
tybounds(f, typarams)
572583
}
573584
clean::Infer => write!(f, "_"),
574-
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
585+
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
586+
clean::Primitive(prim) => write!(f, "{}", prim.as_str()),
575587
clean::BareFunction(ref decl) => {
576588
if f.alternate() {
577589
write!(f, "{}{}fn{:#}{:#}",
@@ -589,26 +601,30 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
589601
}
590602
clean::Tuple(ref typs) => {
591603
match &typs[..] {
592-
&[] => primitive_link(f, PrimitiveType::Tuple, "()"),
593-
&[ref one] => {
604+
&[] if is_not_debug => primitive_link(f, PrimitiveType::Tuple, "()"),
605+
&[] => write!(f, "()"),
606+
&[ref one] if is_not_debug => {
594607
primitive_link(f, PrimitiveType::Tuple, "(")?;
595608
//carry f.alternate() into this display w/o branching manually
596609
fmt::Display::fmt(one, f)?;
597610
primitive_link(f, PrimitiveType::Tuple, ",)")
598611
}
599-
many => {
612+
&[ref one] => write!(f, "({:?},)", one),
613+
many if is_not_debug => {
600614
primitive_link(f, PrimitiveType::Tuple, "(")?;
601615
fmt::Display::fmt(&CommaSep(&many), f)?;
602616
primitive_link(f, PrimitiveType::Tuple, ")")
603617
}
618+
many => write!(f, "({:?})", &CommaSep(&many)),
604619
}
605620
}
606-
clean::Vector(ref t) => {
621+
clean::Vector(ref t) if is_not_debug => {
607622
primitive_link(f, PrimitiveType::Slice, &format!("["))?;
608623
fmt::Display::fmt(t, f)?;
609624
primitive_link(f, PrimitiveType::Slice, &format!("]"))
610625
}
611-
clean::FixedVector(ref t, ref s) => {
626+
clean::Vector(ref t) => write!(f, "[{:?}]", t),
627+
clean::FixedVector(ref t, ref s) if is_not_debug => {
612628
primitive_link(f, PrimitiveType::Array, "[")?;
613629
fmt::Display::fmt(t, f)?;
614630
if f.alternate() {
@@ -619,10 +635,17 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
619635
&format!("; {}]", Escape(s)))
620636
}
621637
}
638+
clean::FixedVector(ref t, ref s) => {
639+
if f.alternate() {
640+
write!(f, "[{:?}; {}]", t, s)
641+
} else {
642+
write!(f, "[{:?}; {}]", t, Escape(s))
643+
}
644+
}
622645
clean::Never => f.write_str("!"),
623646
clean::RawPointer(m, ref t) => {
624647
match **t {
625-
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
648+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} if is_not_debug => {
626649
if f.alternate() {
627650
primitive_link(f, clean::PrimitiveType::RawPointer,
628651
&format!("*{}{:#}", RawMutableSpace(m), t))
@@ -631,11 +654,21 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
631654
&format!("*{}{}", RawMutableSpace(m), t))
632655
}
633656
}
634-
_ => {
657+
clean::Generic(_) | clean::ResolvedPath {is_generic: true, ..} => {
658+
if f.alternate() {
659+
write!(f, "*{}{:#?}", RawMutableSpace(m), t)
660+
} else {
661+
write!(f, "*{}{:?}", RawMutableSpace(m), t)
662+
}
663+
}
664+
_ if is_not_debug => {
635665
primitive_link(f, clean::PrimitiveType::RawPointer,
636666
&format!("*{}", RawMutableSpace(m)))?;
637667
fmt::Display::fmt(t, f)
638668
}
669+
_ => {
670+
write!(f, "*{}{:?}", RawMutableSpace(m), t)
671+
}
639672
}
640673
}
641674
clean::BorrowedRef{ lifetime: ref l, mutability, type_: ref ty} => {
@@ -647,15 +680,23 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
647680
match **ty {
648681
clean::Vector(ref bt) => { // BorrowedRef{ ... Vector(T) } is &[T]
649682
match **bt {
650-
clean::Generic(_) =>
683+
clean::Generic(_) if is_not_debug => {
651684
if f.alternate() {
652685
primitive_link(f, PrimitiveType::Slice,
653686
&format!("&{}{}[{:#}]", lt, m, **bt))
654687
} else {
655688
primitive_link(f, PrimitiveType::Slice,
656689
&format!("&amp;{}{}[{}]", lt, m, **bt))
657-
},
658-
_ => {
690+
}
691+
}
692+
clean::Generic(_) => {
693+
if f.alternate() {
694+
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
695+
} else {
696+
write!(f, "&{}{}[{:?}]", lt, m, **bt)
697+
}
698+
}
699+
_ if is_not_debug => {
659700
if f.alternate() {
660701
primitive_link(f, PrimitiveType::Slice,
661702
&format!("&{}{}[", lt, m))?;
@@ -667,15 +708,26 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
667708
}
668709
primitive_link(f, PrimitiveType::Slice, "]")
669710
}
711+
_ => {
712+
if f.alternate() {
713+
write!(f, "&{}{}[{:#?}]", lt, m, **bt)
714+
} else {
715+
write!(f, "&{}{}[{:?}]", lt, m, **bt)
716+
}
717+
}
670718
}
671719
}
672720
_ => {
673721
if f.alternate() {
674722
write!(f, "&{}{}", lt, m)?;
675-
fmt_type(&ty, f, use_absolute)
723+
fmt_type(&ty, f, use_absolute, is_not_debug)
676724
} else {
677-
write!(f, "&amp;{}{}", lt, m)?;
678-
fmt_type(&ty, f, use_absolute)
725+
if is_not_debug {
726+
write!(f, "&amp;{}{}", lt, m)?;
727+
} else {
728+
write!(f, "&{}{}", lt, m)?;
729+
}
730+
fmt_type(&ty, f, use_absolute, is_not_debug)
679731
}
680732
}
681733
}
@@ -723,9 +775,17 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
723775
}
724776
clean::QPath { ref name, ref self_type, ref trait_ } => {
725777
if f.alternate() {
726-
write!(f, "<{:#} as {:#}>::{}", self_type, trait_, name)
778+
if is_not_debug {
779+
write!(f, "<{:#} as {:#}>::{}", self_type, trait_, name)
780+
} else {
781+
write!(f, "<{:#?} as {:#?}>::{}", self_type, trait_, name)
782+
}
727783
} else {
728-
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
784+
if is_not_debug {
785+
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
786+
} else {
787+
write!(f, "<{:?} as {:?}>::{}", self_type, trait_, name)
788+
}
729789
}
730790
}
731791
clean::Unique(..) => {
@@ -736,7 +796,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
736796

737797
impl fmt::Display for clean::Type {
738798
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
739-
fmt_type(self, f, false)
799+
fmt_type(self, f, false, true)
800+
}
801+
}
802+
803+
impl fmt::Debug for clean::Type {
804+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
805+
fmt_type(self, f, false, false)
740806
}
741807
}
742808

@@ -777,7 +843,7 @@ fn fmt_impl(i: &clean::Impl,
777843
plain.push_str(" for ");
778844
}
779845

780-
fmt_type(&i.for_, f, use_absolute)?;
846+
fmt_type(&i.for_, f, use_absolute, true)?;
781847
plain.push_str(&format!("{:#}", i.for_));
782848

783849
fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;

src/librustdoc/html/highlight.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ impl<U: Write> Writer for U {
144144
-> io::Result<()> {
145145
match klass {
146146
Class::None => write!(self, "{}", text),
147-
klass => write!(self, "<span class='{}'>{}</span>", klass.rustdoc_class(), text),
147+
klass => write!(self, "<span class=\"{}\">{}</span>", klass.rustdoc_class(), text),
148148
}
149149
}
150150

151151
fn enter_span(&mut self, klass: Class) -> io::Result<()> {
152-
write!(self, "<span class='{}'>", klass.rustdoc_class())
152+
write!(self, "<span class=\"{}\">", klass.rustdoc_class())
153153
}
154154

155155
fn exit_span(&mut self) -> io::Result<()> {
@@ -363,7 +363,7 @@ fn write_header(class: Option<&str>,
363363
if let Some(id) = id {
364364
write!(out, "id='{}' ", id)?;
365365
}
366-
write!(out, "class='rust {}'>\n", class.unwrap_or(""))
366+
write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
367367
}
368368

369369
fn write_footer(out: &mut Write) -> io::Result<()> {

0 commit comments

Comments
 (0)