Skip to content

Commit da5b3e7

Browse files
committed
---
yaml --- r: 152027 b: refs/heads/try2 c: 0fca6c6 h: refs/heads/master i: 152025: 2568d6b 152023: 82d1b91 v: v3
1 parent 9129266 commit da5b3e7

File tree

7 files changed

+193
-23
lines changed

7 files changed

+193
-23
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 3100bc5b82257820051774eb4aa0447b12f3616a
8+
refs/heads/try2: 0fca6c6a02d03491ee46ea5c4b656d114a73a53b
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/mk/docs.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
DOCS := index intro tutorial guide-ffi guide-macros guide-lifetimes \
3030
guide-tasks guide-container guide-pointers guide-testing \
3131
guide-runtime complement-bugreport complement-cheatsheet \
32-
complement-lang-faq complement-project-faq rust rustdoc \
33-
guide-unsafe
32+
complement-lang-faq complement-design-faq complement-project-faq rust \
33+
rustdoc guide-unsafe
3434

3535
PDF_DOCS := tutorial rust
3636

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
% Rust Design FAQ
2+
3+
This document describes decisions were arrived at after lengthy discussion and
4+
experimenting with alternatives. Please do not propose reversing them unless
5+
you have a new, extremely compelling argument. Note that this document
6+
specifically talks about the *language* and not any library or implementation.
7+
8+
A few general guidelines define the philosophy:
9+
10+
- [Memory safety][mem] must never be compromised
11+
- [Abstraction][abs] should be zero-cost, while still maintaining safety
12+
- Practicality is key
13+
14+
[mem]: http://en.wikipedia.org/wiki/Memory_safety
15+
[abs]: http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29
16+
17+
# Semantics
18+
19+
## Data layout is unspecified
20+
21+
In the general case, `enum` and `struct` layout is undefined. This allows the
22+
compiler to potentially do optimizations like re-using padding for the
23+
discriminant, compacting variants of nested enums, reordering fields to remove
24+
padding, etc. `enum`s which carry no data ("C-like") are eligible to have a
25+
defined representation. Such `enum`s are easily distinguished in that they are
26+
simply a list of names that carry no data:
27+
28+
```
29+
enum CLike {
30+
A,
31+
B = 32,
32+
C = 34,
33+
D
34+
}
35+
```
36+
37+
The [repr attribute][repr] can be applied to such `enum`s to give them the same
38+
representation as a primitive. This allows using Rust `enum`s in FFI where C
39+
`enum`s are also used, for most use cases. The attribute can also be applied
40+
to `struct`s to get the same layout as a C struct would.
41+
42+
[repr]: http://doc.rust-lang.org/rust.html#miscellaneous-attributes
43+
44+
## There is no GC
45+
46+
A language that requires a GC is a language that opts into a larger, more
47+
complex runtime than Rust cares for. Rust is usable on bare metal with no
48+
extra runtime. Additionally, garbage collection is frequently a source of
49+
non-deterministic behavior. Rust provides the tools to make using a GC
50+
possible and even pleasant, but it should not be a requirement for
51+
implementing the language.
52+
53+
## Non-`Share` `static mut` is unsafe
54+
55+
Types which are [`Share`][share] are thread-safe when multiple shared
56+
references to them are used concurrently. Types which are not `Share` are not
57+
thread-safe, and thus when used in a global require unsafe code to use.
58+
59+
[share]: http://doc.rust-lang.org/core/kinds/trait.Share.html
60+
61+
### If mutable static items that implement `Share` are safe, why is taking &mut SHARABLE unsafe?
62+
63+
Having multiple aliasing `&mut T`s is never allowed. Due to the nature of
64+
globals, the borrow checker cannot possibly ensure that a static obeys the
65+
borrowing rules, so taking a mutable reference to a static is always unsafe.
66+
67+
## There is no life before or after main (no static ctors/dtors)
68+
69+
Globals can not have a non-constant-expression constructor and cannot have a
70+
destructor at all. This is an opinion of the language. Static constructors are
71+
undesirable because they can slow down program startup. Life before main is
72+
often considered a misfeature, never to be used. Rust helps this along by just
73+
not having the feature.
74+
75+
See [the C++ FQA][fqa] about the "static initialization order fiasco", and
76+
[Eric Lippert's blog][elp] for the challenges in C#, which also has this
77+
feature.
78+
79+
A nice replacement is the [lazy constructor macro][lcm] by [Marvin
80+
Löbel][kim].
81+
82+
[fqa]: https://mail.mozilla.org/pipermail/rust-dev/2013-April/003815.html
83+
[elp]: http://ericlippert.com/2013/02/06/static-constructors-part-one/
84+
[lcm]: https://gist.github.com/Kimundi/8782487
85+
[kim]: https://github.com/Kimundi
86+
87+
## The language does not require a runtime
88+
89+
See the above entry on GC. Requiring a runtime limits the utility of the
90+
language, and makes it undeserving of the title "systems language". All Rust
91+
code should need to run is a stack.
92+
93+
## `match` must be exhaustive
94+
95+
`match` being exhaustive has some useful properties. First, if every
96+
possibility is covered by the `match`, adding further variants to the `enum`
97+
in the future will prompt a compilation failure, rather than runtime failure.
98+
Second, it makes cost explicit. In general, only safe way to have a
99+
non-exhaustive match would be to fail the task if nothing is matched, though
100+
it could fall through if the type of the `match` expression is `()`. This sort
101+
of hidden cost and special casing is against the language's philosophy. It's
102+
easy to ignore certain cases by using the `_` wildcard:
103+
104+
```rust,ignore
105+
match val.do_something() {
106+
Cat(a) => { /* ... */ }
107+
_ => { /* ... */ }
108+
}
109+
```
110+
111+
[#3101][iss] is the issue that proposed making this the only behavior, with
112+
rationale and discussion.
113+
114+
[iss]: https://github.com/mozilla/rust/issues/3101
115+
116+
## No guaranteed tail-call optimization
117+
118+
In general, tail-call optimization is not guaranteed: see for a detailed
119+
explanation with references. There is a [proposed extension][tce] that would
120+
allow tail-call elimination in certain contexts. The compiler is still free to
121+
optimize tail-calls [when it pleases][sco], however.
122+
123+
[tml]: https://mail.mozilla.org/pipermail/rust-dev/2013-April/003557.html
124+
[sco]: http://llvm.org/docs/CodeGenerator.html#sibling-call-optimization
125+
[tce]: https://github.com/rust-lang/rfcs/pull/81
126+
127+
## No constructors
128+
129+
Functions can serve the same purpose as constructors without adding any
130+
language complexity.
131+
132+
## No copy constructors
133+
134+
Types which implement [`Copy`][copy], will do a standard C-like "shallow copy"
135+
with no extra work (similar to "plain old data" in C++). It is impossible to
136+
implement `Copy` types that require custom copy behavior. Instead, in Rust
137+
"copy constructors" are created by implementing the [`Clone`][clone] trait,
138+
and explicitly calling the `clone` method. Making user-defined copy operators
139+
explicit surfaces the underlying complexity, forcing the developer to opt-in
140+
to potentially expensive operations.
141+
142+
[copy]: http://doc.rust-lang.org/core/kinds/trait.Copy.html
143+
[clone]: http://doc.rust-lang.org/core/clone/trait.Clone.html
144+
145+
## No move constructors
146+
147+
Values of all types are moved via `memcpy`. This makes writing generic unsafe
148+
code much simpler since assignment, passing and returning are known to never
149+
have a side effect like unwinding.
150+
151+
# Syntax
152+
153+
## Macros require balanced delimiters
154+
155+
This is to make the language easier to parse for machines. Since the body of a
156+
macro can contain arbitrary tokens, some restriction is needed to allow simple
157+
non-macro-expanding lexers and parsers. This comes in the form of requiring
158+
that all delimiters be balanced.
159+
160+
## `->` for function return type
161+
162+
This is to make the language easier to parse for humans, especially in the face
163+
of higher-order functions. `fn foo<T>(f: fn(int): int, fn(T): U): U` is not
164+
particularly easy to read.
165+
166+
## `let` is used to introduce variables
167+
168+
`let` not only defines variables, but can do pattern matching. One can also
169+
redeclare immutable variables with `let`. This is useful to avoid unnecessary
170+
`mut` annotations. An interesting historical note is that Rust comes,
171+
syntactically, most closely from ML, which also uses `let` to introduce
172+
bindings.
173+
174+
See also [a long thread][alt] on renaming `let mut` to `var`.
175+
176+
[alt]: https://mail.mozilla.org/pipermail/rust-dev/2014-January/008319.html

branches/try2/src/doc/guide-runtime.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The runtime is designed with a few goals in mind:
4545
support as well.
4646

4747
* The runtime should not enforce separate "modes of compilation" in order to
48-
work in multiple circumstances. Is it an explicit goal that you compile a Rust
48+
work in multiple circumstances. It is an explicit goal that you compile a Rust
4949
library once and use it forever (in all environments).
5050

5151
* The runtime should be fast. There should be no architectural design barrier

branches/try2/src/doc/index.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ li {list-style-type: none; }
2323
* [Testing](guide-testing.html)
2424
* [Rust's Runtime](guide-runtime.html)
2525

26+
# FAQs
27+
28+
* [Language Design FAQ](complement-design-faq.html)
29+
* [Language FAQ](complement-lang-faq.html)
30+
* [Project FAQ](complement-project-faq.html)
31+
* [Code cheatsheet](complement-cheatsheet.html) - "How do I do X?"
32+
* [How to submit a bug report](complement-bugreport.html)
33+
2634
# Libraries
2735

2836
* [The standard library, `std`](std/index.html)
@@ -60,13 +68,6 @@ li {list-style-type: none; }
6068

6169
* [The `rustdoc` manual](rustdoc.html)
6270

63-
# FAQs
64-
65-
* [Language FAQ](complement-lang-faq.html)
66-
* [Project FAQ](complement-project-faq.html)
67-
* [Code cheatsheet](complement-cheatsheet.html) - "How do I do X?"
68-
* [How to submit a bug report](complement-bugreport.html)
69-
7071
# External resources
7172

7273
* The Rust IRC channels on [irc.mozilla.org](http://irc.mozilla.org/)

branches/try2/src/libcollections/dlist.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,13 @@ impl<T> Deque<T> for DList<T> {
220220
/// Provide a reference to the back element, or None if the list is empty
221221
#[inline]
222222
fn back<'a>(&'a self) -> Option<&'a T> {
223-
let tmp = self.list_tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
224-
tmp.as_ref().map(|tail| &tail.value)
223+
self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
225224
}
226225

227226
/// Provide a mutable reference to the back element, or None if the list is empty
228227
#[inline]
229228
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
230-
let tmp: Option<&'a mut Node<T>> =
231-
self.list_tail.resolve(); // FIXME: #3511: shouldn't need variable
232-
tmp.map(|tail| &mut tail.value)
229+
self.list_tail.resolve().map(|tail| &mut tail.value)
233230
}
234231

235232
/// Add an element first in the list
@@ -449,8 +446,7 @@ impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
449446
if self.nelem == 0 {
450447
return None;
451448
}
452-
let tmp = self.tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
453-
tmp.as_ref().map(|prev| {
449+
self.tail.resolve_immut().as_ref().map(|prev| {
454450
self.nelem -= 1;
455451
self.tail = prev.prev;
456452
&prev.value

branches/try2/src/libstd/ascii.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,10 @@ mod tests {
591591
fn test_ascii_vec() {
592592
let test = &[40u8, 32u8, 59u8];
593593
assert_eq!(test.to_ascii(), v2ascii!([40, 32, 59]));
594-
assert_eq!("( ;".to_ascii(), v2ascii!([40, 32, 59]));
595-
// FIXME: #5475 borrowchk error, owned vectors do not live long enough
596-
// if chained-from directly
594+
assert_eq!("( ;".to_ascii(), v2ascii!([40, 32, 59]));
597595
let v = box [40u8, 32u8, 59u8];
598596
assert_eq!(v.to_ascii(), v2ascii!([40, 32, 59]));
599-
let v = "( ;".to_strbuf();
600-
assert_eq!(v.as_slice().to_ascii(), v2ascii!([40, 32, 59]));
597+
assert_eq!("( ;".to_strbuf().as_slice().to_ascii(), v2ascii!([40, 32, 59]));
601598

602599
assert_eq!("abCDef&?#".to_ascii().to_lower().into_str(), "abcdef&?#".to_strbuf());
603600
assert_eq!("abCDef&?#".to_ascii().to_upper().into_str(), "ABCDEF&?#".to_strbuf());

0 commit comments

Comments
 (0)