为什么`cargo build`没有显示我代码中的所有错误?

6

这段代码无法编译:

extern crate iron;
#[marco_use] //misspelled here
extern crate mime;

use iron::prelude::*;
use iron::status;

fn main() {
    let mut response = Response::new();
    response.set_mut(mime!(Text/Html; Charset=Utf8));
}

它显示:

error: cannot find macro `mime!` in this scope
  --> src/main.rs:10:22
   |
10 |     response.set_mut(mime!(Text/Html; Charset=Utf8));
   |                      ^^^^

如果我添加extern crate hyper; use hyper::mime::*;,那么它会显示:
error: The attribute `marco_use` is currently unknown to the compiler and 
may have meaning added to it in the future (see issue #29642)
 --> src\main.rs:2:1
  |
2 | #[marco_use] extern crate mime;
  | ^^^^^^^^^^^^

如果我早些看到这个,就能帮助我修复错误了...
我猜Cargo只显示一个错误?我在网上找不到任何关于这种行为的信息。如何查看所有错误?

1
extern crate hyper; use hyper::mime::*; 添加到您的代码中,不会产生您在使用 Rust 1.23.0 时所述的第二个错误。您使用的是哪个版本的 Rust? - Shepmaster
@Shepmaster 我正在使用 Rustc 1.23.0 和 Cargo 0.24.0(在 Windows 上) - yassin
1个回答

9
编译过程分为几个阶段,如果在其中一个阶段出现错误导致构建失败,则不能继续进行下一阶段的处理。这不仅适用于Cargo,也适用于rustc(例如:何时将数字文字分配给默认类型?)。目前我没有看到官方文档中对此的说明,但是Japaric已经描述了高级过程(请参考此处):

enter image description here


那么在宏展开之前解析时,不应该找到拼写错误吗? - yassin
2
@yassin 为什么?这不是解析错误。对于编译器来说,mime!#[marco_use] 都是拼写错误,而不是语法错误。只是恰好宏通过示例展开在属性之前,因为它们可以扩展属性中。由于无法展开宏,编译器停止了。 - Shepmaster
你是怎么写的?你从哪里获取了所有这些信息? - Victor Polevoy
1
@VictorPolevoy 我没有写这个 - 这是由japaric编写的,描述在答案中链接。 - ljedrz
1
哦,我没能完全读懂你的信息。这张图片太惊人了,以至于我只读了第一句话。:D - Victor Polevoy

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