]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/build/expr/as_constant.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_mir / build / expr / as_constant.rs
CommitLineData
e9174d1e
SL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! See docs in build/expr/mod.rs
12
b039eaaf 13use build::Builder;
e9174d1e 14use hair::*;
92a42be0 15use rustc::mir::repr::*;
e9174d1e 16
b039eaaf 17impl<'a,'tcx> Builder<'a,'tcx> {
e9174d1e
SL
18 /// Compile `expr`, yielding a compile-time constant. Assumes that
19 /// `expr` is a valid compile-time constant!
b039eaaf
SL
20 pub fn as_constant<M>(&mut self, expr: M) -> Constant<'tcx>
21 where M: Mirror<'tcx, Output=Expr<'tcx>>
e9174d1e
SL
22 {
23 let expr = self.hir.mirror(expr);
24 self.expr_as_constant(expr)
25 }
26
b039eaaf 27 fn expr_as_constant(&mut self, expr: Expr<'tcx>) -> Constant<'tcx> {
e9174d1e 28 let this = self;
b039eaaf
SL
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 _ =>
54a0048b 36 span_bug!(
e9174d1e 37 span,
54a0048b
SL
38 "expression is not a valid constant {:?}",
39 kind),
b039eaaf 40 }
e9174d1e
SL
41 }
42}