Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Macros 为什么Rust在多语句宏中找不到变量?_Macros_Rust - Fatal编程技术网

Macros 为什么Rust在多语句宏中找不到变量?

Macros 为什么Rust在多语句宏中找不到变量?,macros,rust,Macros,Rust,我有一个宏,它在unsafe块中被调用,该块具有以下模式: ( $mrb:expr, $sig:expr, $args:ident, $name:ident : $t:tt) => { let $args = uninitialized::<*const MRValue>(); let count = uninitialized::<i32>(); mrb_get_args($mrb, $sig, args!($name, $t), &am

我有一个宏,它在
unsafe
块中被调用,该块具有以下模式:

( $mrb:expr, $sig:expr, $args:ident, $name:ident : $t:tt) => {
    let $args = uninitialized::<*const MRValue>();
    let count = uninitialized::<i32>();

    mrb_get_args($mrb, $sig, args!($name, $t), &$args as *const *const MRValue,
                 &count as *const i32);
};
args
似乎是相同的(
77
),计数也似乎是相同的(
807
),但我还是得到了以下错误:

<mrusty macros>:24:20: 24:21 error: unresolved name `args`. Did you mean the macro `args!`? [E0425]
<mrusty macros>:24 mrb , sig , $ args , $ ( $ name : $ t ) , * ) ; conv ! (
                                  ^
<mrusty macros>:23:29: 24:48 note: in this expansion of args_rest! (defined in <mrusty macros>)
src/main.rs:12:47: 16:7 note: in this expansion of mrfn! (defined in <mrusty macros>)
<mrusty macros>:24:20: 24:21 help: run `rustc --explain E0425` to see a detailed explanation
<mrusty macros>:6:5: 6:10 error: unresolved name `count` [E0425]
<mrusty macros>:6 , & count as * const i32 ) ; } ; (
                  ^~~~~
<mrusty macros>:23:29: 24:48 note: in this expansion of args_rest! (defined in <mrusty macros>)
src/main.rs:12:47: 16:7 note: in this expansion of mrfn! (defined in  <mrusty macros>)
<mrusty macros>:6:5: 6:10 help: run `rustc --explain E0425` to see a detailed explanation
:24:20:24:21错误:未解析的名称'args'。你是说宏“args!”吗?[E0425]
:24 mrb,sig,$args,$($name:$t),*);卷积和多项式相乘!(
^
:23:29:24:48注:在此args_rest!(在中定义)的扩展中
src/main.rs:12:47:16:7注:在mrfn的扩展中!(在中定义)
:24:20:24:21帮助:运行'rustc--explain E0425'查看详细说明
:6:5:6:10错误:未解析的名称“count”[E0425]
:6,&计为*const i32);};(
^~~~~
:23:29:24:48注:在此args_rest!(在中定义)的扩展中
src/main.rs:12:47:16:7注:在mrfn的扩展中!(在中定义)
:6:5:6:10帮助:运行'rustc--explain E0425'查看详细说明
这看起来有点可疑,似乎是一个bug,但在我提交关于锈病的问题之前,我想再看一眼这个问题。

试试这个:

( $mrb:expr, $sig:expr, $args:ident, $name:ident : $t:tt) => {{
    let $args = uninitialized::<*const MRValue>();
    let count = uninitialized::<i32>();

    mrb_get_args($mrb, $sig, args!($name, $t), &$args as *const *const MRValue,
                 &count as *const i32);
}};
($mrb:expr,$sig:expr,$args:ident,$name:ident:$t:tt)=>{{
让$args=未初始化::();
let count=未初始化::();
mrb_get_args($mrb,$sig,args!($name,$t)和$args as*const*constmrvalue,
&计为*常数i32);
}};
(请注意,宏展开的主体被包装到第二组大括号中)


我不记得您需要它的确切原因,但基本思想是宏扩展块中的每个语句都用自己的卫生上下文展开,因此第一行中的
$args
与最后一行中的
$args
不同。但是,如果将所有语句放在一个块中,卫生上下文xt变为共享,并且
$args
的两个扩展现在引用相同的标识符。因此,这可能不是错误;这只是Rust中宏扩展的工作方式。

这不起作用的基本原因是多语句宏被破坏。任何不返回值的宏(例如块)将只返回第一条语句

macro_rules! example {
    ( $name:ident ) => {
        let mut $name = 0;
        let mut $name = 1;
    }
}

fn main() {
    example!(x);

    println!("{}", x);
}
本例打印的是
0
,而不是
1
。虽然已关闭,但at可能会生锈1.10


同时,在适用的情况下使用块。

在将其作为错误报告之前,最好创建一个说明问题的最小完整示例。@BrianCain当然可以。这是可行的,但由于它们在相同的上下文中,因此它们之间的差异没有任何意义,特别是考虑到我从宏的它的输出。
macro_rules! example {
    ( $name:ident ) => {
        let mut $name = 0;
        let mut $name = 1;
    }
}

fn main() {
    example!(x);

    println!("{}", x);
}