Skip to content

Commit 1f4acfa

Browse files
committed
switch to using constvals for constants, instead of having constant
trees in MIR
1 parent dedde0b commit 1f4acfa

File tree

11 files changed

+107
-236
lines changed

11 files changed

+107
-236
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn lookup_const_fn_by_id<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId)
242242
}
243243
}
244244

245-
#[derive(Clone, PartialEq)]
245+
#[derive(Clone, Debug, PartialEq)]
246246
pub enum ConstVal {
247247
Float(f64),
248248
Int(i64),

src/librustc_mir/build/expr/as_constant.rs

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
//! See docs in build/expr/mod.rs
1212
13-
use rustc_data_structures::fnv::FnvHashMap;
14-
1513
use build::{Builder};
1614
use hair::*;
1715
use 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
}

src/librustc_mir/build/expr/into.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ impl<H:Hair> Builder<H> {
9999
true_block, expr_span, destination,
100100
Constant {
101101
span: expr_span,
102-
kind: ConstantKind::Literal(Literal::Bool { value: true }),
102+
ty: this.hir.bool_ty(),
103+
literal: this.hir.true_literal(),
103104
});
104105

105106
this.cfg.push_assign_constant(
106107
false_block, expr_span, destination,
107108
Constant {
108109
span: expr_span,
109-
kind: ConstantKind::Literal(Literal::Bool { value: false }),
110+
ty: this.hir.bool_ty(),
111+
literal: this.hir.false_literal(),
110112
});
111113

112114
this.cfg.terminate(true_block, Terminator::Goto { target: join_block });

