]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/misc.rs
do use ty::Const in patterns and abstract consts
[rust.git] / compiler / rustc_mir_build / src / build / misc.rs
1 //! Miscellaneous builder routines that are not specific to building any particular
2 //! kind of thing.
3
4 use crate::build::Builder;
5
6 use rustc_middle::mir;
7 use rustc_middle::mir::*;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_span::{Span, DUMMY_SP};
10 use rustc_trait_selection::infer::InferCtxtExt;
11
12 impl<'a, 'tcx> Builder<'a, 'tcx> {
13     /// Adds a new temporary value of type `ty` storing the result of
14     /// evaluating `expr`.
15     ///
16     /// N.B., **No cleanup is scheduled for this temporary.** You should
17     /// call `schedule_drop` once the temporary is initialized.
18     crate fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
19         // Mark this local as internal to avoid temporaries with types not present in the
20         // user's code resulting in ICEs from the generator transform.
21         let temp = self.local_decls.push(LocalDecl::new(ty, span).internal());
22         let place = Place::from(temp);
23         debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
24         place
25     }
26
27     /// Convenience function for creating a literal operand, one
28     /// without any user type annotation.
29     crate fn literal_operand(
30         &mut self,
31         span: Span,
32         literal: mir::ConstantKind<'tcx>,
33     ) -> Operand<'tcx> {
34         let constant = Box::new(Constant { span, user_ty: None, literal });
35         Operand::Constant(constant)
36     }
37
38     // Returns a zero literal operand for the appropriate type, works for
39     // bool, char and integers.
40     crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
41         let literal = ConstantKind::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
42
43         self.literal_operand(span, literal)
44     }
45
46     crate fn push_usize(
47         &mut self,
48         block: BasicBlock,
49         source_info: SourceInfo,
50         value: u64,
51     ) -> Place<'tcx> {
52         let usize_ty = self.tcx.types.usize;
53         let temp = self.temp(usize_ty, source_info.span);
54         self.cfg.push_assign_constant(
55             block,
56             source_info,
57             temp,
58             Constant {
59                 span: source_info.span,
60                 user_ty: None,
61                 literal: ConstantKind::from_usize(self.tcx, value),
62             },
63         );
64         temp
65     }
66
67     crate fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
68         let tcx = self.tcx;
69         let ty = place.ty(&self.local_decls, tcx).ty;
70         if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, DUMMY_SP) {
71             Operand::Move(place)
72         } else {
73             Operand::Copy(place)
74         }
75     }
76 }