about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-03-10 21:03:51 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2021-03-24 21:52:20 +0100
commit7b5908931606c02045482d0de9fbe2aa7f93eda1 (patch)
tree072b485eb4ecdaca5f7e2893cf71e6b345b33447
parentee5093374e036f740d4d7fe12c115bce589d7531 (diff)
downloadrust-7b5908931606c02045482d0de9fbe2aa7f93eda1.tar.gz
rust-7b5908931606c02045482d0de9fbe2aa7f93eda1.zip
Split clean::Constant enum into a struct and an enum
-rw-r--r--src/librustdoc/clean/inline.rs5
-rw-r--r--src/librustdoc/clean/mod.rs16
-rw-r--r--src/librustdoc/clean/types.rs67
-rw-r--r--src/librustdoc/html/render/print_item.rs2
-rw-r--r--src/librustdoc/json/conversions.rs2
5 files changed, 46 insertions, 46 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index 29be2b9f37e..a4c1ee7a0f0 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -500,7 +500,10 @@ crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
 }
 
 fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
-    clean::Constant::Extern { type_: cx.tcx.type_of(def_id).clean(cx), def_id }
+    clean::Constant {
+        type_: cx.tcx.type_of(def_id).clean(cx),
+        kind: clean::ConstantKind::Extern { def_id },
+    }
 }
 
 fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 3dfe1febbd3..80054469f9d 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -393,12 +393,12 @@ impl Clean<Lifetime> for hir::GenericParam<'_> {
 
 impl Clean<Constant> for hir::ConstArg {
     fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
-        Constant::Anonymous {
+        Constant {
             type_: cx
                 .tcx
                 .type_of(cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id())
                 .clean(cx),
-            body: self.value.body,
+            kind: ConstantKind::Anonymous { body: self.value.body },
         }
     }
 }
@@ -1744,7 +1744,10 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
 impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
     fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
         // FIXME: instead of storing the stringified expression, store `self` directly instead.
-        Constant::TyConst { type_: self.ty.clean(cx), expr: self.to_string() }
+        Constant {
+            type_: self.ty.clean(cx),
+            kind: ConstantKind::TyConst { expr: self.to_string() },
+        }
     }
 }
 
@@ -1945,9 +1948,10 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
                 ItemKind::Static(ty, mutability, body_id) => {
                     StaticItem(Static { type_: ty.clean(cx), mutability, expr: Some(body_id) })
                 }
-                ItemKind::Const(ty, body_id) => {
-                    ConstantItem(Constant::Local { type_: ty.clean(cx), body: body_id, def_id })
-                }
+                ItemKind::Const(ty, body_id) => ConstantItem(Constant {
+                    type_: ty.clean(cx),
+                    kind: ConstantKind::Local { body: body_id, def_id },
+                }),
                 ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
                     bounds: ty.bounds.clean(cx),
                     generics: ty.generics.clean(cx),
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
index a27529caf05..4132e187c72 100644
--- a/src/librustdoc/clean/types.rs
+++ b/src/librustdoc/clean/types.rs
@@ -1987,67 +1987,60 @@ crate struct Static {
 }
 
 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
