2020-04-22 14:16:06 +00:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
2023-09-20 18:51:14 +00:00
|
|
|
use rustc_middle::mir::{Const, ConstOperand, Location};
|
2024-03-14 06:28:25 +00:00
|
|
|
use rustc_middle::ty;
|
2020-04-22 14:16:06 +00:00
|
|
|
|
|
|
|
pub struct RequiredConstsVisitor<'a, 'tcx> {
|
2023-09-20 18:51:14 +00:00
|
|
|
required_consts: &'a mut Vec<ConstOperand<'tcx>>,
|
2020-04-22 14:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> RequiredConstsVisitor<'a, 'tcx> {
|
2023-09-20 18:51:14 +00:00
|
|
|
pub fn new(required_consts: &'a mut Vec<ConstOperand<'tcx>>) -> Self {
|
2020-04-22 14:16:06 +00:00
|
|
|
RequiredConstsVisitor { required_consts }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 08:48:37 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
|
2023-09-20 18:51:14 +00:00
|
|
|
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
|
2024-03-14 06:28:25 +00:00
|
|
|
// Only unevaluated consts have to be added to `required_consts` as only those can possibly
|
|
|
|
// still have latent const-eval errors.
|
|
|
|
let is_required = match constant.const_ {
|
2023-09-20 18:51:14 +00:00
|
|
|
Const::Ty(c) => match c.kind() {
|
2024-03-14 06:28:25 +00:00
|
|
|
ty::ConstKind::Value(_) => false, // already a value, cannot error
|
|
|
|
ty::ConstKind::Param(_) | ty::ConstKind::Error(_) => true, // these are errors or could be replaced by errors
|
|
|
|
_ => bug!(
|
|
|
|
"only ConstKind::Param/Value/Error should be encountered here, got {:#?}",
|
|
|
|
c
|
|
|
|
),
|
2022-06-27 14:32:47 +00:00
|
|
|
},
|
2024-03-14 06:28:25 +00:00
|
|
|
Const::Unevaluated(..) => true,
|
|
|
|
Const::Val(..) => false, // already a value, cannot error
|
|
|
|
};
|
|
|
|
if is_required {
|
|
|
|
self.required_consts.push(*constant);
|
2020-04-22 14:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|