Skip to content

Commit c851b85

Browse files
committed
---
yaml --- r: 225247 b: refs/heads/stable c: cc156c2 h: refs/heads/master i: 225245: 4ddcb5a 225243: 6fb97b6 225239: 5557c0f 225231: d0c072b 225215: 66a58fa v: v3
1 parent c5a1ca2 commit c851b85

File tree

8 files changed

+87
-25
lines changed

8 files changed

+87
-25
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ refs/heads/tmp: e5d90d98402475b6e154ce216f9efcb80da1a747
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: 1fe32ca12c51afcd761d9962f51a74ff0d07a591
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: 6bb56b4fa7c52dc3d19d583303baf6d92e580ada
32+
refs/heads/stable: cc156c2f3819e8818c66e5f5d0bb143739e3bbb0
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b

branches/stable/src/doc/trpl/strings.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ fn main() {
4949
}
5050
```
5151

52+
This coercion does not happen for functions that accept one of `&str`’s traits
53+
instead of `&str`. For example, [`TcpStream::connect`][connect] has a parameter
54+
of type `ToSocketAddrs`. A `&str` is okay but a `String` must be explicitly
55+
converted using `&*`.
56+
57+
```rust,no_run
58+
use std::net::TcpStream;
59+
60+
TcpStream::connect("192.168.0.1:3000"); // &str parameter
61+
62+
let addr_string = "192.168.0.1:3000".to_string();
63+
TcpStream::connect(&*addr_string); // convert addr_string to &str
64+
```
65+
5266
Viewing a `String` as a `&str` is cheap, but converting the `&str` to a
5367
`String` involves allocating memory. No reason to do that unless you have to!
5468

@@ -127,3 +141,4 @@ This is because `&String` can automatically coerce to a `&str`. This is a
127141
feature called ‘[`Deref` coercions][dc]’.
128142

129143
[dc]: deref-coercions.html
144+
[connect]: ../std/net/struct.TcpStream.html#method.connect

branches/stable/src/libcore/iter.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,19 @@ pub trait Iterator {
452452
Scan{iter: self, f: f, state: initial_state}
453453
}
454454

455-
/// Creates an iterator that maps each element to an iterator,
456-
/// and yields the elements of the produced iterators.
455+
/// Takes a function that maps each element to a new iterator and yields
456+
/// all the elements of the produced iterators.
457+
///
458+
/// This is useful for unraveling nested structures.
457459
///
458460
/// # Examples
459461
///
460462
/// ```
461-
/// # #![feature(core)]
462-
/// let xs = [2, 3];
463-
/// let ys = [0, 1, 0, 1, 2];
464-
/// let it = xs.iter().flat_map(|&x| (0..).take(x));
465-
/// // Check that `it` has the same elements as `ys`
466-
/// for (i, x) in it.enumerate() {
467-
/// assert_eq!(x, ys[i]);
468-
/// }
463+
/// let words = ["alpha", "beta", "gamma"];
464+
/// let merged: String = words.iter()
465+
/// .flat_map(|s| s.chars())
466+
/// .collect();
467+
/// assert_eq!(merged, "alphabetagamma");
469468
/// ```
470469
#[inline]
471470
#[stable(feature = "rust1", since = "1.0.0")]

branches/stable/src/librustc_trans/trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ pub fn load_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
785785
if !global.is_null() && llvm::LLVMIsGlobalConstant(global) == llvm::True {
786786
let val = llvm::LLVMGetInitializer(global);
787787
if !val.is_null() {
788-
return from_arg_ty(cx, val, t);
788+
return to_arg_ty(cx, val, t);
789789
}
790790
}
791791
}
@@ -807,7 +807,7 @@ pub fn load_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
807807
llvm::LLVMSetAlignment(val, align);
808808
}
809809

810-
from_arg_ty(cx, val, t)
810+
to_arg_ty(cx, val, t)
811811
}
812812

813813
/// Helper for storing values in memory. Does the necessary conversion if the in-memory type
@@ -817,21 +817,21 @@ pub fn store_ty<'blk, 'tcx>(cx: Block<'blk, 'tcx>, v: ValueRef, dst: ValueRef, t
817817
return;
818818
}
819819

820-
let store = Store(cx, to_arg_ty(cx, v, t), to_arg_ty_ptr(cx, dst, t));
820+
let store = Store(cx, from_arg_ty(cx, v, t), to_arg_ty_ptr(cx, dst, t));
821821
unsafe {
822822
llvm::LLVMSetAlignment(store, type_of::align_of(cx.ccx(), t));
823823
}
824824
}
825825

