diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index 2580bbf982b04..6434dccdfc75b 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -15,6 +15,7 @@ use rustc_hir::def_id::DefId;
use rustc_target::spec::abi::Abi;
use crate::clean::{self, PrimitiveType};
+use crate::html::escape::Escape;
use crate::html::item_type::ItemType;
use crate::html::render::{self, cache, CURRENT_DEPTH};
@@ -314,8 +315,14 @@ impl clean::Lifetime {
}
impl clean::Constant {
- crate fn print(&self) -> &str {
- &self.expr
+ crate fn print(&self) -> impl fmt::Display + '_ {
+ display_fn(move |f| {
+ if f.alternate() {
+ f.write_str(&self.expr)
+ } else {
+ write!(f, "{}", Escape(&self.expr))
+ }
+ })
}
}
@@ -689,7 +696,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
clean::Array(ref t, ref n) => {
primitive_link(f, PrimitiveType::Array, "[")?;
fmt::Display::fmt(&t.print(), f)?;
- primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
+ if f.alternate() {
+ primitive_link(f, PrimitiveType::Array, &format!("; {}]", n))
+ } else {
+ primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)))
+ }
}
clean::Never => primitive_link(f, PrimitiveType::Never, "!"),
clean::RawPointer(m, ref t) => {
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index 025257b801fb6..a01e2f793948e 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -2280,7 +2280,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
);
if c.value.is_some() || c.is_literal {
- write!(w, " = {expr};", expr = c.expr);
+ write!(w, " = {expr};", expr = Escape(&c.expr));
} else {
write!(w, ";");
}
@@ -2293,7 +2293,7 @@ fn item_constant(w: &mut Buffer, cx: &Context, it: &clean::Item, c: &clean::Cons
if value_lowercase != expr_lowercase
&& value_lowercase.trim_end_matches("i32") != expr_lowercase
{
- write!(w, " // {value}", value = value);
+ write!(w, " // {value}", value = Escape(value));
}
}
}
diff --git a/src/test/rustdoc/const-generics/const-impl.rs b/src/test/rustdoc/const-generics/const-impl.rs
index 819adfeb9c775..7361b22b74798 100644
--- a/src/test/rustdoc/const-generics/const-impl.rs
+++ b/src/test/rustdoc/const-generics/const-impl.rs
@@ -30,3 +30,10 @@ impl VSet {
Self { inner: Vec::new() }
}
}
+
+pub struct Escape;
+
+// @has foo/struct.Escape.html '//h3[@id="impl"]/code' 'impl Escape<{ r#""# }>'
+impl Escape<{ r#""# }> {
+ pub fn f() {}
+}
diff --git a/src/test/rustdoc/show-const-contents.rs b/src/test/rustdoc/show-const-contents.rs
index 6d95f7827a1d7..e84f6e52c75aa 100644
--- a/src/test/rustdoc/show-const-contents.rs
+++ b/src/test/rustdoc/show-const-contents.rs
@@ -62,3 +62,6 @@ macro_rules! int_module {
// @has show_const_contents/constant.MIN.html '= i16::min_value(); // -32_768i16'
int_module!(i16);
+
+// @has show_const_contents/constant.ESCAPE.html //pre '= r#""#;'
+pub const ESCAPE: &str = r#""#;