LoginSignup
4
1

More than 5 years have passed since last update.

Rust "recursion limit reached while expanding the macro `lazy_static`" でエラーになった場合の対処方法

Last updated at Posted at 2016-05-11

はじめに

タイトルの通りなのですが、Rustでコンパイル時にタイトルのエラーが出たときの回避方法です。

前提条件

項目 説明
検証日 2016.05.10
OS Mac OS X 10.11.4
Rust Stable 1.8.0

エラー

woothee-rust実装時にlazy_static.rsを使っていたのですが、
以下のようなコードを書いたときにコンパイルエラーが発生しました。

parser.rs
use std::collections::HashMap;
use regex::Regex;
use dataset;
use woothee::VALUE_UNKNOWN;

lazy_static! {
    static ref RX_CHROME_PATTERN: Regex = Regex::new(r"(?:Chrome|CrMo|CriOS)/([.0-9]+)").unwrap();
    static ref RX_DOCOMO_VERSION_PATTERN: Regex = Regex::new(r#"DoCoMo/[.0-9]+[ /]([^- /;()"']+)"#).unwrap();
    static ref RX_FIREFOX_PATTERN: Regex = Regex::new(r"Firefox/([.0-9]+)").unwrap();
    static ref RX_FIREFOX_OS_PATTERN: Regex = Regex::new(r"^Mozilla/[.0-9]+ \((?:Mobile|Tablet);(?:.*;)? rv:([.0-9]+)\) Gecko/[.0-9]+ Firefox/[.0-9]+$").unwrap();
    static ref RX_FIREFOX_IOS_PATTERN: Regex = Regex::new(r"FxiOS/([.0-9]+)").unwrap();
    static ref RX_FOMA_VERSION_PATTERN: Regex = Regex::new(r"\(([^;)]+);FOMA;").unwrap();
    static ref RX_HEADLINE_READER_PATTERN: Regex = Regex::new(r"(?i)headline-reader").unwrap();
            :
    static ref RX_MAYBE_CRAWLER_OTHER: Regex = Regex::new(r"(?:Rome Client |UnwindFetchor/|ia_archiver |Summify |PostRank/)").unwrap();
}

ファイル全体

以下コンパイルエラーの内容

$ cargo build --release
   Compiling woothee v0.2.0 (file:///Users/hattori-h/work/woothee-rust)
<lazy_static macros>:14:1: 14:67 error: recursion limit reached while expanding the macro `lazy_static`
<lazy_static macros>:14 lazy_static ! ( @ MAKE TY , $ VIS , $ ( # [ $ attr ] ) * , $ N ) ; impl $
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Could not compile `woothee`.

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

回避方法

#![recursion_limit="xxx"] を記述して再起呼び出し上限を変更して対処しました。woothee-rustでは100にしました。
(ざっくり調べてみたところデフォルト値はおそらく64です。)

おわりに

なぜ lazy_static!{} 内に大量の定義をすると再起上限に引っ掛かるのか理由はわかりませんでした。
小さなコードで rustc -Z unstable-options --pretty expanded example.rs を試してみてもよくわからず。
今後、詳細な理由がわかったら追記する予定です。とりあえずメモを残しておきます。

詳しい方いたら教えてください!!

4
1
2

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
4
1