@@ -2,6 +2,7 @@ use crate::ty::TyCtxt;
22
33use super :: ScalarInt ;
44use rustc_macros:: HashStable ;
5+ use rustc_span:: Symbol ;
56
67#[ derive( Copy , Clone , Debug , Hash , TyEncodable , TyDecodable , Eq , PartialEq , Ord , PartialOrd ) ]
78#[ derive( HashStable ) ]
@@ -30,33 +31,42 @@ pub enum ValTree<'tcx> {
3031 /// Enums are represented by storing their discriminant as a field, followed by all
3132 /// the fields of the variant.
3233 ///
33- /// `&str` and `& [T]` are encoded as if they were `&[T;N]`. So there is no wide pointer
34+ /// `&[T]` are encoded as if they were `&[T;N]`. So there is no wide pointer
3435 /// or metadata encoded, instead the length is taken directly from the number of elements
3536 /// in the branch.
3637 Branch ( & ' tcx [ ValTree < ' tcx > ] ) ,
38+ /// `&str` could be encoded as a `Branch`, but the back and forth between valtree
39+ /// representations and other representations of `str` is expensive.
40+ Str ( Symbol ) ,
3741}
3842
3943impl ValTree < ' tcx > {
4044 pub fn zst ( ) -> Self {
4145 Self :: Branch ( & [ ] )
4246 }
47+ pub fn unwrap_str ( self ) -> Symbol {
48+ match self {
49+ Self :: Str ( s) => s,
50+ _ => bug ! ( "expected str, got {:?}" , self ) ,
51+ }
52+ }
4353 pub fn unwrap_leaf ( self ) -> ScalarInt {
4454 match self {
4555 Self :: Leaf ( s) => s,
46- Self :: Branch ( branch ) => bug ! ( "expected leaf, got {:?}" , branch ) ,
56+ _ => bug ! ( "expected leaf, got {:?}" , self ) ,
4757 }
4858 }
4959 pub fn unwrap_branch ( self ) -> & ' tcx [ Self ] {
5060 match self {
51- Self :: Leaf ( s) => bug ! ( "expected branch, got {:?}" , s) ,
5261 Self :: Branch ( branch) => branch,
62+ _ => bug ! ( "expected branch, got {:?}" , self ) ,
5363 }
5464 }
5565 #[ inline]
5666 pub fn try_to_scalar_int ( self ) -> Option < ScalarInt > {
5767 match self {
5868 Self :: Leaf ( s) => Some ( s) ,
59- Self :: Branch ( _) => None ,
69+ Self :: Str ( _ ) | Self :: Branch ( _) => None ,
6070 }
6171 }
6272
0 commit comments