826-
pub fn to_arg_ty(bcx: Block, val: ValueRef, ty: Ty) -> ValueRef {
826+
pub fn from_arg_ty(bcx: Block, val: ValueRef, ty: Ty) -> ValueRef {
827827
if ty::type_is_bool(ty) {
828828
ZExt(bcx, val, Type::i8(bcx.ccx()))
829829
} else {
830830
val
831831
}
832832
}
833833

834-
pub fn from_arg_ty(bcx: Block, val: ValueRef, ty: Ty) -> ValueRef {
834+
pub fn to_arg_ty(bcx: Block, val: ValueRef, ty: Ty) -> ValueRef {
835835
if ty::type_is_bool(ty) {
836836
Trunc(bcx, val, Type::i1(bcx.ccx()))
837837
} else {

branches/stable/src/librustc_trans/trans/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,9 @@ fn auto_ref<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
21802180
// Construct the resulting datum, using what was the "by ref"
21812181
// ValueRef of type `referent_ty` to be the "by value" ValueRef
21822182
// of type `&referent_ty`.
2183-
DatumBlock::new(bcx, Datum::new(llref, ptr_ty, RvalueExpr(Rvalue::new(ByValue))))
2183+
// Pointers to DST types are non-immediate, and therefore still use ByRef.
2184+
let kind = if type_is_sized(bcx.tcx(), referent_ty) { ByValue } else { ByRef };
2185+
DatumBlock::new(bcx, Datum::new(llref, ptr_ty, RvalueExpr(Rvalue::new(kind))))
21842186
}
21852187

21862188
fn deref_multiple<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,

branches/stable/src/librustc_trans/trans/intrinsic.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
223223
let val = if datum.kind.is_by_ref() {
224224
load_ty(bcx, datum.val, datum.ty)
225225
} else {
226-
datum.val
226+
from_arg_ty(bcx, datum.val, datum.ty)
227227
};
228228

229229
let cast_val = BitCast(bcx, val, llret_ty);
@@ -490,12 +490,12 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
490490
unsafe {
491491
llvm::LLVMSetAlignment(load, type_of::align_of(ccx, tp_ty));
492492
}
493-
from_arg_ty(bcx, load, tp_ty)
493+
to_arg_ty(bcx, load, tp_ty)
494494
},
495495
(_, "volatile_store") => {
496496
let tp_ty = *substs.types.get(FnSpace, 0);
497497
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
498-
let val = to_arg_ty(bcx, llargs[1], tp_ty);
498+
let val = from_arg_ty(bcx, llargs[1], tp_ty);
499499
let store = VolatileStore(bcx, val, ptr);
500500
unsafe {
501501
llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty));
@@ -777,8 +777,8 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
777777

778778
let tp_ty = *substs.types.get(FnSpace, 0);
779779
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
780-
let cmp = to_arg_ty(bcx, llargs[1], tp_ty);
781-
let src = to_arg_ty(bcx, llargs[2], tp_ty);
780+
let cmp = from_arg_ty(bcx, llargs[1], tp_ty);
781+
let src = from_arg_ty(bcx, llargs[2], tp_ty);
782782
let res = AtomicCmpXchg(bcx, ptr, cmp, src, order,
783783
strongest_failure_ordering);
784784
ExtractValue(bcx, res, 0)
@@ -787,12 +787,12 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
787787
"load" => {
788788
let tp_ty = *substs.types.get(FnSpace, 0);
789789
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
790-
from_arg_ty(bcx, AtomicLoad(bcx, ptr, order), tp_ty)
790+
to_arg_ty(bcx, AtomicLoad(bcx, ptr, order), tp_ty)
791791
}
792792
"store" => {
793793
let tp_ty = *substs.types.get(FnSpace, 0);
794794
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
795-
let val = to_arg_ty(bcx, llargs[1], tp_ty);
795+
let val = from_arg_ty(bcx, llargs[1], tp_ty);
796796
AtomicStore(bcx, val, ptr, order);
797797
C_nil(ccx)
798798
}
@@ -826,7 +826,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
826826

827827
let tp_ty = *substs.types.get(FnSpace, 0);
828828
let ptr = to_arg_ty_ptr(bcx, llargs[0], tp_ty);
829-
let val = to_arg_ty(bcx, llargs[1], tp_ty);
829+
let val = from_arg_ty(bcx, llargs[1], tp_ty);
830830
AtomicRMW(bcx, atom_op, ptr, val, order)
831831
}
832832
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct _X([u8]);
12+
13+
impl std::ops::Deref for _X {
14+
type Target = [u8];
15+
16+
fn deref(&self) -> &[u8] {
17+
&self.0
18+
}
19+
}
20+
21+
pub fn _g(x: &_X) -> &[u8] {
22+
x
23+
}
24+
25+
fn main() {
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::mem::transmute;
12+
13+
fn main() {
14+
unsafe {
15+
let _: i8 = transmute(false);
16+
let _: i8 = transmute(true);
17+
let _: bool = transmute(0u8);
18+
let _: bool = transmute(1u8);
19+
}
20+
}

0 commit comments

Comments
 (0)