@@ -1987,67 +1987,60 @@ crate struct Static {
19871987}
19881988
19891989#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
1990- crate enum Constant {
1990+ crate struct Constant {
1991+ crate type_ : Type ,
1992+ crate kind : ConstantKind ,
1993+ }
1994+
1995+ #[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
1996+ crate enum ConstantKind {
19911997 /// This is the wrapper around `ty::Const` for a non-local constant. Because it doesn't have a
19921998 /// `BodyId`, we need to handle it on its own.
1993- TyConst { type_ : Type , expr : String } ,
1994- /// A constant (expression) that’s not an item or associated item. These are usually found
1999+ ///
2000+ /// Note that `ty::Const` includes generic parameters, and may not always be uniquely identified
2001+ /// by a DefId. So this field must be different from `Extern`.
2002+ TyConst { expr : String } ,
2003+ /// A constant (expression) that's not an item or associated item. These are usually found
19952004 /// nested inside types (e.g., array lengths) or expressions (e.g., repeat counts), and also
19962005 /// used to define explicit discriminant values for enum variants.
1997- Anonymous { type_ : Type , body : BodyId } ,
2006+ Anonymous { body : BodyId } ,
19982007 /// A constant from a different crate.
1999- Extern { type_ : Type , def_id : DefId } ,
2000- /// const FOO: u32 = ...;
2001- Local { type_ : Type , def_id : DefId , body : BodyId } ,
2008+ Extern { def_id : DefId } ,
2009+ /// ` const FOO: u32 = ...;`
2010+ Local { def_id : DefId , body : BodyId } ,
20022011}
20032012
20042013impl Constant {
20052014 crate fn expr ( & self , tcx : TyCtxt < ' _ > ) -> String {
2006- match self {
2007- Self :: TyConst { expr, .. } => expr. clone ( ) ,
2008- Self :: Extern { def_id, .. } => print_inlined_const ( tcx, * def_id) ,
2009- Self :: Local { body, .. } | Self :: Anonymous { body, .. } => print_const_expr ( tcx, * body) ,
2015+ match self . kind {
2016+ ConstantKind :: TyConst { ref expr } => expr. clone ( ) ,
2017+ ConstantKind :: Extern { def_id } => print_inlined_const ( tcx, def_id) ,
2018+ ConstantKind :: Local { body, .. } | ConstantKind :: Anonymous { body } => {
2019+ print_const_expr ( tcx, body)
2020+ }
20102021 }
20112022 }
20122023
20132024 crate fn value ( & self , tcx : TyCtxt < ' _ > ) -> Option < String > {
2014- match self {
2015- Self :: TyConst { .. } | Self :: Anonymous { .. } => None ,
2016- Self :: Extern { def_id, .. } | Self :: Local { def_id, .. } => {
2017- print_evaluated_const ( tcx, * def_id)
2025+ match self . kind {
2026+ ConstantKind :: TyConst { .. } | ConstantKind :: Anonymous { .. } => None ,
2027+ ConstantKind :: Extern { def_id } | ConstantKind :: Local { def_id, .. } => {
2028+ print_evaluated_const ( tcx, def_id)
20182029 }
20192030 }
20202031 }
20212032
20222033 crate fn is_literal ( & self , tcx : TyCtxt < ' _ > ) -> bool {
2023- match self {
2024- Self :: TyConst { .. } => false ,
2025- Self :: Extern { def_id, .. } => def_id. as_local ( ) . map_or ( false , |def_id| {
2034+ match self . kind {
2035+ ConstantKind :: TyConst { .. } => false ,
2036+ ConstantKind :: Extern { def_id } => def_id. as_local ( ) . map_or ( false , |def_id| {
20262037 is_literal_expr ( tcx, tcx. hir ( ) . local_def_id_to_hir_id ( def_id) )
20272038 } ) ,
2028- Self :: Local { body, .. } | Self :: Anonymous { body, .. } => {
2039+ ConstantKind :: Local { body, .. } | ConstantKind :: Anonymous { body } => {
20292040 is_literal_expr ( tcx, body. hir_id )
20302041 }
20312042 }
20322043 }
2033-
2034- crate fn type_ ( & self ) -> & Type {
2035- match * self {
2036- Self :: TyConst { ref type_, .. }
2037- | Self :: Extern { ref type_, .. }
2038- | Self :: Local { ref type_, .. }
2039- | Self :: Anonymous { ref type_, .. } => type_,
2040- }
2041- }
2042-
2043- crate fn to_type ( self ) -> Type {
2044- match self {
2045- Self :: TyConst { type_, .. }
2046- | Self :: Extern { type_, .. }
2047- | Self :: Local { type_, .. }
2048- | Self :: Anonymous { type_, .. } => type_,
2049- }
2050- }
20512044}
20522045
20532046#[ derive( Clone , Debug ) ]
0 commit comments