Skip to content

Commit 454ea85

Browse files
committed
Port the reference to mdbook
Part of #39588.
1 parent c1368fc commit 454ea85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3664
-4
lines changed

src/bootstrap/doc.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ pub fn standalone(build: &Build, target: &str) {
115115
.arg("-o").arg(&out)
116116
.arg(&path);
117117

118-
if filename == "reference.md" {
119-
cmd.arg("--html-in-header").arg(&full_toc);
120-
}
121-
122118
if filename == "not_found.md" {
123119
cmd.arg("--markdown-no-toc")
124120
.arg("--markdown-css")

src/bootstrap/step.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,15 @@ pub fn build_rules<'a>(build: &'a Build) -> Rules {
568568
})
569569
.default(build.config.docs)
570570
.run(move |s| doc::rustbook(build, s.target, "nomicon"));
571+
rules.doc("doc-reference", "src/doc/reference")
572+
.dep(move |s| {
573+
s.name("tool-rustbook")
574+
.host(&build.config.build)
575+
.target(&build.config.build)
576+
.stage(0)
577+
})
578+
.default(build.config.docs)
579+
.run(move |s| doc::rustbook(build, s.target, "reference"));
571580
rules.doc("doc-standalone", "src/doc")
572581
.dep(move |s| {
573582
s.name("rustc")

src/doc/reference/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

src/doc/reference/src/SUMMARY.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# The Rust Reference
2+
3+
[Introduction](introduction.md)
4+
5+
- [Notation](notation.md)
6+
- [Unicode productions](unicode-productions.md)
7+
- [String table productions](string-table-productions.md)
8+
9+
- [Lexical structure](lexical-structure.md)
10+
- [Input format](input-format.md)
11+
- [Identifiers](identifiers.md)
12+
- [Comments](comments.md)
13+
- [Whitespace](whitespace.md)
14+
- [Tokens](tokens.md)
15+
- [Paths](paths.md)
16+
17+
- [Macros](macros.md)
18+
- [Macros By Example](macros-by-example.md)
19+
- [Procedrual Macros](procedural-macros.md)
20+
21+
- [Crates and source files](crates-and-source-files.md)
22+
23+
- [Items and attributes](items-and-attributes.md)
24+
- [Items](items.md)
25+
- [Visibility and Privacy](visibility-and-privacy.md)
26+
- [Attributes](attributes.md)
27+
28+
- [Statements and expressions](statements-and-expressions.md)
29+
- [Statements](statements.md)
30+
- [Expressions](expressions.md)
31+
32+
- [Type system](type-system.md)
33+
- [Types](types.md)
34+
- [Subtyping](subtyping.md)
35+
- [Type coercions](type-coercions.md)
36+
37+
- [Special traits](special-traits.md)
38+
- [The Copy trait](the-copy-trait.md)
39+
- [The Sized trait](the-sized-trait.md)
40+
- [The Drop trait](the-drop-trait.md)
41+
- [The Deref trait](the-deref-trait.md)
42+
- [The Send trait](the-send-trait.md)
43+
- [The Sync trait](the-sync-trait.md)
44+
45+
- [Memory model](memory-model.md)
46+
- [Memory allocation and lifetime](memory-allocation-and-lifetime.md)
47+
- [Memory ownership](memory-ownership.md)
48+
- [Variables](variables.md)
49+
50+
- [Linkage](linkage.md)
51+
52+
- [Unsafety](unsafety.md)
53+
- [Unsafe functions](unsafe-functions.md)
54+
- [Unsafe blocks](unsafe-blocks.md)
55+
- [Behavior considered undefined](behavior-considered-undefined.md)
56+
- [Behavior not considered unsafe](behavior-not-considered-unsafe.md)
57+
58+
[Appendix: Influences](influences.md)

src/doc/reference/src/attributes.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# Attributes
2+
3+
Any item declaration may have an _attribute_ applied to it. Attributes in Rust
4+
are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334
5+
(C#). An attribute is a general, free-form metadatum that is interpreted
6+
according to name, convention, and language and compiler version. Attributes
7+
may appear as any of:
8+
9+
* A single identifier, the attribute name
10+
* An identifier followed by the equals sign '=' and a literal, providing a
11+
key/value pair
12+
* An identifier followed by a parenthesized list of sub-attribute arguments
13+
14+
Attributes with a bang ("!") after the hash ("#") apply to the item that the
15+
attribute is declared within. Attributes that do not have a bang after the hash
16+
apply to the item that follows the attribute.
17+
18+
An example of attributes:
19+
20+
```{.rust}
21+
// General metadata applied to the enclosing module or crate.
22+
#![crate_type = "lib"]
23+
24+
// A function marked as a unit test
25+
#[test]
26+
fn test_foo() {
27+
/* ... */
28+
}
29+
30+
// A conditionally-compiled module
31+
#[cfg(target_os="linux")]
32+
mod bar {
33+
/* ... */
34+
}
35+
36+
// A lint attribute used to suppress a warning/error
37+
#[allow(non_camel_case_types)]
38+
type int8_t = i8;
39+
```
40+
41+
> **Note:** At some point in the future, the compiler will distinguish between
42+
> language-reserved and user-available attributes. Until then, there is
43+
> effectively no difference between an attribute handled by a loadable syntax
44+
> extension and the compiler.
45+
46+
## Crate-only attributes
47+
48+
- `crate_name` - specify the crate's crate name.
49+
- `crate_type` - see [linkage](#linkage).
50+
- `feature` - see [compiler features](#compiler-features).
51+
- `no_builtins` - disable optimizing certain code patterns to invocations of
52+
library functions that are assumed to exist
53+
- `no_main` - disable emitting the `main` symbol. Useful when some other
54+
object being linked to defines `main`.
55+
- `no_start` - disable linking to the `native` crate, which specifies the
56+
"start" language item.
57+
- `no_std` - disable linking to the `std` crate.
58+
- `plugin` - load a list of named crates as compiler plugins, e.g.
59+
`#![plugin(foo, bar)]`. Optional arguments for each plugin,
60+
i.e. `#![plugin(foo(... args ...))]`, are provided to the plugin's
61+
registrar function. The `plugin` feature gate is required to use
62+
this attribute.
63+
- `recursion_limit` - Sets the maximum depth for potentially
64+
infinitely-recursive compile-time operations like
65+
auto-dereference or macro expansion. The default is
66+
`#![recursion_limit="64"]`.
67+
68+
### Module-only attributes
69+
70+
- `no_implicit_prelude` - disable injecting `use std::prelude::*` in this
71+
module.
72+
- `path` - specifies the file to load the module from. `#[path="foo.rs"] mod
73+
bar;` is equivalent to `mod bar { /* contents of foo.rs */ }`. The path is
74+
taken relative to the directory that the current module is in.
75+
76+
## Function-only attributes
77+
78+
- `main` - indicates that this function should be passed to the entry point,
79+
rather than the function in the crate root named `main`.
80+
- `plugin_registrar` - mark this function as the registration point for
81+
[compiler plugins][plugin], such as loadable syntax extensions.
82+
- `start` - indicates that this function should be used as the entry point,
83+
overriding the "start" language item. See the "start" [language
84+
item](#language-items) for more details.
85+
- `test` - indicates that this function is a test function, to only be compiled
86+
in case of `--test`.
87+
- `should_panic` - indicates that this test function should panic, inverting the success condition.
88+
- `cold` - The function is unlikely to be executed, so optimize it (and calls
89+
to it) differently.
90+
- `naked` - The function utilizes a custom ABI or custom inline ASM that requires
91+
epilogue and prologue to be skipped.
92+
93+
## Static-only attributes
94+
95+
- `thread_local` - on a `static mut`, this signals that the value of this
96+
static may change depending on the current thread. The exact consequences of
97+
this are implementation-defined.
98+
99+
## FFI attributes
100+
101+
On an `extern` block, the following attributes are interpreted:
102+
103+
- `link_args` - specify arguments to the linker, rather than just the library
104+
name and type. This is feature gated and the exact behavior is
105+
implementation-defined (due to variety of linker invocation syntax).
106+
- `link` - indicate that a native library should be linked to for the
107+
declarations in this block to be linked correctly. `link` supports an optional
108+
`kind` key with three possible values: `dylib`, `static`, and `framework`. See
109+
[external blocks](#external-blocks) for more about external blocks. Two
110+
examples: `#[link(name = "readline")]` and
111+
`#[link(name = "CoreFoundation", kind = "framework")]`.
112+
- `linked_from` - indicates what native library this block of FFI items is
113+
coming from. This attribute is of the form `#[linked_from = "foo"]` where
114+
`foo` is the name of a library in either `#[link]` or a `-l` flag. This
115+
attribute is currently required to export symbols from a Rust dynamic library
116+
on Windows, and it is feature gated behind the `linked_from` feature.
117+
118+
On declarations inside an `extern` block, the following attributes are
119+
interpreted:
120+
121+
- `link_name` - the name of the symbol that this function or static should be
122+
imported as.
123+
- `linkage` - on a static, this specifies the [linkage
124+
type](http://llvm.org/docs/LangRef.html#linkage-types).
125+
126+
On `enum`s:
127+
128+
- `repr` - on C-like enums, this sets the underlying type used for
129+
representation. Takes one argument, which is the primitive
130+
type this enum should be represented for, or `C`, which specifies that it
131+
should be the default `enum` size of the C ABI for that platform. Note that
132+
enum representation in C is undefined, and this may be incorrect when the C
133+
code is compiled with certain flags.
134+
135+
On `struct`s:
136+
137+
- `repr` - specifies the representation to use for this struct. Takes a list
138+
of options. The currently accepted ones are `C` and `packed`, which may be
139+
combined. `C` will use a C ABI compatible struct layout, and `packed` will
140+
remove any padding between fields (note that this is very fragile and may
141+
break platforms which require aligned access).
142+
143+
## Macro-related attributes
144+
145+
- `macro_use` on a `mod` — macros defined in this module will be visible in the
146+
module's parent, after this module has been included.
147+
148+
- `macro_use` on an `extern crate` — load macros from this crate. An optional
149+
list of names `#[macro_use(foo, bar)]` restricts the import to just those
150+
macros named. The `extern crate` must appear at the crate root, not inside
151+
`mod`, which ensures proper function of the [`$crate` macro
152+
variable](book/macros.html#The%20variable%20%24crate).
153+
154+
- `macro_reexport` on an `extern crate` — re-export the named macros.
155+
156+
- `macro_export` - export a macro for cross-crate usage.
157+
158+
- `no_link` on an `extern crate` — even if we load this crate for macros, don't
159+
link it into the output.
160+
161+
See the [macros section of the
162+
book](book/macros.html#Scoping%20and%20macro%20import%2Fexport) for more information on
163+
macro scope.
164+
165+
## Miscellaneous attributes
166+
167+
- `deprecated` - mark the item as deprecated; the full attribute is
168+
`#[deprecated(since = "crate version", note = "...")`, where both arguments
169+
are optional.
170+
- `export_name` - on statics and functions, this determines the name of the
171+
exported symbol.
172+
- `link_section` - on statics and functions, this specifies the section of the
173+
object file that this item's contents will be placed into.
174+
- `no_mangle` - on any item, do not apply the standard name mangling. Set the
175+
symbol for this item to its identifier.
176+
- `simd` - on certain tuple structs, derive the arithmetic operators, which
177+
lower to the target's SIMD instructions, if any; the `simd` feature gate
178+
is necessary to use this attribute.
179+
- `unsafe_destructor_blind_to_params` - on `Drop::drop` method, asserts that the
180+
destructor code (and all potential specializations of that code) will
181+
never attempt to read from nor write to any references with lifetimes
182+
that come in via generic parameters. This is a constraint we cannot
183+
currently express via the type system, and therefore we rely on the
184+
programmer to assert that it holds. Adding this to a Drop impl causes
185+
the associated destructor to be considered "uninteresting" by the
186+
Drop-Check rule, and thus it can help sidestep data ordering
187+
constraints that would otherwise be introduced by the Drop-Check
188+
rule. Such sidestepping of the constraints, if done incorrectly, can
189+
lead to undefined behavior (in the form of reading or writing to data
190+
outside of its dynamic extent), and thus this attribute has the word
191+
"unsafe" in its name. To use this, the
192+
`unsafe_destructor_blind_to_params` feature gate must be enabled.
193+
- `doc` - Doc comments such as `/// foo` are equivalent to `#[doc = "foo"]`.
194+
- `rustc_on_unimplemented` - Write a custom note to be shown along with the error
195+
when the trait is found to be unimplemented on a type.
196+
You may use format arguments like `{T}`, `{A}` to correspond to the
197+
types at the point of use corresponding to the type parameters of the
198+
trait of the same name. `{Self}` will be replaced with the type that is supposed
199+
to implement the trait but doesn't. To use this, the `on_unimplemented` feature gate
200+
must be enabled.
201+
- `must_use` - on structs and enums, will warn if a value of this type isn't used or
202+
assigned to a variable. You may also include an optional message by using
203+
`#[must_use = "message"]` which will be given alongside the warning.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Behavior considered undefined
2+
3+
The following is a list of behavior which is forbidden in all Rust code,
4+
including within `unsafe` blocks and `unsafe` functions. Type checking provides
5+
the guarantee that these issues are never caused by safe code.
6+
7+
* Data races
8+
* Dereferencing a null/dangling raw pointer
9+
* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values)
10+
(uninitialized) memory
11+
* Breaking the [pointer aliasing
12+
rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules)
13+
with raw pointers (a subset of the rules used by C)
14+
* `&mut T` and `&T` follow LLVM’s scoped [noalias] model, except if the `&T`
15+
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
16+
guarantees.
17+
* Mutating non-mutable data (that is, data reached through a shared reference or
18+
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
19+
* Invoking undefined behavior via compiler intrinsics:
20+
* Indexing outside of the bounds of an object with `std::ptr::offset`
21+
(`offset` intrinsic), with
22+
the exception of one byte past the end which is permitted.
23+
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
24+
intrinsics) on overlapping buffers
25+
* Invalid values in primitive types, even in private fields/locals:
26+
* Dangling/null references or boxes
27+
* A value other than `false` (0) or `true` (1) in a `bool`
28+
* A discriminant in an `enum` not included in the type definition
29+
* A value in a `char` which is a surrogate or above `char::MAX`
30+
* Non-UTF-8 byte sequences in a `str`
31+
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
32+
code. Rust's failure system is not compatible with exception handling in
33+
other languages. Unwinding must be caught and handled at FFI boundaries.
34+
35+
[noalias]: http://llvm.org/docs/LangRef.html#noalias
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Behavior not considered unsafe
2+
3+
This is a list of behavior not considered *unsafe* in Rust terms, but that may
4+
be undesired.
5+
6+
* Deadlocks
7+
* Leaks of memory and other resources
8+
* Exiting without calling destructors
9+
* Integer overflow
10+
- Overflow is considered "unexpected" behavior and is always user-error,
11+
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
12+
will insert debug checks that panic on overflow, but in optimized builds overflow
13+
instead results in wrapped values. See [RFC 560] for the rationale and more details.
14+
15+
[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md

src/doc/reference/src/comments.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Comments
2+
3+
Comments in Rust code follow the general C++ style of line (`//`) and
4+
block (`/* ... */`) comment forms. Nested block comments are supported.
5+
6+
Line comments beginning with exactly _three_ slashes (`///`), and block
7+
comments (`/** ... */`), are interpreted as a special syntax for `doc`
8+
[attributes](#attributes). That is, they are equivalent to writing
9+
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
10+
`#[doc="Foo"]`.
11+
12+
Line comments beginning with `//!` and block comments `/*! ... */` are
13+
doc comments that apply to the parent of the comment, rather than the item
14+
that follows. That is, they are equivalent to writing `#![doc="..."]` around
15+
the body of the comment. `//!` comments are usually used to document
16+
modules that occupy a source file.
17+
18+
Non-doc comments are interpreted as a form of whitespace.

0 commit comments

Comments
 (0)