Skip to content

Commit

Permalink
Auto merge of #28529 - Manishearth:rollup, r=Manishearth
Browse files Browse the repository at this point in the history
- Successful merges: #28463, #28507, #28522, #28525, #28526
- Failed merges:
  • Loading branch information
bors committed Sep 20, 2015
2 parents fd38a75 + d7ec69a commit 25aaeb4
Show file tree
Hide file tree
Showing 102 changed files with 215 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/doc/trpl/choosing-your-guarantees.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ there's a lot of concurrent access happening.

# Composition

A common gripe when reading Rust code is with types like `Rc<RefCell<Vec<T>>>` (or even more more
A common gripe when reading Rust code is with types like `Rc<RefCell<Vec<T>>>` (or even more
complicated compositions of such types). It's not always clear what the composition does, or why the
author chose one like this (and when one should be using such a composition in one's own code)

Expand Down
59 changes: 28 additions & 31 deletions src/doc/trpl/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ to help us make sense of code that can possibly be concurrent.
### `Send`

The first trait we're going to talk about is
[`Send`](../std/marker/trait.Send.html). When a type `T` implements `Send`, it indicates
to the compiler that something of this type is able to have ownership transferred
[`Send`](../std/marker/trait.Send.html). When a type `T` implements `Send`, it
indicates that something of this type is able to have ownership transferred
safely between threads.

This is important to enforce certain restrictions. For example, if we have a
Expand All @@ -42,13 +42,19 @@ us enforce that it can't leave the current thread.
### `Sync`

The second of these traits is called [`Sync`](../std/marker/trait.Sync.html).
When a type `T` implements `Sync`, it indicates to the compiler that something
When a type `T` implements `Sync`, it indicates that something
of this type has no possibility of introducing memory unsafety when used from
multiple threads concurrently.

For example, sharing immutable data with an atomic reference count is
threadsafe. Rust provides a type like this, `Arc<T>`, and it implements `Sync`,
so it is safe to share between threads.
multiple threads concurrently through shared references. This implies that
types which don't have [interior mutability](mutability.html) are inherently
`Sync`, which includes simple primitive types (like `u8`) and aggregate types
containing them.

For sharing references across threads, Rust provides a wrapper type called
`Arc<T>`. `Arc<T>` implements `Send` and `Sync` if and only if `T` implements
both `Send` and `Sync`. For example, an object of type `Arc<RefCell<U>>` cannot
be transferred across threads because
[`RefCell`](choosing-your-guarantees.html#refcell%3Ct%3E) does not implement
`Sync`, consequently `Arc<RefCell<U>>` would not implement `Send`.

These two traits allow you to use the type system to make strong guarantees
about the properties of your code under concurrency. Before we demonstrate
Expand All @@ -70,7 +76,7 @@ fn main() {
}
```

The `thread::spawn()` method accepts a closure, which is executed in a
The `thread::spawn()` method accepts a [closure](closures.html), which is executed in a
new thread. It returns a handle to the thread, that can be used to
wait for the child thread to finish and extract its result:

Expand Down Expand Up @@ -215,29 +221,18 @@ fn main() {
}
```

Note that the value of `i` is bound (copied) to the closure and not shared
among the threads.

If we'd tried to use `Mutex<T>` without wrapping it in an `Arc<T>` we would have
seen another error like:

```text
error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` [E0277]
thread::spawn(move || {
^~~~~~~~~~~~~
note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec<u32>>` cannot be sent between threads safely
thread::spawn(move || {
^~~~~~~~~~~~~
```

You see, [`Mutex`](../std/sync/struct.Mutex.html) has a
[`lock`](../std/sync/struct.Mutex.html#method.lock)
method which has this signature:
Also note that [`lock`](../std/sync/struct.Mutex.html#method.lock) method of
[`Mutex`](../std/sync/struct.Mutex.html) has this signature:

```ignore
fn lock(&self) -> LockResult<MutexGuard<T>>
```

and because `Send` is not implemented for `MutexGuard<T>`, we couldn't have
transferred the guard across thread boundaries on it's own.
and because `Send` is not implemented for `MutexGuard<T>`, the guard cannot
cross thread boundaries, ensuring thread-locality of lock acquire and release.

Let's examine the body of the thread more closely:

Expand Down Expand Up @@ -317,22 +312,24 @@ use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();

for _ in 0..10 {
for i in 0..10 {
let tx = tx.clone();

thread::spawn(move || {
let answer = 42;
let answer = i * i;

tx.send(answer);
});
}

rx.recv().ok().expect("Could not receive answer");
for _ in 0..10 {
println!("{}", rx.recv().unwrap());
}
}
```

A `u32` is `Send` because we can make a copy. So we create a thread, ask it to calculate
the answer, and then it `send()`s us the answer over the channel.
Here we create 10 threads, asking each to calculate the square of a number (`i`
at the time of `spawn()`), and then `send()` back the answer over the channel.


## Panics
Expand Down
12 changes: 10 additions & 2 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,18 @@ const FOO: i32 = { 0 }; // but brackets are useless here
```
"##,

// FIXME(#24111) Change the language here when const fn stabilizes
E0015: r##"
The only functions that can be called in static or constant expressions are
`const` functions. Rust currently does not support more general compile-time
function execution.
`const` functions, and struct/enum constructors. `const` functions are only
available on a nightly compiler. Rust currently does not support more general
compile-time function execution.
```
const FOO: Option<u8> = Some(1); // enum constructor
struct Bar {x: u8}
const BAR: Bar = Bar {x: 1}; // struct constructor
```
See [RFC 911] for more details on the design of `const fn`s.
Expand Down
20 changes: 16 additions & 4 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use util::nodemap::NodeMap;
use rustc_front::hir;
use syntax::ast;
use syntax::codemap::Span;
use syntax::feature_gate::UnstableFeatures;
use rustc_front::visit::{self, FnKind, Visitor};

use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -709,10 +710,21 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
if !is_const {
v.add_qualif(ConstQualif::NOT_CONST);
if v.mode != Mode::Var {
span_err!(v.tcx.sess, e.span, E0015,
"function calls in {}s are limited to \
constant functions, \
struct and enum constructors", v.msg());
// FIXME(#24111) Remove this check when const fn stabilizes
if let UnstableFeatures::Disallow = v.tcx.sess.opts.unstable_features {
span_err!(v.tcx.sess, e.span, E0015,
"function calls in {}s are limited to \
struct and enum constructors", v.msg());
v.tcx.sess.span_note(e.span,
"a limited form of compile-time function \
evaluation is available on a nightly \
compiler via `const fn`");
} else {
span_err!(v.tcx.sess, e.span, E0015,
"function calls in {}s are limited to \
constant functions, \
struct and enum constructors", v.msg());
}
}
}
}
Expand Down
49 changes: 25 additions & 24 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,15 +684,15 @@ fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool
// logic as for expression-position macro invocations.
pub fn expand_item_mac(it: P<ast::Item>,
fld: &mut MacroExpander) -> SmallVector<P<ast::Item>> {
let (extname, path_span, tts) = match it.node {
let (extname, path_span, tts, span, attrs, ident) = it.and_then(|it| { match it.node {
ItemMac(codemap::Spanned {
node: MacInvocTT(ref pth, ref tts, _),
node: MacInvocTT(pth, tts, _),
..
}) => {
(pth.segments[0].identifier.name, pth.span, (*tts).clone())
(pth.segments[0].identifier.name, pth.span, tts, it.span, it.attrs, it.ident)
}
_ => fld.cx.span_bug(it.span, "invalid item macro invocation")
};
}});

let fm = fresh_mark();
let items = {
Expand All @@ -706,56 +706,56 @@ pub fn expand_item_mac(it: P<ast::Item>,
}

Some(rc) => match *rc {
NormalTT(ref expander, span, allow_internal_unstable) => {
if it.ident.name != parse::token::special_idents::invalid.name {
NormalTT(ref expander, tt_span, allow_internal_unstable) => {
if ident.name != parse::token::special_idents::invalid.name {
fld.cx
.span_err(path_span,
&format!("macro {}! expects no ident argument, given '{}'",
extname,
it.ident));
ident));
return SmallVector::zero();
}
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
call_site: span,
callee: NameAndSpan {
format: MacroBang(extname),
span: span,
span: tt_span,
allow_internal_unstable: allow_internal_unstable,
}
});
// mark before expansion:
let marked_before = mark_tts(&tts[..], fm);
expander.expand(fld.cx, it.span, &marked_before[..])
expander.expand(fld.cx, span, &marked_before[..])
}
IdentTT(ref expander, span, allow_internal_unstable) => {
if it.ident.name == parse::token::special_idents::invalid.name {
IdentTT(ref expander, tt_span, allow_internal_unstable) => {
if ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_err(path_span,
&format!("macro {}! expects an ident argument",
extname));
return SmallVector::zero();
}
fld.cx.bt_push(ExpnInfo {
call_site: it.span,
call_site: span,
callee: NameAndSpan {
format: MacroBang(extname),
span: span,
span: tt_span,
allow_internal_unstable: allow_internal_unstable,
}
});
// mark before expansion:
let marked_tts = mark_tts(&tts[..], fm);
expander.expand(fld.cx, it.span, it.ident, marked_tts)
expander.expand(fld.cx, span, ident, marked_tts)
}
MacroRulesTT => {
if it.ident.name == parse::token::special_idents::invalid.name {
if ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_err(path_span,
&format!("macro_rules! expects an ident argument")
);
return SmallVector::zero();
}

fld.cx.bt_push(ExpnInfo {
call_site: it.span,
call_site: span,
callee: NameAndSpan {
format: MacroBang(extname),
span: None,
Expand All @@ -767,7 +767,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
});
// DON'T mark before expansion.

let allow_internal_unstable = attr::contains_name(&it.attrs,
let allow_internal_unstable = attr::contains_name(&attrs,
"allow_internal_unstable");

// ensure any #[allow_internal_unstable]s are
Expand All @@ -777,18 +777,19 @@ pub fn expand_item_mac(it: P<ast::Item>,
feature_gate::emit_feature_err(
&fld.cx.parse_sess.span_diagnostic,
"allow_internal_unstable",
it.span,
span,
feature_gate::GateIssue::Language,
feature_gate::EXPLAIN_ALLOW_INTERNAL_UNSTABLE)
}

let export = attr::contains_name(&attrs, "macro_export");
let def = ast::MacroDef {
ident: it.ident,
attrs: it.attrs.clone(),
ident: ident,
attrs: attrs,
id: ast::DUMMY_NODE_ID,
span: it.span,
span: span,
imported_from: None,
export: attr::contains_name(&it.attrs, "macro_export"),
export: export,
use_locally: true,
allow_internal_unstable: allow_internal_unstable,
body: tts,
Expand All @@ -800,7 +801,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
return SmallVector::zero();
}
_ => {
fld.cx.span_err(it.span,
fld.cx.span_err(span,
&format!("{}! is not legal in item position",
extname));
return SmallVector::zero();
Expand Down
15 changes: 13 additions & 2 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status

// allow overloading augmented assignment operations like `a += b`
("augmented_assignments", "1.5.0", None, Active),

// allow `#[no_debug]`
("no_debug", "1.5.0", None, Active),

// allow `#[omit_gdb_pretty_printer_section]`
("omit_gdb_pretty_printer_section", "1.5.0", None, Active),
];
// (changing above list without updating src/doc/reference.md makes @cmr sad)

Expand Down Expand Up @@ -320,8 +326,13 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat
("link_section", Whitelisted, Ungated),
("no_builtins", Whitelisted, Ungated),
("no_mangle", Whitelisted, Ungated),
("no_debug", Whitelisted, Ungated),
("omit_gdb_pretty_printer_section", Whitelisted, Ungated),
("no_debug", Whitelisted, Gated("no_debug",
"the `#[no_debug]` attribute \
is an experimental feature")),
("omit_gdb_pretty_printer_section", Whitelisted, Gated("omit_gdb_pretty_printer_section",
"the `#[omit_gdb_pretty_printer_section]` \
attribute is just used for the Rust test \
suite")),
("unsafe_no_drop_flag", Whitelisted, Gated("unsafe_no_drop_flag",
"unsafe_no_drop_flag has unstable semantics \
and may be removed in the future")),
Expand Down
1 change: 1 addition & 0 deletions src/test/auxiliary/cross_crate_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![crate_type = "rlib"]

#![allow(unused_variables)]
#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

// no-prefer-dynamic
Expand Down
12 changes: 12 additions & 0 deletions src/test/compile-fail/feature-gate-no-debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[no_debug] //~ ERROR the `#[no_debug]` attribute is
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[omit_gdb_pretty_printer_section] //~ ERROR the `#[omit_gdb_pretty_printer_section]` attribute is
fn main() {}
1 change: 1 addition & 0 deletions src/test/debuginfo/associated-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

#![allow(unused_variables)]
#![allow(dead_code)]
#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

trait TraitWithAssocType {
Expand Down
1 change: 1 addition & 0 deletions src/test/debuginfo/basic-types-globals-metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#![allow(unused_variables)]
#![allow(dead_code)]
#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

// N.B. These are `mut` only so they don't constant fold away.
Expand Down
1 change: 1 addition & 0 deletions src/test/debuginfo/basic-types-globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
// gdb-command:continue

#![allow(unused_variables)]
#![feature(omit_gdb_pretty_printer_section)]
#![omit_gdb_pretty_printer_section]

// N.B. These are `mut` only so they don't constant fold away.
Expand Down
Loading

0 comments on commit 25aaeb4

Please sign in to comment.