From e238ab9c3469959be9ea585aa54fbae5955cf526 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Fri, 22 Jan 2021 21:24:57 +0800 Subject: [PATCH 01/11] add 6 and create book --- .gitignore | 1 + README.md | 12 +- src/SUMMARY.md | 31 ++- src/codegen-options/index.md | 1 + src/json.md | 1 + src/lints/groups.md | 2 +- src/lints/listing/allowed-by-default.md | 267 ++++++++++++++---------- src/lints/listing/deny-by-default.md | 160 ++++++++++++++ src/lints/listing/warn-by-default.md | 162 ++++++++++++++ src/platform-support.md | 214 +++++++++++++++++++ 10 files changed, 727 insertions(+), 124 deletions(-) create mode 100644 src/codegen-options/index.md create mode 100644 src/json.md create mode 100644 src/lints/listing/deny-by-default.md create mode 100644 src/lints/listing/warn-by-default.md create mode 100644 src/platform-support.md diff --git a/.gitignore b/.gitignore index 7585238..c5d9ac3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ book +*.txt \ No newline at end of file diff --git a/README.md b/README.md index 6e150ae..ff56702 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,14 @@ # The-rustc-book-cn [The rustc book](https://doc.rust-lang.org/rustc/index.html) 的中文翻译 -原[仓库地址](https://github.com/rust-lang/rust/tree/master/src/doc/rustc) \ No newline at end of file +原[仓库地址](https://github.com/rust-lang/rust/tree/master/src/doc/rustc) + + + +本书阅读地址: + +[Github](https://1024chen.github.io/) + + + +国内访问加速: \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md index dd19861..a75ff67 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,22 +1,21 @@ # The Rustc Book -- [What is rustc?](what-is-rustc.md) -- [Command-line arguments](command-line-arguments.md) +- [什么是 rustc?](what-is-rustc.md) +- [命令行参数](command-line-arguments.md) - [Lints](lints/index.md) - - [Lint levels](lints/levels.md) - - [Lint Groups](lints/groups.md) - - [Lint listing](lints/listing/index.md) - - [Allowed-by-default lints](lints/listing/allowed-by-default.md) - - [Warn-by-default lints](lints/listing/warn-by-default.md) - - [Deny-by-default lints](lints/listing/deny-by-default.md) + - [Lint 级别](lints/levels.md) + - [Lint 组](lints/groups.md) + - [Lint 清单](lints/listing/index.md) + - [默认等级为允许的 lints](lints/listing/allowed-by-default.md) + - [默认等级为警告的 lints](lints/listing/warn-by-default.md) + - [默认等级为拒绝的 lints](lints/listing/deny-by-default.md) - [Codegen options](codegen-options/index.md) -- [JSON Output](json.md) -- [Platform Support](platform-support.md) +- [JSON 输出](json.md) +- [平台支持](platform-support.md) - [Targets](targets/index.md) - - [Built-in Targets](targets/built-in.md) - - [Custom Targets](targets/custom.md) - - [Known Issues](targets/known-issues.md) -- [Profile-guided Optimization](profile-guided-optimization.md) + - [内置 Targets](targets/built-in.md) + - [自定义 Targets](targets/custom.md) + - [已知问题](targets/known-issues.md) +- [PGO](profile-guided-optimization.md) - [Linker-plugin based LTO](linker-plugin-lto.md) -- [Exploit Mitigations](exploit-mitigations.md) -- [Contributing to `rustc`](contributing.md) +- [为 `rustc` 做贡献](contributing.md) diff --git a/src/codegen-options/index.md b/src/codegen-options/index.md new file mode 100644 index 0000000..4350db5 --- /dev/null +++ b/src/codegen-options/index.md @@ -0,0 +1 @@ +# Codegen options diff --git a/src/json.md b/src/json.md new file mode 100644 index 0000000..3c54f9a --- /dev/null +++ b/src/json.md @@ -0,0 +1 @@ +# JSON Output diff --git a/src/lints/groups.md b/src/lints/groups.md index 0372719..2dfc88d 100644 --- a/src/lints/groups.md +++ b/src/lints/groups.md @@ -16,7 +16,7 @@ $ rustc -D non-camel-case-types -D non-snake-case -D non-upper-case-globals | 组 | 描述 | 包含的lint | | :---------------------- | :------------------------------------------- | :----------------------------------------------------------- | -| warning | 所有设置为发出问题警告的 lint | 请查看本章 warn-by-default | +| warning | 所有设置为发出问题警告的 lint | 请查看本章 [warn-by-default](listing/warn-by-default.md) | | future-incompatible | 用来检测代码未来兼容性问题的 lint | [absolute-paths-not-starting-with-crate](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#absolute-paths-not-starting-with-crate), [ambiguous-associated-items](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#ambiguous-associated-items), [anonymous-parameters](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#anonymous-parameters), [array-into-iter](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#array-into-iter), [cenum-impl-drop-cast](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#cenum-impl-drop-cast), [coherence-leak-check](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#coherence-leak-check), [conflicting-repr-hints](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#conflicting-repr-hints), [const-evaluatable-unchecked](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#const-evaluatable-unchecked), [ill-formed-attribute-input](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#ill-formed-attribute-input), [illegal-floating-point-literal-pattern](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#illegal-floating-point-literal-pattern), [indirect-structural-match](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#indirect-structural-match), [invalid-type-param-default](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#invalid-type-param-default), [keyword-idents](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#keyword-idents), [late-bound-lifetime-arguments](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#late-bound-lifetime-arguments), [macro-expanded-macro-exports-accessed-by-absolute-paths](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#macro-expanded-macro-exports-accessed-by-absolute-paths), [missing-fragment-specifier](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#missing-fragment-specifier), [mutable-borrow-reservation-conflict](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#mutable-borrow-reservation-conflict), [nontrivial-structural-match](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#nontrivial-structural-match), [order-dependent-trait-objects](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#order-dependent-trait-objects), [patterns-in-fns-without-body](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#patterns-in-fns-without-body), [pointer-structural-match](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#pointer-structural-match), [private-in-public](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#private-in-public), [proc-macro-derive-resolution-fallback](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#proc-macro-derive-resolution-fallback), [pub-use-of-private-extern-crate](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#pub-use-of-private-extern-crate), [safe-packed-borrows](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#safe-packed-borrows), [soft-unstable](https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#soft-unstable), [tyvar-behind-raw-pointer](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#tyvar-behind-raw-pointer), [uninhabited-static](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#uninhabited-static), [unstable-name-collisions](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unstable-name-collisions), [where-clauses-object-safety](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#where-clauses-object-safety) | | nonstandard-style | 违反标准命令约定 | [non-camel-case-types](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#non-camel-case-types), [non-snake-case](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#non-snake-case), [non-upper-case-globals](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#non-upper-case-globals) | | rust-2018-compatibility | 用来将代码从 Rust 2015向 Rust 2018转移的lint | [absolute-paths-not-starting-with-crate](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#absolute-paths-not-starting-with-crate), [anonymous-parameters](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#anonymous-parameters), [keyword-idents](https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#keyword-idents), [tyvar-behind-raw-pointer](https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#tyvar-behind-raw-pointer) | diff --git a/src/lints/listing/allowed-by-default.md b/src/lints/listing/allowed-by-default.md index 4bfd70c..aabe3fa 100644 --- a/src/lints/listing/allowed-by-default.md +++ b/src/lints/listing/allowed-by-default.md @@ -2,11 +2,46 @@ 默认情况下,这些 lint 都设置为'allow'级别。因此,除非您使用标志或属性将它们设置为更高的 lint 级别,否则它们将不会显示。 ++ [`absolute_paths_not_starting_with_crate`](#absolute_paths_not_starting_with_crate) ++ [`anonymous_parameters`](#anonymous_parameters) ++ [`box_pointers`](#box_pointers) ++ [`elided_lifetimes_in_paths`](#elided_lifetimes_in_paths) ++ [`explicit_outlives_requirements`](#explicit_outlives_requirements) ++ [`invalid_html_tags`](#invalid_html_tags) ++ [`keyword_idents`](#keyword_idents) ++ [`macro_use_extern_crate`](#macro_use_extern_crate) ++ [`meta_variable_misuse`](#meta_variable_misuse) ++ [`missing_copy_implementations`](#missing_copy_implementations) ++ [`missing_crate_level_docs`](#missing_crate_level_docs) ++ [`missing_debug_implementations`](#missing_debug_implementations) ++ [`missing_doc_code_examples`](#missing_doc_code_examples) ++ [`missing_docs`](#missing_docs) ++ [`non_ascii_idents`](#non_ascii_idents) ++ [`pointer_structural_match`](#pointer_structural_match) ++ [`private_doc_tests`](#private_doc_tests) ++ [`single_use_lifetimes`](#single_use_lifetimes) ++ [`trivial_casts`](#trivial_casts) ++ [`trivial_numeric_casts`](#trivial_numeric_casts) ++ [`unaligned_references`](#unaligned_references) ++ [`unreachable_pub`](#unreachable_pub) ++ [`unsafe_code`](#unsafe_code) ++ [`unsafe_op_in_unsafe_fn`](#unsafe_op_in_unsafe_fn) ++ [`unstable_features`](#unstable_features) ++ [`unused_crate_dependencies`](#unused_crate_dependencies) ++ [`unused_extern_crates`](#unused_extern_crates) ++ [`unused_import_braces`](#unused_import_braces) ++ [`unused_lifetimes`](#unused_lifetimes) ++ [`unused_qualifications`](#unused_qualifications) ++ [`unused_results`](#unused_results) ++ [`variant_size_differences`](#variant_size_differences) + + + ## absolute_paths_not_starting_with_crate `absolute_paths_not_starting_with_crate` lint 检测完全合乎“路径以模块名称开头”的这个要求,即以 `crate` , `self` ,或是以一个 extern crate 名称作为开头。
-**样例** +**样例** ```rust #![deny(absolute_paths_not_starting_with_crate)] @@ -36,7 +71,7 @@ note: the lint level is defined here = note: for more information, see issue #53130
``` -**解释** +**解释** Rust [语义版本][edition_guide]允许语言向前发展而不破坏其向后兼容性(译者注:向后兼容指的是新版本可以运行旧代码)。此 lint 捕获使用 2015语义版本路径样式的代码。在2015语义版本中,绝对路径(以 :: 开头)指的是(本 crate 的) crate 根 或者是外部 crate 。应该使用 `crate::`路径前缀指向来自 crate 根的项。 @@ -48,7 +83,7 @@ Rust [语义版本][edition_guide]允许语言向前发展而不破坏其向后 此 lint 检测匿名参数。一些触发此 lint 的示例代码:
-**样例** +**样例** ```rust #![deny(anonymous_parameters)] @@ -78,7 +113,7 @@ note: the lint level is defined here = note: for more information, see issue #41686 ```
-**解释** +**解释** 这种语法大多是历史意外,可以通过添加 `_` 模式(译者注:通配符)或描述性标识符很轻松地解决: @@ -93,13 +128,16 @@ trait Foo { `box_pointers` lint 用于 Box 类型。
-**样例** +**样例** ```rust +# #![allow(unused)] #![deny(box_pointers)] +# fn main() { struct Foo { x: Box, } +# } ``` 显示如下结果: @@ -118,26 +156,31 @@ note: the lint level is defined here | ^^^^^^^^^^^^ ``` -
-**解释** + +**解释** 这种 lint 主要是历史性的,并不是特别有用。以前 `Box`是用于构建语言,以及进行堆分配的唯一方法。今天的 Rust 可以调用其他堆分配器等。 + + ## elided-lifetimes-in-paths `elided_lifetimes_in_paths` lint 用于检测隐藏生命周期参数。 -
-**样例** + +**样例** ```rust +# #![allow(unused)] #![deny(elided_lifetimes_in_paths)] +# fn main() { struct Foo<'a> { x: &'a u32 } fn foo(x: &Foo) { } +# } ``` 显示如下结果: ```text @@ -154,8 +197,8 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** + +**解释** 省略的生命周期参数可能使您一眼就看不到发生了借用。该 lint 确保生命周期参数总是被显式指出,即便是`_` [占位符的生命周期](https://doc.rust-lang.org/reference/lifetime-elision.html#lifetime-elision-in-functions)。 @@ -165,9 +208,13 @@ note: the lint level is defined here `explicit_outlives_requirements` lint 检测指出不必要的生命周期约束(bounds)。
-**样例** +**样例** ```rust + +# #![allow(unused)] +# fn main() { +# #![allow(unused)] #![deny(explicit_outlives_requirements)] struct SharedRef<'a, T> @@ -176,6 +223,7 @@ where { data: &'a T, } +# } ``` 显示如下结果: ```text @@ -195,8 +243,8 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** + +**解释** 如果结构体包含引用,例如 `&'a T`,编译器要求 `T` 的生命周期长度比 `'a` 更久。以往要求写上明确的生命周期约束( bound )来此满足此条件。然而,这可能过于明显,且会导致混乱和不必要的复杂性。语言已经改进为如果没有指定则会自动推断约束。具体举例来说,如果结构体包含引用,直接或间接地包含 `T` 和生命周期 `'x` ,则它会自动推断要求(requirement) `T: 'x`。 @@ -207,13 +255,16 @@ note: the lint level is defined here ## keyword-idents `keyword-idents` lint 检测被用作标识符的版本关键字。 -
-**样例** + +**样例** ```rust -#![deny(keyword_idents)] -// edition 2015 +# #![allow(unused)] +#![deny(keyword_idents)] +# fn main() { +// edition 2015 fn dyn() {} +# } ``` 显示如下结果: ```text @@ -232,8 +283,8 @@ note: the lint level is defined here = note: for more information, see issue #49716 ``` -
-**解释** + +**解释** Rust [语义版本][edition_guide]允许语言向前发展而不破坏其向后兼容性。此 lint 捕获代码中的被用作标识符(例如变量名、函数名等等)的新增关键。如果你没有更新代码就切换编译器到一个新语义版本,就会在你将新关键字作为标识符的情况下编译失败。 @@ -243,8 +294,8 @@ Rust [语义版本][edition_guide]允许语言向前发展而不破坏其向后 ## macro-use-extern-crate `macro-use-extern-crate` lint 用于检测 [`macro_use`](https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute)属性的使用 -
-**样例** + +**样例** ```rust #![deny(macro_use_extern_crate)] @@ -272,15 +323,15 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** + +**解释** [`macro_use`](https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute)属性放在 `extern crate` 项上使其宏可被使用,而这个外部 crate 可能会被放进该 crate 的路径前缀,导致导入宏在作用域内无处不在。在 [2018 版本][2018 edition]中致力于简化依赖项的处理,`extern crate` 的使用已经淘汰了。要将宏从外部 crate 导入作用域,建议使用 [`use`](https://doc.rust-lang.org/reference/items/use-declarations.html) 导入。 ## meta_variable_misuse `meta_variable_misuse` lint 检测宏定义中可能存在的元变量滥用。 -
-**样例** + +**样例** ```rust #![deny(meta_variable_misuse)] @@ -309,8 +360,8 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** + +**解释** [`macro_rules`](https://doc.rust-lang.org/reference/macros-by-example.html)宏有许多不恰当的定义方式,这些错误以前只有在宏被展开或根本不( not at all )展开时才能才能检测得到。该 lint 尝试在当定义了宏的时候捕获一些问题。 该 lint 默认等级是 “allow”的,因为其有误报或其他问题。更多细节请参阅 [issue #61053](https://github.com/rust-lang/rust/issues/61053)。 @@ -318,14 +369,15 @@ note: the lint level is defined here ## missing_copy_implementations `missing_copy_implementations` lint 检测潜在的忘记实现 [`Copy`][Copy trait] trait。 -
-**样例** + +**样例** ```rust #![deny(missing_copy_implementations)] pub struct Foo { pub field: i32 } +# fn main() {} ``` 显示如下: ```text @@ -344,8 +396,8 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** + +**解释** 1.0版本以前,类型会被尽可能自动标记为 `Copy`。后面对此进行了更改,并要求实现 `Cpoy` trait 来明确选择添加此 trait。此更改的一部分内容是,如果未为一个可复制类型标记 `Copy`,一个 lint 会发出警告。 @@ -355,13 +407,14 @@ note: the lint level is defined here `missing_crate_level_docs` lint 检测 crate 根是否缺失其文档。这是一个仅用于 `rustdoc` 的 lint,请参阅 [rustdoc book](https://doc.rust-lang.org/rustdoc/lints.html#invalid_html_tags)中的文档。 ## missing_debug_implementations -`missing_debug_implementations` lint 检测 `fmt::Debug` 的缺失。 -
-**样例** +`missing_debug_implementations` lint 检测 [`fmt::Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html) 的缺失。 + +**样例** ```rust #![deny(missing_debug_implementations)] pub struct Foo; +# fn main() {} ``` 显示如下: ```test @@ -378,11 +431,10 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -
-**解释** -在类型上实现 `Debug` 有助于调试。因为它提供了个格式化和显示值的便捷方法。 使用 `#[derive(Debug)]` 属性会自动生成一个典型实现,或者手动实现该 `Debug` trait 添加自定义实现。 +**解释** +在类型上实现 `Debug` 有助于调试。因为它提供了个格式化和显示值的便捷方法。 使用 `#[derive(Debug)]` 属性会自动生成一个典型实现,或者手动实现该 `Debug` trait 添加自定义实现。 该 lint 默认等级为 “allow” ,因为添加 `Debug` 到所有类型可能会对编译时长和代码体积产生负面作用。它还要求对每种类型都添加样板,这有时会是种(编码上的)阻碍。 @@ -396,13 +448,14 @@ note: the lint level is defined here ## missing_docs `missing_docs` lint 检测缺失文档的公有项目。(译者注:`missing_docs` 与 `missing_doc_code_examples` ,一个检测有没有,一个检测文档有没有代码样例) -
- -**样例** +**样例** ```rust +# #![allow(unused)] #![deny(missing_docs)] +# fn main() { pub fn foo() {} +# } ``` 显示如下: ```text @@ -422,7 +475,7 @@ note: the lint level is defined here | ^^^^^^^^^^^^ ``` -**解释** +**解释** 该 lint 旨在确保一个库有良好的文档记录。没有文档的项目对于用户来说很难理解如何正确使用。 该lint 默认等级是 “allow”是因为其可能会造成干扰,并且不是所有项目都需要强制将一切用文档记录。 @@ -430,11 +483,10 @@ note: the lint level is defined here ## non_ascii_idents `non_ascii_idents` lint 检测非 ascii 标识符。 -
- -**样例** +**样例** ```rust +# #![allow(unused)] #![feature(non_ascii_idents)] #![deny(non_ascii_idents)] fn main() { @@ -456,18 +508,15 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^ ``` -
-**解释** +**解释** 在稳定版的 Rust 上,标识符必须包含 ASCII 字符。 `non_ascii_idents` 只在 nightly feature 允许标识符包含非 ASCII 字符。该 lint 允许项目希望切换该 lint 等级为 “forbid” 以保持只使用 ASCII 字符的限制(例如,简化协作或是为了安全)。更多细节请参阅 [RFC 2457](https://github.com/rust-lang/rfcs/blob/master/text/2457-non-ascii-idents.md)。 ## pointer_structural_match `pointer_structural_match` lint 检测那些在不同编译器版本和优化级别依赖上不能用于模式中的指针。 -
- -**样例** +**样例** ```rust #![deny(pointer_structural_match)] @@ -498,9 +547,7 @@ note: the lint level is defined here = note: for more information, see issue #62411 ``` -
- -**解释** +**解释** 早期 Rust 版本允许在模式中使用函数指针和泛(wide)原始指针。尽管许多情况下可以按用户期望的方式运行,但由于编译器进行优化,在运行时,指针可能已经 “不等于自身” 或者是指向不同函数的函数指针相等。这是因为如果函数体相等, LLVM 会优化掉重复函数(译者注:即保留一个),因此也会使得这些指向他们的函数指针指向同一位置。另外,如果重复的函数在不同 crate 中,且又没有通过 LTO 进行优化(删除相同代码数据),那么就会造成重复。 @@ -509,15 +556,15 @@ note: the lint level is defined here ## single_use_lifetimes `single_use_lifetimes` lint 检测只使用一次的生命期。 - -
- -**样例** +**样例** ```rust +# #![allow(unused)] #![deny(single_use_lifetimes)] +# fn main() { fn foo<'a>(x: &'a u32) {} +# } ``` 显示如下: ```text @@ -540,9 +587,8 @@ help: elide the single-use lifetime | -- -- ``` -
-**解释** +**解释** 显式指定一个生命周期,例如在函数或 `impl` 中的 `'a`应该用来链接这两者。否则,应该使用 `'_` 表明生命周期并未链接到两者,或者如果有可能的话干脆直接省略生命周期。 @@ -553,14 +599,16 @@ help: elide the single-use lifetime ## trivial_casts `trivial_casts` lint 检测可以被强制类型转换替代的平凡类型转换,这可能需要[类型归因](https://github.com/rust-lang/rust/issues/23416)(type ascription)或临时变量。 -
- -**样例** +**样例** ```rust + +# #![allow(unused)] #![deny(trivial_casts)] +# fn main() { let x: &u32 = &42; let y = x as *const u32; +# } ``` 显示如下: ```text @@ -578,9 +626,8 @@ note: the lint level is defined here = help: cast can be replaced by coercion; this might require a temporary variable ``` -
-**解释** +**解释** 平凡类型转换是一种 `e` 含有 `U` 类型,而且 `U` 是 `T` 的一个子类型的 `e as T` 转换。这种类型转换通常是不必要的,其通常可以被推断出来。 @@ -589,13 +636,14 @@ note: the lint level is defined here ## trivial_numeric_casts `trivial_numeric_casts` lint 检测可能已经被移除的平凡数值类型转换。 -
- -**样例** +**样例** ```rust +# #![allow(unused)] #![deny(trivial_numeric_casts)] +# fn main() { let x = 42_i32 as i32; +# } ``` 显示如下: ```text @@ -612,20 +660,17 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^ = help: cast can be replaced by coercion; this might require a temporary variable ``` -
-**解释** +**解释** 平凡数值类型转换指的是将数值类型转换为相同数值类型的转换。这种转换通常是不必要的。 该 lint 默认等级为 “allow” ,因为在某些情况下,例如 FFI 接口或者是 复杂类型别名,可能会被不正确地触发,或者是在一些难以表达清楚意图的情况下。在未来它可能会成为一个 警告(warning),可能会因为类型归因提供了一种解决当前问题的方法。历史内容请参阅 [RFC 401](https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md) ## unaligned_references -`unaligned_references` lint 检测对包装结构体字段的未对其引用。 +`unaligned_references` lint 检测对包装结构体字段的未对齐引用。 -
- -**样例** +**样例** ```rust #![deny(unaligned_references)] @@ -659,9 +704,8 @@ note: the lint level is defined here = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) ``` -
-**解释** +**解释** 创建对未充分对其的包装字段的引用是一种[未定义的行为](https://doc.rust-lang.org/reference/behavior-considered-undefined.html)并且应该被禁止。 默认情况下,此 lint 等级为 “allow” ,因为没有稳定的替代方法,并且尚不确定现有代码将如何触发此 lint 。有关更多讨论请参阅 [issue #27060](https://github.com/rust-lang/rust/issues/27060) 。 @@ -669,17 +713,18 @@ note: the lint level is defined here ## unreachable_pub `unreachable_pub` lint 被无法从 crate 根到达的 pub 项所触发。 -
- -**样例** +**样例** ```rust +# #![allow(unused)] #![deny(unreachable_pub)] +# fn main() { mod foo { pub mod bar { } } +# } ``` 显示如下: ```text @@ -699,20 +744,17 @@ note: the lint level is defined here = help: or consider exporting it for use by other crates ``` -
-**解释** +**解释** -一个裸( bare ) pub 项的可见性可能会因为该项无法从 crate 导出而被误导。该 `pub(crate)` 可见性建议用可见性仅在其自身 crate 这种清晰的表达来替代。 +一个裸( bare ) `pub` 项的可见性可能会因为该项无法从 crate 导出而被误导。该 `pub(crate)` 可见性建议用可见性仅在其自身 crate 这种清晰的表达来替代。 默认情况下,此 lint 等级为 “allow” ,因为它会被大量现有的 Rust 代码触发,并且会有一些误报。最终我们希望它成为一个默认警告。 ## unsafe_code `unsafe_code` lint 捕捉 `unsafe` 代码的使用。 -
- -**样例** +**样例** ```rust #![deny(unsafe_code)] @@ -739,18 +781,15 @@ note: the lint level is defined here | ^^^^^^^^^^^ ``` -
-**解释** +**解释** 该 lint 意在限制 `unsafe` 的使用,这很难被正确使用。(译者注:此处的“很难被正确使用”,一者指的是 unsafe 代码的不安全操作,一者指的是对 unsafe 代码的严格限制 lint 很难说是正确的)。 ## unsafe_op_in_unsafe_fn `unsafe_op_in_unsafe_fn` lint 检测非 unsafe 块中 unsafe 函数中的 unsafe 操作。该 lint 仅在 [nightly 通道](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html)( nightly channel )中使用`#![feature(unsafe_block_in_unsafe_fn)]`时有效。 -
- -**样例** +**样例** ```rust #![feature(unsafe_block_in_unsafe_fn)] @@ -781,25 +820,27 @@ note: the lint level is defined here = note: consult the function's documentation for information on how to avoid undefined behavior ``` -
-**解释** +**解释** -当前,unsafe 函数允许在其中进行任何的 unsafe 操作。然而,这可能会因为需要对代码行为进行适当仔细的检查而增加代码体积。 `unsafe` 块提供了一种简便的,可以清楚说明代码的哪部分正在进行 unsafe 操作。在未来,我们希望修改它以便不能在一个非 unsafe 块的 `unsafe 函数`中执行 unsafe 操作。 +当前,[`unsafe`](https://doc.rust-lang.org/reference/unsafe-functions.html) 函数允许在其中进行任何的 [unsafe 操作][unsafe operate]。然而,这可能会因为需要对代码行为进行适当仔细的检查而增加代码体积。 [`unsafe` 块][unsafe block] 提供了一种简便的,可以清楚说明代码的哪部分正在进行 unsafe 操作。在未来,我们希望修改它以便不能在一个非 unsafe 块的 `unsafe 函数`中执行 unsafe 操作。 解决此问题的方法是将将此 unsafe 代码包装进 unsafe 块中。 该 lint 默认等级为 “allow” ,因为其尚未稳定,也尚未完成。更多细节请参阅 [RFC #2585](https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md) 和 [issue #71668](https://github.com/rust-lang/rust/issues/71668)。 +[unsafe block]:https://doc.rust-lang.org/reference/expressions/block-expr.html#unsafe-blocks +[unsafe operate]:https://doc.rust-lang.org/reference/unsafety.html + ## unstable_features `unstable_features` lint 已被废弃,不应再使用。 ## unused_crate_dependencies `unused_crate_dependencies` lint 检测未被使用的 crate 依赖。 -**样例** +**样例** -```rust +```rust,ignore #![deny(unused_crate_dependencies)] ``` 显示如下: @@ -813,7 +854,8 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -**解释** + +**解释** 将使用了依赖项的代码移除之后,通常需要从构建配置中删除依赖。然而,有时可能会忘记这一步,导致浪费时间来构建不再使用的依赖项。该 lint 可以被用来检测从未使用的依赖项(更具体地说,那些从未被 [`use`](https://doc.rust-lang.org/reference/items/use-declarations.html) , [`extern crate`](https://doc.rust-lang.org/reference/items/extern-crates.html) ,或者任何[路径](https://doc.rust-lang.org/reference/paths.html)指向的,通过 `--extern`命令行标签指定的依赖) @@ -822,11 +864,14 @@ note: the lint level is defined here ## unused_extern_crates `unused_extern_crates` lint 防止从未被使用的 `extern crate` 项。 -**样例** +**样例** ```rust +# #![allow(unused)] #![deny(unused_extern_crates)] +# fn main() { extern crate proc_macro; +# } ``` 显示如下: ```text @@ -843,7 +888,7 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^ ``` -**解释** +**解释** 未使用的 `extern crate` 项是无效的应该被删除。请注意,在某些情况下,需要指定 `extern crate` 以确保他们被 crate 所链接,即使没有直接引用它。可以通过为 crate 取一个下划线别名来消除检测,例如 `extern crate foo as _`。还要注意的是 `extern crate` 在 [2018 语义版本](https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html#no-more-extern-crate)中已经不常用,因为现在外部 crate(译者注:被指定的)会被自动添加到域中。 @@ -852,7 +897,7 @@ note: the lint level is defined here ## unused_import_braces `unused_import_braces` lint 捕捉导入项中不必要的大括号。 -**样例** +**样例** ```rust #![deny(unused_import_braces)] @@ -861,6 +906,7 @@ use test::{A}; pub mod test { pub struct A; } +# fn main() {} ``` 显示如下: ```text @@ -877,7 +923,7 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^ ``` -**解释** +**解释** 如果仅有单个项,应该移除大括号(例如 `use test::A;`)。 @@ -886,12 +932,16 @@ note: the lint level is defined here ## unused_lifetimes `unused_lifetimes` lint 检测未使用的生命周期参数。 -**样例** +**样例** ```rust +# #![allow(unused)] +# fn main() { #[deny(unused_lifetimes)] pub fn foo<'a>() {} +# } + ``` 显示如下: ```text @@ -907,14 +957,14 @@ note: the lint level is defined here 2 | #[deny(unused_lifetimes)] | ^^^^^^^^^^^^^^^^ ``` -**解释** +**解释** 未使用的生命周期参数可能是个错误或是代码未完成。(译者注:如果是错误)应该考虑删除该参数 ## unused_qualifications `unused_qualifications` lint 检测不必要的限定名。 -**样例** +**样例** ```rust #![deny(unused_qualifications)] @@ -942,7 +992,7 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^ ``` -**解释** +**解释** 如果来自另一个模块的项已经被导入了这个域,在这种情况下无需为该项加限定名,你可以不加 `foo::` 直接调用 `bar()`。 @@ -951,7 +1001,7 @@ note: the lint level is defined here ## unused_results `unused_results` lint 检查语句中表达式未使用的 result 。 -**样例** +**样例** ```rust #![deny(unused_results)] @@ -975,7 +1025,7 @@ note: the lint level is defined here 1 | #![deny(unused_results)] | ^^^^^^^^^^^^^^ ``` -**解释** +**解释** 忽略的函数返回值可能会指出一个错误。在可以确定使用 result 的情况下推荐使用 [`must_use`](https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute)属性来注解函数。如果不使用此类返回值将会触发默认为警告级别的 `unused_must_use` lint。`unused_results` lint 本质上是一样的,但是其被所有的返回值触发。 @@ -983,14 +1033,18 @@ note: the lint level is defined here ## variant_size_differences `variant_size_differences` lint 检测具有不同变量大小的枚举。 -**样例** + +**样例** ```rust +# #![allow(unused)] #![deny(variant_size_differences)] +# fn main() { enum En { V0(u8), VBig([u8; 1024]), } +# } ``` 显示如下: ```text @@ -1007,6 +1061,7 @@ note: the lint level is defined here | ^^^^^^^^^^^^^^^^^^^^^^^^ ``` + **解释** 向枚举中添加一个比其他变量大得多的变量可能是个错误,这会增加所有变量所需空间的总大小。这可能会影响性能和内存使用。如果第一大的变量比第二大的变量所需空间大三倍以上,就会触发这个 lint。 diff --git a/src/lints/listing/deny-by-default.md b/src/lints/listing/deny-by-default.md new file mode 100644 index 0000000..9c1aa5d --- /dev/null +++ b/src/lints/listing/deny-by-default.md @@ -0,0 +1,160 @@ +# 默认等级为拒绝的 lints + +默认情况下,这些 lint 都设置为 'deny' 级别。 + +- [`ambiguous_associated_items`](#ambiguous_associated_items) +- [`arithmetic_overflow`](#arithmetic_overflow) +- [`conflicting_repr_hints`](#conflicting_repr_hints) +- [`const_err`](#const_err) +- [`ill_formed_attribute_input`](#ill_formed_attribute_input) +- [`incomplete_include`](#incomplete_include) +- [`invalid_type_param_default`](#invalid_type_param_default) +- [`macro_expanded_macro_exports_accessed_by_absolute_paths`](#macro_expanded_macro_exports_accessed_by_absolute_paths) +- [`missing_fragment_specifier`](#missing_fragment_specifier) +- [`mutable_transmutes`](#mutable_transmutes) +- [`no_mangle_const_items`](#no_mangle_const_items) +- [`order_dependent_trait_objects`](#order_dependent_trait_objects) +- [`overflowing_literals`](#overflowing_literals) +- [`patterns_in_fns_without_body`](#patterns_in_fns_without_body) +- [`pub_use_of_private_extern_crate`](#pub_use_of_private_extern_crate) +- [`soft_unstable`](#soft_unstable) +- [`unconditional_panic`](#unconditional_panic) +- [`unknown_crate_types`](#unknown_crate_types) +- [`useless_deprecated`](#useless_deprecated) + + + +## ambiguous_associated_items + +`ambiguous_associated_items` lint 检测枚举变量和关联项之间的模糊不清。 + + + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +enum E { + V +} + +trait Tr { + type V; + fn foo() -> Self::V; +} + +impl Tr for E { + type V = u8; + // `Self::V` is ambiguous because it may refer to the associated type or + // the enum variant. + fn foo() -> Self::V { 0 } +} +# } +``` + +显示如下: + +```text +error: ambiguous associated item + --> lint_example.rs:15:17 + | +15 | fn foo() -> Self::V { 0 } + | ^^^^^^^ help: use fully-qualified syntax: `::V` + | + = note: `#[deny(ambiguous_associated_items)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #57644 +note: `V` could refer to the variant defined here + --> lint_example.rs:3:5 + | +3 | V + | ^ +note: `V` could also refer to the associated type defined here + --> lint_example.rs:7:5 + | +7 | type V; + | ^^^^^^^ + +``` + + + +**解释** + + + + + +## arithmetic_overflow + + + + + +## conflicting_repr_hints + + + +## const_err + + + +## ill_formed_attribute_input + + + +## incomplete_include + + + +## invalid_type_param_default + + + +## macro_expanded_macro_exports_accessed_by_absolute_paths + + + +## missing_fragment_specifier + + + +## mutable_transmutes + + + +## no_mangle_const_items + + + +## order_dependent_trait_objects + + + +## overflowing_literals + + + +## patterns_in_fns_without_body + + + +## pub_use_of_private_extern_crate + + + +## soft_unstable + + + +## unconditional_panic + + + +## unknown_crate_types + + + +## useless_deprecated + diff --git a/src/lints/listing/warn-by-default.md b/src/lints/listing/warn-by-default.md new file mode 100644 index 0000000..c65bf90 --- /dev/null +++ b/src/lints/listing/warn-by-default.md @@ -0,0 +1,162 @@ +# Warn-by-default lints + +(译者注:本章尚未翻译) + +这些 lint 的默认等级被设置为 “警告”。 + ++ [`array_into_iter`](#array_into_iter) ++ [`asm_sub_register`](#asm_sub_register) ++ [`bare_trait_objects`](#bare_trait_objects) ++ [`bindings_with_variant_name`](#bindings_with_variant_name) ++ [`broken_intra_doc_links`](#broken_intra_doc_links) ++ [`cenum_impl_drop_cast`](#cenum_impl_drop_cast) ++ [`clashing_extern_declarations`](#clashing_extern_declarations) ++ [`coherence_leak_check`](#coherence_leak_check) ++ [`confusable_idents`](#confusable_idents) ++ [`const_evaluatable_unchecked`](#const_evaluatable_unchecked) ++ [`const_item_mutation`](#const_item_mutation) ++ [`dead_code`](#dead_code) ++ [`deprecated`](#deprecated) ++ [`drop_bounds`](#drop_bounds) ++ [`ellipsis_inclusive_range_patterns`](#ellipsis_inclusive_range_patterns) ++ [`exported_private_dependencies`](#exported_private_dependencies) ++ [`function_item_references`](#function_item_references) ++ [`illegal_floating_point_literal_pattern`](#illegal_floating_point_literal_pattern) ++ [`improper_ctypes`](#improper_ctypes) ++ [`improper_ctypes_definitions`](#improper_ctypes_definitions) ++ [`incomplete_features`](#incomplete_features) ++ [`indirect_structural_match`](#indirect_structural_match) ++ [`inline_no_sanitize`](#inline_no_sanitize) ++ [`invalid_codeblock_attributes`](#invalid_codeblock_attributes) ++ [`invalid_value`](#invalid_value) ++ [`irrefutable_let_patterns`](#irrefutable_let_patterns) ++ [`late_bound_lifetime_arguments`](#late_bound_lifetime_arguments) ++ [`mixed_script_confusables`](#mixed_script_confusables) ++ [`mutable_borrow_reservation_conflict`](#mutable_borrow_reservation_conflict) ++ [`no_mangle_generic_items`](#no_mangle_generic_items) ++ [`non_autolinks`](#non_autolinks) ++ [`non_camel_case_types`](#non_camel_case_types) ++ [`non_shorthand_field_patterns`](#non_shorthand_field_patterns) ++ [`non_snake_case`](#non_snake_case) ++ [`non_upper_case_globals`](#non_upper_case_globals) ++ [`nontrivial_structural_match`](#nontrivial_structural_match) ++ [`overlapping_patterns`](#overlapping_patterns) ++ [`path_statements`](#path_statements) ++ [`private_in_public`](#private_in_public) ++ [`private_intra_doc_links`](#private_intra_doc_links) ++ [`proc_macro_derive_resolution_fallback`](#proc_macro_derive_resolution_fallback) ++ [`redundant_semicolons`](#redundant_semicolons) ++ [`renamed_and_removed_lints`](#renamed_and_removed_lints) ++ [`safe_packed_borrows`](#safe_packed_borrows) ++ [`stable_features`](#stable_features) ++ [`temporary_cstring_as_ptr`](#temporary_cstring_as_ptr) ++ [`trivial_bounds`](#trivial_bounds) ++ [`type_alias_bounds`](#type_alias_bounds) ++ [`tyvar_behind_raw_pointer`](#tyvar_behind_raw_pointer) ++ [`uncommon_codepoints`](#uncommon_codepoints) ++ [`unconditional_recursion`](#unconditional_recursion) ++ [`uninhabited_static`](#uninhabited_static) ++ [`unknown_lints`](#unknown_lints) ++ [`unnameable_test_items`](#unnameable_test_items) ++ [`unreachable_code`](#unreachable_code) ++ [`unreachable_patterns`](#unreachable_patterns) ++ [`unstable_name_collisions`](#unstable_name_collisions) ++ [`unused_allocation`](#unused_allocation) ++ [`unused_assignments`](#unused_assignments) ++ [`unused_attributes`](#unused_attributes) ++ [`unused_braces`](#unused_braces) ++ [`unused_comparisons`](#unused_comparisons) ++ [`unused_doc_comments`](#unused_doc_comments) ++ [`unused_features`](#unused_features) ++ [`unused_imports`](#unused_imports) ++ [`unused_labels`](#unused_labels) ++ [`unused_macros`](#unused_macros) ++ [`unused_must_use`](#unused_must_use) ++ [`unused_mut`](#unused_mut) ++ [`unused_parens`](#unused_parens) ++ [`unused_unsafe`](#unused_unsafe) ++ [`unused_variables`](#unused_variables) ++ [`warnings`](#warnings) ++ [`where_clauses_object_safety`](#where_clauses_object_safety) ++ [`while_true`](#while_true) + + + +## array_into_iter + +## asm_sub_register + +## bare_trait_objects + +## bindings_with_variant_name +## broken_intra_doc_links +## cenum_impl_drop_cast +## clashing_extern_declarations +## coherence_leak_check +## confusable_idents +## const_evaluatable_unchecked +## const_item_mutation +## dead_code +## deprecated +## drop_bounds +## ellipsis_inclusive_range_patterns +## exported_private_dependencies +## function_item_references +## illegal_floating_point_literal_pattern +## improper_ctypes +## improper_ctypes_definitions +## incomplete_features +## indirect_structural_match +## inline_no_sanitize +## invalid_codeblock_attributes +## invalid_value +## irrefutable_let_patterns +## late_bound_lifetime_arguments +## mixed_script_confusables +## mutable_borrow_reservation_conflict +## no_mangle_generic_items +## non_autolinks +## non_camel_case_types +## non_shorthand_field_patterns +## non_snake_case +## non_upper_case_globals +## nontrivial_structural_match +## overlapping_patterns +## path_statements +## private_in_public +## private_intra_doc_links +## proc_macro_derive_resolution_fallback +## redundant_semicolons +## renamed_and_removed_lints +## safe_packed_borrows +## stable_features +## temporary_cstring_as_ptr +## trivial_bounds +## type_alias_bounds +## tyvar_behind_raw_pointer +## uncommon_codepoints +## unconditional_recursion +## uninhabited_static +## unknown_lints +## unnameable_test_items +## unreachable_code +## unreachable_patterns +## unstable_name_collisions +## unused_allocation +## unused_assignments +## unused_attributes +## unused_braces +## unused_comparisons +## unused_doc_comments +## unused_features +## unused_imports +## unused_labels +## unused_macros +## unused_must_use +## unused_mut +## unused_parens +## unused_unsafe +## unused_variables +## warnings +## where_clauses_object_safety +## while_true \ No newline at end of file diff --git a/src/platform-support.md b/src/platform-support.md new file mode 100644 index 0000000..e7de4be --- /dev/null +++ b/src/platform-support.md @@ -0,0 +1,214 @@ +# 平台支持 + + + +对不同平台的支持被分成三层,每一层都有不同的保证集。 + +平台由他们的 “目标三元组” ("target triple") 标识,“目标三元组”是一个用来通知编译器应该产生何种输出的字符串。下表中的列具有以下含义: + +* std: + * ✓ 表明完整的标准库可用。 + * \* 表明目标仅支持 [`no_std`] 开发。 + * ? 表明标准库支持未知或者支持工作正在开展。 +* host: ✓ 表明 `rustc` 和 `cargo` 都能够在主机平台运行。 + +[`no_std`]: https://rust-embedded.github.io/book/intro/no-std.html + +## 第一层 + +第一层平台可以视为 “保证可以工作”。具体说,它们每一种都将满足以下要求: + +* 对该平台提供了官方二进制发行版本。 +* 设置了自动测试以运行该平台的测试。 +* 对 `rust-lang/rust` 仓库 master 分支的更改取决于测试是否通过。 +* 提供了如何使用和构建此平台(程序)的文档。 + +target | std | host | 备注 +-------|-----|------|------- +`aarch64-unknown-linux-gnu` | ✓ | ✓ | ARM64 Linux (kernel 4.2, glibc 2.17+) [^missing-stack-probes] +`i686-pc-windows-gnu` | ✓ | ✓ | 32-bit MinGW (Windows 7+) +`i686-pc-windows-msvc` | ✓ | ✓ | 32-bit MSVC (Windows 7+) +`i686-unknown-linux-gnu` | ✓ | ✓ | 32-bit Linux (kernel 2.6.32+, glibc 2.11+) +`x86_64-apple-darwin` | ✓ | ✓ | 64-bit macOS (10.7+, Lion+) +`x86_64-pc-windows-gnu` | ✓ | ✓ | 64-bit MinGW (Windows 7+) +`x86_64-pc-windows-msvc` | ✓ | ✓ | 64-bit MSVC (Windows 7+) +`x86_64-unknown-linux-gnu` | ✓ | ✓ | 64-bit Linux (kernel 2.6.32+, glibc 2.11+) + +[^missing-stack-probes]: `aarch64-unknown-linux-gnu` 缺少对堆栈探针(Stack probes)的支持,计划在不久的将来实现。跟踪实现可以查看 [issue #77071][77071]. + +[77071]: https://github.com/rust-lang/rust/issues/77071 + +## 第二层 + +第二层平台可以视为 “保证可以构建”。不会运行自动测试,因此不保证生成的构建可工作,但是(如果能工作)平台通常能工作的相当好,补丁也很受欢迎!具体说,它们每一种都将满足以下要求: + +* 对该平台提供了官方二进制发行版本。 +* 设置了自动构建,但可能没有运行测试。 +* 对 `rust-lang/rust` 仓库 master 分支的更改取决于平台构建。一些平台只编译标准库,但另一些也能编译运行 `rustc` 和 `cargo` 。 + +target | std | host | notes +-------|-----|------|------- +`aarch64-apple-darwin` | ✓ | ✓ | ARM64 macOS (11.0+, Big Sur+) +`aarch64-apple-ios` | ✓ | | ARM64 iOS +`aarch64-fuchsia` | ✓ | | ARM64 Fuchsia +`aarch64-linux-android` | ✓ | | ARM64 Android +`aarch64-pc-windows-msvc` | ✓ | ✓ | ARM64 Windows MSVC +`aarch64-unknown-linux-musl` | ✓ | ✓ | ARM64 Linux with MUSL +`aarch64-unknown-none` | * | | Bare ARM64, hardfloat +`aarch64-unknown-none-softfloat` | * | | Bare ARM64, softfloat +`arm-linux-androideabi` | ✓ | | ARMv7 Android +`arm-unknown-linux-gnueabi` | ✓ | ✓ | ARMv6 Linux (kernel 3.2, glibc 2.17) +`arm-unknown-linux-gnueabihf` | ✓ | ✓ | ARMv6 Linux, hardfloat (kernel 3.2, glibc 2.17) +`arm-unknown-linux-musleabi` | ✓ | | ARMv6 Linux with MUSL +`arm-unknown-linux-musleabihf` | ✓ | | ARMv6 Linux with MUSL, hardfloat +`armebv7r-none-eabi` | * | | Bare ARMv7-R, Big Endian +`armebv7r-none-eabihf` | * | | Bare ARMv7-R, Big Endian, hardfloat +`armv5te-unknown-linux-gnueabi` | ✓ | | ARMv5TE Linux (kernel 4.4, glibc 2.23) +`armv5te-unknown-linux-musleabi` | ✓ | | ARMv5TE Linux with MUSL +`armv7-linux-androideabi` | ✓ | | ARMv7a Android +`armv7a-none-eabi` | * | | Bare ARMv7-A +`armv7r-none-eabi` | * | | Bare ARMv7-R +`armv7r-none-eabihf` | * | | Bare ARMv7-R, hardfloat +`armv7-unknown-linux-gnueabi` | ✓ | | ARMv7 Linux (kernel 4.15, glibc 2.27) +`armv7-unknown-linux-gnueabihf` | ✓ | ✓ | ARMv7 Linux, hardfloat (kernel 3.2, glibc 2.17) +`armv7-unknown-linux-musleabi` | ✓ | | ARMv7 Linux, MUSL +`armv7-unknown-linux-musleabihf` | ✓ | | ARMv7 Linux with MUSL +`asmjs-unknown-emscripten` | ✓ | | asm.js via Emscripten +`i586-pc-windows-msvc` | ✓ | | 32-bit Windows w/o SSE +`i586-unknown-linux-gnu` | ✓ | | 32-bit Linux w/o SSE (kernel 4.4, glibc 2.23) +`i586-unknown-linux-musl` | ✓ | | 32-bit Linux w/o SSE, MUSL +`i686-linux-android` | ✓ | | 32-bit x86 Android +`i686-unknown-freebsd` | ✓ | | 32-bit FreeBSD +`i686-unknown-linux-musl` | ✓ | | 32-bit Linux with MUSL +`mips-unknown-linux-gnu` | ✓ | ✓ | MIPS Linux (kernel 4.4, glibc 2.23) +`mips-unknown-linux-musl` | ✓ | | MIPS Linux with MUSL +`mips64-unknown-linux-gnuabi64` | ✓ | ✓ | MIPS64 Linux, n64 ABI (kernel 4.4, glibc 2.23) +`mips64-unknown-linux-muslabi64` | ✓ | | MIPS64 Linux, n64 ABI, MUSL +`mips64el-unknown-linux-gnuabi64` | ✓ | ✓ | MIPS64 (LE) Linux, n64 ABI (kernel 4.4, glibc 2.23) +`mips64el-unknown-linux-muslabi64` | ✓ | | MIPS64 (LE) Linux, n64 ABI, MUSL +`mipsel-unknown-linux-gnu` | ✓ | ✓ | MIPS (LE) Linux (kernel 4.4, glibc 2.23) +`mipsel-unknown-linux-musl` | ✓ | | MIPS (LE) Linux with MUSL +`nvptx64-nvidia-cuda` | ✓ | | --emit=asm generates PTX code that [runs on NVIDIA GPUs] +`powerpc-unknown-linux-gnu` | ✓ | ✓ | PowerPC Linux (kernel 2.6.32, glibc 2.11) +`powerpc64-unknown-linux-gnu` | ✓ | ✓ | PPC64 Linux (kernel 2.6.32, glibc 2.11) +`powerpc64le-unknown-linux-gnu` | ✓ | ✓ | PPC64LE Linux (kernel 3.10, glibc 2.17) +`riscv32i-unknown-none-elf` | * | | Bare RISC-V (RV32I ISA) +`riscv32imac-unknown-none-elf` | * | | Bare RISC-V (RV32IMAC ISA) +`riscv32imc-unknown-none-elf` | * | | Bare RISC-V (RV32IMC ISA) +`riscv64gc-unknown-linux-gnu` | ✓ | ✓ | RISC-V Linux (kernel 4.20, glibc 2.29) +`riscv64gc-unknown-none-elf` | * | | Bare RISC-V (RV64IMAFDC ISA) +`riscv64imac-unknown-none-elf` | * | | Bare RISC-V (RV64IMAC ISA) +`s390x-unknown-linux-gnu` | ✓ | ✓ | S390x Linux (kernel 2.6.32, glibc 2.11) +`sparc64-unknown-linux-gnu` | ✓ | | SPARC Linux (kernel 4.4, glibc 2.23) +`sparcv9-sun-solaris` | ✓ | | SPARC Solaris 10/11, illumos +`thumbv6m-none-eabi` | * | | Bare Cortex-M0, M0+, M1 +`thumbv7em-none-eabi` | * | | Bare Cortex-M4, M7 +`thumbv7em-none-eabihf` | * | | Bare Cortex-M4F, M7F, FPU, hardfloat +`thumbv7m-none-eabi` | * | | Bare Cortex-M3 +`thumbv7neon-linux-androideabi` | ✓ | | Thumb2-mode ARMv7a Android with NEON +`thumbv7neon-unknown-linux-gnueabihf` | ✓ | | Thumb2-mode ARMv7a Linux with NEON (kernel 4.4, glibc 2.23) +`thumbv8m.base-none-eabi` | * | | ARMv8-M Baseline +`thumbv8m.main-none-eabi` | * | | ARMv8-M Mainline +`thumbv8m.main-none-eabihf` | * | | ARMv8-M Mainline, hardfloat +`wasm32-unknown-emscripten` | ✓ | | WebAssembly via Emscripten +`wasm32-unknown-unknown` | ✓ | | WebAssembly +`wasm32-wasi` | ✓ | | WebAssembly with WASI +`x86_64-apple-ios` | ✓ | | 64-bit x86 iOS +`x86_64-fortanix-unknown-sgx` | ✓ | | [Fortanix ABI] for 64-bit Intel SGX +`x86_64-fuchsia` | ✓ | | 64-bit Fuchsia +`x86_64-linux-android` | ✓ | | 64-bit x86 Android +`x86_64-rumprun-netbsd` | ✓ | | 64-bit NetBSD Rump Kernel +`x86_64-sun-solaris` | ✓ | | 64-bit Solaris 10/11, illumos +`x86_64-unknown-freebsd` | ✓ | ✓ | 64-bit FreeBSD +`x86_64-unknown-illumos` | ✓ | ✓ | illumos +`x86_64-unknown-linux-gnux32` | ✓ | | 64-bit Linux (x32 ABI) (kernel 4.15, glibc 2.27) +`x86_64-unknown-linux-musl` | ✓ | ✓ | 64-bit Linux with MUSL +`x86_64-unknown-netbsd` | ✓ | ✓ | NetBSD/amd64 +`x86_64-unknown-redox` | ✓ | | Redox OS + +[Fortanix ABI]: https://edp.fortanix.com/ + +## 第三层 + +第三层平台是 Rust 代码库支持的平台,但不会自动构建或测试,并且可能无法工作,官方构建不可用。 + +target | std | host | notes +-------|-----|------|------- +`aarch64-apple-ios-macabi` | ? | | Apple Catalyst on ARM64 +`aarch64-apple-tvos` | * | | ARM64 tvOS +`aarch64-unknown-freebsd` | ✓ | ✓ | ARM64 FreeBSD +`aarch64-unknown-hermit` | ? | | +`aarch64-unknown-netbsd` | ✓ | ✓ | +`aarch64-unknown-openbsd` | ✓ | ✓ | ARM64 OpenBSD +`aarch64-unknown-redox` | ? | | ARM64 Redox OS +`aarch64-uwp-windows-msvc` | ? | | +`aarch64-wrs-vxworks` | ? | | +`armv4t-unknown-linux-gnueabi` | ? | | +`armv5te-unknown-linux-uclibceabi` | ? | | ARMv5TE Linux with uClibc +`armv6-unknown-freebsd` | ✓ | ✓ | ARMv6 FreeBSD +`armv6-unknown-netbsd-eabihf` | ? | | +`armv7-apple-ios` | ✓ | | ARMv7 iOS, Cortex-a8 +`armv7-unknown-freebsd` | ✓ | ✓ | ARMv7 FreeBSD +`armv7-unknown-netbsd-eabihf` | ✓ | ✓ | +`armv7-wrs-vxworks-eabihf` | ? | | +`armv7a-none-eabihf` | * | | ARM Cortex-A, hardfloat +`armv7s-apple-ios` | ✓ | | +`avr-unknown-gnu-atmega328` | ✗ | | AVR. Requires `-Z build-std=core` +`hexagon-unknown-linux-musl` | ? | | +`i386-apple-ios` | ✓ | | 32-bit x86 iOS +`i686-apple-darwin` | ✓ | ✓ | 32-bit macOS (10.7+, Lion+) +`i686-pc-windows-msvc` | ✓ | | 32-bit Windows XP support +`i686-unknown-uefi` | ? | | 32-bit UEFI +`i686-unknown-haiku` | ✓ | ✓ | 32-bit Haiku +`i686-unknown-netbsd` | ✓ | ✓ | NetBSD/i386 with SSE2 +`i686-unknown-openbsd` | ✓ | ✓ | 32-bit OpenBSD +`i686-uwp-windows-gnu` | ? | | +`i686-uwp-windows-msvc` | ? | | +`i686-wrs-vxworks` | ? | | +`mips-unknown-linux-uclibc` | ✓ | | MIPS Linux with uClibc +`mipsel-unknown-linux-uclibc` | ✓ | | MIPS (LE) Linux with uClibc +`mipsel-unknown-none` | * | | Bare MIPS (LE) softfloat +`mipsel-sony-psp` | * | | MIPS (LE) Sony PlayStation Portable (PSP) +`mipsisa32r6-unknown-linux-gnu` | ? | | +`mipsisa32r6el-unknown-linux-gnu` | ? | | +`mipsisa64r6-unknown-linux-gnuabi64` | ? | | +`mipsisa64r6el-unknown-linux-gnuabi64` | ? | | +`msp430-none-elf` | * | | 16-bit MSP430 microcontrollers +`powerpc-unknown-linux-gnuspe` | ✓ | | PowerPC SPE Linux +`powerpc-unknown-linux-musl` | ? | | +`powerpc-unknown-netbsd` | ✓ | ✓ | +`powerpc-wrs-vxworks` | ? | | +`powerpc-wrs-vxworks-spe` | ? | | +`powerpc64-unknown-freebsd` | ✓ | ✓ | PPC64 FreeBSD (ELFv1 and ELFv2) +`powerpc64-unknown-linux-musl` | ? | | +`powerpc64-wrs-vxworks` | ? | | +`powerpc64le-unknown-linux-musl` | ? | | +`riscv32gc-unknown-linux-gnu` | | | RISC-V Linux (kernel 5.4, glibc 2.33) +`sparc-unknown-linux-gnu` | ✓ | | 32-bit SPARC Linux +`sparc64-unknown-netbsd` | ✓ | ✓ | NetBSD/sparc64 +`sparc64-unknown-openbsd` | ? | | +`thumbv7a-pc-windows-msvc` | ? | | +`thumbv7a-uwp-windows-msvc` | ✓ | | +`thumbv7neon-unknown-linux-musleabihf` | ? | | Thumb2-mode ARMv7a Linux with NEON, MUSL +`thumbv4t-none-eabi` | * | | ARMv4T T32 +`x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst on x86_64 +`x86_64-apple-tvos` | * | | x86 64-bit tvOS +`x86_64-linux-kernel` | * | | Linux kernel modules +`x86_64-pc-solaris` | ? | | +`x86_64-pc-windows-msvc` | ✓ | | 64-bit Windows XP support +`x86_64-unknown-dragonfly` | ✓ | ✓ | 64-bit DragonFlyBSD +`x86_64-unknown-haiku` | ✓ | ✓ | 64-bit Haiku +`x86_64-unknown-hermit` | ? | | +`x86_64-unknown-hermit-kernel` | ? | | HermitCore kernel +`x86_64-unknown-l4re-uclibc` | ? | | +`x86_64-unknown-openbsd` | ✓ | ✓ | 64-bit OpenBSD +`x86_64-unknown-uefi` | ? | | +`x86_64-uwp-windows-gnu` | ✓ | | +`x86_64-uwp-windows-msvc` | ✓ | | +`x86_64-wrs-vxworks` | ? | | + +[runs on NVIDIA GPUs]: https://github.com/japaric-archived/nvptx#targets From a14cf9cca18681eafafcc96c266ec4dd43ef7780 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Fri, 22 Jan 2021 22:08:50 +0800 Subject: [PATCH 02/11] modify readme --- README.md | 5 +---- src/SUMMARY.md | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ff56702..a7d3e2c 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,5 @@ 本书阅读地址: -[Github](https://1024chen.github.io/) +[Gitee](http://chen0adapter.gitee.io/rustc-book-cn) - - -国内访问加速: \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a75ff67..19195cf 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -17,5 +17,6 @@ - [自定义 Targets](targets/custom.md) - [已知问题](targets/known-issues.md) - [PGO](profile-guided-optimization.md) -- [Linker-plugin based LTO](linker-plugin-lto.md) +- [基于 LTO 的链接器插件](linker-plugin-lto.md) - [为 `rustc` 做贡献](contributing.md) + From 85b3df166bfe7a277decc730933b3cb55b064c73 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Sun, 24 Jan 2021 16:48:33 +0800 Subject: [PATCH 03/11] add 3.3.3 --- src/command-line-arguments.md | 2 +- src/lints/listing/deny-by-default.md | 513 ++++++++++++++++++++++++++- src/what-is-rustc.md | 2 +- 3 files changed, 508 insertions(+), 9 deletions(-) diff --git a/src/command-line-arguments.md b/src/command-line-arguments.md index fc2b47e..b2d3438 100644 --- a/src/command-line-arguments.md +++ b/src/command-line-arguments.md @@ -74,7 +74,7 @@ ATTR_NAME:LINK_NAME` 形式覆盖,其中 `ATTR_NAME` 是 `link` 属性中的 这将告知 `rustc` 您的 crate 的名称。 -## `--edition`: 指定使用的语义版本 +## `--edition`: 指定使用的edition 版次 该标签值为 `2015` 或 `2018`。 默认为 `2015`。 更多关于版本的信息可以在 [版本指南][edition guide]中找到。 diff --git a/src/lints/listing/deny-by-default.md b/src/lints/listing/deny-by-default.md index 9c1aa5d..f3f31e4 100644 --- a/src/lints/listing/deny-by-default.md +++ b/src/lints/listing/deny-by-default.md @@ -26,9 +26,7 @@ ## ambiguous_associated_items -`ambiguous_associated_items` lint 检测枚举变量和关联项之间的模糊不清。 - - +`ambiguous_associated_items` lint 检测枚举变量和关联项之间的不确定项。 **样例** @@ -78,83 +76,584 @@ note: `V` could also refer to the associated type defined here ``` +**解释** +早期 Rust 版本不允许通过类型别名访问枚举变量,当添加此功能时(请参阅 [RFC 2338][RFC-2338]),这引入了某些情况,即类型所指的可能不明确。 -**解释** - +要解决该歧义,应使用[路径限定][qualified path]明确声明要使用的类型。例如,以上示例中函数可以被写作`fn f() -> ::V { 0 } ` 以明确引用关联的类型。 +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #57644][issue-#57644] +[RFC-2338]:https://github.com/rust-lang/rfcs/blob/master/text/2338-type-alias-enum-variants.md +[qualified path]:https://doc.rust-lang.org/reference/paths.html#qualified-paths +[issue-#57644]:https://github.com/rust-lang/rust/issues/57644 ## arithmetic_overflow +`arithmetic_overflow` lint 检测会溢出的算术运算 +**样例** +```rust +# #![allow(unused)] +# fn main() { +1_i32 << 32; +# } +``` +显示如下: +```text +error: this arithmetic operation will overflow + --> lint_example.rs:2:1 + | +2 | 1_i32 << 32; + | ^^^^^^^^^^^ attempt to shift left by `32_i32`, which would overflow + | + = note: `#[deny(arithmetic_overflow)]` on by default +``` +**解释** - +执行值溢出运算很可能是错误,如果编译器能够在编译时检测到这些溢出,就会触发这个 lint。请考虑调整表达式避免溢出,或者使用不会溢出的数据类型。 ## conflicting_repr_hints +`conflicting_repr_hints` lint 检测带有冲突提示的 [`repr`](https://doc.rust-lang.org/reference/type-layout.html#representations) 属性。 +**样例** +```rust + +# #![allow(unused)] +# fn main() { +#[repr(u32, u64)] +enum Foo { + Variant1, +} +# } +``` +显示如下: +```text +error[E0566]: conflicting representation hints + --> lint_example.rs:2:8 + | +2 | #[repr(u32, u64)] + | ^^^ ^^^ + | + = note: `#[deny(conflicting_repr_hints)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #68585 + +``` +**解释** + +过去编译器错误的接受了这些冲突的表示形式。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。了解更多细节请参阅 [issue #68585](https://github.com/rust-lang/rust/issues/68585) + +想要更正该问题,请移除冲突的提示之一。 ## const_err +`const_err` lint 检测常量求值时的错误表达式。 + +**样例** + +```rust +# #![allow(unused)] +#![allow(unconditional_panic)] +# fn main() { +let x: &'static i32 = &(1 / 0); +# } +``` +显示如下: +```text +error: reaching this expression at runtime will panic or abort + --> lint_example.rs:3:24 + | +3 | let x: &'static i32 = &(1 / 0); + | -^^^^^^^ + | | + | dividing by zero + | + = note: `#[deny(const_err)]` on by default +``` +**解释** +该 lint 检测很可能错误的代码,如果将该 lint 等级变为允许,那么代码将不会在编译时进行计算,而是继续生成代码,在运行时计算,这可能会在运行时 panic 。 +注意,该 lint 可以在 [const 上下文][const-context]内部或外部触发。在 [const 上下文][const-context]外部,编译器有时会在编译时对表达式求值以生成更高效的代码。如果编译器要在这方面做得更好,它需要决定当遇到肯定会panic 或是不正确的代码时应该怎么做。将此设置为固有错误(hard error)将阻止表现出此行为的现有代码编译,破坏向后兼容性。然而,这肯定是不正确的代码,因此这是一个默认等级为拒绝的 lint。更多细节请参阅 [RFC 1229][RFC-1229] 和 [issue #28238][issue-28238]。 +注意还有几个其他更特定的编译时计算相关的 lint,例如:[`arithmetic_overflow`][arithmetic-overflow],[`unconditional_panic`][unconditional-panic] 。 +[const-context]:https://doc.rust-lang.org/reference/const_eval.html#const-context +[RFC-1229]:https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md +[issue-28238]:https://github.com/rust-lang/rust/issues/28238 +[arithmetic-overflow]:https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#arithmetic-overflow +[unconditional-panic]:https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#unconditional-panic ## ill_formed_attribute_input +`ill_formed_attribute_input` lint 检测以前被接收并且用于实践中的不良格式的属性输入。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +#[inline = "this is not valid"] +fn foo() {} +# } +``` +这会显示: +```text +error: attribute must be of the form `#[inline]` or `#[inline(always|never)]` + --> lint_example.rs:2:1 + | +2 | #[inline = "this is not valid"] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(ill_formed_attribute_input)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #57571 + +``` +**解释** + +以前,许多内置属性的输入没有经过验证,无意义的属性输入被接收。在添加了验证之后,明确了一些现有的项目使用了这些无效的格式。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #57571](https://github.com/rust-lang/rust/issues/57571) 。 +有关有效输入的属性,更多细节请参阅 [attribute reference](https://doc.rust-lang.org/nightly/reference/attributes.html) 。 ## incomplete_include +`incomplete_include` lint 检测一个文件包含多于一个表达式的 [`include!`][include-macro] 宏。 + +**样例** +```rust,igonre +fn main() { + include!("foo.txt"); +} +``` +当 `foo.txt`文件包含以下内容: +```text +println!("hi!"); +``` +显示如下: +```text +error: include macro expected single expression in source + --> foo.txt:1:14 + | +1 | println!("1"); + | ^ + | + = note: `#[deny(incomplete_include)]` on by default +``` +**解释** +[`include!`][include-macro] 宏当前仅打算用于单个[表达式][expression]或多个[项][item]。从以前看,它会忽略第一个表达式之后的任何内容,但这可能会令人困惑。在上例中,println! 表达式( println! expression )刚好在分号之前结束,从而使分号成为多余的信息而被忽略,更令人惊讶的是,如果包含的文件有多个打印语句,后续的语句将被忽略! +一个解决办法是将内容放在大括号中创建[块表达式][block-expression]。还可以考虑其他办法,例如函数封装表达式或者使用[过程宏][proc-macros]。 +这是个 lint 而不是固有错误是因为现有项目已经发现并报过错。谨慎起见,它现在还是个 lint 。`include!` 宏的未来语义还不确定,请参阅 [issue #35560](https://github.com/rust-lang/rust/issues/35560)。 +[include-macro]:https://doc.rust-lang.org/std/macro.include.html +[block-expression]:https://doc.rust-lang.org/reference/expressions/block-expr.html +[proc-macros]:https://doc.rust-lang.org/reference/procedural-macros.html ## invalid_type_param_default +`invalid_type_param_default` lint 检测在无效位置中错误地允许 (allowed) 使用类型参数默认值。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +fn foo(t: T) {} +# } +``` +显示如下: +```text +error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions. + --> lint_example.rs:2:8 + | +2 | fn foo(t: T) {} + | ^ + | + = note: `#[deny(invalid_type_param_default)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #36887 +``` +**解释** +默认类型参数仅在某些情况下才允许使用,但是以前编译器在任何地方都允许使用。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #36887](https://github.com/rust-lang/rust/issues/36887) 。 ## macro_expanded_macro_exports_accessed_by_absolute_paths +`macro_expanded_macro_exports_accessed_by_absolute_paths` lint 检测当前 crate 中不能被绝对路径引用的 [`macro_export`][macro-export]宏的宏展开。 +**样例** +```rust +macro_rules! define_exported { + () => { + #[macro_export] + macro_rules! exported { + () => {}; + } + }; +} +define_exported!(); +fn main() { + crate::exported!(); +} +``` +显示如下: +```text +error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths + --> lint_example.rs:13:5 + | +13 | crate::exported!(); + | ^^^^^^^^^^^^^^^ + | + = note: `#[deny(macro_expanded_macro_exports_accessed_by_absolute_paths)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #52234 +note: the macro is defined here + --> lint_example.rs:4:9 + | +4 | / macro_rules! exported { +5 | | () => {}; +6 | | } + | |_________^ +... +10 | define_exported!(); + | ------------------- in this macro invocation + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +``` +**解释** + +我们的目的是所有使用 `#[macro_export]` 属性的宏在 crate 根是可用的。然而,当一个 `macro_rules!` 定义由另一个宏生成之时,宏展开是无法遵循该规则的。 +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #53495](https://github.com/rust-lang/rust/issues/53495) 。 + +[macro-export]:https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope ## missing_fragment_specifier +`missing_fragment_specifier` lint 当有一个未使用的模式在未跟有片段说明符 (fragment specifier) (例如:`:expr`)的元变量(例如:`$e`)的 `macro_rules!` 宏定义中时被触发。 +始终可以通过移除 `macro_rules!` 宏定义中未使用的模式来解决此警告。 +**样例** +```rust +macro_rules! foo { + () => {}; + ($name) => { }; +} + +fn main() { + foo!(); +} +``` +显示如下: +```text +error: missing fragment specifier + --> lint_example.rs:3:5 + | +3 | ($name) => { }; + | ^^^^^ + | + = note: `#[deny(missing_fragment_specifier)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #40107 +``` +**解释** +要修复此问题,从 `macro_rules!` 定义中移除此未使用模式: +```rust +macro_rules! foo { + () => {}; +} +fn main() { + foo!(); +} +``` ## mutable_transmutes +`mutable_transmutes` lint 捕捉从 `&T` 到 `&mut T` 这种[未定义行为][undefined-behavior] 的转换。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +unsafe { + let y = std::mem::transmute::<&i32, &mut i32>(&5); +} +# } +``` +显示如下: +```text +error: mutating transmuted &mut T from &T may cause undefined behavior, consider instead using an UnsafeCell + --> lint_example.rs:3:13 + | +3 | let y = std::mem::transmute::<&i32, &mut i32>(&5); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(mutable_transmutes)]` on by default +``` +**解释** +我们对数据别名做出了一些假设,而这种转换是违反这些假设的。考虑使用 [`UnsafeCell`](https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html)。 +[undefined-behavior]:https://doc.rust-lang.org/reference/behavior-considered-undefined.html ## no_mangle_const_items +`no_mangle_const_items` lint 检测 [`no_mangle`](https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute)属性的所有 `const` 项。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +#[no_mangle] +const FOO: i32 = 5; +# } +``` +显示如下: +```text +error: const items should never be `#[no_mangle]` + --> lint_example.rs:3:1 + | +3 | const FOO: i32 = 5; + | -----^^^^^^^^^^^^^^ + | | + | help: try a static value: `pub static` + | + = note: `#[deny(no_mangle_const_items)]` on by default +``` +**解释** +常量没有其导出符号,因此这可能意味着你得用 [`static`][static] 而不是 [`const`][const]。 +[static]:https://doc.rust-lang.org/reference/items/static-items.html +[const]:https://doc.rust-lang.org/reference/items/constant-items.html ## order_dependent_trait_objects +`order_dependent_trait_objects` lint 检测一种 trait 一致性冲突,该冲突即为同一个包含标记 trait (marker traits)的 dynamic trait object 创建两个 trait 实现。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +pub trait Trait {} +impl Trait for dyn Send + Sync { } +impl Trait for dyn Sync + Send { } +# } +``` +显示如下: +```text +error: conflicting implementations of trait `main::Trait` for type `(dyn std::marker::Send + std::marker::Sync + 'static)`: (E0119) + --> lint_example.rs:5:1 + | +4 | impl Trait for dyn Send + Sync { } + | ------------------------------ first implementation here +5 | impl Trait for dyn Sync + Send { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + | + = note: `#[deny(order_dependent_trait_objects)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #56484 -## overflowing_literals +``` +**解释** +以前的一个 bug 导致编译器将不同顺序的 trait (例如 `Send + Sync` 和 `Sync + Send`)解释为不同的类型,然而它们应该被认为是相同的。这允许代码在出现一致性错误的时候定义单独的 trait 实现。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #56484](https://github.com/rust-lang/rust/issues/56484) 。 +## overflowing_literals +`overflowing_literals` lint 检测超出其所属类型范围的字面量。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +let x: u8 = 1000; +# } +``` +显示如下: +```text +error: literal out of range for `u8` + --> lint_example.rs:2:13 + | +2 | let x: u8 = 1000; + | ^^^^ + | + = note: `#[deny(overflowing_literals)]` on by default + = note: the literal `1000` does not fit into the type `u8` whose range is `0..=255` +``` +**解释** + +使用溢出其所用类型的字面量通常是错误。要么就使用在其类型范围内的字面量,要么就更改其类型以能容纳该字面量。 ## patterns_in_fns_without_body +`patterns_in_fns_without_body` lint 检测 `mut` [标识符模式][identifier-patterns]用于没有函数体的函数的参数。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +trait Trait { + fn foo(mut arg: u8); +} +# } +``` +显示如下: +```text +error: patterns aren't allowed in functions without bodies + --> lint_example.rs:3:12 + | +3 | fn foo(mut arg: u8); + | ^^^^^^^ + | + = note: `#[deny(patterns_in_fns_without_body)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #35203 +``` +**解释** + +要想修复此问题, trait 定义中从参数移除 `mut` ;也可以使用默认实现。也就是说,以下两种都行: +```rust +# #![allow(unused)] +# fn main() { +trait Trait { + fn foo(arg: u8); // Removed `mut` here +} + +impl Trait for i32 { + fn foo(mut arg: u8) { // `mut` here is OK + } +} +# } +``` +trait 定义中可以定义没有函数体的函数以指定实现必须实现的函数体。无函数体的函数形参名仅允许是 `_` 或为了文档目的的(仅类型相关)的[标识符](https://doc.rust-lang.org/reference/identifiers.html)。以前的编译器版本错误地允许[标识符模式][identifier-patterns]使用 `mut` 关键字,但这是不被允许的。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #35203](https://github.com/rust-lang/rust/issues/35203) 。 +[identifier-patterns]:https://doc.rust-lang.org/reference/patterns.html#identifier-patterns ## pub_use_of_private_extern_crate +`pub_use_of_private_extern_crate` lint 检测私有 `extern crate` 重导出的具体情况。 +**样例** +```rust,ignore +# #![allow(unused)] +# fn main() { +extern crate core; +pub use core as reexported_core; +# } +``` +显示如下: +```text +error: extern crate `core` is private, and cannot be re-exported (error E0365), consider declaring with `pub` + --> lint_example.rs:3:9 + | +3 | pub use core as reexported_core; + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(pub_use_of_private_extern_crate)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 +``` +**解释** +一个公开的 `use` 声明不应该用于 公开性地重导出私有 `extern crate`。应该使用 `pub extern crate`。 +过去是允许该行为的,但是根据可见性规则这是不符合预期的。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #34537](https://github.com/rust-lang/rust/issues/34537) 。 ## soft_unstable +`soft_unstable` lint 检测 在 stable 上无意间允许(allowed)的 unstable feature。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +#[cfg(test)] +extern crate test; +#[bench] +fn name(b: &mut test::Bencher) { + b.iter(|| 123) +} +# } +``` +显示如下: +```text +error: use of unstable library feature 'test': `bench` is a part of custom test frameworks which are unstable + --> lint_example.rs:5:3 + | +5 | #[bench] + | ^^^^^ + | + = note: `#[deny(soft_unstable)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #64266 +``` +**解释** +[`bench`](https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html) 属性意外地在 [stable release channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) 上被指定。将此转化为固有错误会破坏一些(现有)项目。当使用 `--cap-lints` 时该 lint 允许项目正确地构建,否则会发出一个错误提示。`#[bench]` 不应该被用在 stable channel。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #64266](https://github.com/rust-lang/rust/issues/64266) 。 ## unconditional_panic +`unconditional_panic` lint 检测将在运行时引起 panic 的操作。 +**样例** +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +let x = 1 / 0; +# } +``` +显示如下: +```text +error: this operation will panic at runtime + --> lint_example.rs:3:9 + | +3 | let x = 1 / 0; + | ^^^^^ attempt to divide `1_i32` by zero + | + = note: `#[deny(unconditional_panic)]` on by default +``` +**解释** +该 lint 检测很可能不正确的代码。如果可能,编译器将尝试检测代码能够在编译时进行计算的情况,以生成更高效的代码。在计算这类代码时,如果检测到代码会无条件地 panic ,通常表示(要执行的计算)在做一些错误的事情。如果该 lint 等级被改为允许,然后此代码将不会在编译时被计算,而是继续生成代码在运行时计算,这也可能会在运行时 panic。 ## unknown_crate_types +`unknown_crate_types` lint 检测在 [`crate_type`](https://doc.rust-lang.org/reference/linkage.html)属性中找到的未知 crate 类型。 +**样例** +```rust +#![crate_type="lol"] +fn main() {} +``` +显示如下: +```text +error: invalid `crate_type` value + --> lint_example.rs:1:15 + | +1 | #![crate_type="lol"] + | ^^^^^ + | + = note: `#[deny(unknown_crate_types)]` on by default +``` +**解释** +给 `crate_type` 属性赋未知值可以肯定说是一个错误。 ## useless_deprecated +`useless_deprecated` lint 检测无效且弃用的属性。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +struct X; + +#[deprecated = "message"] +impl Default for X { + fn default() -> Self { + X + } +} +# } +``` +显示如下: +```text +error: this `#[deprecated]` annotation has no effect + --> lint_example.rs:4:1 + | +4 | #[deprecated = "message"] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the unnecessary deprecation attribute + | + = note: `#[deny(useless_deprecated)]` on by default +``` +**解释** +弃用属性对 trait 实现是无影响的。 +[future-incompatible]:https://doc.rust-lang.org/rustc/lints/index.html#future-incompatible-lints +[expression]:https://doc.rust-lang.org/reference/expressions.html +[item]:https://doc.rust-lang.org/reference/items.html diff --git a/src/what-is-rustc.md b/src/what-is-rustc.md index b8a3449..1ffab2a 100644 --- a/src/what-is-rustc.md +++ b/src/what-is-rustc.md @@ -56,4 +56,4 @@ $ rustc main.rs ``` -你不需要告诉 `rustc` 关于 `foo.rs`的信息; `mod` 语句会给出需要关联的一切。这与你使用C编译器有所不同, c编译器中,你可以在每个文件上单独调用编译器,然后将他们都链接起来。换句话说, *crate* 是一个翻译单元(translation unit),而不是特定的模块。 \ No newline at end of file +你不需要告诉 `rustc` 关于 `foo.rs`的信息; `mod` 语句会给出需要关联的一切。这与你使用C编译器有所不同,C编译器中,你可以在每个文件上单独调用编译器,然后将他们都链接起来。换句话说, *crate* 是一个翻译单元(translation unit),而不是特定的模块。 \ No newline at end of file From bea0a1dfcc01dbc73af79e26217cf6deba060a4c Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Tue, 26 Jan 2021 22:20:02 +0800 Subject: [PATCH 04/11] add 3.3.2 --- src/lints/listing/warn-by-default.md | 2051 +++++++++++++++++++++++++- 1 file changed, 2050 insertions(+), 1 deletion(-) diff --git a/src/lints/listing/warn-by-default.md b/src/lints/listing/warn-by-default.md index c65bf90..6b1abe9 100644 --- a/src/lints/listing/warn-by-default.md +++ b/src/lints/listing/warn-by-default.md @@ -84,79 +84,2128 @@ ## array_into_iter +`` lint 检测数组中调用 `into_iter`。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +[1, 2, 3].into_iter().for_each(|n| { *n; }); +# } +``` +显示如下: +```text +warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. + --> lint_example.rs:3:11 + | +3 | [1, 2, 3].into_iter().for_each(|n| { *n; }); + | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` + | + = note: `#[warn(array_into_iter)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #66145 +``` +**解释** +在将来计划为数组添加一个 `IntoIter` 实现,这样的话它将遍历的是数组的值而不是引用。由于方法解析的工作方式,这将改变在数组上使用 `into_iter` 的那部分现有代码。避免此警告的办法是使用 `iter()` 而不是 `into_iter()`。 + +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误(hard error)。更多细节和更全面的 关于此 int 的描述请参阅 [issue #66145](https://github.com/rust-lang/rust/issues/66145)。 + + ## asm_sub_register +`asm_sub_register` lint 检测仅使用寄存器子集进行内联汇编输入。 + +**样例** +```rust,ignore +#![feature(asm)] + +fn main() { + #[cfg(target_arch="x86_64")] + unsafe { + asm!("mov {0}, {0}", in(reg) 0i16); + } +} +``` +显示如下: +```text +warning: formatting may not be suitable for sub-register argument + --> src/main.rs:6:19 + | +6 | asm!("mov {0}, {0}", in(reg) 0i16); + | ^^^ ^^^ ---- for this argument + | + = note: `#[warn(asm_sub_register)]` on by default + = help: use the `x` modifier to have the register formatted as `ax` + = help: or use the `r` modifier to keep the default formatting of `rax` +``` +**解释** + +一些体系结构上的寄存器可以使用不同的名称来引用寄存器的一个子集。默认情况下,编译器会使用该名称来表示整个寄存器大小。想要显式使用寄存器子集,可以通过模板字符串操作数上使用修饰符来指定何时使用子寄存器来覆盖默认值。如果传入的数据类型小于默认寄存器大小的值,就出触发此 lint,以警告你可能使用了错误的(位)宽度。要想修复此问题,向模板添加建议的修饰符,或者或者将值转为正确的大小。 + +更多细节请参阅[寄存器模板修饰符](https://doc.rust-lang.org/nightly/unstable-book/library-features/asm.html#register-template-modifiers)。 ## bare_trait_objects +`bare_trait_objects` lint 暗示为 trait 对象使用 `dyn Trait` 。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +trait Trait { } + +fn takes_trait_object(_: Box) { +} +# } +``` +显示如下: +```text +warning: trait objects without an explicit `dyn` are deprecated + --> lint_example.rs:4:30 + | +4 | fn takes_trait_object(_: Box) { + | ^^^^^ help: use `dyn`: `dyn Trait` + | + = note: `#[warn(bare_trait_objects)]` on by default +``` +**解释** + +没有 `dyn` 关键字,当阅读代码时你是否是在查看 trait 对象这可能会造成模棱两可或困惑。`dyn` 关键字将其明确,并且添加了对称性来与 [`impl Trait`](https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters)对比。 ## bindings_with_variant_name +`bindings_with_variant_name` lint 检测与匹配变量之一同名的模式绑定。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +pub enum Enum { + Foo, + Bar, +} + +pub fn foo(x: Enum) { + match x { + Foo => {} + Bar => {} + } +} +# } +``` +显示如下: +```text +warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `Enum` + --> lint_example.rs:9:9 + | +9 | Foo => {} + | ^^^ help: to match on the variant, qualify the path: `Enum::Foo` + | + = note: `#[warn(bindings_with_variant_name)]` on by default + +``` +**解释** + +将枚举变量名称指定为[标识符模式][identifier-pattern]通常是个错误。在上例中,`match` 分支指定了变量名来绑定 `x` 。 第二个分支被忽略,因为第一个分支匹配到了所有的值。可能的意图是第一个分支意在匹配枚举变量。 + +两个可能的解决办法是: ++ 使用[路径模式][path-pattern]指定枚举变量,例如 `Enum::Foo`。 ++ 将枚举变量引入本地作用域,例如上例在 `foo` 函数的开头添加 `use Enum::*;` 。 +[path-pattern]:https://doc.rust-lang.org/reference/patterns.html#path-patterns + ## broken_intra_doc_links +`broken_intra_doc_links` lint 检测解析内部文档链接目标失败。这是一个仅用于 `rustdoc` 的lint,请查看 [rustdoc book](https://doc.rust-lang.org/rustdoc/lints.html#broken_intra_doc_links)中的文档。 + ## cenum_impl_drop_cast +`cenum_impl_drop_cast` lint 检测实现了 [`Drop`](https://doc.rust-lang.org/std/ops/trait.Drop.html) trait 的无字段(field-less)枚举体 的 `as` 强制转换。 + +**样例** + +```rust +# #![allow(unused)] +enum E { + A, +} + +impl Drop for E { + fn drop(&mut self) { + println!("Drop"); + } +} + +fn main() { + let e = E::A; + let i = e as u32; +} +``` +显示如下: +```text +warning: cannot cast enum `E` into integer `u32` because it implements `Drop` + --> lint_example.rs:14:13 + | +14 | let i = e as u32; + | ^^^^^^^^ + | + = note: `#[warn(cenum_impl_drop_cast)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #73333 +``` +**解释** + +将未实现整数的 [`Copy`](https://doc.rust-lang.org/std/marker/trait.Copy.html) 的无字段枚举体将移动值而不调用 `drop` 。如果期望是调用 `drop`,这可能会导致令人惊讶的行为。自动调用 `drop` 将与其他操作不同。由于两种行为都不是清晰或一致,因此决定不允许这种性质的转换。 + +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误(hard error)。更多细节请参阅 [issue #73333](https://github.com/rust-lang/rust/issues/73333)。 + ## clashing_extern_declarations +`clashing_extern_declarations` lint 检测当同名 `extern fn` 被声明但是不同类型的情况。 + +**样例** + +```rust,ignore +# #![allow(unused)] +# fn main() { +mod m { + extern "C" { + fn foo(); + } +} + +extern "C" { + fn foo(_: u32); +} +# } +``` +显示如下: +```text +warning: `foo` redeclared with a different signature + --> lint_example.rs:9:5 + | +4 | fn foo(); + | --------- `foo` previously declared here +... +9 | fn foo(_: u32); + | ^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration + | + = note: `#[warn(clashing_extern_declarations)]` on by default + = note: expected `unsafe extern "C" fn()` + found `unsafe extern "C" fn(u32)` +``` +**解释** + +因为在链接中不能将同名符号(symbols)解析为两个不同的函数,且一个函数不能有两个类型,一个相冲突的外部声明可以肯定是个错误。检查以确保 `extern` 定义正确且有效,且考虑将它们统一在一个位置。 + +该 lint 不能跨 crate 运行因为一个项目可能有依赖于相同外部函数的依赖项,但是外部函数以不同(但有效)的方式声明。例如,它们可能都为一个或多个参数声明一个不透明类型(opaque type) (最终会得到不同的类型),或者使用 `extern fn` 定义的语言中有效的转换类型,在这些情况下,编译器不能说冲突声明(clashing declaration)不正确。 + ## coherence_leak_check +`coherence_leak_check` lint 检测仅能由旧版泄露检查代码(the old leak-check code)区分的 trait 的冲突实现。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +trait SomeTrait { } +impl SomeTrait for for<'a> fn(&'a u8) { } +impl<'a> SomeTrait for fn(&'a u8) { } +# } +``` +显示如下: +```text +warning: conflicting implementations of trait `main::SomeTrait` for type `for<'a> fn(&'a u8)`: + --> lint_example.rs:4:1 + | +3 | impl SomeTrait for for<'a> fn(&'a u8) { } + | ------------------------------------- first implementation here +4 | impl<'a> SomeTrait for fn(&'a u8) { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `for<'a> fn(&'a u8)` + | + = note: `#[warn(coherence_leak_check)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #56105 + = note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details +``` +**解释** + +过去编译器接受相同功能但唯一不同是生命周期绑定器(lifetime binder)出现的位置的 trait 实现。由于借用检查器的更改实现了多个 bug 的修复,因此不再允许该行为。然而,因为这会影响现有代码,所以这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误(hard error)。 +依赖此模式的代码应引入 "[newtypes](https://doc.rust-lang.org/book/ch19-04-advanced-types.html#using-the-newtype-pattern-for-type-safety-and-abstraction)",例如 `struct Foo(for<'a> fn(&'a u8))` 。 + +更多细节请参阅 [issue #56105](https://github.com/rust-lang/rust/issues/56105)。 + ## confusable_idents +`confusable_idents` lint 检测容易混淆的标识符对。 + +**样例** +```rust +# #![allow(unused)] +#![feature(non_ascii_idents)] + +# fn main() { +// Latin Capital Letter E With Caron +pub const Ě: i32 = 1; +// Latin Capital Letter E With Breve +pub const Ĕ: i32 = 2; +# } +``` +显示如下: +```text +warning: identifier pair considered confusable between `Ě` and `Ĕ` + --> lint_example.rs:7:11 + | +5 | pub const Ě: i32 = 1; + | - this is where the previous identifier occurred +6 | // Latin Capital Letter E With Breve +7 | pub const Ĕ: i32 = 2; + | ^ + | + = note: `#[warn(confusable_idents)]` on by default + +``` +**解释** + +上面的 [`non_ascii_idents`][non-ascii-idents] 是只能用于 nightly的,其允许使用非 ASCII 字符作为标识符。该 lint 当不同标识符看起来很像的时候发出警告,因为(长得像)这可能会令人困惑。 + +混淆检测的算法是基于 [Unicode® Technical Standard #39 Unicode Security Mechanisms Section 4 Confusable Detection](https://www.unicode.org/reports/tr39/#Confusable_Detection) 。对每个不同的标识符 X 执行 `skelenton(X)` 函数。如果在一个 crate 中存在两个不同的标识符 X 和 Y 但是却得到 `skeleton(X) = skeleton(Y)` (就会触发警告)。编译器用与此相同的机制来检查标识符与关键字相似。 + +请注意,易混淆字符集可能会随时间而变化。注意,如果你将该 lint 等级调整为 “禁止”,则现有代码可能在将来会(编译)失败。 + ## const_evaluatable_unchecked +`const_evaluatable_unchecked` lint 检测类型中使用的泛型常量。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +const fn foo() -> usize { + if std::mem::size_of::<*mut T>() < 8 { // size of *mut T does not depend on T + 4 + } else { + 8 + } +} + +fn test() { + let _ = [0; foo::()]; +} +# } +``` +显示如下: +```text +warning: cannot use constants which depend on generic parameters in types + --> lint_example.rs:11:17 + | +11 | let _ = [0; foo::()]; + | ^^^^^^^^^^ + | + = note: `#[warn(const_evaluatable_unchecked)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #76200 +``` +**解释** + +在 1.43 发行版本,会意外地允许在数组重复表达式中使用泛型参数。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误(hard error)。更多细节描述和可能的修复请参阅 [issue #76200](https://github.com/rust-lang/rust/issues/76200) 。 + ## const_item_mutation +`const_item_mutation` lint 检测试图更改 `const` 项。 + +**样例** +```rust +const FOO: [i32; 1] = [0]; + +fn main() { + FOO[0] = 1; + // This will print "[0]". + println!("{:?}", FOO); +} +``` +显示如下: +```text +warning: attempting to modify a `const` item + --> lint_example.rs:4:5 + | +4 | FOO[0] = 1; + | ^^^^^^^^^^ + | + = note: `#[warn(const_item_mutation)]` on by default + = note: each usage of a `const` item creates a new temporary; the original `const` item will not be modified +note: `const` item defined here + --> lint_example.rs:1:1 + | +1 | const FOO: [i32; 1] = [0]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +``` +**解释** + +试图直接更改 `const` 项几乎总是个错误。上例中发生的是临时的 `const` 副本被改变,但是原本的 `const` 并没有改变。每次通过名称来引用 `const`(例如上例中的 `FOO`)时,该值的一个单独副本都会内联到该位置。 +该 lint 检查直接写入字段(`FOO.field = some_value`) 或数组输入(`FOO[0] = val`),或对 `const` 进行可变引用(`&mut FOO`),包括通过自动解引用(`FOO.some_mut_self_method()`)。 + +以下有多种选择取决于你想要完成的方式: ++ 首先,总是重新考虑是否使用可变全局变量,因为它们可能很难正确使用,而且会使得代码更难以使用或理解。 ++ 如果你试图对一个全局变量进行一次性初始化: + - 如果想要该值可以在编译器计算,请考虑使用常量兼容(const-compatible)的值(请参阅[常量值计算](https://doc.rust-lang.org/reference/const_eval.html) )。 + - 对于更复杂的单次初始化(single-intialization),可以考虑使用第三方 crate ,例如 [`lazy_static`](https://crates.io/crates/lazy_static)或 [`once_cell`](https://crates.io/crates/once_cell) 。 + - 如果你在使用 [nightly channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html),考虑使用标准库中的新 [`lazy`](https://doc.rust-lang.org/nightly/std/lazy/index.html) 模块。 ++ 如果确实需要可变的全局变量,请考虑使用具有多种选择的 [`static`](https://doc.rust-lang.org/reference/items/static-items.html) 。 + - 可以直接定义简单数据类型并可用 [原子类型](https://doc.rust-lang.org/std/sync/atomic/index.html)进行转变。 + - 更复杂的类型可以放在如 [`mutex`](https://doc.rust-lang.org/std/sync/struct.Mutex.html) 之类的同步原语中,可以使用上面列出的选项之一对其进行初始化。 + - [可变静态变量](https://doc.rust-lang.org/reference/items/static-items.html#mutable-statics) 是低级原语,要求使用 unsafe。通常情况下应该避免该情况,使用上述之一。 + ## dead_code +`dead_code` lint 检测未使用,未导出的代码。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +fn foo() {} +# } +``` +显示如下: +```text +warning: function is never used: `foo` + --> lint_example.rs:2:4 + | +2 | fn foo() {} + | ^^^ + | + = note: `#[warn(dead_code)]` on by default +``` +**解释** + +Dead code 表示错误或未完成的代码。要使单个项的警告静默,在名称前加上下划线例如 `_foo`。如果打算将项导出 crate 之外,考虑添加可见修饰符如 `pub` 。否则请考虑移除未使用的代码。 + ## deprecated +`deprecated` lint 检测不推荐使用的项。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +#[deprecated] +fn foo() {} + +fn bar() { + foo(); +} +# } +``` +显示如下: +```text +warning: use of deprecated function `main::foo` + --> lint_example.rs:6:5 + | +6 | foo(); + | ^^^ + | + = note: `#[warn(deprecated)]` on by default +``` +**解释** + +项可以被标记为 “被遗弃的”,通过 [`deprecated`](https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute) 属性指明不该再使用。通常该属性应包含如何使用替换的提示或者检查文档。 + ## drop_bounds +`drop_bounds` lint 检查使用 `std::ops::Drop` 作为约束的泛型。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +fn foo() {} +# } +``` +显示如下: +```text +warning: bounds on `T: Drop` are useless, consider instead using `std::mem::needs_drop` to detect if a type has a destructor + --> lint_example.rs:2:11 + | +2 | fn foo() {} + | ^^^^ + | + = note: `#[warn(drop_bounds)]` on by default +``` +**解释** + +`Drop` 约束并没有真正完成(accomplish)任何事。一个类型可能由编译器生成 drop 而没有实现 `Drop` trait 本身。`Drop` trait 也只有一个方法 `Drop::drop` ,而且该函数在用户代码中不可调用。所以实际上没有实际使用 `Drop` trait 的用例。 + +drop trait 最有可能的用例是区分有析构函数和没有析构函数的类型。结合泛型特化,初级程序员会编写一个实现并认为类型会被简单 drop,然后为 `T:Drop` 写了泛型特化 实际上调用了析构函数。实际上这是不正确的,例如,String 实际上并没有实现 Drop,但因为 String 包含 Vec,假设其可以被简单丢弃将会造成内存泄漏。 + + + ## ellipsis_inclusive_range_patterns +`ellipsis_inclusive_range_patterns` lint 检测 `...` 这种已经被遗弃的[范围模式](https://doc.rust-lang.org/reference/patterns.html#range-patterns)。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +let x = 123; +match x { + 0...100 => {} + _ => {} +} +# } +``` +显示如下: +```text +warning: `...` range patterns are deprecated + --> lint_example.rs:4:6 + | +4 | 0...100 => {} + | ^^^ help: use `..=` for an inclusive range + | + = note: `#[warn(ellipsis_inclusive_range_patterns)]` on by default +``` +**解释** + +`...` 范围模式语法已经被改为 `..=`,以避免和 [`..` 范围表达式](https://doc.rust-lang.org/reference/expressions/range-expr.html)的潜在混乱。请使用新形式。 + ## exported_private_dependencies +`exported_private_dependencies` lint 检测在公共接口公开的私有依赖。 + +**样例** +```rust,ignore +pub fn foo() -> Option { + None +} +``` +显示如下: +```text +warning: type `bar::Thing` from private dependency 'bar' in public interface + --> src/lib.rs:3:1 + | +3 | pub fn foo() -> Option { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(exported_private_dependencies)]` on by default +``` +**解释** + +依赖可以被标记为 “私有” 以指明其不能在 crate 中的公共接口公开。可以被 Cargo 独立地解析这些依赖项,因为可以假定它不需要使用相同的依赖将它们与其他包统一。该 lint 指明违反了该规则。 +要修复此问题,应避免在公共接口中公开此依赖,或将此依赖切换为公有依赖。 +注意仅在 nightly channel 中支持此 lint 。更多细节请参阅 [RFC 1977](https://github.com/rust-lang/rfcs/blob/master/text/1977-public-private-dependencies.md),以及 [Cargo 文档](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#public-dependency)。 + ## function_item_references +`function_item_references` lint 检测使用 [`fmt::Pointer`][fmt-Pointer]格式化或转换的函数引用。 + +**样例** +```rust +fn foo() { } + +fn main() { + println!("{:p}", &foo); +} +``` +显示如下: +```text +warning: taking a reference to a function item does not give a function pointer + --> lint_example.rs:4:22 + | +4 | println!("{:p}", &foo); + | ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn()` + | + = note: `#[warn(function_item_references)]` on by default +``` +**解释** + +引用一个函数可能被误认为是获取函数指针的一种方式。将引用格式化为指针或对其进行转换的时候可能会产生意外的结果。当函数引用被格式化为指针,作为 [`fmt::Pointer`][fmt-Pointer] 约束的参数传递或转换时,就会触发该 lint。 + +[fmt-Pointer]:https://doc.rust-lang.org/std/fmt/trait.Pointer.html + ## illegal_floating_point_literal_pattern +`illegal_floating_point_literal_pattern` lint 检测用于模式中的浮点数字面量。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let x = 42.0; + +match x { + 5.0 => {} + _ => {} +} +# } +``` +显示如下: +```text +warning: floating-point types cannot be used in patterns + --> lint_example.rs:5:5 + | +5 | 5.0 => {} + | ^^^ + | + = note: `#[warn(illegal_floating_point_literal_pattern)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #41620 +``` +**解释** + +早期版本的编译器接受用于模式中的浮点数字面量,但是后来确定了是个错误。当与”结构相等“进行对比时,比较浮点数值的语义可能不会再模式中明确。通常你可以通过使用 match 守卫(match guard) 来解决此问题,例如: +```rust +# #![allow(unused)] +# fn main() { +# let x = 42.0; + +match x { + y if y == 5.0 => {} + _ => {} +} +# } +``` +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #41620](https://github.com/rust-lang/rust/issues/41620)。 + ## improper_ctypes +`improper_ctypes` lint 检测在外部模块中错误使用的类型。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +extern "C" { + static STATIC: String; +} +# } +``` +显示如下: +```text +warning: `extern` block uses type `String`, which is not FFI-safe + --> lint_example.rs:3:20 + | +3 | static STATIC: String; + | ^^^^^^ not FFI-safe + | + = note: `#[warn(improper_ctypes)]` on by default + = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct + = note: this struct has unspecified layout +``` +**解释** + +编译器做了几项检查以验证外部块中使用的类型是安全的,并遵循某些规则以确保与外部接口的适当兼容性。当其在定义中检测到可能的错误时,将触发此 lint。该 lint 通常应该提供问题描述,并尽可能提示如何解决。 + ## improper_ctypes_definitions +`improper_ctypes_definitions` lint 检测对 [`extern` 函数](https://doc.rust-lang.org/reference/items/functions.html#extern-function-qualifier) 定义的错误使用。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +pub extern "C" fn str_type(p: &str) { } +# } +``` +显示如下: +```text +warning: `extern` fn uses type `str`, which is not FFI-safe + --> lint_example.rs:3:31 + | +3 | pub extern "C" fn str_type(p: &str) { } + | ^^^^ not FFI-safe + | + = note: `#[warn(improper_ctypes_definitions)]` on by default + = help: consider using `*const u8` and a length instead + = note: string slices have no C equivalent +``` +**解释** + +在 `extern` 函数中可能指定了许多与给定的 ABI 不兼容的参数和返回类型。该 lint 是一个关于这些类型都不应该使用的警告。该 lint 应该提供问题的描述,并尽可能提示如何解决问题。 + ## incomplete_features +`incomplete_features` lint 检测使用 [`feature`](https://doc.rust-lang.org/nightly/unstable-book/) 属性启用的不稳定 feature,这可能在一些或全部情况下不能正常工作。 + +**样例** +```rust +# #![allow(unused)] +#![feature(generic_associated_types)] +# fn main() { +# } +``` +显示如下: +```text +warning: the feature `generic_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes + --> lint_example.rs:1:12 + | +1 | #![feature(generic_associated_types)] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #44265 for more information +``` +**解释** + +尽管鼓励人们尝试不稳定的性能,其中的一些已知不完整或有缺陷。该 lint 是一个关于 feature 尚未完成的信号,并且你可能会遇到一些问题。 + ## indirect_structural_match +`indirect_structural_match` lint 检测手动实现的 [`PartialEq`](https://doc.rust-lang.org/std/cmp/trait.PartialEq.html) 和 [`Eq`](https://doc.rust-lang.org/std/cmp/trait.Eq.html) 的模式中的 `const`。 + +**样例** +```rust +#![deny(indirect_structural_match)] + +struct NoDerive(i32); +impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } } +impl Eq for NoDerive { } +#[derive(PartialEq, Eq)] +struct WrapParam(T); +const WRAP_INDIRECT_PARAM: & &WrapParam = & &WrapParam(NoDerive(0)); +fn main() { + match WRAP_INDIRECT_PARAM { + WRAP_INDIRECT_PARAM => { } + _ => { } + } +} +``` +显示如下: +```text +error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]` + --> lint_example.rs:11:9 + | +11 | WRAP_INDIRECT_PARAM => { } + | ^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> lint_example.rs:1:9 + | +1 | #![deny(indirect_structural_match)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #62411 +``` + +**解释** + +编译器过去无意间接受了此种形式。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。完整的问题描述和一些可能的解决办法请参阅 [issue #62411](https://github.com/rust-lang/rust/issues/62411)。 + ## inline_no_sanitize +`inline_no_sanitize` lint 检测 [`#[inline(always)]`][inline-always]和[`#[no_sanitize(...)]`][no-sanitize] 之间的不兼容性。 + +**样例** +```rust +#![feature(no_sanitize)] + +#[inline(always)] +#[no_sanitize(address)] +fn x() {} + +fn main() { + x() +} +``` +显示如下: +```text +warning: `no_sanitize` will have no effect after inlining + --> lint_example.rs:4:1 + | +4 | #[no_sanitize(address)] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(inline_no_sanitize)]` on by default +note: inlining requested here + --> lint_example.rs:3:1 + | +3 | #[inline(always)] + | ^^^^^^^^^^^^^^^^^ +``` +**解释** + + [`#[inline(always)]`][inline-always] 属性的使用会阻止 [`#[no_sanitize(...)]`][no-sanitize] 属性正常工作。考虑暂时移除 `inline` 属性。 + +[inline-always]:https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute +[no-sanitize]:https://doc.rust-lang.org/nightly/unstable-book/language-features/no-sanitize.html + ## invalid_codeblock_attributes +`invalid_codeblock_attributes` lint 检测文档示例中那些可能有类型错误的代码块。这是个仅用于 `rustdoc` 的 lint ,请参阅 [rustdoc book](https://doc.rust-lang.org/rustdoc/lints.html#broken_intra_doc_links) 中的文档。 + ## invalid_value +`invalid_value` lint 检测创建的无效值,例如 NULL 引用。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +unsafe { + let x: &'static i32 = std::mem::zeroed(); +} +# } +``` +显示如下: +```text +warning: the type `&i32` does not permit zero-initialization + --> lint_example.rs:4:27 + | +4 | let x: &'static i32 = std::mem::zeroed(); + | ^^^^^^^^^^^^^^^^^^ + | | + | this code causes undefined behavior when executed + | help: use `MaybeUninit` instead, and only call `assume_init` after initialization is done + | + = note: `#[warn(invalid_value)]` on by default + = note: references must be non-null +``` +**解释** + +一些情况下,编译器可以检测到代码创建了无效的值,这应该是避免的。 +特别地,该 lint 会检测是否有不恰当的使用 [`mem::zeroed`](https://doc.rust-lang.org/std/mem/fn.zeroed.html),[`mem::uninitialized`](https://doc.rust-lang.org/std/mem/fn.uninitialized.html),[`mem::transmute`](https://doc.rust-lang.org/std/mem/fn.transmute.html)和[`MaybeUninit::assume_init`](https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#method.assume_init) 可能造成[未定义行为](https://doc.rust-lang.org/reference/behavior-considered-undefined.html)。 + ## irrefutable_let_patterns +`irrefutable_let_patterns` lint 检测 在 [if-let][https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions] 和 [while-let][https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-pattern-loops] 语句中的[不可辩驳模式][https://doc.rust-lang.org/reference/patterns.html#refutability] 。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +if let _ = 123 { + println!("always runs!"); +} +# } +``` +显示如下: +```text +warning: irrefutable if-let pattern + --> lint_example.rs:2:1 + | +2 | / if let _ = 123 { +3 | | println!("always runs!"); +4 | | } + | |_^ + | + = note: `#[warn(irrefutable_let_patterns)]` on by default +``` +**解释** + +通常没理由在 if-let 或 while-let 语句中使用不可辩驳模式,因为这样的话模式总是会匹配成功,要是这样的话 [`let`](https://doc.rust-lang.org/reference/statements.html#let-statements) 或 [`loop`](https://doc.rust-lang.org/reference/expressions/loop-expr.html#infinite-loops) 语句就够了。然而,当用宏生成代码时,在宏不知道模式是否是可辨驳的情况下,禁止不可辩驳模式是一种笨拙的解决办法。该 lint 允许宏接受此形式,并警告普通代码这可能是不正确的使用。 +更多细节请参阅 [RFC 2086](https://github.com/rust-lang/rfcs/blob/master/text/2086-allow-if-let-irrefutables.md)。 + ## late_bound_lifetime_arguments +`late_bound_lifetime_arguments` lint 检测后绑定生命周期参数路径段中的泛型生命周期参数。 + +**样例** +```rust +struct S; + +impl S { + fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {} +} + +fn main() { + S.late::<'static>(&0, &0); +} +``` +显示如下: +```text +warning: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present + --> lint_example.rs:8:14 + | +4 | fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {} + | -- the late bound lifetime parameter is introduced here +... +8 | S.late::<'static>(&0, &0); + | ^^^^^^^ + | + = note: `#[warn(late_bound_lifetime_arguments)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #42868 +``` +**解释** + +如果将先绑定生命周期参数与同一参数列表中的后绑定生命周期参数混合在一起,则不清楚如何为其提供参数。目前,如果存在后绑定参数,提供显式参数将触发此 lint。因此将来解决方案可以被采用而不会遇到向后兼容性问题。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节以及先绑定和后绑定之间差异的描述请参阅 [issue #42868](https://github.com/rust-lang/rust/issues/42868)。 + ## mixed_script_confusables +`mixed_script_confusables` lint 检测在不同[脚本](https://en.wikipedia.org/wiki/Script_(Unicode))标识符(identifiers between different scripts)间的可视性易混淆的字符。 + +**样例** +```rust +# #![allow(unused)] +#![feature(non_ascii_idents)] + +# fn main() { +// The Japanese katakana character エ can be confused with the Han character 工. +const エ: &'static str = "アイウ"; +# } +``` +显示如下: +```text +warning: The usage of Script Group `Japanese, Katakana` in this crate consists solely of mixed script confusables + --> lint_example.rs:5:7 + | +5 | const エ: &'static str = "アイウ"; + | ^^ + | + = note: `#[warn(mixed_script_confusables)]` on by default + = note: The usage includes 'エ' (U+30A8). + = note: Please recheck to make sure their usages are indeed what you want. +``` +**解释** + +上面的 [`non_ascii_idents`][non-ascii-idents] 是只能用于 nightly的,其允许使用非 ASCII 字符作为标识符。该 lint 当不同脚本字符在视觉上看起来很像的时候发出警告,因为(长得像)这可能会造成混乱。 +如果 crate包含在相同的脚本中不会引起混淆的字符,那么此 lint 将不会被触发。例如,如果上例还有另一个带有片假名字符的标识符(例如 `let カタカナ = 123;`),然后就会表明你在有意使用片假名,并且不会对此发出警告。 +请注意,易混淆字符集可能会随时间而变化。注意,如果你将该 lint 等级调整为 “禁止”,则现有代码可能在将来会(编译)失败。 + ## mutable_borrow_reservation_conflict +`mutable_borrow_reservation_conflict` lint 检测与其他共享借用相冲突的第二阶段借用这种保留。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let mut v = vec![0, 1, 2]; +let shared = &v; +v.push(shared.len()); +# } +``` +显示如下: +```text +warning: cannot borrow `v` as mutable because it is also borrowed as immutable + --> lint_example.rs:4:1 + | +3 | let shared = &v; + | -- immutable borrow occurs here +4 | v.push(shared.len()); + | ^ ------ immutable borrow later used here + | | + | mutable borrow occurs here + | + = note: `#[warn(mutable_borrow_reservation_conflict)]` on by default + = warning: this borrowing pattern was not meant to be accepted, and may become a hard error in the future + = note: for more information, see issue #59159 +``` +**解释** + +这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。完整问题描述和一些可能的解决办法请参阅 [issue #59159](https://github.com/rust-lang/rust/issues/59159)。 + ## no_mangle_generic_items +`no_mangle_generic_items` lint 检测必须使用 改发(mangle)的泛型项。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +#[no_mangle] +fn foo(t: T) { + +} +# } +``` +显示如下: +```text +warning: functions generic over types or consts must be mangled + --> lint_example.rs:3:1 + | +2 | #[no_mangle] + | ------------ help: remove this attribute +3 | / fn foo(t: T) { +4 | | +5 | | } + | |_^ + | + = note: `#[warn(no_mangle_generic_items)]` on by default +``` +**解释** + +泛型函数必须改发其符号以适应泛型参数。[`no_mangle`](https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute) 属性对此无效,应该被移除。 + ## non_autolinks +`non_autolinks` lint 检测何时仅能用尖括号写入 URL 。这是一个仅用于 `rustdoc` 的lint,请查看 [rustdoc book](https://doc.rust-lang.org/rustdoc/lints.html#broken_intra_doc_links)中的文档。 + ## non_camel_case_types +`non_camel_case_types` lint 检测没有使用驼峰命名的类型、变量、trait 和类型参数。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +struct my_struct; +# } +``` +显示如下: +```text +warning: type `my_struct` should have an upper camel case name + --> lint_example.rs:2:8 + | +2 | struct my_struct; + | ^^^^^^^^^ help: convert the identifier to upper camel case: `MyStruct` + | + = note: `#[warn(non_camel_case_types)]` on by default +``` +**解释** + +标识符的首选样式是使用 ”驼峰大小写“,例如 `MyStruct`,其中首字母不应小写,且字母之间不应使用下划线。在标识符的开头和结尾以及非字母之间(例如 `X86_64`),都可以使用下划线。 + ## non_shorthand_field_patterns +`non_shorthand_field_patterns` lint 检测在模式中使用 `Struct { x: x }` 而非 `Struct { x }`。 + +**样例** +```rust +struct Point { + x: i32, + y: i32, +} + + +fn main() { + let p = Point { + x: 5, + y: 5, + }; + + match p { + Point { x: x, y: y } => (), + } +} +``` +显示如下: +```text +warning: the `x:` in this pattern is redundant + --> lint_example.rs:14:17 + | +14 | Point { x: x, y: y } => (), + | ^^^^ help: use shorthand field pattern: `x` + | + = note: `#[warn(non_shorthand_field_patterns)]` on by default +``` +**解释** + +首选的样式是避免在两个标识符相同的情况下重复指定字段名和绑定名。 + ## non_snake_case +`non_snake_case` lint 检测没有使用蛇形命名的变量、方法、函数、生命周期参数和模块。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let MY_VALUE = 5; +# } +``` +显示如下: +```text +warning: variable `MY_VALUE` should have a snake case name + --> lint_example.rs:2:5 + | +2 | let MY_VALUE = 5; + | ^^^^^^^^ help: convert the identifier to snake case: `my_value` + | + = note: `#[warn(non_snake_case)]` on by default +``` +**解释** +标识符首选样式是使用蛇形命名,即所有字符均为小写,单词之间用单个下划线分隔,例如 `my_value`。 + ## non_upper_case_globals +`non_upper_case_globals` lint 检测没有使用大写标识符的 static 项。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +static max_points: i32 = 5; +# } +``` +显示如下: +```text +warning: static variable `max_points` should have an upper case name + --> lint_example.rs:2:8 + | +2 | static max_points: i32 = 5; + | ^^^^^^^^^^ help: convert the identifier to upper case: `MAX_POINTS` + | + = note: `#[warn(non_upper_case_globals)]` on by default +``` +**解释** + +静态项命名的首选样式是都使用大写,例如 `MAX_POINTS`。 + ## nontrivial_structural_match +`nontrivial_structural_match` lint 检测用于模式中的常量,该常量类型是非结构化匹配(not structural match)的且初始化体实际上所使用的值是非结构化匹配的。所以如果常数仅是 `None`,`Option` 是对的。 + +**样例** +```rust +#![deny(nontrivial_structural_match)] + +#[derive(Copy, Clone, Debug)] +struct NoDerive(u32); +impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } } +impl Eq for NoDerive { } +fn main() { + const INDEX: Option = [None, Some(NoDerive(10))][0]; + match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), }; +} +``` +显示如下: +```text +error: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]` + --> lint_example.rs:9:47 + | +9 | match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), }; + | ^^^^^ + | +note: the lint level is defined here + --> lint_example.rs:1:9 + | +1 | #![deny(nontrivial_structural_match)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #73448 +``` +**解释** + +早期的 Rust 接受在模式中使用常量,甚至就算常量类型没有派生 `PartialEq` 。因此编译器会回退到 `PartialEq` 执行的运行时,即使两个常量位等效该运行时也报告为不等。 + ## overlapping_patterns +`overlapping_patterns` lint 检测具有重叠的[范围模式](https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns)的 `match` 分支。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let x = 123u8; +match x { + 0..=100 => { println!("small"); } + 100..=255 => { println!("large"); } +} +# } +``` +显示如下: +```text +warning: multiple patterns covering the same range + --> lint_example.rs:5:5 + | +4 | 0..=100 => { println!("small"); } + | ------- this range overlaps on `100_u8` +5 | 100..=255 => { println!("large"); } + | ^^^^^^^^^ overlapping patterns + | + = note: `#[warn(overlapping_patterns)]` on by default +``` +**解释** + + 在 match 表达式中重叠的范围模式可能是错误的。检查开始和结束值是否符合你的期望,并记住,使用 `..=` 时,左边界和右边界是包括在内的。 + ## path_statements +`path_statements` lint 检测无效的路径语句(path statements)。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let x = 42; + +x; +# } +``` +显示如下: +```text +warning: path statement with no effect + --> lint_example.rs:4:1 + | +4 | x; + | ^^ + | + = note: `#[warn(path_statements)]` on by default +``` +**解释** + +无效的语句通常是个错误。 + ## private_in_public +`private_in_public` lint 检测以前的实现没有捕获的公有接口中的私有项。 + +**样例** +```rust +# #![allow(unused)] +struct SemiPriv; + +mod m1 { + struct Priv; + impl super::SemiPriv { + pub fn f(_: Priv) {} + } +} +# fn main() {} +``` +显示如下: +```text +warning: private type `Priv` in public interface (error E0446) + --> lint_example.rs:7:9 + | +7 | pub fn f(_: Priv) {} + | ^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(private_in_public)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 +``` +**解释** + +可见性规则旨在防止公共接口公开私有项。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #34537](https://github.com/rust-lang/rust/issues/34537)。 + ## private_intra_doc_links +该 lint 是当从公有项链接到私有项时会发出警告的 `broken_intra_doc_links` 的子集。这是一个仅用于 `rustdoc` 的lint,请查看 [rustdoc book](https://doc.rust-lang.org/rustdoc/lints.html#broken_intra_doc_links)中的文档。 + + ## proc_macro_derive_resolution_fallback +`proc_macro_derive_resolution_fallback` lint 检测使用父模块中无法访问的名称的 proc 宏派生。 + +**样例** +```rust,ignore +// foo.rs +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::*; + +#[proc_macro_derive(Foo)] +pub fn foo1(a: TokenStream) -> TokenStream { + drop(a); + "mod __bar { static mut BAR: Option = None; }".parse().unwrap() +} +``` + +```rust,ignore +// bar.rs +#[macro_use] +extern crate foo; + +struct Something; + +#[derive(Foo)] +struct Another; + +fn main() {} +``` +显示如下: +```text +warning: cannot find type `Something` in this scope + --> src/main.rs:8:10 + | +8 | #[derive(Foo)] + | ^^^ names from parent modules are not accessible without an explicit import + | + = note: `#[warn(proc_macro_derive_resolution_fallback)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #50504 +``` +**解释** + +如果 proc-macro 生成了一个模块,则编译器会无意地允许该模块中的项引用 crate 根中的项而不用导入。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #50504](https://github.com/rust-lang/rust/issues/50504)。 + ## redundant_semicolons +`redundant_semicolons` lint 检测不必要的尾部分号。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let _ = 123;; +# } +``` +显示如下: +```text +warning: unnecessary trailing semicolon + --> lint_example.rs:2:13 + | +2 | let _ = 123;; + | ^ help: remove this semicolon + | + = note: `#[warn(redundant_semicolons)]` on by default +``` +**解释** + +多余的分号是不需要的,可以将其删除以避免混淆和视觉混乱。 + ## renamed_and_removed_lints +`renamed_and_removed_lints` lint 检测已经呗重命名或移除的 lint 。 + +**样例** +```rust +# #![allow(unused)] +#![deny(raw_pointer_derive)] +# fn main() { +# } +``` +显示如下: +```text +warning: lint `raw_pointer_derive` has been removed: `using derive with raw pointers is ok` + --> lint_example.rs:1:9 + | +1 | #![deny(raw_pointer_derive)] + | ^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(renamed_and_removed_lints)]` on by default +``` +**解释** + +要修复此问题,要么移除此 lint,要么使用推荐的新名字。这可以帮助避免与不再有效的 lint 的混淆,并且保持重命名后的 lint 的一致性。 + ## safe_packed_borrows +`safe_packed_borrows` lint 检测借用包装结构体对齐方式不是1 的字段。 + +**样例** +```rust +#[repr(packed)] +pub struct Unaligned(pub T); + +pub struct Foo { + start: u8, + data: Unaligned, +} + +fn main() { + let x = Foo { start: 0, data: Unaligned(1) }; + let y = &x.data.0; +} +``` +显示如下: +```text +warning: borrow of packed field is unsafe and requires unsafe function or block (error E0133) + --> lint_example.rs:11:13 + | +11 | let y = &x.data.0; + | ^^^^^^^^^ + | + = note: `#[warn(safe_packed_borrows)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #46043 + = note: fields of packed structs might be misaligned: dereferencing a misaligned pointer or even just creating a misaligned reference is undefined behavior +``` + +**解释** + +这种借用方式是不安全的,且可能再某些平台上导致错误和违反编译器所做的某些假设,以前无意中会允许这样做。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。有关如何解决该问题的引导和更多细节请参阅 [issue #46043](https://github.com/rust-lang/rust/issues/46043)。 + ## stable_features +`stable_features` lint 检测已经变为 stable 的 [`feature`](https://doc.rust-lang.org/nightly/unstable-book/) 属性。 + +**样例** +```rust +#![feature(test_accepted_feature)] +fn main() {} +``` +显示如下: +```text +warning: the feature `test_accepted_feature` has been stable since 1.0.0 and no longer requires an attribute to enable + --> lint_example.rs:1:12 + | +1 | #![feature(test_accepted_feature)] + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(stable_features)]` on by default +``` +**解释** + +当一个 feature 稳定后,就不再需要包含 `#![feature]` 属性了。要修复此问题,只需简单地移除 `#![feature]` 属性就行。 + ## temporary_cstring_as_ptr +`temporary_cstring_as_ptr` lint 检测临时获取 `CString` 的内部指针。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +# use std::ffi::CString; +let c_str = CString::new("foo").unwrap().as_ptr(); +# } +``` +显示如下: +```text +warning: getting the inner pointer of a temporary `CString` + --> lint_example.rs:4:42 + | +4 | let c_str = CString::new("foo").unwrap().as_ptr(); + | ---------------------------- ^^^^^^ this pointer will be invalid + | | + | this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime + | + = note: `#[warn(temporary_cstring_as_ptr)]` on by default + = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned + = help: for more information, see https://doc.rust-lang.org/reference/destructors.html +``` +**解释** + + `CString` 的内部指针 存活时间和其指向的 `CString` 一样长。获取临时的 `CString` 的内部指针允许在语句结尾释放 `CString` ,因为就类型系统而言,其并未被引用。这意味着在语句之外,该指针将会指向已释放的内存,如果之后解引用指针会导致未定义的行为。 + ## trivial_bounds +`trivial_bounds` lint 检测没有依赖任何参数的 triat 约束。 + +**样例** +```rust +# #![allow(unused)] +#![feature(trivial_bounds)] +# fn main() { +pub struct A where i32: Copy; +# } +``` +显示如下: +```text +warning: Trait bound i32: Copy does not depend on any type or lifetime parameters + --> lint_example.rs:3:25 + | +3 | pub struct A where i32: Copy; + | ^^^^ + | + = note: `#[warn(trivial_bounds)]` on by default +``` +**解释** + +通常,你不会写出一个你知道它永远是对的,或者永远不对的 trait 约束。然而,使用宏时,宏可能不知道在生成代码时约束是否成立。当前,如果约束始终正确,编译器不会警告你;如果约束不对,编译器会生成错误。在这两种情况下, `trivial_bounds` feature 都将其更改为警告,从而使得宏有更大的自由度和灵活性来生成代码,同时在产生存在问题的非宏代码时会发出相应信号表明存在问题。 +更多细节请参阅 [RFC 2056](https://github.com/rust-lang/rfcs/blob/master/text/2056-allow-trivial-where-clause-constraints.md)。该 feature 目前仅在 nightly channel 有效,跟踪问题请参阅 [issue #48214](https://github.com/rust-lang/rust/issues/48214)。 + ## type_alias_bounds +`type_alias_bounds` lint 检测类型别名中的约束。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +type SendVec = Vec; +# } +``` +显示如下: +```text +warning: bounds on generic parameters are not enforced in type aliases + --> lint_example.rs:2:17 + | +2 | type SendVec = Vec; + | ^^^^ + | + = note: `#[warn(type_alias_bounds)]` on by default +help: the bound will not be checked when the type alias is used, and should be removed + | +2 | type SendVec = Vec; + | -- +``` +**解释** + +类型别名中的 trait 约束 当前是被忽略的,并且不应该包含在内以免造成混淆。以前会无意中允许这样做,这在将来可能会转换为固有错误。 + ## tyvar_behind_raw_pointer +`tyvar_behind_raw_pointer` lint 检测指向推断变量(inference variable)的原生指针(raw pointer)。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +// edition 2015 +let data = std::ptr::null(); +let _ = &data as *const *const (); + +if data.is_null() {} +# } +``` +显示如下: +```text +warning: type annotations needed + --> lint_example.rs:6:9 + | +6 | if data.is_null() {} + | ^^^^^^^ + | + = note: `#[warn(tyvar_behind_raw_pointer)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition! + = note: for more information, see issue #46906 +``` +**解释** + +这种推断以前是允许的,但是随着将来 [arbitrary self type](https://github.com/rust-lang/rust/issues/44874)的引入,这可能会引起歧义。要解决此问题,请使用显式类型而非依赖类型推导。 +这是个[将来不兼容][future-incompatible] 的 lint ,在 2018 版本中会转化为固有错误。更多细节请参阅 [issue #46906](https://github.com/rust-lang/rust/issues/46906) 。目前在 2018 版本中是个固有错误,在2018版本中默认等级是警告。 + ## uncommon_codepoints +`uncommon_codepoints` lint 检测在标识符中不常见的 Unicode 字符码(codepoint)。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +# #![allow(unused)] +#![feature(non_ascii_idents)] +const µ: f64 = 0.000001; +# } +``` +显示如下: +```text +warning: identifier contains uncommon Unicode codepoints + --> lint_example.rs:4:7 + | +4 | const µ: f64 = 0.000001; + | ^ + | + = note: `#[warn(uncommon_codepoints)]` on by default +``` +**解释** + +上面的 [`non_ascii_idents`][non-ascii-idents] 是只能用于 nightly的,其允许使用非 ASCII 字符作为标识符。该 lint发出警告不要使用不常用字符,并且可能会引起视觉混乱。 +该 lint 由包含不属于 “Allowed” 字符码集的字符码的标识符所触发,该 “Allowed” 字符码集被描述为 [Unicode® Technical Standard #39 Unicode Security Mechanisms Section 3.1 General Security Profile for Identifiers](https://www.unicode.org/reports/tr39/#General_Security_Profile)。 + ## unconditional_recursion +`unconditional_recursion` lint 检测不调用自身无法返回的函数。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +fn foo() { + foo(); +} +# } +``` +显示如下: +```text +warning: function cannot return without recursing + --> lint_example.rs:2:1 + | +2 | fn foo() { + | ^^^^^^^^ cannot return without recursing +3 | foo(); + | ----- recursive call site + | + = note: `#[warn(unconditional_recursion)]` on by default + = help: a `loop` may express intention better if this is on purpose +``` +**解释** + +进行没有一定条件终止的递归调用通常是个错误。如果确实想要进行无限循环,推荐使用 `loop` 表达式。 + ## uninhabited_static +`uninhabited_static` lint 检测 uninhabited 静态项。(译者想把 uninhabited 翻译为空巢,但是想想还是算了,太花哨了) + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +enum Void {} +extern { + static EXTERN: Void; +} +# } +``` +显示如下: +```text +warning: static of uninhabited type + --> lint_example.rs:4:5 + | +4 | static EXTERN: Void; + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(uninhabited_static)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #74840 + = note: uninhabited statics cannot be initialized, and any access would be an immediate error +``` +**解释** + +一个 uninhabited 的 static 类型永远不会被初始化,因此无法定义。然而,该问题可以使用 `extern static` 来避开。(uninhabited static)假定其没有初始化的 uninhabited 的地方(例如本地或静态变量)。(uninhabited static)确实被允许这么做,但是其正在被淘汰。 + ## unknown_lints +`unknown_lints` lint 无法识别的 lint 属性。 + +**样例** +```rust +# #![allow(unused)] +#![allow(not_a_real_lint)] +# fn main() { +# } +``` +显示如下: +```text +warning: unknown lint: `not_a_real_lint` + --> lint_example.rs:1:10 + | +1 | #![allow(not_a_real_lint)] + | ^^^^^^^^^^^^^^^ + | + = note: `#[warn(unknown_lints)]` on by default +``` +**解释** + +指定一个不存在的 lint 通常是个错误。检查拼写和 lint 列表中的正确名称是否相同。同时考虑是否是在使用旧版本的编译器,而此 lint 仅在新版本中可用。 + ## unnameable_test_items +`unnameable_test_items` lint 检测不能由测试工具运行的 [`#[test]`](https://doc.rust-lang.org/reference/attributes/testing.html#the-test-attribute) 函数,因为它们处于不可命名的地方。 + +**样例** +```rust +fn main() { + #[test] + fn foo() { + // This test will not fail because it does not run. + assert_eq!(1, 2); + } +} +``` +显示如下: +```text +warning: cannot test inner items + --> lint_example.rs:2:5 + | +2 | #[test] + | ^^^^^^^ + | + = note: `#[warn(unnameable_test_items)]` on by default + = note: this warning originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info) +``` +**解释** + +为了让测试工具能够进行测试,测试函数必须位于可以从 crate 根访问的位置。通常来说,这意味着必须在模块中定义它,而不是在其他地方,例如在另一个函数中定义。编译器以前允许这样做而没有发出错误消息,因此添加了一个 lint 发出未使用测试的警告。如今尚未确定是否应该允许这么做,请参阅 [RFC 2471](https://github.com/rust-lang/rfcs/pull/2471#issuecomment-397414443) 和 [issue #36629](https://github.com/rust-lang/rust/issues/36629) 。 + ## unreachable_code +`unreachable_code` lint 检测无法到达的代码路径。 + +**样例** +```rust,ignore +# #![allow(unused)] +# fn main() { +panic!("we never go past here!"); + +let x = 5; +# } +``` +显示如下: +```text +warning: unreachable statement + --> lint_example.rs:4:1 + | +2 | panic!("we never go past here!"); + | --------------------------------- any code following this expression is unreachable +3 | +4 | let x = 5; + | ^^^^^^^^^^ unreachable statement + | + = note: `#[warn(unreachable_code)]` on by default +``` +**解释** + +无法到达的代码可能意味着是个错误或代码未完成。如果代码不再使用,请考虑移除它。 + ## unreachable_patterns +`unreachable_patterns` lint 检测无法到达的模式。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let x = 5; +match x { + y => (), + 5 => (), +} +# } +``` +显示如下: +```text +warning: unreachable pattern + --> lint_example.rs:5:5 + | +4 | y => (), + | - matches any value +5 | 5 => (), + | ^ unreachable pattern + | + = note: `#[warn(unreachable_patterns)]` on by default +``` +**解释** + +这通常意味着模式的指定或顺序有误。在上例中,`y` 模式总是会匹配,所以 5 是不可能到达的。记住,match 分支是按顺序匹配的,你可以将 `5` 调整到 `y` 的上面。 + ## unstable_name_collisions +`unstable_name_collisions` lint 检测使用了标准库计划在将来添加的名称。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +trait MyIterator : Iterator { + // is_sorted is an unstable method that already exists on the Iterator trait + fn is_sorted(self) -> bool where Self: Sized {true} +} + +impl MyIterator for T where T: Iterator { } + +let x = vec![1,2,3]; +let _ = x.iter().is_sorted(); +# } +``` +显示如下: +```text +warning: a method with this name may be added to the standard library in the future + --> lint_example.rs:10:18 + | +10 | let _ = x.iter().is_sorted(); + | ^^^^^^^^^ + | + = note: `#[warn(unstable_name_collisions)]` on by default + = warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior! + = note: for more information, see issue #48919 + = help: call with fully qualified syntax `MyIterator::is_sorted(...)` to keep using the current method + = help: add `#![feature(is_sorted)]` to the crate attributes to enable `is_sorted` +``` +**解释** + +当标准库中的 trait 添加了新方法之时,它们通常在具有 `feature` 属性的 nightly channel 中以 “unstable” 形式添加。如果有任何之前已存在的代码扩展了具有同名方法的 trait,则这些名称将会发生冲突。将来,当方法稳定后,由于歧义将会造成错误。该 lint 是一个让你知道将来可能会发生碰撞的预警。可以通过添加类型注解来消除要调用的 trait 方法的歧义避免此歧义,例如 `MyIterator::is_sorted(my_iter)` 或重名或删除该方法。 + ## unused_allocation +`unused_allocation` lint 检测可以被消除的不必要的(内存)分配。 + +**样例** +```rust +#![feature(box_syntax)] +fn main() { + let a = (box [1,2,3]).len(); +} +``` +显示如下: +```text +warning: unnecessary allocation, use `&` instead + --> lint_example.rs:3:13 + | +3 | let a = (box [1,2,3]).len(); + | ^^^^^^^^^^^^^ + | + = note: `#[warn(unused_allocation)]` on by default +``` +**解释** + +当一个 `box` 表达式立即被强转为引用时,说明这个分配时不必要的,且应该使用引用( 使用 `&` 或 `&mut` )来避免引用。 + ## unused_assignments +`unused_assignments` lint 检测从未被读取的赋值。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let mut x = 5; +x = 6; +# } +``` +显示如下: +```text +warning: value assigned to `x` is never read + --> lint_example.rs:3:1 + | +3 | x = 6; + | ^ + | + = note: `#[warn(unused_assignments)]` on by default + = help: maybe it is overwritten before being read? +``` +**解释** + +未使用的赋值可能意味着是个错误或未完成的代码。如果变量自赋值之后就从未被使用,那么这个赋值也可以被移除。带有下划线前缀的变量例如 `_x` 将不会触发此 lint 。 + ## unused_attributes +`unused_attributes` lint 检测编译器未使用的属性。 + +**样例** +```rust +# #![allow(unused)] +#![ignore] +# fn main() { +# } +``` +显示如下: +```text +warning: unused attribute + --> lint_example.rs:1:1 + | +1 | #![ignore] + | ^^^^^^^^^^ + | + = note: `#[warn(unused_attributes)]` on by default +``` +**解释** + +未使用的[属性](https://doc.rust-lang.org/reference/attributes.html)可能意味着属性放在了错误的位置。考虑移除它,或将其放在正确的地方。还应考虑是否使用属性所在项的内部属性(用 `!`,例如`#![allow(unused)]`),或者应用于属性后面项的外部属性(没有 `!`,例如 `[allow(unused)]`)。 + ## unused_braces +`unused_braces` lint 检测表达式周边不必要的大括号。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +if { true } { + // ... +} +# } +``` +显示如下: +```text +warning: unnecessary braces around `if` condition + --> lint_example.rs:2:4 + | +2 | if { true } { + | ^^^^^^^^ help: remove these braces + | + = note: `#[warn(unused_braces)]` on by default +``` +**解释** + +该大括号是不需要的,应该将其移除。这是编写这些表达式的首选样式。 + ## unused_comparisons +`unused_comparisons` lint 检测由于所涉及类型的限制而变得无用的比较。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +fn foo(x: u8) { + x >= 0; +} +# } +``` +显示如下: +```text +warning: comparison is useless due to type limits + --> lint_example.rs:3:5 + | +3 | x >= 0; + | ^^^^^^ + | + = note: `#[warn(unused_comparisons)]` on by default +``` +**解释** + +一个无用的比较或许表明是一个错误,或者应该被修复或移除。 + ## unused_doc_comments +`unused_doc_comments` lint 检测并未用于 `rustdoc` 的文档注释。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +/// docs for x +let x = 12; +# } +``` +显示如下: +```text +warning: unused doc comment + --> lint_example.rs:2:1 + | +2 | /// docs for x + | ^^^^^^^^^^^^^^ +3 | let x = 12; + | ----------- rustdoc does not generate documentation for statements + | + = note: `#[warn(unused_doc_comments)]` on by default +``` +**解释** + +`rustdoc` 并不会使用所有地方的文档注释,因此某些文档注释将会被忽略。尝试使用 // 将其改为普通注释,以免出现警告。 + ## unused_features +`unused_features` lint 检测未使用或未知的在 crate-level [`feature`](https://doc.rust-lang.org/nightly/unstable-book/) 属性找到的 feature 。 + +注意:该 lint 目前还无法运作,更多细节请参阅 [issue #44232](https://github.com/rust-lang/rust/issues/44232)。 + ## unused_imports +`unused_imports` lint 检测从未被使用的 import 项。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +use std::collections::HashMap; +# } +``` +显示如下: +```text +warning: unused import: `std::collections::HashMap` + --> lint_example.rs:2:5 + | +2 | use std::collections::HashMap; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default +``` +**解释** + +未使用的 import 项可能意味着是个错误或未完成的代码,并且会使代码混乱,应该将其移除。如果要重导出项使得在模块外可用,请添加可见修饰符如 `pub` 。 + ## unused_labels +`unused_labels` lint 检测未使用的 [标记](https://doc.rust-lang.org/reference/expressions/loop-expr.html#loop-labels)。 + +**样例** + +```rust +# #![allow(unused)] +# fn main() { +'unused_label: loop {} +# } +``` +显示如下: +```text +warning: unused label + --> lint_example.rs:2:1 + | +2 | 'unused_label: loop {} + | ^^^^^^^^^^^^^ + | + = note: `#[warn(unused_labels)]` on by default +``` +**解释** + +未使用的标记可能意味着是个错误或未完成的代码。要使单个标记的该警告沉默,在前面添加下划线,如 `'_my_label:`。 + ## unused_macros +`unused_macros` lint 检测从未使用的宏。 + +**样例** +```rust +macro_rules! unused { + () => {}; +} + +fn main() { +} +``` +显示如下: +```text +warning: unused macro definition + --> lint_example.rs:1:1 + | +1 | / macro_rules! unused { +2 | | () => {}; +3 | | } + | |_^ + | + = note: `#[warn(unused_macros)]` on by default +``` +**解释** + +未使用的宏可能意味着是个错误或未完成的代码。使单个宏的该警告沉默,在前面添加下划线,如 `'_my_macro`。如果想要导出宏以使其在 crate 之外可用,请使用 [`macro_export`](https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope) 属性。 + ## unused_must_use +`unused_must_use` lint 检测被标记为 `#[must_use]` 却没被使用的类型的 result 。 + +**样例** +```rust +fn returns_result() -> Result<(), ()> { + Ok(()) +} + +fn main() { + returns_result(); +} +``` +显示如下: +```text +warning: unused `std::result::Result` that must be used + --> lint_example.rs:6:5 + | +6 | returns_result(); + | ^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_must_use)]` on by default + = note: this `Result` may be an `Err` variant, which should be handled +``` +**解释** + +`#[must_use]` 属性表明忽略返回值是个错误。更多细节请参阅 [reference](https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute)。 + ## unused_mut +`unused_mut` lint 不需要可变的可变变量。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +let mut x = 5; +# } +``` +显示如下: +```text +warning: variable does not need to be mutable + --> lint_example.rs:2:5 + | +2 | let mut x = 5; + | ----^ + | | + | help: remove this `mut` + | + = note: `#[warn(unused_mut)]` on by default +``` +**解释** + +首选样式是仅在需要时才将变量标记为 mut 。 + ## unused_parens +`unused_parens` lint 检测 `if`,`match`,`while`和`return` 带有圆括号,它们不需要圆括号。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +if(true) {} +# } +``` +显示如下: +```text +warning: unnecessary parentheses around `if` condition + --> lint_example.rs:2:3 + | +2 | if(true) {} + | ^^^^^^ help: remove these parentheses + | + = note: `#[warn(unused_parens)]` on by default +``` +**解释** + +圆括号是不需要的,应该被移除。这是这些表达式的首选样式。 + ## unused_unsafe +`unused_unsafe` lint 检测不必要的 `unsafe` 块的使用。 + +**样例** +```rust +# #![allow(unused)] +# fn main() { +unsafe {} +# } +``` +显示如下: +```text +warning: unnecessary `unsafe` block + --> lint_example.rs:2:1 + | +2 | unsafe {} + | ^^^^^^ unnecessary `unsafe` block + | + = note: `#[warn(unused_unsafe)]` on by default +``` +**解释** + +如果块中没有内容需要 `unsafe`,应该移除 `unsafe` 标记,因为其不是必要并且可能会造成混乱。 + ## unused_variables +`unused_variables` lint 检测未以任何方式使用的变量。 + +**样例** +```rust + +# #![allow(unused)] +# fn main() { +let x = 5; +# } +``` +显示如下: +```text +warning: unused variable: `x` + --> lint_example.rs:2:5 + | +2 | let x = 5; + | ^ help: if this is intentional, prefix it with an underscore: `_x` + | + = note: `#[warn(unused_variables)]` on by default +``` +**解释** + +未使用变量可能意味着是个错误或未完成的代码。要对单个变量使该警告沉默,前缀加上下划线例如 `_x` 。 + ## warnings +`warnings` lint 允许你更改那些将会产生警告的 lint 的等级 为其他等级。 + +**样例** +```rust +#![deny(warnings)] +# fn main() { +fn foo() {} +} +``` +显示如下: +```text +error: function is never used: `foo` + --> lint_example.rs:3:4 + | +3 | fn foo() {} + | ^^^ + | +note: the lint level is defined here + --> lint_example.rs:1:9 + | +1 | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]` +``` +**解释** + +`warning` lint 有点特殊;通过改变其等级,可以改变所有其他会根据你想要的任何值生成警告的警告。这样说来,你永远不会在代码中直接触发此 lint 。 + ## where_clauses_object_safety -## while_true \ No newline at end of file +`where_clauses_object_safety` lint 检测 [where 子句](https://doc.rust-lang.org/reference/items/generics.html#where-clauses)的 [对象安全](https://doc.rust-lang.org/reference/items/traits.html#object-safety)。 + +**样例** +```rust +trait Trait {} + +trait X { fn foo(&self) where Self: Trait; } + +impl X for () { fn foo(&self) {} } + +impl Trait for dyn X {} + +// Segfault at opt-level 0, SIGILL otherwise. +pub fn main() { ::foo(&()); } +``` +显示如下: +```text +warning: the trait `X` cannot be made into an object + --> lint_example.rs:3:14 + | +3 | trait X { fn foo(&self) where Self: Trait; } + | ^^^ + | + = note: `#[warn(where_clauses_object_safety)]` on by default + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #51443 +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> lint_example.rs:3:14 + | +3 | trait X { fn foo(&self) where Self: Trait; } + | - ^^^ ...because method `foo` references the `Self` type in its `where` clause + | | + | this trait cannot be made into an object... + = help: consider moving `foo` to another trait +``` + +**解释** + +编译器以前会允许这些对象不安全的约束,这是不安全的。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #51443](https://github.com/rust-lang/rust/issues/51443)。 + +## while_true +`while_true` lint 检测 `while true { }`。 + +**样例** +```rust + +# #![allow(unused)] +# fn main() { +while true { + +} +# } +``` +显示如下: +```text +warning: denote infinite loops with `loop { ... }` + --> lint_example.rs:2:1 + | +2 | while true { + | ^^^^^^^^^^ help: use `loop` + | + = note: `#[warn(while_true)]` on by default +``` + +**解释** +`while true` 应该被 `loop` 所代替。`loop` 表达式是编写无限循环的首选方法因为其直接表达了无限循环的意图。 + + + +[future-incompatible]:https://doc.rust-lang.org/rustc/lints/index.html#future-incompatible-lints +[identifier-pattern]:https://doc.rust-lang.org/reference/patterns.html#identifier-patterns +[non-ascii-idents]:https://doc.rust-lang.org/nightly/unstable-book/language-features/non-ascii-idents.html \ No newline at end of file From 4494de6d3184f5261d58f0eadd4c0aa983b927dd Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Thu, 28 Jan 2021 16:20:55 +0800 Subject: [PATCH 05/11] add alternate site --- README.md | 3 +++ src/lints/listing/warn-by-default.md | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a7d3e2c..535aaff 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ [Gitee](http://chen0adapter.gitee.io/rustc-book-cn) + + +[上海服务器结点加速](http://chen0adapter.top/books/rustc_book_cn/index.html) \ No newline at end of file diff --git a/src/lints/listing/warn-by-default.md b/src/lints/listing/warn-by-default.md index 6b1abe9..936ceb4 100644 --- a/src/lints/listing/warn-by-default.md +++ b/src/lints/listing/warn-by-default.md @@ -1,7 +1,5 @@ # Warn-by-default lints -(译者注:本章尚未翻译) - 这些 lint 的默认等级被设置为 “警告”。 + [`array_into_iter`](#array_into_iter) From 3e7df3464f3827bf6d83ac5c17538351e93f8262 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Sat, 20 Feb 2021 18:22:44 +0800 Subject: [PATCH 06/11] add 5 && part of 4 --- book.toml | 4 + src/codegen-options/index.md | 495 ++++++++++++++++++++++++++- src/json.md | 193 ++++++++++- src/lints/listing/deny-by-default.md | 75 +++- 4 files changed, 746 insertions(+), 21 deletions(-) diff --git a/book.toml b/book.toml index cc161eb..bec3571 100644 --- a/book.toml +++ b/book.toml @@ -5,6 +5,10 @@ author = "The Rust Project Developers" translator = "1024chen" language = "zh-CN" +[output.html] +git-repository-url = "https://github.com/1024chen/irwa-cn" +git-repository-icon = "fa-github" + [output.html.playpen] editable = true editor = "ace" \ No newline at end of file diff --git a/src/codegen-options/index.md b/src/codegen-options/index.md index 4350db5..c2442b3 100644 --- a/src/codegen-options/index.md +++ b/src/codegen-options/index.md @@ -1 +1,494 @@ -# Codegen options +# Codegen 选项 +> (译者注:Codegen,即 Code generation ,代码生成 ,所以本文是在说各种代码生成选项。) + +所有的这些选项都通过 `-C` 标签传递给 `rustc` ,是 "codegen" 的缩写。通过运行 `rustc -C help` + +## ar + +这个选项是被废弃的并且没什么用。 + +## code-model + +这个选项让你可以选择要使用的代码模型。 \ +代码模型对程序及其符号可能使用的地址范围进行了约束。 \ +有了更小的地址范围,机器指令就可以使用更紧凑的寻址模式。 + +该具体范围依赖于目标体系结构及其可用的寻址模式。 \ +对于 x86 体系结构,更多细节性的描述可以在 [System V 应用程序二进制接口](https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf) 规范中找到。 + +该选项支持的值有: + +- `tiny` - 微代码模型。 +- `small` - 小代码模型。这是大多数所支持的目标的默认模式。 +- `kernel` - 内核代码模式。 +- `medium` - 中型代码模式。 +- `large` - 大型代码模式。 + +也可以通过运行 `rustc --print code-models` 查找受支持的值(也就是平台可用的代码模型)。 + +## codegen-units + +该标签控制将 crate 分隔进多少个代码生成单元,这个(代码生成单元数)大于 0 。 + +当一个 crate 被分割进多个代码生成单元,LLVM 就可以并行处理它们。提高并行性或许会加快编译时间(缩短编译耗时),但是也可能会产生更慢的代码。设置该标签为 1 可能会提升生成代码的性能,但是也可能会编译得更慢。 + +如果没有指明默认值,对于非增量构建默认值就是 16 。对于增量构建,默认值是 256,可以使缓存更加细粒度。 + +## control-flow-guard + +该标签控制 LLVM 是否启用 Windows 的 [控制流守卫](https://docs.microsoft.com/en-us/windows/win32/secbp/control-flow-guard) 平台安全功能。该标签当前会忽略非 Windows 目标(即在非 Windows 平台上不会生效)。它使用下列值中的一个: + +* `y`, `yes`, `on`, `checks`, 或者没有值: 启用控制流守卫。 +* `nochecks`: 不进行运行时强制检查的情况下触发控制流守卫元数据(这理应只用于测试目的,因为它不提供安全强制)。 +* `n`, `no`, `off`: 不启用控制流守卫(默认值)。 + +## debug-assertions + +该标签让你可以打开或关闭 `cfg(debug_assertions)` [条件编译](https://doc.rust-lang.org/reference/conditional-compilation.html#debug_assertions)。其采用以下值之一: + +* `y`, `yes`, `on`, 或者无值 :开启 debug-assertions。 +* `n`, `no`, 或 `off` : 禁用 debug-assertions。 + +如果没有指明( debug-assertions ),仅在 [opt-level](#opt-level) 是 0 的时候开启 debug assertions 。 + +## debuginfo + +该标签控制调试信息的生成。其采用以下值之一: + +* `0`: 没有任何调试信息 (默认)。 +* `1`: 仅在行表中。 +* `2`: 完整的调试信息。 + +注意:[`-g` 标签][option-g-debug] 是 `-C debuginfo=2` 的别名。 + +## default-linker-libraries + +该标签控制链接器是否包含其默认链接库。其采用以下值之一: + +* `y`, `yes`, `on`, 或者没有值 : 包含默认库(默认值)。 +* `n`。 `no`, 或者 `off`: 除开默认库。 + +例如,对于 gcc flavor 链接器,这会向链接器传递 `-nodefaultlibs` 标签。 + +## embed-bitcode + +该标签控制编译器是否将 LLVM 位码嵌入目标文件(object files)中。其采用以下值之一: + +* `y`, `yes`, `on`, 或者无值: 将位码放入 rlib (默认值)。 +* `n`, `no`, 或 `off`: 省略 rlib 中的位码。 + +当 rustc 执行 LTO (link-time optimization ,链接时优化)时需要 LLVM 位码,在像 iOS 这样的目标平台上也需要。被嵌入的位码将会出现在由 rustc 生成的目标文件中,其名称由目标平台定义,大多数时候是 `.llvmbc` 。 + +如果你的编译实际上不需要位码(例如,如果你不针对 iOS 进行编译或不执行 LTO ),`-C embed-bitcode=no` 的使用可以大大缩短编译时间并且减少生成文件的大小.由于这些原因,Cargo 会尽可能使用 `-C embed-bitcode=no` 。同样,如果你直接用 rustc 进行构建,我们推荐你在不使用 LTO 的时候使用 `-C embed-bitcode=no` 。 + +如果结合 `-C lto`,`-C embed-bitcode=no` 将会导致 `rustc` 启动时就中止,因为该(标签)组合是无效的。 + +> **注意**: 如果你使用 LTO 构建 Rust 代码,你就可能甚至不需要打开 `embed-bitcode` 选项。你可能会想要使用 `-Clinker-plugin-lto` 来代替,它会完全跳过生成目标文件,并且用 LLVM 位码来简单替换目标文件。唯一的使用 `-Cembed-bitcode` 的目的是当你要生成同时使用和不使用 LTO 的 rlib 时,例如 Rust 标准库带有嵌入的位码,因为用户可以使用或不使用 LTO 进行链接。 +> +> 这也可能会让你想知道为什么该选项的默认值是 `yes` 。理由是在 1.45 中及更早版本中是这样的。在 1.45 中,将此选项默认值调整为关闭。 + +## extra-filename + +该选项允许你将额外的数据放进每个输出文件。它需要一个字符串作为后缀添加到文件名中。更多信息请参阅 [`--emit` 标签][option-emit] 。 + +## force-frame-pointers + +该标签可以强制栈帧指针的使用。其采用以下值之一: + +* `y`, `yes`, `on`, 或者无值: 强制启用栈帧指针。 +* `n`, `no`, 或 `off`: 不强制启用栈帧指针,但这并不意味着移除栈帧指针。 + +如果没有强制启用栈帧指针,则默认行为取决于目标平台。 + +## force-unwind-tables + +该标签强制栈回溯表的生成。其采用以下值之一: + +* `y`, `yes`, `on`, 或者无值: 强制生成栈回溯表。 +* `n`, `no`, 或 `off`: 不强制生成栈回溯表。如果目标平台或 `-C panic=unwind` 需要栈回溯表,就会触发一个错误。 + +默认值如果没有指明则取决于目标平台。 + +## incremental + +该标签允许你启用增量编译,这允许 `rustc` 在编译完 crate 之后保存信息,以便重新编译 crate 的时候可以重用,缩短重编译的时间。这将采用一个存放增量文件的目录的路径。 + +## inline-threshold + +该选项使你可以设置内联函数的默认阈值。其值采用无符号整数。内联基于成本模型( cost model ),较高的阈值将会允许更多的内联。 + +默认(阈值)取决于 [opt-level](#opt-level): + +| opt-level | Threshold | +|-----------|-----------| +| 0 | N/A, 仅内联强制内联函数( always-inline functions ) | +| 1 | N/A, 仅内联强制内联函数和 LLVM 生命周期内联函数 ( LLVM lifetime intrinsics ) | +| 2 | 225 | +| 3 | 275 | +| s | 75 | +| z | 25 | + +## link-arg + +该标签使你可以在链接器调用后附加一个额外的参数。 + +“附加” 是很重要的;你可以多次传递该标签以添加多个参数。 + +## link-args + +该标签使你可以在链接器调用后附加多个额外的参数。该选项(后的参数)应该用空格分隔。 + +## link-dead-code + +该标签控制链接器是否保留无效代码。其采用以下值之一: + +* `y`, `yes`, `on`, 或者无值: 保留无效代码。 +* `n`, `no`, 或 `off`: 移除无效代码(默认值)。 + +该标签的一个有用的例子是当试图构造代码覆盖率指标的时候(译者注:也就是说试图保留无效代码,咳咳。。)。 + +## link-self-contained + +在支持该标签的目标平台上,该标签控制链接器使用 Rust 附带的库和对象,还是使用系统中的库和对象。其采用以下值之一: + +* 无值: 如果系统已经有了必要的工具, rustc 将使用启发式禁用 self-contained 模式。 +* `y`, `yes`, `on`: 仅使用 Rust 附带的库/对象。 +* `n`, `no`, 或 `off`: 取决于用户或链接器(是否)提供非 Rust 的 库/对象。 + +当检测失败或用户想要使用附带的库时,这可以覆盖大多数情况。 + +## linker + +该标签控制 `rustc` 调用哪个链接器来链接代码。它接受链接器可执行文件的路径。如果该标签未指明,使用的链接器就会根据目标来推断。另外,请参阅 [linker-flavor](#linker-flavor) 标签 —— 指定链接器的另一种方法。 + +## linker-flavor + +This flag controls the linker flavor used by `rustc`. If a linker is given with +the [`-C linker` flag](#linker), then the linker flavor is inferred from the +value provided. If no linker is given then the linker flavor is used to +determine the linker to use. Every `rustc` target defaults to some linker +flavor. Valid options are: + +* `em`: use [Emscripten `emcc`](https://emscripten.org/docs/tools_reference/emcc.html). +* `gcc`: use the `cc` executable, which is typically gcc or clang on many systems. +* `ld`: use the `ld` executable. +* `msvc`: use the `link.exe` executable from Microsoft Visual Studio MSVC. +* `ptx-linker`: use + [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker) for Nvidia + NVPTX GPGPU support. +* `wasm-ld`: use the [`wasm-ld`](https://lld.llvm.org/WebAssembly.html) + executable, a port of LLVM `lld` for WebAssembly. +* `ld64.lld`: use the LLVM `lld` executable with the [`-flavor darwin` + flag][lld-flavor] for Apple's `ld`. +* `ld.lld`: use the LLVM `lld` executable with the [`-flavor gnu` + flag][lld-flavor] for GNU binutils' `ld`. +* `lld-link`: use the LLVM `lld` executable with the [`-flavor link` + flag][lld-flavor] for Microsoft's `link.exe`. + +[lld-flavor]: https://lld.llvm.org/Driver.html + +## linker-plugin-lto + +This flag defers LTO optimizations to the linker. See +[linker-plugin-LTO](../linker-plugin-lto.md) for more details. It takes one of +the following values: + +* `y`, `yes`, `on`, or no value: enable linker plugin LTO. +* `n`, `no`, or `off`: disable linker plugin LTO (the default). +* A path to the linker plugin. + +More specifically this flag will cause the compiler to replace its typical +object file output with LLVM bitcode files. For example an rlib produced with +`-Clinker-plugin-lto` will still have `*.o` files in it, but they'll all be LLVM +bitcode instead of actual machine code. It is expected that the native platform +linker is capable of loading these LLVM bitcode files and generating code at +link time (typically after performing optimizations). + +Note that rustc can also read its own object files produced with +`-Clinker-plugin-lto`. If an rlib is only ever going to get used later with a +`-Clto` compilation then you can pass `-Clinker-plugin-lto` to speed up +compilation and avoid generating object files that aren't used. + +## llvm-args + +This flag can be used to pass a list of arguments directly to LLVM. + +The list must be separated by spaces. + +Pass `--help` to see a list of options. + +## lto + +This flag controls whether LLVM uses [link time +optimizations](https://llvm.org/docs/LinkTimeOptimization.html) to produce +better optimized code, using whole-program analysis, at the cost of longer +linking time. It takes one of the following values: + +* `y`, `yes`, `on`, `fat`, or no value: perform "fat" LTO which attempts to + perform optimizations across all crates within the dependency graph. +* `n`, `no`, `off`: disables LTO. +* `thin`: perform ["thin" + LTO](http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html). + This is similar to "fat", but takes substantially less time to run while + still achieving performance gains similar to "fat". + +If `-C lto` is not specified, then the compiler will attempt to perform "thin +local LTO" which performs "thin" LTO on the local crate only across its +[codegen units](#codegen-units). When `-C lto` is not specified, LTO is +disabled if codegen units is 1 or optimizations are disabled ([`-C +opt-level=0`](#opt-level)). That is: + +* When `-C lto` is not specified: + * `codegen-units=1`: disable LTO. + * `opt-level=0`: disable LTO. +* When `-C lto=true`: + * `lto=true`: 16 codegen units, perform fat LTO across crates. + * `codegen-units=1` + `lto=true`: 1 codegen unit, fat LTO across crates. + +See also [linker-plugin-lto](#linker-plugin-lto) for cross-language LTO. + +## metadata + +This option allows you to control the metadata used for symbol mangling. This +takes a space-separated list of strings. Mangled symbols will incorporate a +hash of the metadata. This may be used, for example, to differentiate symbols +between two different versions of the same crate being linked. + +## no-prepopulate-passes + +This flag tells the pass manager to use an empty list of passes, instead of the +usual pre-populated list of passes. + +## no-redzone + +This flag allows you to disable [the +red zone](https://en.wikipedia.org/wiki/Red_zone_\(computing\)). It takes one +of the following values: + +* `y`, `yes`, `on`, or no value: disable the red zone. +* `n`, `no`, or `off`: enable the red zone. + +The default behaviour, if the flag is not specified, depends on the target. + +## no-stack-check + +This option is deprecated and does nothing. + +## no-vectorize-loops + +This flag disables [loop +vectorization](https://llvm.org/docs/Vectorizers.html#the-loop-vectorizer). + +## no-vectorize-slp + +This flag disables vectorization using +[superword-level +parallelism](https://llvm.org/docs/Vectorizers.html#the-slp-vectorizer). + +## opt-level + +This flag controls the optimization level. + +* `0`: no optimizations, also turns on + [`cfg(debug_assertions)`](#debug-assertions) (the default). +* `1`: basic optimizations. +* `2`: some optimizations. +* `3`: all optimizations. +* `s`: optimize for binary size. +* `z`: optimize for binary size, but also turn off loop vectorization. + +Note: The [`-O` flag][option-o-optimize] is an alias for `-C opt-level=2`. + +The default is `0`. + +## overflow-checks + +This flag allows you to control the behavior of [runtime integer +overflow](../../reference/expressions/operator-expr.md#overflow). When +overflow-checks are enabled, a panic will occur on overflow. This flag takes +one of the following values: + +* `y`, `yes`, `on`, or no value: enable overflow checks. +* `n`, `no`, or `off`: disable overflow checks. + +If not specified, overflow checks are enabled if +[debug-assertions](#debug-assertions) are enabled, disabled otherwise. + +## panic + +This option lets you control what happens when the code panics. + +* `abort`: terminate the process upon panic +* `unwind`: unwind the stack upon panic + +If not specified, the default depends on the target. + +## passes + +This flag can be used to add extra [LLVM +passes](http://llvm.org/docs/Passes.html) to the compilation. + +The list must be separated by spaces. + +See also the [`no-prepopulate-passes`](#no-prepopulate-passes) flag. + +## prefer-dynamic + +By default, `rustc` prefers to statically link dependencies. This option will +indicate that dynamic linking should be used if possible if both a static and +dynamic versions of a library are available. There is an internal algorithm +for determining whether or not it is possible to statically or dynamically +link with a dependency. For example, `cdylib` crate types may only use static +linkage. This flag takes one of the following values: + +* `y`, `yes`, `on`, or no value: use dynamic linking. +* `n`, `no`, or `off`: use static linking (the default). + +## profile-generate + +This flag allows for creating instrumented binaries that will collect +profiling data for use with profile-guided optimization (PGO). The flag takes +an optional argument which is the path to a directory into which the +instrumented binary will emit the collected data. See the chapter on +[profile-guided optimization] for more information. + +## profile-use + +This flag specifies the profiling data file to be used for profile-guided +optimization (PGO). The flag takes a mandatory argument which is the path +to a valid `.profdata` file. See the chapter on +[profile-guided optimization] for more information. + +## relocation-model + +This option controls generation of +[position-independent code (PIC)](https://en.wikipedia.org/wiki/Position-independent_code). + +Supported values for this option are: + +#### Primary relocation models + +- `static` - non-relocatable code, machine instructions may use absolute addressing modes. + +- `pic` - fully relocatable position independent code, +machine instructions need to use relative addressing modes. \ +Equivalent to the "uppercase" `-fPIC` or `-fPIE` options in other compilers, +depending on the produced crate types. \ +This is the default model for majority of supported targets. + +#### Special relocation models + +- `dynamic-no-pic` - relocatable external references, non-relocatable code. \ +Only makes sense on Darwin and is rarely used. \ +If StackOverflow tells you to use this as an opt-out of PIC or PIE, don't believe it, +use `-C relocation-model=static` instead. +- `ropi`, `rwpi` and `ropi-rwpi` - relocatable code and read-only data, relocatable read-write data, +and combination of both, respectively. \ +Only makes sense for certain embedded ARM targets. +- `default` - relocation model default to the current target. \ +Only makes sense as an override for some other explicitly specified relocation model +previously set on the command line. + +Supported values can also be discovered by running `rustc --print relocation-models`. + +#### Linking effects + +In addition to codegen effects, `relocation-model` has effects during linking. + +If the relocation model is `pic` and the current target supports position-independent executables +(PIE), the linker will be instructed (`-pie`) to produce one. \ +If the target doesn't support both position-independent and statically linked executables, +then `-C target-feature=+crt-static` "wins" over `-C relocation-model=pic`, +and the linker is instructed (`-static`) to produce a statically linked +but not position-independent executable. + +## remark + +This flag lets you print remarks for optimization passes. + +The list of passes should be separated by spaces. + +`all` will remark on every pass. + +## rpath + +This flag controls whether [`rpath`](https://en.wikipedia.org/wiki/Rpath) is +enabled. It takes one of the following values: + +* `y`, `yes`, `on`, or no value: enable rpath. +* `n`, `no`, or `off`: disable rpath (the default). + +## save-temps + +This flag controls whether temporary files generated during compilation are +deleted once compilation finishes. It takes one of the following values: + +* `y`, `yes`, `on`, or no value: save temporary files. +* `n`, `no`, or `off`: delete temporary files (the default). + +## soft-float + +This option controls whether `rustc` generates code that emulates floating +point instructions in software. It takes one of the following values: + +* `y`, `yes`, `on`, or no value: use soft floats. +* `n`, `no`, or `off`: use hardware floats (the default). + +## target-cpu + +This instructs `rustc` to generate code specifically for a particular processor. + +You can run `rustc --print target-cpus` to see the valid options to pass +here. Each target has a default base CPU. Special values include: + +* `native` can be passed to use the processor of the host machine. +* `generic` refers to an LLVM target with minimal features but modern tuning. + +## target-feature + +Individual targets will support different features; this flag lets you control +enabling or disabling a feature. Each feature should be prefixed with a `+` to +enable it or `-` to disable it. + +Features from multiple `-C target-feature` options are combined. \ +Multiple features can be specified in a single option by separating them +with commas - `-C target-feature=+x,-y`. \ +If some feature is specified more than once with both `+` and `-`, +then values passed later override values passed earlier. \ +For example, `-C target-feature=+x,-y,+z -Ctarget-feature=-x,+y` +is equivalent to `-C target-feature=-x,+y,+z`. + +To see the valid options and an example of use, run `rustc --print +target-features`. + +Using this flag is unsafe and might result in [undefined runtime +behavior](../targets/known-issues.md). + +See also the [`target_feature` +attribute](../../reference/attributes/codegen.md#the-target_feature-attribute) +for controlling features per-function. + +This also supports the feature `+crt-static` and `-crt-static` to control +[static C runtime linkage](../../reference/linkage.html#static-and-dynamic-c-runtimes). + +Each target and [`target-cpu`](#target-cpu) has a default set of enabled +features. + +## tune-cpu + +This instructs `rustc` to schedule code specifically for a particular +processor. This does not affect the compatibility (instruction sets or ABI), +but should make your code slightly more efficient on the selected CPU. + +The valid options are the same as those for [`target-cpu`](#target-cpu). +The default is `None`, which LLVM translates as the `target-cpu`. + +This is an unstable option. Use `-Z tune-cpu=machine` to specify a value. + +Due to limitations in LLVM (12.0.0-git9218f92), this option is currently +effective only for x86 targets. + +[option-emit]: ../command-line-arguments.md#option-emit +[option-o-optimize]: ../command-line-arguments.md#option-o-optimize +[profile-guided optimization]: ../profile-guided-optimization.md +[option-g-debug]: ../command-line-arguments.md#option-g-debug diff --git a/src/json.md b/src/json.md index 3c54f9a..1feeb89 100644 --- a/src/json.md +++ b/src/json.md @@ -1 +1,192 @@ -# JSON Output +# JSON 输出 + +本章节介绍由 `rustc` 所发出的 JSON 的结构。可以通过 [`--error-format=json` 标签][option-error-format] 启用 JSON。可以使用 [`--json` 标签][option-json] 指定其他可以更改生成的消息以及消息的格式的选项。 + +JSON 消息每行都被发送到 stderr 。 + +如果使用 Rust 解析输出, [`cargo_metadata`](https://crates.io/crates/cargo_metadata) crate 提供了解析消息的一些支持。 + +当解析时,应注意与将来的格式更改保持向前兼容(译者注:向前兼容指的是以前的兼容以后的,而向后兼容就是以后的兼容以前的)。可选值可以为 `null` 。新的字段可能会添加。枚举字段如 “level” 或 “suggestion_applicability” 可以添加新的值。 + +## 诊断 (Diagnostics) + +诊断消息会提供错误或在编译中可能产生的问题。 `rustc` 提供有关诊断来源的详细信息,以及提示和建议。 + +诊断以 parent/child 关系排列,其中 parent 诊断是诊断的核心,附加的 children 提供了其他的上下文,帮助和信息。 + +诊断有如下格式: + +```javascript +{ + /* 主消息 */ + "message": "unused variable: `x`", + /* 诊断码 + 一些消息可能将这个值设置为 null 。 + */ + "code": { + /* 标识了触发哪个诊断的独一的字符串。 */ + "code": "unused_variables", + /* 可选字符串,解释关于诊断码的更多细节。*/ + "explanation": null + }, + /* 诊断信息的重要程度。 + 值可能是: + - "error": 无法编译的致命错误。 + - "warning": 可能的错误或预警。 + - "note": 诊断的附加信息或上下文。 + - "help": 解决诊断问题的建议。 + - "failure-note": 为了获取更多信息的附带说明。 + - "error: internal compiler error": 指明编译器内部的错误。 + */ + "level": "warning", + /* 一个源代码位置数组,指出有关诊断来源的详细信息。这个数组可能是空的,例如, + 对于一些全局消息,或者附加到 parent 的 child 消息,就可能是空的。 + + 字符偏移量是 Unicode 标量的偏移量。 + */ + "spans": [ + { + /* span 所在的文件。 + 注意,此路径可能不存在。例如,如果路径指向标准库,而 + rust src 在系统根目录中不可用, 那么它可能会指向一个不存在的文件。 + 注意,这也可能指向外部 crate 源。 + */ + "file_name": "lib.rs", + /* span 开始的字节偏移量(基于0且包含0)。 */ + "byte_start": 21, + /* span 结束的字节偏移量(基于0且不包含0)。 */ + "byte_end": 22, + /* span 的第一行编号(基于1且包含1)。 */ + "line_start": 2, + /* span 的最后一行编号(基于1且包含1)。 */ + "line_end": 2, + /* 行首(line_start)的第一个字符偏移量(基于1且包含1) */ + "column_start": 9, + /* 行尾(line_end)的最后一个字符偏移量(基于1且不包含1) */ + "column_end": 10, + /* 不管这是不是 "primary" span。 + + 这表明这个 span 是该诊断的焦点所在(focal)。 + + 在少数情况下,多个 span 可能被标记为 primary。 例如 "immutable borrow occurs here" 和 + "mutable borrow ends here" 可以是两个独立的 primary span。 + + top (parent) 消息应该有至少一个 rimary span ,除非有 0 个 span 。 + child 消息有 0 个或多个 primary span 。 + */ + "is_primary": true, + /* 一个对象数组,显示该 span 的原始源代码。这将展示 span 所在的整行文本 。跨多行的 span 对于每行会有一个单独对应的值。 + */ + "text": [ + { + /* 完整的原始源代码行。 */ + "text": " let x = 123;", + /* span 所指行的首字符偏移量(基于1且包含1)。 */ + "highlight_start": 9, + /* span 所指行的最后一个字符偏移量(基于1且不包含1)。 */ + "highlight_end": 10 + } + ], + /* 一个可选消息,用于显示 span 位置。对于主 span ,这通常为 null 。 + */ + "label": null, + /* 一个可选字符串,对于解决该问题建议的替换。 工具( Tools )可能会尝试用此文本替换该 span 的内容 。 + */ + "suggested_replacement": null, + /* 一个可选字符串,指出 "suggested_replacement" 的置信度。工具( Tools )可能会用这个值来决定是否自动应用建议(的替换)。 + + 可能的值可以是: + - "MachineApplicable": 该建议无疑是用户的意图。该建议应该自动被应用。 + - "MaybeIncorrect": 该建议可能是用户的意图,但不确定。如果应用了该建议,则应该产生有效的 Rust 代码。 + - "HasPlaceholders": 该建议包含占位符,如 + `(...)`。 建议不会被自动应用因为它不会产生有效的 Rust 代码。用户需要填充该占位符。 + - "Unspecified": 建议的适用性尚不清楚。 + */ + "suggestion_applicability": null, + /* 一个可选对象,指出该 span 内的宏展开。 + + 如果消息发生在宏调用中,该对象将会提供该消息在宏展开所在位置的详细信息。 + */ + "expansion": { + /* 宏调用的 span 。 + 使用与 "spans" 数组相同的 span 定义。 + */ + "span": {/*...*/} + /* 宏名称, 例如 "foo!" 或 "#[derive(Eq)]"。 */ + "macro_decl_name": "some_macro!", + /* 可选的 span ,定义了宏的相关部分 */ + "def_site_span": {/*...*/}, + } + } + ], + /* 附加诊断消息的数组。 + 这是一个对象数组,使用与 parent 消息相同的格式。 + children 没有嵌套 ( children 自身不包含 "children" 的定义 ) + */ + "children": [ + { + "message": "`#[warn(unused_variables)]` on by default", + "code": null, + "level": "note", + "spans": [], + "children": [], + "rendered": null + }, + { + "message": "if this is intentional, prefix it with an underscore", + "code": null, + "level": "help", + "spans": [ + { + "file_name": "lib.rs", + "byte_start": 21, + "byte_end": 22, + "line_start": 2, + "line_end": 2, + "column_start": 9, + "column_end": 10, + "is_primary": true, + "text": [ + { + "text": " let x = 123;", + "highlight_start": 9, + "highlight_end": 10 + } + ], + "label": null, + "suggested_replacement": "_x", + "suggestion_applicability": "MachineApplicable", + "expansion": null + } + ], + "children": [], + "rendered": null + } + ], + /* 可选字符串,由 rustc 显示的诊断的 rendered 版本。注意这可能会受到 `--json` 标签的影响。 + */ + "rendered": "warning: unused variable: `x`\n --> lib.rs:2:9\n |\n2 | let x = 123;\n | ^ help: if this is intentional, prefix it with an underscore: `_x`\n |\n = note: `#[warn(unused_variables)]` on by default\n\n" +} +``` + +## 工件通知 + +当使用 [`--json=artifacts` 标签][option-json] 时就会触发工件通知( Artifact notification ,译者注,artifact 真的是一个很难恰到其处翻译的词语,所以这里只能约定俗称地翻译为“工件”) 。 它们指明了文件工件已经被保存到了磁盘中。有关发出(工件)类型的更多信息可以在 [`--emit` 标签][option-emit] 文档中找到。 + +```javascript +{ + /* 生成工件的文件名 */ + "artifact": "libfoo.rlib", + /* 生成的工件的种类,可能的值有: + - "link": 根据 crate 类型指定生成的 crate 。 + - "dep-info": 具有依赖信息的 `.d` 文件,所用语法类似于 Makefile 文件的语法。 + - "metadata": 包含有关 crate 元数据的 Rust `.rmeta` 文件。 + - "save-analysis": 由 `-Zsave-analysis` feature 所发出的 JSON 文件 。 + */ + "emit": "link" +} +``` + +[option-emit]: command-line-arguments.md#option-emit +[option-error-format]: command-line-arguments.md#option-error-format +[option-json]: command-line-arguments.md#option-json diff --git a/src/lints/listing/deny-by-default.md b/src/lints/listing/deny-by-default.md index f3f31e4..81df0d3 100644 --- a/src/lints/listing/deny-by-default.md +++ b/src/lints/listing/deny-by-default.md @@ -2,25 +2,26 @@ 默认情况下,这些 lint 都设置为 'deny' 级别。 -- [`ambiguous_associated_items`](#ambiguous_associated_items) -- [`arithmetic_overflow`](#arithmetic_overflow) -- [`conflicting_repr_hints`](#conflicting_repr_hints) -- [`const_err`](#const_err) -- [`ill_formed_attribute_input`](#ill_formed_attribute_input) -- [`incomplete_include`](#incomplete_include) -- [`invalid_type_param_default`](#invalid_type_param_default) -- [`macro_expanded_macro_exports_accessed_by_absolute_paths`](#macro_expanded_macro_exports_accessed_by_absolute_paths) -- [`missing_fragment_specifier`](#missing_fragment_specifier) -- [`mutable_transmutes`](#mutable_transmutes) -- [`no_mangle_const_items`](#no_mangle_const_items) -- [`order_dependent_trait_objects`](#order_dependent_trait_objects) -- [`overflowing_literals`](#overflowing_literals) -- [`patterns_in_fns_without_body`](#patterns_in_fns_without_body) -- [`pub_use_of_private_extern_crate`](#pub_use_of_private_extern_crate) -- [`soft_unstable`](#soft_unstable) -- [`unconditional_panic`](#unconditional_panic) -- [`unknown_crate_types`](#unknown_crate_types) -- [`useless_deprecated`](#useless_deprecated) +- [默认等级为拒绝的 lints](#默认等级为拒绝的-lints) + - [ambiguous_associated_items](#ambiguous_associated_items) + - [arithmetic_overflow](#arithmetic_overflow) + - [conflicting_repr_hints](#conflicting_repr_hints) + - [const_err](#const_err) + - [ill_formed_attribute_input](#ill_formed_attribute_input) + - [incomplete_include](#incomplete_include) + - [invalid_type_param_default](#invalid_type_param_default) + - [macro_expanded_macro_exports_accessed_by_absolute_paths](#macro_expanded_macro_exports_accessed_by_absolute_paths) + - [missing_fragment_specifier](#missing_fragment_specifier) + - [mutable_transmutes](#mutable_transmutes) + - [no_mangle_const_items](#no_mangle_const_items) + - [order_dependent_trait_objects](#order_dependent_trait_objects) + - [overflowing_literals](#overflowing_literals) + - [patterns_in_fns_without_body](#patterns_in_fns_without_body) + - [pub_use_of_private_extern_crate](#pub_use_of_private_extern_crate) + - [soft_unstable](#soft_unstable) + - [unconditional_panic](#unconditional_panic) + - [unknown_crate_types](#unknown_crate_types) + - [useless_deprecated](#useless_deprecated) @@ -209,6 +210,7 @@ error: attribute must be of the form `#[inline]` or `#[inline(always|never)]` = note: for more information, see issue #57571 ``` + **解释** 以前,许多内置属性的输入没有经过验证,无意义的属性输入被接收。在添加了验证之后,明确了一些现有的项目使用了这些无效的格式。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #57571](https://github.com/rust-lang/rust/issues/57571) 。 @@ -218,6 +220,7 @@ error: attribute must be of the form `#[inline]` or `#[inline(always|never)]` `incomplete_include` lint 检测一个文件包含多于一个表达式的 [`include!`][include-macro] 宏。 **样例** + ```rust,igonre fn main() { include!("foo.txt"); @@ -237,6 +240,7 @@ error: include macro expected single expression in source | = note: `#[deny(incomplete_include)]` on by default ``` + **解释** [`include!`][include-macro] 宏当前仅打算用于单个[表达式][expression]或多个[项][item]。从以前看,它会忽略第一个表达式之后的任何内容,但这可能会令人困惑。在上例中,println! 表达式( println! expression )刚好在分号之前结束,从而使分号成为多余的信息而被忽略,更令人惊讶的是,如果包含的文件有多个打印语句,后续的语句将被忽略! @@ -249,7 +253,9 @@ error: include macro expected single expression in source ## invalid_type_param_default `invalid_type_param_default` lint 检测在无效位置中错误地允许 (allowed) 使用类型参数默认值。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -269,13 +275,16 @@ error: defaults for type parameters are only allowed in `struct`, `enum`, `type` = note: for more information, see issue #36887 ``` + **解释** 默认类型参数仅在某些情况下才允许使用,但是以前编译器在任何地方都允许使用。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #36887](https://github.com/rust-lang/rust/issues/36887) 。 ## macro_expanded_macro_exports_accessed_by_absolute_paths `macro_expanded_macro_exports_accessed_by_absolute_paths` lint 检测当前 crate 中不能被绝对路径引用的 [`macro_export`][macro-export]宏的宏展开。 + **样例** + ```rust macro_rules! define_exported { () => { @@ -316,6 +325,7 @@ note: the macro is defined here = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) ``` + **解释** 我们的目的是所有使用 `#[macro_export]` 属性的宏在 crate 根是可用的。然而,当一个 `macro_rules!` 定义由另一个宏生成之时,宏展开是无法遵循该规则的。 @@ -329,6 +339,7 @@ note: the macro is defined here 始终可以通过移除 `macro_rules!` 宏定义中未使用的模式来解决此警告。 **样例** + ```rust macro_rules! foo { () => {}; @@ -352,6 +363,7 @@ error: missing fragment specifier = note: for more information, see issue #40107 ``` **解释** + 要修复此问题,从 `macro_rules!` 定义中移除此未使用模式: ```rust macro_rules! foo { @@ -364,7 +376,9 @@ fn main() { ## mutable_transmutes `mutable_transmutes` lint 捕捉从 `&T` 到 `&mut T` 这种[未定义行为][undefined-behavior] 的转换。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -383,6 +397,7 @@ error: mutating transmuted &mut T from &T may cause undefined behavior, consider | = note: `#[deny(mutable_transmutes)]` on by default ``` + **解释** 我们对数据别名做出了一些假设,而这种转换是违反这些假设的。考虑使用 [`UnsafeCell`](https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html)。 @@ -391,7 +406,9 @@ error: mutating transmuted &mut T from &T may cause undefined behavior, consider ## no_mangle_const_items `no_mangle_const_items` lint 检测 [`no_mangle`](https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute)属性的所有 `const` 项。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -411,6 +428,7 @@ error: const items should never be `#[no_mangle]` | = note: `#[deny(no_mangle_const_items)]` on by default ``` + **解释** 常量没有其导出符号,因此这可能意味着你得用 [`static`][static] 而不是 [`const`][const]。 @@ -422,6 +440,7 @@ error: const items should never be `#[no_mangle]` `order_dependent_trait_objects` lint 检测一种 trait 一致性冲突,该冲突即为同一个包含标记 trait (marker traits)的 dynamic trait object 创建两个 trait 实现。 **样例** + ```rust # #![allow(unused)] # fn main() { @@ -446,13 +465,16 @@ error: conflicting implementations of trait `main::Trait` for type `(dyn std::ma = note: for more information, see issue #56484 ``` + **解释** 以前的一个 bug 导致编译器将不同顺序的 trait (例如 `Send + Sync` 和 `Sync + Send`)解释为不同的类型,然而它们应该被认为是相同的。这允许代码在出现一致性错误的时候定义单独的 trait 实现。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #56484](https://github.com/rust-lang/rust/issues/56484) 。 ## overflowing_literals `overflowing_literals` lint 检测超出其所属类型范围的字面量。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -470,13 +492,16 @@ error: literal out of range for `u8` = note: `#[deny(overflowing_literals)]` on by default = note: the literal `1000` does not fit into the type `u8` whose range is `0..=255` ``` + **解释** 使用溢出其所用类型的字面量通常是错误。要么就使用在其类型范围内的字面量,要么就更改其类型以能容纳该字面量。 ## patterns_in_fns_without_body `patterns_in_fns_without_body` lint 检测 `mut` [标识符模式][identifier-patterns]用于没有函数体的函数的参数。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -497,6 +522,7 @@ error: patterns aren't allowed in functions without bodies = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #35203 ``` + **解释** 要想修复此问题, trait 定义中从参数移除 `mut` ;也可以使用默认实现。也就是说,以下两种都行: @@ -522,6 +548,7 @@ trait 定义中可以定义没有函数体的函数以指定实现必须实现 `pub_use_of_private_extern_crate` lint 检测私有 `extern crate` 重导出的具体情况。 **样例** + ```rust,ignore # #![allow(unused)] # fn main() { @@ -541,6 +568,7 @@ error: extern crate `core` is private, and cannot be re-exported (error E0365), = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #34537 ``` + **解释** 一个公开的 `use` 声明不应该用于 公开性地重导出私有 `extern crate`。应该使用 `pub extern crate`。 @@ -548,7 +576,9 @@ error: extern crate `core` is private, and cannot be re-exported (error E0365), ## soft_unstable `soft_unstable` lint 检测 在 stable 上无意间允许(allowed)的 unstable feature。 + **样例** + ```rust # #![allow(unused)] # fn main() { @@ -573,6 +603,7 @@ error: use of unstable library feature 'test': `bench` is a part of custom test = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #64266 ``` + **解释** [`bench`](https://doc.rust-lang.org/nightly/unstable-book/library-features/test.html) 属性意外地在 [stable release channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) 上被指定。将此转化为固有错误会破坏一些(现有)项目。当使用 `--cap-lints` 时该 lint 允许项目正确地构建,否则会发出一个错误提示。`#[bench]` 不应该被用在 stable channel。这是个[将来不兼容][future-incompatible] 的 lint ,将来会转化为固有错误。更多细节请参阅 [issue #64266](https://github.com/rust-lang/rust/issues/64266) 。 @@ -581,6 +612,7 @@ error: use of unstable library feature 'test': `bench` is a part of custom test `unconditional_panic` lint 检测将在运行时引起 panic 的操作。 **样例** + ```rust # #![allow(unused)] # fn main() { @@ -606,6 +638,7 @@ error: this operation will panic at runtime `unknown_crate_types` lint 检测在 [`crate_type`](https://doc.rust-lang.org/reference/linkage.html)属性中找到的未知 crate 类型。 **样例** + ```rust #![crate_type="lol"] fn main() {} @@ -620,6 +653,7 @@ error: invalid `crate_type` value | = note: `#[deny(unknown_crate_types)]` on by default ``` + **解释** 给 `crate_type` 属性赋未知值可以肯定说是一个错误。 @@ -628,6 +662,7 @@ error: invalid `crate_type` value `useless_deprecated` lint 检测无效且弃用的属性。 **样例** + ```rust # #![allow(unused)] # fn main() { @@ -651,7 +686,9 @@ error: this `#[deprecated]` annotation has no effect | = note: `#[deny(useless_deprecated)]` on by default ``` + **解释** + 弃用属性对 trait 实现是无影响的。 [future-incompatible]:https://doc.rust-lang.org/rustc/lints/index.html#future-incompatible-lints From f3da1b0f9e81636e4f5857f309a57789acab9c0d Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Sat, 20 Feb 2021 19:08:04 +0800 Subject: [PATCH 07/11] add APACHE and --- LICENSE-APACHE | 176 +++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE-MIT | 23 +++++++ 2 files changed, 199 insertions(+) create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..1b5ec8b --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..31aa793 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. From 3280722a598f0b108ffa491bef8617ff8b42468b Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Tue, 16 Mar 2021 21:19:29 +0800 Subject: [PATCH 08/11] add 5 chapter --- src/json.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/json.md b/src/json.md index 1feeb89..a4d0879 100644 --- a/src/json.md +++ b/src/json.md @@ -171,7 +171,7 @@ JSON 消息每行都被发送到 stderr 。 ## 工件通知 -当使用 [`--json=artifacts` 标签][option-json] 时就会触发工件通知( Artifact notification ,译者注,artifact 真的是一个很难恰到其处翻译的词语,所以这里只能约定俗称地翻译为“工件”) 。 它们指明了文件工件已经被保存到了磁盘中。有关发出(工件)类型的更多信息可以在 [`--emit` 标签][option-emit] 文档中找到。 +当使用 [`--json=artifacts` 标签][option-json] 时就会触发工件通知( Artifact notification ,译者注,artifact 真的是一个很难恰到其处翻译的词语,所以这里只能约定俗成地翻译为“工件”) 。 它们指明了文件工件已经被保存到了磁盘中。有关发出(工件)类型的更多信息可以在 [`--emit` 标签][option-emit] 文档中找到。 ```javascript { From 69b6ae26b5d69cd5d17bafe9b204772ffa39b2d2 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Wed, 17 Mar 2021 19:23:34 +0800 Subject: [PATCH 09/11] add 4 chapter --- src/codegen-options/index.md | 326 +++++++++++++---------------------- 1 file changed, 122 insertions(+), 204 deletions(-) diff --git a/src/codegen-options/index.md b/src/codegen-options/index.md index c2442b3..9ba3c25 100644 --- a/src/codegen-options/index.md +++ b/src/codegen-options/index.md @@ -163,330 +163,248 @@ ## linker-flavor -This flag controls the linker flavor used by `rustc`. If a linker is given with -the [`-C linker` flag](#linker), then the linker flavor is inferred from the -value provided. If no linker is given then the linker flavor is used to -determine the linker to use. Every `rustc` target defaults to some linker -flavor. Valid options are: - -* `em`: use [Emscripten `emcc`](https://emscripten.org/docs/tools_reference/emcc.html). -* `gcc`: use the `cc` executable, which is typically gcc or clang on many systems. -* `ld`: use the `ld` executable. -* `msvc`: use the `link.exe` executable from Microsoft Visual Studio MSVC. -* `ptx-linker`: use - [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker) for Nvidia - NVPTX GPGPU support. -* `wasm-ld`: use the [`wasm-ld`](https://lld.llvm.org/WebAssembly.html) - executable, a port of LLVM `lld` for WebAssembly. -* `ld64.lld`: use the LLVM `lld` executable with the [`-flavor darwin` - flag][lld-flavor] for Apple's `ld`. -* `ld.lld`: use the LLVM `lld` executable with the [`-flavor gnu` - flag][lld-flavor] for GNU binutils' `ld`. -* `lld-link`: use the LLVM `lld` executable with the [`-flavor link` - flag][lld-flavor] for Microsoft's `link.exe`. +该标签控制 `rustc` 使用的链接器的样式。 如果给编译器附加 [`-C linker` 标签](#linker) ,那么链接器样式将从提供的值中推断出来。如果没有给出链接器,则会使用链接器样式来确定要使用的链接器。每个 `rustc` 目标都默认有一些链接器样式。有效的选项有: + +* `em`: 使用 [Emscripten `emcc`](https://emscripten.org/docs/tools_reference/emcc.html). +* `gcc`: 使用 `cc` 可执行文件, 在许多系统上通常是 gcc 或 clang 。 +* `ld`: 使用 `ld` 可执行文件 +* `msvc`: 使用 Microsoft Visual Studio MSVC 的 `link.exe` 可执行文件。 +* `ptx-linker`: 使用 Nvidia NVPTX GPGPU 所支持的 [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker) 。 +* `wasm-ld`: 使用可执行文件 [`wasm-ld`](https://lld.llvm.org/WebAssembly.html) , + 一个用于 WebAssembly 的 LLVM `lld` 端口。 +* `ld64.lld`: 对于 apple 的 `ld`,使用附加 [`-flavor darwin` 标签][lld-flavor] 的 LLVM `lld` 可执行文件。 +* `ld.lld`: 对于 GNU binutil 的 `ld`,使用附加 [`-flavor gnu` 标签][lld-flavor] 的 LLVM `lld` 可执行文件。 +* `lld-link`: 对于Microsoft 的 `link.exe`,使用附加 [`-flavor link` 标签][lld-flavor] 的 LLVM `lld` 可执行文件。 [lld-flavor]: https://lld.llvm.org/Driver.html ## linker-plugin-lto -This flag defers LTO optimizations to the linker. See -[linker-plugin-LTO](../linker-plugin-lto.md) for more details. It takes one of -the following values: +该标签将 LTO 优化推迟到 链接器阶段。 更多细节请参阅 [linker-plugin-LTO](../linker-plugin-lto.md) 。 其采用以下值之一: -* `y`, `yes`, `on`, or no value: enable linker plugin LTO. -* `n`, `no`, or `off`: disable linker plugin LTO (the default). -* A path to the linker plugin. +* `y`, `yes`, `on`, 或者无值:启用 linker plugin LTO 。 +* `n`, `no`, 或 `off`: 禁用 linker plugin LTO (默认)。 +* 一个链接器插件( linker plugin )的路径。 -More specifically this flag will cause the compiler to replace its typical -object file output with LLVM bitcode files. For example an rlib produced with -`-Clinker-plugin-lto` will still have `*.o` files in it, but they'll all be LLVM -bitcode instead of actual machine code. It is expected that the native platform -linker is capable of loading these LLVM bitcode files and generating code at -link time (typically after performing optimizations). +更具体地说,该标签将使得编译器将其通常的目标文件输出( typical object file output )替换为 LLVM 位码文件。 例如,使用 `-Clinker-plugin-lto` 生成的 rlib 会仍然有 `*.o` 文件在其中, 但实际上它们都是 LLVM 位码而非机器码。 预计本平台链接器可以加载这些 LLVM 位码文件并在链接时生成代码(通常在执行优化之后)。 -Note that rustc can also read its own object files produced with -`-Clinker-plugin-lto`. If an rlib is only ever going to get used later with a -`-Clto` compilation then you can pass `-Clinker-plugin-lto` to speed up -compilation and avoid generating object files that aren't used. +注意, rustc 也可以读取由 `-Clinker-plugin-lto` 生成的它自己的目标文件。如果 rlib 只会在使用 `-Clto` 编译时使用,那么可以传递 `-Clinker-plugin-lto` 标签来加速编译,并避免生成不使用的目标文件。 ## llvm-args -This flag can be used to pass a list of arguments directly to LLVM. +该标签可以用来将参数列表直接传递给 LLVM 。 -The list must be separated by spaces. +该列表必须用空格来分隔。 -Pass `--help` to see a list of options. +传递 `--help` 来查看选项列表。 ## lto -This flag controls whether LLVM uses [link time -optimizations](https://llvm.org/docs/LinkTimeOptimization.html) to produce -better optimized code, using whole-program analysis, at the cost of longer -linking time. It takes one of the following values: - -* `y`, `yes`, `on`, `fat`, or no value: perform "fat" LTO which attempts to - perform optimizations across all crates within the dependency graph. -* `n`, `no`, `off`: disables LTO. -* `thin`: perform ["thin" - LTO](http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html). - This is similar to "fat", but takes substantially less time to run while - still achieving performance gains similar to "fat". - -If `-C lto` is not specified, then the compiler will attempt to perform "thin -local LTO" which performs "thin" LTO on the local crate only across its -[codegen units](#codegen-units). When `-C lto` is not specified, LTO is -disabled if codegen units is 1 or optimizations are disabled ([`-C -opt-level=0`](#opt-level)). That is: - -* When `-C lto` is not specified: - * `codegen-units=1`: disable LTO. - * `opt-level=0`: disable LTO. -* When `-C lto=true`: - * `lto=true`: 16 codegen units, perform fat LTO across crates. - * `codegen-units=1` + `lto=true`: 1 codegen unit, fat LTO across crates. - -See also [linker-plugin-lto](#linker-plugin-lto) for cross-language LTO. +该标签控制是否 LLVM 用 [链接时间优化](https://llvm.org/docs/LinkTimeOptimization.html) 来产生更好的优化代码,使用完整的程序分析,以更长的链接时间为代价。其采用以下值之一: + +* `y`, `yes`, `on`, `fat`, 或无值:执行 "fat" LTO ,即尝试将依赖图中的所有依赖跨 crate 执行优化。 +* `n`, `no`, `off`: 禁用 LTO 。 +* `thin`: 执行 ["thin" LTO](http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html) 。这类似于 "fat" ,但会花费更少的时间(优化),同时仍然可以得到类似于 "fat" 的性能提升。 + +如果没有指定 `-C lto` , 那么编译器就会尝试执行 "thin local LTO" ,即只在本地 crate 上跨其[代码生成单元](#codegen-units) 执行 "thin" LTO (which performs "thin" LTO on the local crate only across its [codegen units](#codegen-units) )。 当未指定 `-C lto` 时,如果代码生成单元是 1 或优化被禁用([`-C opt-level=0`](#opt-level)),则 LTO 将被禁用。即是: + +* 当 `-C lto` 未指定: + * `codegen-units=1`: 禁用 LTO 。 + * `opt-level=0`: 禁用 LTO。 +* 当 `-C lto=true`: + * `lto=true`: 16 个代码生成单元, 跨 crate 执行 fat LTO 。 + * `codegen-units=1` + `lto=true`: 1 个代码生成单元, 跨 crate 执行 fat LTO 。 + +跨语言 LTO 请参阅 [linker-plugin-lto](#linker-plugin-lto)。 ## metadata -This option allows you to control the metadata used for symbol mangling. This -takes a space-separated list of strings. Mangled symbols will incorporate a -hash of the metadata. This may be used, for example, to differentiate symbols -between two different versions of the same crate being linked. +该选项使你可以控制用于符号修饰(symbol mangling)。其采用值为以空格分隔的字符串列表。修饰后的符号将会合并元数据的 hash 。例如,这可以在链接时用来区分相同 crate 的两个不同版本之间符号。 ## no-prepopulate-passes -This flag tells the pass manager to use an empty list of passes, instead of the -usual pre-populated list of passes. +该标签告诉传递管理器(pass manager)使用一个空的传递列表而非通常的预填充了的传递列表。 ## no-redzone -This flag allows you to disable [the -red zone](https://en.wikipedia.org/wiki/Red_zone_\(computing\)). It takes one -of the following values: +该标签允许你禁用 [the red zone](https://en.wikipedia.org/wiki/Red_zone_\(computing\)) 。 其采用以下值之一: -* `y`, `yes`, `on`, or no value: disable the red zone. -* `n`, `no`, or `off`: enable the red zone. +* `y`, `yes`, `on`,或无值: 禁用 the red zone 。 +* `n`, `no`, 或 `off`: 启用 the red zone 。 -The default behaviour, if the flag is not specified, depends on the target. +如果该标签未指定,默认行为取决于目标系统。 ## no-stack-check -This option is deprecated and does nothing. +该选项已被弃用,且不执行任何操作。 ## no-vectorize-loops -This flag disables [loop -vectorization](https://llvm.org/docs/Vectorizers.html#the-loop-vectorizer). +该标签禁用 [循环矢量化](https://llvm.org/docs/Vectorizers.html#the-loop-vectorizer)( loop vectorization )。 ## no-vectorize-slp -This flag disables vectorization using -[superword-level -parallelism](https://llvm.org/docs/Vectorizers.html#the-slp-vectorizer). +该标签禁用矢量化使用 [superword-level parallelism](https://llvm.org/docs/Vectorizers.html#the-slp-vectorizer) 。 ## opt-level -This flag controls the optimization level. +该标签控制优化级别。 -* `0`: no optimizations, also turns on - [`cfg(debug_assertions)`](#debug-assertions) (the default). -* `1`: basic optimizations. -* `2`: some optimizations. -* `3`: all optimizations. -* `s`: optimize for binary size. -* `z`: optimize for binary size, but also turn off loop vectorization. +* `0`: 没有优化,同时打开 [`cfg(debug_assertions)`](#debug-assertions)(默认值)。 +* `1`:基本的优化(basic optimizations)。 +* `2`:一些优化(some optimizations)。 +* `3`: 全部优化(all optimizations)。 +* `s`:优化二进制大小(optimize for binary size)。 +* `z`:优化二进制大小,同时关闭循环矢量化。 -Note: The [`-O` flag][option-o-optimize] is an alias for `-C opt-level=2`. +注意: [`-O` 标签][option-o-optimize] 是 `-C opt-level=2` 的一个别名。 -The default is `0`. +默认值为 `0`。 ## overflow-checks -This flag allows you to control the behavior of [runtime integer -overflow](../../reference/expressions/operator-expr.md#overflow). When -overflow-checks are enabled, a panic will occur on overflow. This flag takes -one of the following values: +该标签允许你控制 [运行时整数溢出](https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow)的行为。当 overflow-checks 被启用时,溢出时会发出一个 panic 。该标签采用以下值之一: -* `y`, `yes`, `on`, or no value: enable overflow checks. -* `n`, `no`, or `off`: disable overflow checks. +* `y`, `yes`, `on`, 或无值:开启 overflow checks 。 +* `n`, `no`, or `off`: 禁用 overflow checks 。 -If not specified, overflow checks are enabled if -[debug-assertions](#debug-assertions) are enabled, disabled otherwise. +倘若未指定 overflow checks,如果 [debug-assertions](#debug-assertions) 是启用的 overflow checks 才会启用,否则是禁用的。 ## panic -This option lets you control what happens when the code panics. +该标签使你在代码 panic 时控制(程序栈)行为情况。 -* `abort`: terminate the process upon panic -* `unwind`: unwind the stack upon panic +* `abort`:在 panic 时终止进程。 +* `unwind`:在 panic 时对栈进行展开。 -If not specified, the default depends on the target. +如果未指定,默认值取决于目标。 ## passes -This flag can be used to add extra [LLVM -passes](http://llvm.org/docs/Passes.html) to the compilation. +该标签可以用于添加额外的 [LLVM passes](http://llvm.org/docs/Passes.html) 到编译中。 -The list must be separated by spaces. +列表必须以空格分隔。 -See also the [`no-prepopulate-passes`](#no-prepopulate-passes) flag. +另请参阅 [`no-prepopulate-passes`](#no-prepopulate-passes) 标签。 ## prefer-dynamic -By default, `rustc` prefers to statically link dependencies. This option will -indicate that dynamic linking should be used if possible if both a static and -dynamic versions of a library are available. There is an internal algorithm -for determining whether or not it is possible to statically or dynamically -link with a dependency. For example, `cdylib` crate types may only use static -linkage. This flag takes one of the following values: +默认情况下,`rustc` 更倾向于静态链接依赖项。该选项表明,如果库的静态和动态版本都是可用的,则应尽可能使用动态链接。有一个内部算法用于确定是否可以静态或动摇地链接依赖项。例如, `cdylib` crate 类型只能使用静态链接。该标采用以下值之一: -* `y`, `yes`, `on`, or no value: use dynamic linking. -* `n`, `no`, or `off`: use static linking (the default). +* `y`, `yes`, `on`, 或无值: 使用动态链接。 +* `n`, `no`, 或 `off`: 使用静态链接(默认值)。 ## profile-generate -This flag allows for creating instrumented binaries that will collect -profiling data for use with profile-guided optimization (PGO). The flag takes -an optional argument which is the path to a directory into which the -instrumented binary will emit the collected data. See the chapter on -[profile-guided optimization] for more information. +该标签允许创建用于收集分析数据的仪器化二进制文件(instrumented binaries),用于 PGO 。该标签采用一个可选参数,该参数是一个目录的路径,仪器化二进制文件将会把收集到的数据发送到该目录中。更多信息请参阅 [PGO][profile-guided optimization] 章节。 ## profile-use -This flag specifies the profiling data file to be used for profile-guided -optimization (PGO). The flag takes a mandatory argument which is the path -to a valid `.profdata` file. See the chapter on -[profile-guided optimization] for more information. +该标签指定用于 PGO 的分析数据文件。该标签采用一个强制参数,它是一个有效的 `.profdata` 文件的路径。更多信息请参阅 [PGO][profile-guided optimization] 章节。 ## relocation-model -This option controls generation of -[position-independent code (PIC)](https://en.wikipedia.org/wiki/Position-independent_code). +该选项控制 [地址无关代码 (PIC)](https://en.wikipedia.org/wiki/Position-independent_code) 的生成。 -Supported values for this option are: +该选项支持的值有: #### Primary relocation models -- `static` - non-relocatable code, machine instructions may use absolute addressing modes. +- `static` - 不可重定位代码,机器指令得使用绝对寻址模式。 -- `pic` - fully relocatable position independent code, -machine instructions need to use relative addressing modes. \ -Equivalent to the "uppercase" `-fPIC` or `-fPIE` options in other compilers, -depending on the produced crate types. \ -This is the default model for majority of supported targets. +- `pic` - 完全可重定位独立代码,机器指令需要使用相对寻址模式。 \ +等效于在其它编译器 “大写” `-fPIC` 或 `-fPIE` 选项,取决于产生的 crate 类型。 \ +这是大多数受支持目标的默认 model。 #### Special relocation models -- `dynamic-no-pic` - relocatable external references, non-relocatable code. \ -Only makes sense on Darwin and is rarely used. \ -If StackOverflow tells you to use this as an opt-out of PIC or PIE, don't believe it, -use `-C relocation-model=static` instead. -- `ropi`, `rwpi` and `ropi-rwpi` - relocatable code and read-only data, relocatable read-write data, -and combination of both, respectively. \ -Only makes sense for certain embedded ARM targets. -- `default` - relocation model default to the current target. \ -Only makes sense as an override for some other explicitly specified relocation model -previously set on the command line. +- `dynamic-no-pic` - 可重定位外部引用,不可重定位代码。 \ +仅对 Darwin 操作系统有用,并且很少使用 \ +如果 StackOverflow 告诉你将其作为 PIC 或 PIE 的退出选项, 不要相信它,应该使用 `-C relocation-model=static` 。 +- `ropi`, `rwpi` 和 `ropi-rwpi` - 可重定位代码和只读数据, 可重定位的读写数据, +以及两者的组合。 \ +只对特定的 嵌入式 ARM 目标有意义。. +- `default` -重定位默认模型到当前目标 \ +仅有的意义是作为对先前在命令行上设置的其他一些明确指定的重定位模型的替代。 -Supported values can also be discovered by running `rustc --print relocation-models`. +也可以通过运行 `rustc --print relocation-models` 查找支持的值。 #### Linking effects -In addition to codegen effects, `relocation-model` has effects during linking. +除了代码生成效果之外, `relocation-model` 在链接期间也有效果。 -If the relocation model is `pic` and the current target supports position-independent executables -(PIE), the linker will be instructed (`-pie`) to produce one. \ -If the target doesn't support both position-independent and statically linked executables, -then `-C target-feature=+crt-static` "wins" over `-C relocation-model=pic`, -and the linker is instructed (`-static`) to produce a statically linked -but not position-independent executable. +如果重定位模型是 `pic` ,并且当前目标支持地址无关可执行文件(PIE),则将指示链接器生成一个(地址无关可执行文件)。 \ +如果目标不支持地址无关且静态链接的可执行文件,那么 `-C target-feature=+crt-static` 会“超过” `-C relocation-model=pic`,并指示链接器(`-static`)生成静态链接但不是地址无关的可执行文件。 ## remark -This flag lets you print remarks for optimization passes. +该标签允许你打印优化传递命令的备注。 -The list of passes should be separated by spaces. +传递命令的列表应该用空格分隔。 -`all` will remark on every pass. +(如果是)`all` 将会对每个传递命令备注。 ## rpath -This flag controls whether [`rpath`](https://en.wikipedia.org/wiki/Rpath) is -enabled. It takes one of the following values: +该标签控制是否启用 [`rpath`](https://en.wikipedia.org/wiki/Rpath) 。其采用以下值之一: -* `y`, `yes`, `on`, or no value: enable rpath. -* `n`, `no`, or `off`: disable rpath (the default). +* `y`, `yes`, `on`, 或者无值: 启用 rpath 。 +* `n`, `no`, 或 `off`: 禁用 rpath (默认值)。 ## save-temps -This flag controls whether temporary files generated during compilation are -deleted once compilation finishes. It takes one of the following values: +该标签控制在编译完成后是否删除编译过程中生成的临时文件。其采用以下值之一: -* `y`, `yes`, `on`, or no value: save temporary files. -* `n`, `no`, or `off`: delete temporary files (the default). +* `y`, `yes`, `on`, 或者无值:保存临时文件。 +* `n`, `no`, 或 `off`:删除临时文件(默认值)。 ## soft-float -This option controls whether `rustc` generates code that emulates floating -point instructions in software. It takes one of the following values: +该选项控制是否 `rustc` 生成模拟软件中浮点指令的代码。其采用以下值之一: -* `y`, `yes`, `on`, or no value: use soft floats. -* `n`, `no`, or `off`: use hardware floats (the default). +* `y`, `yes`, `on`, 或者无值: 使用软浮点数。 +* `n`, `no`, 或 `off`: 使用硬件浮点数(默认值)。 ## target-cpu -This instructs `rustc` to generate code specifically for a particular processor. +这会指示 `rustc` 生成专用于特定处理器的代码。 -You can run `rustc --print target-cpus` to see the valid options to pass -here. Each target has a default base CPU. Special values include: +你可以运行 `rustc --print target-cpus` 来查看此处有效的传递选项。每个目标都有一个默认的基础 CPU 。 特殊值包括: -* `native` can be passed to use the processor of the host machine. -* `generic` refers to an LLVM target with minimal features but modern tuning. +* `native` 可以传递给使用该主机处理器(同样的机器)。 +* `generic` 指具有最少的 features 但经过现代调优的一个 LLVM 目标。 ## target-feature -Individual targets will support different features; this flag lets you control -enabling or disabling a feature. Each feature should be prefixed with a `+` to -enable it or `-` to disable it. +各个目标会支持不同的 features ; 该标签使你可以控制启用或禁用 feature 。 每个 feature 应该以一个 `+` 或 `-` 来做前缀来启用或禁用 feature 。 -Features from multiple `-C target-feature` options are combined. \ -Multiple features can be specified in a single option by separating them -with commas - `-C target-feature=+x,-y`. \ -If some feature is specified more than once with both `+` and `-`, -then values passed later override values passed earlier. \ -For example, `-C target-feature=+x,-y,+z -Ctarget-feature=-x,+y` -is equivalent to `-C target-feature=-x,+y,+z`. +多个 features 通过 `-C target-feature` 选项的是可组合的。 \ +多个 features 可以在单个选项中使用逗号分隔符指定—— `-C target-feature=+x,-y`。 \ +如果某些 feature 同时使用 `+` 和 `-` 指定了一次或多次,那么后面传递的值会覆盖前面传递的值。 \ +例如, `-C target-feature=+x,-y,+z -Ctarget-feature=-x,+y` +等价于 `-C target-feature=-x,+y,+z`. -To see the valid options and an example of use, run `rustc --print -target-features`. +要查看有效的选项和使用示例,请运行 `rustc --print +target-features`。 -Using this flag is unsafe and might result in [undefined runtime -behavior](../targets/known-issues.md). +使用该标签是不安全的,可能会导致 [未定义的运行时行为](../targets/known-issues.md). -See also the [`target_feature` -attribute](../../reference/attributes/codegen.md#the-target_feature-attribute) -for controlling features per-function. +同样请参阅 [`target_feature` attribute](../../reference/attributes/codegen.md#the-target_feature-attribute) 控制每个函数的 features 。 -This also supports the feature `+crt-static` and `-crt-static` to control -[static C runtime linkage](../../reference/linkage.html#static-and-dynamic-c-runtimes). +这也支持 `+crt-static` 和 `-crt-static` feature 来控制 +[静态 C 运行时链接](../../reference/linkage.html#static-and-dynamic-c-runtimes). -Each target and [`target-cpu`](#target-cpu) has a default set of enabled -features. +每个 target 和 [`target-cpu`](#target-cpu) 都有一组默认启用的 features 。 ## tune-cpu -This instructs `rustc` to schedule code specifically for a particular -processor. This does not affect the compatibility (instruction sets or ABI), -but should make your code slightly more efficient on the selected CPU. +这会指示 `rustc` 为特定处理器调度代码。这不会影响兼容性(指令集或 ABI),但应该会使得代码在所选定的 CPU 上更有效率。 -The valid options are the same as those for [`target-cpu`](#target-cpu). -The default is `None`, which LLVM translates as the `target-cpu`. +有效的选项与 [`target-cpu`](#target-cpu) 的相同。默认值为 `None`, LLVM 将其转化为 `target-cpu` 。 -This is an unstable option. Use `-Z tune-cpu=machine` to specify a value. +这是个不稳定的选项。使用 `-Z tune-cpu=machine` 来指定值。 -Due to limitations in LLVM (12.0.0-git9218f92), this option is currently -effective only for x86 targets. +由于 LLVM (12.0.0-git9218f92)的限制,此选项当前仅针对 x86 目标有效。 [option-emit]: ../command-line-arguments.md#option-emit [option-o-optimize]: ../command-line-arguments.md#option-o-optimize From e9749b441a5ba7b5532dc6964383dbb8908219e7 Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Wed, 17 Mar 2021 19:43:09 +0800 Subject: [PATCH 10/11] The translation is completed --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 535aaff..8c2940d 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,14 @@ -[上海服务器结点加速](http://chen0adapter.top/books/rustc_book_cn/index.html) \ No newline at end of file +[上海服务器结点加速](http://chen0adapter.top/books/rustc_book_cn/index.html) + + + +## 翻译说明: +本书采用的是文档 2021年1月20日的版本,现已翻译完成。译者在二月份就已经完成了除4、5章之外章节的翻译,三月份继续翻译了剩余这两个章节。 + +当回去看原文档的时候发现官方添加了新章节[Exploit Mitigations](https://doc.rust-lang.org/rustc/exploit-mitigations.html),介绍的是一些 Rust 编译器所支持的漏洞安全措施(其中有部分在前面章节中有所提及),对于部分读者来说不是那么重要,且译者由于学业关系,将暂时不会跟进最新的官方章节更新,所以本书暂时主观上认为翻译完成。 + +欢迎社区的朋友进行勘误和跟进 ! + From f92d40a53f3c781084abf886f129f002a474e87b Mon Sep 17 00:00:00 2001 From: 1024chen <1946715297@qq.com> Date: Wed, 17 Mar 2021 19:47:14 +0800 Subject: [PATCH 11/11] modified mdbook --- book.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book.toml b/book.toml index bec3571..e979a5a 100644 --- a/book.toml +++ b/book.toml @@ -6,7 +6,7 @@ translator = "1024chen" language = "zh-CN" [output.html] -git-repository-url = "https://github.com/1024chen/irwa-cn" +git-repository-url = "https://github.com/1024chen/The-rustc-book-cn" git-repository-icon = "fa-github" [output.html.playpen]