|
1 | 1 | //! A subset of a mir body used for const evaluatability checking.
|
2 |
| -use crate::ty::{self, Const, EarlyBinder, FallibleTypeFolder, GenericArg, TyCtxt, TypeFoldable}; |
| 2 | +use crate::ty::{ |
| 3 | + self, subst::SubstsRef, Const, EarlyBinder, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, |
| 4 | + TypeSuperFoldable, TypeVisitable, |
| 5 | +}; |
3 | 6 | use rustc_errors::ErrorGuaranteed;
|
4 | 7 | use rustc_hir::def_id::DefId;
|
5 | 8 |
|
@@ -33,71 +36,79 @@ pub type BoundAbstractConst<'tcx> = Result<Option<EarlyBinder<ty::Const<'tcx>>>,
|
33 | 36 |
|
34 | 37 | impl<'tcx> TyCtxt<'tcx> {
|
35 | 38 | /// Returns a const with substs applied by
|
36 |
| - pub fn bound_abstract_const( |
| 39 | + fn bound_abstract_const(self, uv: ty::WithOptConstParam<DefId>) -> BoundAbstractConst<'tcx> { |
| 40 | + let ac = if let Some((did, param_did)) = uv.as_const_arg() { |
| 41 | + self.thir_abstract_const_of_const_arg((did, param_did)) |
| 42 | + } else { |
| 43 | + self.thir_abstract_const(uv.did) |
| 44 | + }; |
| 45 | + Ok(ac?.map(|ac| EarlyBinder(ac))) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn expand_abstract_consts<T: TypeFoldable<'tcx>>( |
37 | 49 | self,
|
38 |
| - uv: ty::WithOptConstParam<DefId>, |
39 |
| - ) -> BoundAbstractConst<'tcx> { |
40 |
| - self.thir_abstract_const_opt_const_arg(uv).map(|ac| ac.map(|ac| EarlyBinder(ac))) |
| 50 | + ac: T, |
| 51 | + ) -> Result<Option<T>, ErrorGuaranteed> { |
| 52 | + self._expand_abstract_consts(ac, true) |
41 | 53 | }
|
42 |
| - #[inline] |
43 |
| - pub fn thir_abstract_const_opt_const_arg( |
| 54 | + |
| 55 | + pub fn expand_unevaluated_abstract_const( |
44 | 56 | self,
|
45 |
| - def: ty::WithOptConstParam<DefId>, |
| 57 | + did: ty::WithOptConstParam<DefId>, |
| 58 | + substs: SubstsRef<'tcx>, |
46 | 59 | ) -> Result<Option<ty::Const<'tcx>>, ErrorGuaranteed> {
|
47 |
| - if let Some((did, param_did)) = def.as_const_arg() { |
48 |
| - self.thir_abstract_const_of_const_arg((did, param_did)) |
49 |
| - } else { |
50 |
| - self.thir_abstract_const(def.did) |
51 |
| - } |
| 60 | + let Some(ac) = self.bound_abstract_const(did)? else { |
| 61 | + return Ok(None); |
| 62 | + }; |
| 63 | + let substs = self.erase_regions(substs); |
| 64 | + let ac = ac.subst(self, substs); |
| 65 | + self._expand_abstract_consts(ac, false) |
52 | 66 | }
|
53 | 67 |
|
54 |
| - pub fn expand_bound_abstract_const( |
| 68 | + fn _expand_abstract_consts<T: TypeFoldable<'tcx>>( |
55 | 69 | self,
|
56 |
| - ct: BoundAbstractConst<'tcx>, |
57 |
| - substs: &[GenericArg<'tcx>], |
58 |
| - ) -> Result<Option<Const<'tcx>>, ErrorGuaranteed> { |
| 70 | + ac: T, |
| 71 | + first: bool, |
| 72 | + ) -> Result<Option<T>, ErrorGuaranteed> { |
59 | 73 | struct Expander<'tcx> {
|
60 | 74 | tcx: TyCtxt<'tcx>,
|
| 75 | + first: bool, |
61 | 76 | }
|
| 77 | + |
62 | 78 | impl<'tcx> FallibleTypeFolder<'tcx> for Expander<'tcx> {
|
63 |
| - type Error = ErrorGuaranteed; |
| 79 | + type Error = Option<ErrorGuaranteed>; |
64 | 80 | fn tcx(&self) -> TyCtxt<'tcx> {
|
65 | 81 | self.tcx
|
66 | 82 | }
|
67 |
| - fn try_fold_const(&mut self, c: Const<'tcx>) -> Result<Const<'tcx>, ErrorGuaranteed> { |
68 |
| - use ty::ConstKind::*; |
69 |
| - let uv = match c.kind() { |
70 |
| - Unevaluated(uv) => uv, |
71 |
| - Param(..) | Infer(..) | Bound(..) | Placeholder(..) | Value(..) | Error(..) => { |
72 |
| - return Ok(c); |
73 |
| - } |
74 |
| - Expr(e) => { |
75 |
| - let new_expr = match e { |
76 |
| - ty::Expr::Binop(op, l, r) => { |
77 |
| - ty::Expr::Binop(op, l.try_fold_with(self)?, r.try_fold_with(self)?) |
78 |
| - } |
79 |
| - ty::Expr::UnOp(op, v) => ty::Expr::UnOp(op, v.try_fold_with(self)?), |
80 |
| - ty::Expr::Cast(k, c, t) => { |
81 |
| - ty::Expr::Cast(k, c.try_fold_with(self)?, t.try_fold_with(self)?) |
82 |
| - } |
83 |
| - ty::Expr::FunctionCall(func, args) => ty::Expr::FunctionCall( |
84 |
| - func.try_fold_with(self)?, |
85 |
| - args.try_fold_with(self)?, |
86 |
| - ), |
87 |
| - }; |
88 |
| - return Ok(self.tcx().mk_const(ty::ConstKind::Expr(new_expr), c.ty())); |
| 83 | + fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> { |
| 84 | + if ty.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) { |
| 85 | + ty.try_super_fold_with(self) |
| 86 | + } else { |
| 87 | + Ok(ty) |
| 88 | + } |
| 89 | + } |
| 90 | + fn try_fold_const(&mut self, c: Const<'tcx>) -> Result<Const<'tcx>, Self::Error> { |
| 91 | + let ct = match c.kind() { |
| 92 | + ty::ConstKind::Unevaluated(uv) => { |
| 93 | + if let Some(bac) = self.tcx.bound_abstract_const(uv.def)? { |
| 94 | + let substs = self.tcx.erase_regions(uv.substs); |
| 95 | + bac.subst(self.tcx, substs) |
| 96 | + } else if self.first { |
| 97 | + return Err(None); |
| 98 | + } else { |
| 99 | + c |
| 100 | + } |
89 | 101 | }
|
| 102 | + _ => c, |
90 | 103 | };
|
91 |
| - let bac = self.tcx.bound_abstract_const(uv.def); |
92 |
| - let ac = self.tcx.expand_bound_abstract_const(bac, uv.substs); |
93 |
| - if let Ok(Some(ac)) = ac { ac.try_fold_with(self) } else { Ok(c) } |
| 104 | + self.first = false; |
| 105 | + ct.try_super_fold_with(self) |
94 | 106 | }
|
95 | 107 | }
|
96 |
| - |
97 |
| - let Some(ac) = ct? else { |
98 |
| - return Ok(None); |
99 |
| - }; |
100 |
| - let ac = ac.subst(self, substs); |
101 |
| - Ok(Some(ac.try_fold_with(&mut Expander { tcx: self })?)) |
| 108 | + match ac.try_fold_with(&mut Expander { tcx: self, first }) { |
| 109 | + Ok(c) => Ok(Some(c)), |
| 110 | + Err(None) => Ok(None), |
| 111 | + Err(Some(e)) => Err(e), |
| 112 | + } |
102 | 113 | }
|
103 | 114 | }
|
0 commit comments