LoginSignup
5
2

More than 3 years have passed since last update.

Rustマクロのバックトレースをnightly版で確認する

Posted at

この記事は Rustその2 Advent Calendar 2019 10日目 の記事です.
小ネタです。

テスト環境

cargo 1.41.0-nightly (750cb1482 2019-11-23)
rustc 1.41.0-nightly (fdc001156 2019-12-02)

マクロでエラーが。。

初心者なので、コンパイラに怒られます。一般的にRustのエラー説明は親切なんですが、マクロが絡むと読解が困難な場合があります。以下はマクロのエラー例です。

main.rc
fn main() {
    let vec = vec![];
    println!("{:?}", vec);
}

コンパイル結果

terminal
$ cargo check
    Checking samples v0.1.0 (/usr/local/src)
error[E0282]: type annotations needed for `std::vec::Vec<T>`
 --> src/main.rs:2:15
  |
2 |     let vec = vec![];
  |         ---   ^^^^^^ cannot infer type for `T`
  |         |
  |         consider giving `vec` the explicit type `std::vec::Vec<T>`, where the type parameter `T` is specified
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
error: could not compile `samples`.

To learn more, run the command again with --verbose.

note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Nightly版で -Z external-macro-backtrace つければ詳細が出るようです。

Nightly版 について

一言でいうと実験版です。以下参照

付録G:Rustの作られ方と“Nightly Rust”

お試し

dockerを使います。cargoコマンドを行うディレクトリで以下コマンドを実行します。docker上のpathにボリュームをマウントします。exitすればコンテナは消えます。ローカルが汚れないので良いです。

terminal
samples $ docker run -it --rm -v $(pwd):/usr/local/src rustlang/rust:nightly bash
root@12c9fcfd5432:/# cd /usr/local/src/
root@12c9fcfd5432:/usr/local/src# ls
Cargo.lock  Cargo.toml  src  target

起動しました。早速、 -Z helpexternal-macor-backtrace を確認します。

terminal
root@12c9fcfd5432:/usr/local/src# cargo -Z help

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 such as --registry
    -Z config-profile   -- Read profiles from .cargo/config files
    -Z timings          -- Display concurrency information
    -Z doctest-xcompile -- Compile and run doctests for non-host target using runner config

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

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

helpにはフラグの説明がありません。以下のURLにもそんなフラグはありません。

Cargo Unstable Features

環境変数で
RUSTFLAGS="-Z external-macro-backtrace"
として指定するようです。他のフラグはコマンドラインでいいのにこれだけこの扱いなのは謎です。展開されたマクロ式が確認できています。

root@12c9fcfd5432:/usr/local/src# RUSTFLAGS="-Z external-macro-backtrace" cargo check
    Checking samples v0.1.0 (/usr/local/src)
error[E0282]: type annotations needed for `std::vec::Vec<T>`
 --> <::alloc::macros::vec macros>:2:25
  |
1 | / ($ elem : expr ; $ n : expr) => ($ crate :: vec :: from_elem ($ elem, $ n)) ;
2 | | ($ ($ x : expr), *) => (< [_] > :: into_vec (box [$ ($ x), *])) ;
  | |                         ^^^^^^^^^^^^^^^^^^^ cannot infer type for `T`
3 | | ($ ($ x : expr,) *) => ($ crate :: vec ! [$ ($ x), *])
  | |______________________________________________________- in this expansion of `vec!`
  | 
 ::: src/main.rs:2:9
  |
2 |       let vec = vec![];
  |           ---   ------ in this macro invocation
  |           |
  |           consider giving `vec` the explicit type `std::vec::Vec<T>`, where the type parameter `T` is specified

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
error: could not compile `samples`.

To learn more, run the command again with --verbose.

ただし、確認が可能なのは、マクロに関連するエラー時のみのようです。確認できる場合は、エラーメッセージにnoteが出てる場合に限ると思われます。

便利なので本バージョンに入るといいですね。

参考ページ

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2