Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plugins 如何从常量中获取元组?_Plugins_Rust_Internals - Fatal编程技术网

Plugins 如何从常量中获取元组?

Plugins 如何从常量中获取元组?,plugins,rust,internals,Plugins,Rust,Internals,在当前的夜间睡眠中,可以使用rustc::middle::const\u eval\u partial(..)获得结果。但是,consval是元组值的Tuple{node:NodeId}。如何获取此元组的内容 示例代码(这里是用作编译器插件的最小lint): 如果查看对ExprTupField表达式(索引到元组)进行常量计算的代码,您可以看到如何提取特定字段: if let hir::ExprTup(ref fields) = tcx.map.expect_expr(tup_id).node {

在当前的夜间睡眠中,可以使用
rustc::middle::const\u eval\u partial(..)
获得
结果。但是,
consval
是元组值的
Tuple{node:NodeId}
。如何获取此元组的内容

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


如果查看对
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!()
}
如果让hir::ExprTup(ref字段)=tcx.map.expect\u expr(tup\u id).node{
如果index.node
字段
是一个
Vec
。因此,您可以迭代该
Vec
并对其调用
eval_const_expr_partial
,以获得元组字段的
ConstVal

请注意,如果元组是在
常量fn
中创建的,则会遇到麻烦:

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!()
}