无法在cargo中使用“-Z macro-backtrace”不稳定选项

50

我正在编写 Rust 宏,但遇到了一个我无法理解的宏错误。为了更好地理解它,我尝试按照编译器的建议设置 -Z macro-backtrace 不稳定选项,并重新编译。这是建议内容:

note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

但是做这个:

cargo run -Z macro-backtrace

导致此结果:

error: unknown `-Z` flag specified: macro-backtrace

我已经通过在项目目录中运行 rustup override nightly 和未来使用此功能时运行 rustup default nightly 来完全切换到夜间工具链,但错误仍然存在。

在搜索网络时,我发现了一种使用 cargo -Z help 列出所有 -Z 选项的方法:


Available unstable (nightly-only) flags:

    -Z avoid-dev-deps   -- Avoid installing dev-dependencies if possible
    -Z minimal-versions -- Install minimal dependency versions instead of maximum
    -Z no-index-update  -- Do not update the registry, avoids a network request for benchmarking
    -Z unstable-options -- Allow the usage of unstable options
    -Z timings          -- Display concurrency information
    -Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
    -Z terminal-width   -- Provide a terminal width to rustc for error truncation

Run with 'cargo -Z [FLAG] [SUBCOMMAND]'

See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information about these flags.

没有 -Z macro-backtrace 这个宏回溯...我访问了指定的链接,但即使在那里搜索 macro-backtrace 也没有结果。

所以我被卡住了...我非常想使用这个功能,但似乎找不到激活它的方法。

任何帮助将不胜感激。

1个回答

76

-Z macro-backtrace 是一个 rustc 标志,而不是一个 cargo 标志。你可以使用 cargo +nightly rustc -- -Z macro-backtrace 将其传递给 rustc。(如果您已经将夜间编译器设置为默认值,则命令行中的 +nightly 是可选的。)

或者,您可以设置 RUSTFLAGS 环境变量:

export RUSTFLAGS="-Z macro-backtrace"

29
非常有效,谢谢。如果错误提示能提到这个就更好了... - Dincio
13
我建议这样使用:RUSTFLAGS="-Z macro-backtrace" cargo ...,但它不会污染环境变量。 - Xiaojiba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接