Rust Build Errors Guide
This document lists Rust-related build errors that are noteworthy in the context of Chromium builds.
[TOC]
Unsafe Rust
Chromium builds disallow unsafe Rust by default.
Unexpected unsafe Rust can cause build errors below:
error: usage of an `unsafe` block
error: declaration of an `unsafe` function
error: implementation of an `unsafe` trait
error: implementation of an `unsafe` method
...
note: requested on the command line with `-F unsafe-code`
unsafe Rust is disallowed by default to:
- Discourage using
unsafeRust code - Make code reviews easier (e.g.
//third_party/rustcrates withallow_unsafe = falsecan get a bit less scrutiny).
To fix the errors above you can either:
- Express the same code in safe Rust if possible
(e.g. using
slice[i]orslice.get(i)rather thanslice.get_unchecked(i), unless disassembly results show that the compiler cannot elide the checks and/or performance measurements show significant runtime difference) - Allow
unsafecode in the given crate (ideally only for a small, easy-to-reason-about crate that encapsulates the unsafety behind a safe public API).- In manually-authored
BUILD.gnfiles (e.g. in first-party code) you can setallow_unsafe = trueinrust_static_library - In
gnrt-generatedBUILD.gnfiles you can setextra_kv.allow_unsafeproperty totrueinthird_party/rust/chromium_crates_io/gnrt_config.toml(and then regenerateBUILD.gnwithtools/crates/run_gnrt.py gen).
- In manually-authored
Unstable features
Chromium builds require an explicit opt-in to use unstable Rust features
(see the policy in //tools/rust/unstable_rust_feature_usage.md).
Unexpected usage of unstable Rust features can cause build errors below:
error[E0725]: the feature `feature_name` is not in the list of allowed features
error[E0658]: use of unstable library feature `feature_name`
...
note: see issue #XXXXX <https://github.com/rust-lang/rust/issues/XXXXX> for more information
help: add `#![feature(feature_name)]` to the crate attributes to enable
To opt into allowing certain unstable features, you need to:
- Opt into allowing an unstable feature in the
BUILD.gnof a Rust crate (justifying edits to the policy in//tools/rust/unstable_rust_feature_usage.mdas needed)- In manually-authored
BUILD.gnfiles (e.g. in first-party code) you can set the following properties ofrust_static_library:rustflags = [ "-Zallow-features=feature_name" ]configs -= [ "//build/config/compiler:disallow_unstable_features" ]
- In
gnrt-generatedBUILD.gnfiles you can setextra_kv.allow_unstable_featuresproperty inthird_party/rust/chromium_crates_io/gnrt_config.tomlto the list of allowed feature names (and then regenerateBUILD.gnwithtools/crates/run_gnrt.py gen).
- In manually-authored
- Opt into allowing an unstable feature in the root module of a Rust crate.
To do this add
#![feature(feature_name)]tolib.rs. See also:- The brief
language reference documentation
of the
featureattribute - An example usage in the Unstable Book
- The brief
language reference documentation
of the