Skip to content

Commit e0cc22b

Browse files
committed
Auto merge of #42336 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 7 pull requests - Successful merges: #42126, #42196, #42252, #42277, #42315, #42329, #42330 - Failed merges:
2 parents fd7b44b + 7f286a8 commit e0cc22b

File tree

18 files changed

+171
-73
lines changed

18 files changed

+171
-73
lines changed

src/libcore/convert.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,40 @@ pub trait AsMut<T: ?Sized> {
169169
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
170170
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
171171
///
172+
/// # Implementing `Into`
173+
///
174+
/// There is one exception to implementing `Into`, and it's kind of esoteric.
175+
/// If the destination type is not part of the current crate, and it uses a
176+
/// generic variable, then you can't implement `From` directly. For example,
177+
/// take this crate:
178+
///
179+
/// ```compile_fail
180+
/// struct Wrapper<T>(Vec<T>);
181+
/// impl<T> From<Wrapper<T>> for Vec<T> {
182+
/// fn from(w: Wrapper<T>) -> Vec<T> {
183+
/// w.0
184+
/// }
185+
/// }
186+
/// ```
187+
///
188+
/// To fix this, you can implement `Into` directly:
189+
///
190+
/// ```
191+
/// struct Wrapper<T>(Vec<T>);
192+
/// impl<T> Into<Vec<T>> for Wrapper<T> {
193+
/// fn into(self) -> Vec<T> {
194+
/// self.0
195+
/// }
196+
/// }
197+
/// ```
198+
///
199+
/// This won't always allow the conversion: for example, `try!` and `?`
200+
/// always use `From`. However, in most cases, people use `Into` to do the
201+
/// conversions, and this will allow that.
202+
///
203+
/// In almost all cases, you should try to implement `From`, then fall back
204+
/// to `Into` if `From` can't be implemented.
205+
///
172206
/// # Examples
173207
///
174208
/// [`String`] implements `Into<Vec<u8>>`:
@@ -285,9 +319,11 @@ pub trait From<T>: Sized {
285319
/// Library authors should not directly implement this trait, but should prefer
286320
/// implementing the [`TryFrom`] trait, which offers greater flexibility and
287321
/// provides an equivalent `TryInto` implementation for free, thanks to a
288-
/// blanket implementation in the standard library.
322+
/// blanket implementation in the standard library. For more information on this,
323+
/// see the documentation for [`Into`].
289324
///
290325
/// [`TryFrom`]: trait.TryFrom.html
326+
/// [`Into`]: trait.Into.html
291327
#[unstable(feature = "try_from", issue = "33417")]
292328
pub trait TryInto<T>: Sized {
293329
/// The type returned in the event of a conversion error.

src/libcore/iter/iterator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ pub trait Iterator {
130130
///
131131
/// ```
132132
/// // an infinite iterator has no upper bound
133+
/// // and the maximum possible lower bound
133134
/// let iter = 0..;
134135
///
135-
/// assert_eq!((0, None), iter.size_hint());
136+
/// assert_eq!((usize::max_value(), None), iter.size_hint());
136137
/// ```
137138
#[inline]
138139
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/iter/range.rs

+5
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ impl<A: Step> Iterator for ops::RangeFrom<A> where
543543
mem::swap(&mut n, &mut self.start);
544544
Some(n)
545545
}
546+
547+
#[inline]
548+
fn size_hint(&self) -> (usize, Option<usize>) {
549+
(usize::MAX, None)
550+
}
546551
}
547552

548553
#[unstable(feature = "fused", issue = "35602")]

src/libcore/iter/traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub trait FromIterator<A>: Sized {
109109
///
110110
/// See the [module-level documentation] for more.
111111
///
112-
/// [module-level documentation]: trait.FromIterator.html
112+
/// [module-level documentation]: index.html
113113
///
114114
/// # Examples
115115
///
@@ -219,7 +219,7 @@ pub trait IntoIterator {
219219
///
220220
/// See the [module-level documentation] for more.
221221
///
222-
/// [module-level documentation]: trait.IntoIterator.html
222+
/// [module-level documentation]: index.html
223223
///
224224
/// # Examples
225225
///

src/libcore/mem.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
220220

221221
/// Returns the [ABI]-required minimum alignment of a type.
222222
///
223-
/// Every valid address of a value of the type `T` must be a multiple of this number.
223+
/// Every reference to a value of the type `T` must be a multiple of this number.
224224
///
225225
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
226226
///
@@ -243,7 +243,7 @@ pub fn min_align_of<T>() -> usize {
243243

244244
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
245245
///
246-
/// Every valid address of a value of the type `T` must be a multiple of this number.
246+
/// Every reference to a value of the type `T` must be a multiple of this number.
247247
///
248248
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
249249
///
@@ -264,7 +264,7 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
264264

265265
/// Returns the [ABI]-required minimum alignment of a type.
266266
///
267-
/// Every valid address of a value of the type `T` must be a multiple of this number.
267+
/// Every reference to a value of the type `T` must be a multiple of this number.
268268
///
269269
/// This is the alignment used for struct fields. It may be smaller than the preferred alignment.
270270
///
@@ -285,7 +285,7 @@ pub fn align_of<T>() -> usize {
285285

286286
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
287287
///
288-
/// Every valid address of a value of the type `T` must be a multiple of this number.
288+
/// Every reference to a value of the type `T` must be a multiple of this number.
289289
///
290290
/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
291291
///

src/libcore/tests/iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ fn test_iterator_size_hint() {
764764
let v2 = &[10, 11, 12];
765765
let vi = v.iter();
766766

767+
assert_eq!((0..).size_hint(), (usize::MAX, None));
767768
assert_eq!(c.size_hint(), (usize::MAX, None));
768769
assert_eq!(vi.clone().size_hint(), (10, Some(10)));
769770

src/librustc/infer/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16821682
{
16831683
if let InferTables::InProgress(tables) = self.tables {
16841684
if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
1685-
return tables.borrow().closure_kinds.get(&id).cloned();
1685+
return tables.borrow()
1686+
.closure_kinds
1687+
.get(&id)
1688+
.cloned()
1689+
.map(|(kind, _)| kind);
16861690
}
16871691
}
16881692

src/librustc/session/config.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
13541354
};
13551355

13561356
let unparsed_crate_types = matches.opt_strs("crate-type");
1357-
let (crate_types, emit_metadata) = parse_crate_types_from_list(unparsed_crate_types)
1357+
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
13581358
.unwrap_or_else(|e| early_error(error_format, &e[..]));
13591359

13601360
let mut lint_opts = vec![];
@@ -1402,9 +1402,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
14021402
}
14031403
}
14041404
};
1405-
if emit_metadata {
1406-
output_types.insert(OutputType::Metadata, None);
1407-
} else if output_types.is_empty() {
1405+
if output_types.is_empty() {
14081406
output_types.insert(OutputType::Exe, None);
14091407
}
14101408

@@ -1629,9 +1627,8 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
16291627
}
16301628

16311629
pub fn parse_crate_types_from_list(list_list: Vec<String>)
1632-
-> Result<(Vec<CrateType>, bool), String> {
1630+
-> Result<Vec<CrateType>, String> {
16331631
let mut crate_types: Vec<CrateType> = Vec::new();
1634-
let mut emit_metadata = false;
16351632
for unparsed_crate_type in &list_list {
16361633
for part in unparsed_crate_type.split(',') {
16371634
let new_part = match part {
@@ -1642,13 +1639,6 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>)
16421639
"cdylib" => CrateTypeCdylib,
16431640
"bin" => CrateTypeExecutable,
16441641
"proc-macro" => CrateTypeProcMacro,
1645-
// FIXME(#38640) remove this when Cargo is fixed.
1646-
"metadata" => {
1647-
early_warn(ErrorOutputType::default(), "--crate-type=metadata is deprecated, \
1648-
prefer --emit=metadata");
1649-
emit_metadata = true;
1650-
CrateTypeRlib
1651-
}
16521642
_ => {
16531643
return Err(format!("unknown crate type: `{}`",
16541644
part));
@@ -1660,7 +1650,7 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>)
16601650
}
16611651
}
16621652

1663-
return Ok((crate_types, emit_metadata));
1653+
Ok(crate_types)
16641654
}
16651655

16661656
pub mod nightly_options {

src/librustc/ty/context.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use syntax::abi;
5858
use syntax::ast::{self, Name, NodeId};
5959
use syntax::attr;
6060
use syntax::symbol::{Symbol, keywords};
61+
use syntax_pos::Span;
6162

6263
use hir;
6364

@@ -229,8 +230,9 @@ pub struct TypeckTables<'tcx> {
229230
/// Records the type of each closure.
230231
pub closure_tys: NodeMap<ty::PolyFnSig<'tcx>>,
231232

232-
/// Records the kind of each closure.
233-
pub closure_kinds: NodeMap<ty::ClosureKind>,
233+
/// Records the kind of each closure and the span and name of the variable
234+
/// that caused the closure to be this kind.
235+
pub closure_kinds: NodeMap<(ty::ClosureKind, Option<(Span, ast::Name)>)>,
234236

235237
/// For each fn, records the "liberated" types of its arguments
236238
/// and return type. Liberated means that all bound regions

src/librustc_borrowck/borrowck/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ use rustc::middle::free_region::RegionRelations;
3939
use rustc::ty::{self, TyCtxt};
4040
use rustc::ty::maps::Providers;
4141

42-
use syntax_pos::DUMMY_SP;
43-
4442
use std::fmt;
4543
use std::rc::Rc;
4644
use std::hash::{Hash, Hasher};
@@ -587,9 +585,15 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
587585
verb, msg, nl);
588586
let need_note = match lp.ty.sty {
589587
ty::TypeVariants::TyClosure(id, _) => {
590-
if let Ok(ty::ClosureKind::FnOnce) =
591-
ty::queries::closure_kind::try_get(self.tcx, DUMMY_SP, id) {
592-
err.help("closure was moved because it only implements `FnOnce`");
588+
let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
589+
if let Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) =
590+
self.tables.closure_kinds.get(&node_id)
591+
{
592+
err.span_note(span, &format!(
593+
"closure cannot be invoked more than once because \
594+
it moves the variable `{}` out of its environment",
595+
name
596+
));
593597
false
594598
} else {
595599
true

src/librustc_typeck/check/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
103103
self.tables.borrow_mut().closure_tys.insert(expr.id, sig);
104104
match opt_kind {
105105
Some(kind) => {
106-
self.tables.borrow_mut().closure_kinds.insert(expr.id, kind);
106+
self.tables.borrow_mut().closure_kinds.insert(expr.id, (kind, None));
107107
}
108108
None => {}
109109
}

src/librustc_typeck/check/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
814814

815815
let closure_kinds = &self.tables.borrow().closure_kinds;
816816
let closure_kind = match closure_kinds.get(&closure_id) {
817-
Some(&k) => k,
817+
Some(&(k, _)) => k,
818818
None => {
819819
return Err(MethodError::ClosureAmbiguity(trait_def_id));
820820
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ fn closure_kind<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
702702
def_id: DefId)
703703
-> ty::ClosureKind {
704704
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
705-
tcx.typeck_tables_of(def_id).closure_kinds[&node_id]
705+
tcx.typeck_tables_of(def_id).closure_kinds[&node_id].0
706706
}
707707

708708
fn adt_destructor<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

0 commit comments

Comments
 (0)