如何从 ConstVal 中取出一个元组?

How to get a tuple out of a ConstVal?

对于当前的每晚,可以使用 rustc::middle::const_eval_partial(..) 获得 Result<ConstVal, _>。但是,ConstVal 是元组值的 Tuple { node: NodeId }。我怎样才能得到这个元组的内容?

示例代码(这里是用作编译器插件的最小 lint):

use rustc::lint::*;
use syntax::ptr::P;
use rustc_front::hir::*;
use rustc::middle::const_eval::ConstVal::Tuple;
use rustc::middle::const_eval::eval_const_expr_partial;
use rustc::middle::const_eval::EvalHint::ExprTypeChecked;

declare_lint! { pub TEST_LINT, Warn, "Just a test, ignore this" }

#[derive(Copy,Clone)]
pub struct TestLint;

impl LintPass for TestLint {
    fn get_lints(&self) -> LintArray {
        lint_array!TEST_LINT)
    }
}

impl LateLintPass for BitMask {
    fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
        let res = eval_const_expr_partial(cx.tcx, expr, ExprTypeChecked, None);
        if let Ok(Tuple(node_id))) = res {
            // ... how to get the parts of the tuple?
        }
    }
}

如果您查看 ExprTupField 表达式的常量求值代码(索引到元组中),您可以了解如何提取特定字段:

if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {
    if index.node < fields.len() {
        return eval_const_expr_partial(tcx, &fields[index.node], base_hint, fn_args)
    } else {
        signal!(e, TupleIndexOutOfBounds);
    }
} else {
    unreachable!()
}

fields 是一个 Vec<P<Expr>>。因此,您可以迭代 Vec 并对其调用 eval_const_expr_partial 以获得元组字段的 ConstVal

请注意,如果元组是在 const fn 中创建的,您将会遇到麻烦:https://github.com/rust-lang/rust/issues/29928