src/librustc_mir/build/matches/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ enum TestKind<H:Hair> {
221221
Switch { adt_def: H::AdtDef },
222222

223223
// test for equality
224-
Eq { value: Constant<H>, ty: H::Ty },
224+
Eq { value: Literal<H>, ty: H::Ty },
225225

226226
// test whether the value falls within an inclusive range
227-
Range { lo: Constant<H>, hi: Constant<H>, ty: H::Ty },
227+
Range { lo: Literal<H>, hi: Literal<H>, ty: H::Ty },
228228

229229
// test length of the slice is equal to len
230230
Len { len: usize, op: BinOp },
@@ -267,9 +267,12 @@ impl<H:Hair> Builder<H> {
267267
// If so, apply any bindings, test the guard (if any), and
268268
// branch to the arm.
269269
let candidate = candidates.pop().unwrap();
270-
match self.bind_and_guard_matched_candidate(block, var_extent, candidate) {
271-
None => { return; }
272-
Some(b) => { block = b; }
270+
if let Some(b) = self.bind_and_guard_matched_candidate(block, var_extent, candidate) {
271+
block = b;
272+
} else {
273+
// if None is returned, then any remaining candidates
274+
// are unreachable (at least not through this path).
275+
return;
273276
}
274277
}
275278

src/librustc_mir/build/matches/test.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ impl<H:Hair> Builder<H> {
3333
}
3434
}
3535

36-
PatternKind::Constant { ref expr } => {
37-
let expr = self.as_constant(expr.clone());
36+
PatternKind::Constant { ref value } => {
3837
Test {
3938
span: match_pair.pattern.span,
40-
kind: TestKind::Eq { value: expr, ty: match_pair.pattern.ty.clone() },
39+
kind: TestKind::Eq { value: value.clone(),
40+
ty: match_pair.pattern.ty.clone() },
4141
}
4242
}
4343

4444
PatternKind::Range { ref lo, ref hi } => {
45-
let lo = self.as_constant(lo.clone());
46-
let hi = self.as_constant(hi.clone());
4745
Test {
4846
span: match_pair.pattern.span,
49-
kind: TestKind::Range { lo: lo, hi: hi, ty: match_pair.pattern.ty.clone() },
47+
kind: TestKind::Range { lo: lo.clone(),
48+
hi: hi.clone(),
49+
ty: match_pair.pattern.ty.clone() },
5050
}
5151
}
5252

@@ -90,15 +90,15 @@ impl<H:Hair> Builder<H> {
9090

9191
TestKind::Eq { value, ty } => {
9292
// call PartialEq::eq(discrim, constant)
93-
let constant = self.push_constant(block, test.span, ty.clone(), value);
93+
let constant = self.push_literal(block, test.span, ty.clone(), value);
9494
let item_ref = self.hir.partial_eq(ty);
9595
self.call_comparison_fn(block, test.span, item_ref, lvalue.clone(), constant)
9696
}
9797

9898
TestKind::Range { lo, hi, ty } => {
9999
// Test `v` by computing `PartialOrd::le(lo, v) && PartialOrd::le(v, hi)`.
100-
let lo = self.push_constant(block, test.span, ty.clone(), lo);
101-
let hi = self.push_constant(block, test.span, ty.clone(), hi);
100+
let lo = self.push_literal(block, test.span, ty.clone(), lo);
101+
let hi = self.push_literal(block, test.span, ty.clone(), hi);
102102
let item_ref = self.hir.partial_le(ty);
103103

104104
let lo_blocks =

src/librustc_mir/build/misc.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ impl<H:Hair> Builder<H> {
3333
lvalue
3434
}
3535

36-
pub fn push_constant(&mut self,
37-
block: BasicBlock,
38-
span: H::Span,
39-
ty: H::Ty,
40-
constant: Constant<H>)
41-
-> Lvalue<H> {
42-
let temp = self.temp(ty);
36+
pub fn push_literal(&mut self,
37+
block: BasicBlock,
38+
span: H::Span,
39+
ty: H::Ty,
40+
literal: Literal<H>)
41+
-> Lvalue<H> {
42+
let temp = self.temp(ty.clone());
43+
let constant = Constant { span: span, ty: ty, literal: literal };
4344
self.cfg.push_assign_constant(block, span, &temp, constant);
4445
temp
4546
}
@@ -55,8 +56,8 @@ impl<H:Hair> Builder<H> {
5556
block, span, &temp,
5657
Constant {
5758
span: span,
58-
kind: ConstantKind::Literal(Literal::Uint { bits: IntegralBits::BSize,
59-
value: value as u64 }),
59+
ty: self.hir.usize_ty(),
60+
literal: self.hir.usize_literal(value),
6061
});
6162
temp
6263
}
@@ -66,13 +67,7 @@ impl<H:Hair> Builder<H> {
6667
span: H::Span,
6768
item_ref: ItemRef<H>)
6869
-> Lvalue<H> {
69-
let constant = Constant {
70-
span: span,
71-
kind: ConstantKind::Literal(Literal::Item {
72-
def_id: item_ref.def_id,
73-
substs: item_ref.substs
74-
})
75-
};
76-
self.push_constant(block, span, item_ref.ty, constant)
70+
let literal = Literal::Item { def_id: item_ref.def_id, substs: item_ref.substs };
71+
self.push_literal(block, span, item_ref.ty, literal)
7772
}
7873
}

src/librustc_mir/hair.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub trait Hair: Sized+Debug+Clone+Eq+Hash { // (*)
3838
type Ty: Clone+Debug+Eq; // e.g., ty::Ty<'tcx>
3939
type Region: Copy+Debug; // e.g., ty::Region
4040
type CodeExtent: Copy+Debug+Hash+Eq; // e.g., region::CodeExtent
41+
type ConstVal: Clone+Debug+PartialEq; // e.g., ConstVal
4142
type Pattern: Clone+Debug+Mirror<Self,Output=Pattern<Self>>; // e.g., &P<ast::Pat>
4243
type Expr: Clone+Debug+Mirror<Self,Output=Expr<Self>>; // e.g., &P<ast::Expr>
4344
type Stmt: Clone+Debug+Mirror<Self,Output=Stmt<Self>>; // e.g., &P<ast::Stmt>
@@ -55,9 +56,18 @@ pub trait Hair: Sized+Debug+Clone+Eq+Hash { // (*)
5556
/// Returns the type `usize`.
5657
fn usize_ty(&mut self) -> Self::Ty;
5758

59+
/// Returns the literal for `true`
60+
fn usize_literal(&mut self, value: usize) -> Literal<Self>;
61+
5862
/// Returns the type `bool`.
5963
fn bool_ty(&mut self) -> Self::Ty;
6064

65+
/// Returns the literal for `true`
66+
fn true_literal(&mut self) -> Literal<Self>;
67+
68+
/// Returns the literal for `true`
69+
fn false_literal(&mut self) -> Literal<Self>;
70+
6171
/// Returns a reference to `PartialEq::<T,T>::eq`
6272
fn partial_eq(&mut self, ty: Self::Ty) -> ItemRef<Self>;
6373

@@ -261,9 +271,9 @@ pub enum PatternKind<H:Hair> {
261271

262272
Deref { subpattern: PatternRef<H> }, // box P, &P, &mut P, etc
263273

264-
Constant { expr: ExprRef<H> },
274+
Constant { value: Literal<H> },
265275

266-
Range { lo: ExprRef<H>, hi: ExprRef<H> },
276+
Range { lo: Literal<H>, hi: Literal<H> },
267277

268278
// matches against a slice, checking the length and extracting elements
269279
Slice { prefix: Vec<PatternRef<H>>,

src/librustc_mir/repr.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -646,44 +646,12 @@ impl<H:Hair> Debug for Rvalue<H> {
646646
#[derive(Clone, Debug, PartialEq)]
647647
pub struct Constant<H:Hair> {
648648
pub span: H::Span,
649-
pub kind: ConstantKind<H>
650-
}
651-
652-
#[derive(Clone, Debug, PartialEq)]
653-
pub enum ConstantKind<H:Hair> {
654-
Literal(Literal<H>),
655-
Aggregate(AggregateKind<H>, Vec<Constant<H>>),
656-
Call(Box<Constant<H>>, Vec<Constant<H>>),
657-
Cast(Box<Constant<H>>, H::Ty),
658-
Repeat(Box<Constant<H>>, Box<Constant<H>>),
659-
Ref(BorrowKind, Box<Constant<H>>),
660-
BinaryOp(BinOp, Box<Constant<H>>, Box<Constant<H>>),
661-
UnaryOp(UnOp, Box<Constant<H>>),
662-
Projection(Box<ConstantProjection<H>>)
649+
pub ty: H::Ty,
650+
pub literal: Literal<H>
663651
}
664652

665-
pub type ConstantProjection<H> =
666-
Projection<H,Constant<H>,Constant<H>>;
667-
668653
#[derive(Clone, Debug, PartialEq)]
669654
pub enum Literal<H:Hair> {
670655
Item { def_id: H::DefId, substs: H::Substs },
671-
Projection { projection: H::Projection },
672-
Int { bits: IntegralBits, value: i64 },
673-
Uint { bits: IntegralBits, value: u64 },
674-
Float { bits: FloatBits, value: f64 },
675-
Char { c: char },
676-
Bool { value: bool },
677-
Bytes { value: H::Bytes },
678-
String { value: H::InternedString },
679-
}
680-
681-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
682-
pub enum IntegralBits {
683-
B8, B16, B32, B64, BSize
684-
}
685-
686-
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
687-
pub enum FloatBits {
688-
F32, F64
656+
Value { value: H::ConstVal },
689657
}

0 commit comments

Comments
 (0)