@@ -3,8 +3,8 @@ use std::ops::Deref;
33use rustc_hir:: def:: DefKind ;
44use rustc_hir:: def_id:: LocalDefId ;
55use rustc_middle:: mir:: {
6- Body , ConstValue , Operand , Place , RETURN_PLACE , Rvalue , START_BLOCK , StatementKind ,
7- TerminatorKind ,
6+ Body , Const , ConstValue , Operand , Place , RETURN_PLACE , Rvalue , START_BLOCK , StatementKind ,
7+ TerminatorKind , UnevaluatedConst ,
88} ;
99use rustc_middle:: ty:: { Ty , TyCtxt , TypeVisitableExt } ;
1010
@@ -13,7 +13,9 @@ use rustc_middle::ty::{Ty, TyCtxt, TypeVisitableExt};
1313/// A "trivial const" is a const which can be easily proven to evaluate successfully, and the value
1414/// that it evaluates to can be easily found without going through the usual MIR phases for a const.
1515///
16- /// Currently the only form of trivial const that is supported is this:
16+ /// Currently, we support two forms of trivial const.
17+ ///
18+ /// The base case is this:
1719/// ```
1820/// const A: usize = 0;
1921/// ```
@@ -34,6 +36,13 @@ use rustc_middle::ty::{Ty, TyCtxt, TypeVisitableExt};
3436/// This scenario meets the required criteria because:
3537/// * Control flow cannot panic, we don't have any calls or assert terminators
3638/// * The value of the const is already computed, so it cannot fail
39+ ///
40+ /// In addition to assignment of literals, assignments of trivial consts are also considered
41+ /// trivial consts. In this case, both `A` and `B` are trivial:
42+ /// ```
43+ /// const A: usize = 0;
44+ /// const B: usize = A;
45+ /// ```
3746pub ( crate ) fn trivial_const < ' a , ' tcx : ' a , F , B > (
3847 tcx : TyCtxt < ' tcx > ,
3948 def : LocalDefId ,
@@ -74,13 +83,19 @@ where
7483 return None ;
7584 }
7685
77- if let Rvalue :: Use ( Operand :: Constant ( c) ) = rvalue {
78- if let rustc_middle:: mir:: Const :: Val ( v, ty) = c. const_ {
79- return Some ( ( v, ty) ) ;
86+ let Rvalue :: Use ( Operand :: Constant ( c) ) = rvalue else {
87+ return None ;
88+ } ;
89+ match c. const_ {
90+ Const :: Ty ( ..) => None ,
91+ Const :: Unevaluated ( UnevaluatedConst { def, args, .. } , _ty) => {
92+ if !args. is_empty ( ) {
93+ return None ;
94+ }
95+ tcx. trivial_const ( def)
8096 }
97+ Const :: Val ( v, ty) => Some ( ( v, ty) ) ,
8198 }
82-
83- return None ;
8499}
85100
86101// The query provider is based on calling the free function trivial_const, which calls mir_built,
0 commit comments