繁体   English   中英

如何使用 `rustc` 板条箱?

[英]How to use `rustc` crate?

我正在尝试在我的程序中使用rustc crate。

#[macro_use]
extern crate rustc;
extern crate rustc_typeck;
extern crate syntax;
extern crate syntax_pos;

use rustc::hir;

fn main() {
    println!("Hello, world!");

}                              

我还在Cargo.toml文件中添加了额外的依赖Cargo.toml

[dependencies]
log = "0.4.1"

cargo run会发出一堆错误,这些错误是私有的,并且只在夜间进行。

error: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)
 --> src/main.rs:2:1
  |
2 | extern crate rustc;
  | ^^^^^^^^^^^^^^^^^^^

Cargo 似乎想要一个来自crates.io的稳定板条箱,但我不知道我应该使用哪个板条箱。 rustc上没有名为rustccrates.io

这是我的 Rust 安装版本。

  • rustc 1.23.0 (766bd11c8 2018-01-01)
  • 货物 0.24.0 (45043115c 2017-12-05)

我使用rustup安装了它。

如何在我的程序中使用rustc crate?


更新

我将rustc = "1.23.0"添加到Cargo.toml ,但它仍然Cargo.toml此错误。

error: no matching package named `rustc` found (required by `rust-swift-serde-gen`)

rustc确实没有在 crates.io 上发布。

由于rustc crate 的 API 不稳定,您必须切换到夜间编译器并通过在 crate 根目录( main.rslib.rs )的开头添加以下行来选择加入:

#![feature(rustc_private)]

当然,由于 API 不稳定,每次更新夜间编译器时,事情可能会毫无征兆地崩溃!

方法一:使用 rustc-dev 组件

rustup 组件添加 rustc-dev

然后你可以使用

#![feature(rustc_private)]
extern crate rustc_ast;

方法二:使用 rustc-ap-rustc_* crates

根据rustc-auto-publish repo

crate rustc-ap-rustc_ast 与 rust 源代码中的 rustc_ast 相同

Racer, rust-analyzer 使用 rustc-ap-rustc_* crates

这是来自 Racer 的 Cargo.toml 的一些代码:

[dependencies.rustc_errors]
package = "rustc-ap-rustc_errors"
version = "712.0.0"

[dependencies.rustc_parse]
package = "rustc-ap-rustc_parse"
version = "712.0.0"

[dependencies.rustc_session]
package = "rustc-ap-rustc_session"
version = "712.0.0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM