Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

컴파일러 의존성에서의 cfg(bootstrap)

Rust 컴파일러가 사용하는 일부 외부 크레이트는 컴파일러 자체와 순환 의존성이 생길 수 있습니다. 컴파일러를 빌드하려면 업데이트된 크레이트가 필요하지만, 그 크레이트에는 업데이트된 컴파일러가 필요합니다. 이 페이지에서는 #[cfg(bootstrap)]을 사용하여 이 순환을 끊는 방법을 설명합니다.

#[cfg(bootstrap)] 활성화하기

일반적으로 외부 크레이트에서 #[cfg(bootstrap)]을 사용하면 경고가 발생합니다:

warning: unexpected `cfg` condition name: `bootstrap`
 --> src/main.rs:1:7
  |
1 | #[cfg(bootstrap)]
  |       ^^^^^^^^^
  |
  = help: expected names are: `docsrs`, `feature`, and `test` and 31 more
  = help: consider using a Cargo feature instead
  = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
           [lints.rust]
           unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bootstrap)'] }
  = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(bootstrap)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default

이 경고는 프로젝트의 Cargo.toml에 다음 줄을 추가하여 없앨 수 있습니다:

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(bootstrap)'] }

이제 컴파일러에서와 마찬가지로 크레이트에서도 #[cfg(bootstrap)]을 사용할 수 있습니다. 부트스트랩 컴파일러를 사용할 때는 #[cfg(bootstrap)]이 지정된 코드가 컴파일되고, 그렇지 않을 때는 #[cfg(not(bootstrap))]이 지정된 코드가 컴파일됩니다.

업데이트 절차

구체적인 예로, #[naked] 속성을 unsafe 속성으로 바꾸면서 compiler-builtins 크레이트와 순환 의존성이 생겼던 변경을 살펴보겠습니다.

1단계: 컴파일러에서 새 동작 허용하기 (#139797)

이 예에서는 오류를 비활성화하여 이전 동작과 새 동작을 동시에 허용할 수 있습니다.

2단계: 크레이트 업데이트하기 (#821)

이제 크레이트에서 이전 동작을 사용하려면 #[cfg(bootstrap)]을, 새 동작을 사용하려면 #[cfg(not(bootstrap))]을 사용하십시오.

3단계: 컴파일러가 사용하는 크레이트 버전 업데이트하기 (#139934)

compiler-builtins의 경우에는 버전을 올리는 것이었으며, 다른 경우에는 git 서브모듈 업데이트일 수 있습니다.

4단계: 컴파일러에서 이전 동작 제거하기 (#139753)

이제 업데이트된 크레이트를 사용할 수 있습니다. 이 예에서는 그 결과 이전 동작을 제거할 수 있게 되었습니다.