-crate enum Constant {
+crate struct Constant {
+    crate type_: Type,
+    crate kind: ConstantKind,
+}
+
+#[derive(Clone, PartialEq, Eq, Hash, Debug)]
+crate enum ConstantKind {
     /// This is the wrapper around `ty::Const` for a non-local constant. Because it doesn't have a
     /// `BodyId`, we need to handle it on its own.
-    TyConst { type_: Type, expr: String },
-    /// A constant (expression) that’s not an item or associated item. These are usually found
+    ///
+    /// Note that `ty::Const` includes generic parameters, and may not always be uniquely identified
+    /// by a DefId. So this field must be different from `Extern`.
+    TyConst { expr: String },
+    /// A constant (expression) that's not an item or associated item. These are usually found
     /// nested inside types (e.g., array lengths) or expressions (e.g., repeat counts), and also
     /// used to define explicit discriminant values for enum variants.
-    Anonymous { type_: Type, body: BodyId },
+    Anonymous { body: BodyId },
     /// A constant from a different crate.
-    Extern { type_: Type, def_id: DefId },
-    /// const FOO: u32 = ...;
-    Local { type_: Type, def_id: DefId, body: BodyId },
+    Extern { def_id: DefId },
+    /// `const FOO: u32 = ...;`
+    Local { def_id: DefId, body: BodyId },
 }
 
 impl Constant {
     crate fn expr(&self, tcx: TyCtxt<'_>) -> String {
-        match self {
-            Self::TyConst { expr, .. } => expr.clone(),
-            Self::Extern { def_id, .. } => print_inlined_const(tcx, *def_id),
-            Self::Local { body, .. } | Self::Anonymous { body, .. } => print_const_expr(tcx, *body),
+        match self.kind {
+            ConstantKind::TyConst { ref expr } => expr.clone(),
+            ConstantKind::Extern { def_id } => print_inlined_const(tcx, def_id),
+            ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
+                print_const_expr(tcx, body)
+            }
         }
     }
 
     crate fn value(&self, tcx: TyCtxt<'_>) -> Option<String> {
-        match self {
-            Self::TyConst { .. } | Self::Anonymous { .. } => None,
-            Self::Extern { def_id, .. } | Self::Local { def_id, .. } => {
-                print_evaluated_const(tcx, *def_id)
+        match self.kind {
+            ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
+            ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
+                print_evaluated_const(tcx, def_id)
             }
         }
     }
 
     crate fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
-        match self {
-            Self::TyConst { .. } => false,
-            Self::Extern { def_id, .. } => def_id.as_local().map_or(false, |def_id| {
+        match self.kind {
+            ConstantKind::TyConst { .. } => false,
+            ConstantKind::Extern { def_id } => def_id.as_local().map_or(false, |def_id| {
                 is_literal_expr(tcx, tcx.hir().local_def_id_to_hir_id(def_id))
             }),
-            Self::Local { body, .. } | Self::Anonymous { body, .. } => {
+            ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
                 is_literal_expr(tcx, body.hir_id)
             }
         }
     }
-
-    crate fn type_(&self) -> &Type {
-        match *self {
-            Self::TyConst { ref type_, .. }
-            | Self::Extern { ref type_, .. }
-            | Self::Local { ref type_, .. }
-            | Self::Anonymous { ref type_, .. } => type_,
-        }
-    }
-
-    crate fn to_type(self) -> Type {
-        match self {
-            Self::TyConst { type_, .. }
-            | Self::Extern { type_, .. }
-            | Self::Local { type_, .. }
-            | Self::Anonymous { type_, .. } => type_,
-        }
-    }
 }
 
 #[derive(Clone, Debug)]
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index 7662dcf60e3..cc93e55fc67 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -982,7 +982,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
         "{vis}const {name}: {typ}",
         vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
         name = it.name.as_ref().unwrap(),
-        typ = c.type_().print(cx.cache(), cx.tcx()),
+        typ = c.type_.print(cx.cache(), cx.tcx()),
     );
 
     let value = c.value(cx.tcx());
diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs
index 110046bebdf..7516fc1eaa1 100644
--- a/src/librustdoc/json/conversions.rs
+++ b/src/librustdoc/json/conversions.rs
@@ -142,7 +142,7 @@ impl FromWithTcx<clean::Constant> for Constant {
         let expr = constant.expr(tcx);
         let value = constant.value(tcx);
         let is_literal = constant.is_literal(tcx);
-        Constant { type_: constant.to_type().into_tcx(tcx), expr, value, is_literal }
+        Constant { type_: constant.type_.into_tcx(tcx), expr, value, is_literal }
     }
 }