1010
1111//! See docs in build/expr/mod.rs
1212
13- use rustc_data_structures:: fnv:: FnvHashMap ;
14-
1513use build:: { Builder } ;
1614use hair:: * ;
1715use repr:: * ;
@@ -28,93 +26,16 @@ impl<H:Hair> Builder<H> {
2826
2927 fn expr_as_constant ( & mut self , expr : Expr < H > ) -> Constant < H > {
3028 let this = self ;
31- let Expr { ty : _, temp_lifetime : _, span, kind } = expr;
32- let kind = match kind {
33- ExprKind :: Scope { extent : _, value } => {
34- return this. as_constant ( value) ;
35- }
36- ExprKind :: Literal { literal } => {
37- ConstantKind :: Literal ( literal)
38- }
39- ExprKind :: Vec { fields } => {
40- let fields = this. as_constants ( fields) ;
41- ConstantKind :: Aggregate ( AggregateKind :: Vec , fields)
42- }
43- ExprKind :: Tuple { fields } => {
44- let fields = this. as_constants ( fields) ;
45- ConstantKind :: Aggregate ( AggregateKind :: Tuple , fields)
46- }
47- ExprKind :: Adt { adt_def, variant_index, substs, fields, base : None } => {
48- let field_names = this. hir . fields ( adt_def, variant_index) ;
49- let fields = this. named_field_constants ( field_names, fields) ;
50- ConstantKind :: Aggregate ( AggregateKind :: Adt ( adt_def, variant_index, substs) , fields)
51- }
52- ExprKind :: Repeat { value, count } => {
53- let value = Box :: new ( this. as_constant ( value) ) ;
54- let count = Box :: new ( this. as_constant ( count) ) ;
55- ConstantKind :: Repeat ( value, count)
56- }
57- ExprKind :: Binary { op, lhs, rhs } => {
58- let lhs = Box :: new ( this. as_constant ( lhs) ) ;
59- let rhs = Box :: new ( this. as_constant ( rhs) ) ;
60- ConstantKind :: BinaryOp ( op, lhs, rhs)
61- }
62- ExprKind :: Unary { op, arg } => {
63- let arg = Box :: new ( this. as_constant ( arg) ) ;
64- ConstantKind :: UnaryOp ( op, arg)
65- }
66- ExprKind :: Field { lhs, name } => {
67- let lhs = this. as_constant ( lhs) ;
68- ConstantKind :: Projection (
69- Box :: new ( ConstantProjection {
70- base : lhs,
71- elem : ProjectionElem :: Field ( name) ,
72- } ) )
73- }
74- ExprKind :: Deref { arg } => {
75- let arg = this. as_constant ( arg) ;
76- ConstantKind :: Projection (
77- Box :: new ( ConstantProjection {
78- base : arg,
79- elem : ProjectionElem :: Deref ,
80- } ) )
81- }
82- ExprKind :: Call { fun, args } => {
83- let fun = this. as_constant ( fun) ;
84- let args = this. as_constants ( args) ;
85- ConstantKind :: Call ( Box :: new ( fun) , args)
86- }
87- _ => {
29+ let Expr { ty, temp_lifetime : _, span, kind } = expr;
30+ match kind {
31+ ExprKind :: Scope { extent : _, value } =>
32+ this. as_constant ( value) ,
33+ ExprKind :: Literal { literal } =>
34+ Constant { span : span, ty : ty, literal : literal } ,
35+ _ =>
8836 this. hir . span_bug (
8937 span,
90- & format ! ( "expression is not a valid constant {:?}" , kind) ) ;
91- }
92- } ;
93- Constant { span : span, kind : kind }
94- }
95-
96- fn as_constants ( & mut self ,
97- exprs : Vec < ExprRef < H > > )
98- -> Vec < Constant < H > >
99- {
100- exprs. into_iter ( ) . map ( |expr| self . as_constant ( expr) ) . collect ( )
101- }
102-
103- fn named_field_constants ( & mut self ,
104- field_names : Vec < Field < H > > ,
105- field_exprs : Vec < FieldExprRef < H > > )
106- -> Vec < Constant < H > >
107- {
108- let fields_map: FnvHashMap < _ , _ > =
109- field_exprs. into_iter ( )
110- . map ( |f| ( f. name , self . as_constant ( f. expr ) ) )
111- . collect ( ) ;
112-
113- let fields: Vec < _ > =
114- field_names. into_iter ( )
115- . map ( |n| fields_map[ & n] . clone ( ) )
116- . collect ( ) ;
117-
118- fields
38+ & format ! ( "expression is not a valid constant {:?}" , kind) ) ,
39+ }
11940 }
12041}
0 commit comments