Skip to content

Rollup of 7 pull requests #42178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f8b66a0
trace_macro: Show both the macro call and its expansion. #42072.
jorendorff May 19, 2017
0b85b64
libstd/sync/mpsc: relicense under rust license
dvyukov May 22, 2017
6dde3f4
Move some tests from compile-fail to ui
oli-obk May 16, 2017
ec1fa6d
Change macro typo helps to suggestions
oli-obk May 16, 2017
27359bd
Change enum variant help into a suggestion
oli-obk May 16, 2017
e328ef0
Change some "did you mean `self.*`" messages to suggestions
oli-obk May 16, 2017
8add02a
Changes in ui tests
oli-obk May 16, 2017
e618a4f
Add some guidelines for suggestion messages and follow them
oli-obk May 18, 2017
a9b54ab
Update ui tests output
oli-obk May 18, 2017
9d41d0e
Move a compile-fail test to ui
oli-obk May 18, 2017
bff4795
Tidy errors
oli-obk May 18, 2017
6f681c2
Fix line numbers in ui test
oli-obk May 22, 2017
1343bc6
Readd //~ERROR messages
oli-obk May 22, 2017
14b767d
Add example of recursive drop to Drop trait.
Havvy May 22, 2017
ca909c8
Add example of variable declaration drop order to Drop trait.
Havvy May 22, 2017
d7927ff
Add description of how values are dropped to Drop trait.
Havvy May 22, 2017
5f4b0ff
Fix trailing whitespace.
Havvy May 22, 2017
72eb010
update-all-references.sh doesn't deterministically work on ui-fulldeps
oli-obk May 23, 2017
b41b294
Suggested changes by birkenfeld
Havvy May 23, 2017
57f260d
Override size_hint and propagate ExactSizeIterator for iter::StepBy
scottmcm May 23, 2017
4be488c
Add iterator_step_by to the unstable book's summary
scottmcm May 21, 2017
fcb3a71
Update description of iter::StepBy
scottmcm May 21, 2017
794e572
Give step_trait a distinct tracking issue from step_by
scottmcm May 23, 2017
e860655
Remove some needless // gate-test- comments
est31 May 23, 2017
c20a157
Rollup merge of #42033 - oli-obk:suggestions, r=petrochenkov
Mark-Simulacrum May 23, 2017
84db8c1
Rollup merge of #42103 - jorendorff:master, r=estebank
Mark-Simulacrum May 23, 2017
7e4dda1
Rollup merge of #42149 - dvyukov:license, r=brson
Mark-Simulacrum May 23, 2017
20a5a5d
Rollup merge of #42159 - Havvy:doc-drop, r=steveklabnik
Mark-Simulacrum May 23, 2017
99dca25
Rollup merge of #42167 - scottmcm:iter-stepby-sizehint, r=alexcrichton
Mark-Simulacrum May 23, 2017
30a272e
Rollup merge of #42169 - scottmcm:new-step-trait-issue, r=alexcrichton
Mark-Simulacrum May 23, 2017
d8afdf1
Rollup merge of #42177 - est31:master, r=Mark-Simulacrum
Mark-Simulacrum May 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
- [io](library-features/io.md)
- [ip](library-features/ip.md)
- [iter_rfind](library-features/iter-rfind.md)
- [iterator_step_by](library-features/iterator-step-by.md)
- [libstd_io_internals](library-features/libstd-io-internals.md)
- [libstd_sys_internals](library-features/libstd-sys-internals.md)
- [libstd_thread_internals](library-features/libstd-thread-internals.md)
Expand Down
4 changes: 2 additions & 2 deletions src/doc/unstable-book/src/library-features/step-trait.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `step_trait`

The tracking issue for this feature is: [#27741]
The tracking issue for this feature is: [#42168]

[#27741]: https://github.com/rust-lang/rust/issues/27741
[#42168]: https://github.com/rust-lang/rust/issues/42168

------------------------
23 changes: 21 additions & 2 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub use self::iterator::Iterator;

#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
issue = "42168")]
pub use self::range::Step;
#[unstable(feature = "step_by", reason = "recent addition",
issue = "27741")]
Expand Down Expand Up @@ -520,7 +520,7 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator {
#[unstable(feature = "fused", issue = "35602")]
impl<I> FusedIterator for Cycle<I> where I: Clone + Iterator {}

/// An iterator that steps by n elements every iteration.
/// An adapter for stepping iterators by a custom amount.
///
/// This `struct` is created by the [`step_by`] method on [`Iterator`]. See
/// its documentation for more.
Expand Down Expand Up @@ -553,8 +553,27 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
self.iter.nth(self.step)
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let inner_hint = self.iter.size_hint();

if self.first_take {
let f = |n| if n == 0 { 0 } else { 1 + (n-1)/(self.step+1) };
(f(inner_hint.0), inner_hint.1.map(f))
} else {
let f = |n| n / (self.step+1);
(f(inner_hint.0), inner_hint.1.map(f))
}
}
}

// StepBy can only make the iterator shorter, so the len will still fit.
#[unstable(feature = "iterator_step_by",
reason = "unstable replacement of Range::step_by",
issue = "27741")]
impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}

/// An iterator that strings two iterators together.
///
/// This `struct` is created by the [`chain`] method on [`Iterator`]. See its
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::{FusedIterator, TrustedLen};
/// two `Step` objects.
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
issue = "42168")]
pub trait Step: PartialOrd + Sized {
/// Steps `self` if possible.
fn step(&self, by: &Self) -> Option<Self>;
Expand Down Expand Up @@ -55,7 +55,7 @@ macro_rules! step_impl_unsigned {
($($t:ty)*) => ($(
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
issue = "42168")]
impl Step for $t {
#[inline]
fn step(&self, by: &$t) -> Option<$t> {
Expand Down Expand Up @@ -115,7 +115,7 @@ macro_rules! step_impl_signed {
($($t:ty)*) => ($(
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
issue = "42168")]
impl Step for $t {
#[inline]
fn step(&self, by: &$t) -> Option<$t> {
Expand Down Expand Up @@ -187,7 +187,7 @@ macro_rules! step_impl_no_between {
($($t:ty)*) => ($(
#[unstable(feature = "step_trait",
reason = "likely to be replaced by finer-grained traits",
issue = "27741")]
issue = "42168")]
impl Step for $t {
#[inline]
fn step(&self, by: &$t) -> Option<$t> {
Expand Down
44 changes: 44 additions & 0 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ use marker::Unsize;
/// The `Drop` trait is used to run some code when a value goes out of scope.
/// This is sometimes called a 'destructor'.
///
/// When a value goes out of scope, if it implements this trait, it will have
/// its `drop` method called. Then any fields the value contains will also
/// be dropped recursively.
///
/// Because of the recursive dropping, you do not need to implement this trait
/// unless your type needs its own destructor logic.
///
/// # Examples
///
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
Expand All @@ -171,6 +178,43 @@ use marker::Unsize;
/// let _x = HasDrop;
/// }
/// ```
///
/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
///
/// ```
/// struct Inner;
/// struct Outer(Inner);
///
/// impl Drop for Inner {
/// fn drop(&mut self) {
/// println!("Dropping Inner!");
/// }
/// }
///
/// impl Drop for Outer {
/// fn drop(&mut self) {
/// println!("Dropping Outer!");
/// }
/// }
///
/// fn main() {
/// let _x = Outer(Inner);
/// }
/// ```
///
/// Because variables are dropped in the reverse order they are declared,
/// `main` will print `Declared second!` and then `Declared first!`.
///
/// ```
/// struct PrintOnDrop(&'static str);
///
/// fn main() {
/// let _first = PrintOnDrop("Declared first!");
/// let _second = PrintOnDrop("Declared second!");
/// }
/// ```
#[lang = "drop"]
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Drop {
Expand Down
73 changes: 73 additions & 0 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,79 @@ fn test_iterator_step_by_zero() {
it.next();
}

#[test]
fn test_iterator_step_by_size_hint() {
struct StubSizeHint(usize, Option<usize>);
impl Iterator for StubSizeHint {
type Item = ();
fn next(&mut self) -> Option<()> {
self.0 -= 1;
if let Some(ref mut upper) = self.1 {
*upper -= 1;
}
Some(())
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.0, self.1)
}
}

// The two checks in each case are needed because the logic
// is different before the first call to `next()`.

let mut it = StubSizeHint(10, Some(10)).step_by(1);
assert_eq!(it.size_hint(), (10, Some(10)));
it.next();
assert_eq!(it.size_hint(), (9, Some(9)));

// exact multiple
let mut it = StubSizeHint(10, Some(10)).step_by(3);
assert_eq!(it.size_hint(), (4, Some(4)));
it.next();
assert_eq!(it.size_hint(), (3, Some(3)));

// larger base range, but not enough to get another element
let mut it = StubSizeHint(12, Some(12)).step_by(3);
assert_eq!(it.size_hint(), (4, Some(4)));
it.next();
assert_eq!(it.size_hint(), (3, Some(3)));

// smaller base range, so fewer resulting elements
let mut it = StubSizeHint(9, Some(9)).step_by(3);
assert_eq!(it.size_hint(), (3, Some(3)));
it.next();
assert_eq!(it.size_hint(), (2, Some(2)));

// infinite upper bound
let mut it = StubSizeHint(usize::MAX, None).step_by(1);
assert_eq!(it.size_hint(), (usize::MAX, None));
it.next();
assert_eq!(it.size_hint(), (usize::MAX-1, None));

// still infinite with larger step
let mut it = StubSizeHint(7, None).step_by(3);
assert_eq!(it.size_hint(), (3, None));
it.next();
assert_eq!(it.size_hint(), (2, None));

// propagates ExactSizeIterator
let a = [1,2,3,4,5];
let it = a.iter().step_by(2);
assert_eq!(it.len(), 3);

// Cannot be TrustedLen as a step greater than one makes an iterator
// with (usize::MAX, None) no longer meet the safety requirements
trait TrustedLenCheck { fn test(self) -> bool; }
impl<T:Iterator> TrustedLenCheck for T {
default fn test(self) -> bool { false }
}
impl<T:TrustedLen> TrustedLenCheck for T {
fn test(self) -> bool { true }
}
assert!(TrustedLenCheck::test(a.iter()));
assert!(!TrustedLenCheck::test(a.iter().step_by(1)));
}

#[test]
fn test_filter_map() {
let it = (0..).step_by(1).take(10)
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#![feature(slice_patterns)]
#![feature(sort_internals)]
#![feature(sort_unstable)]
#![feature(specialization)]
#![feature(step_by)]
#![feature(step_trait)]
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(unicode)]
#![feature(unique)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
.span_suggestion(err.span,
&format!("to force the closure to take ownership of {} \
(and any other referenced variables), \
use the `move` keyword, as shown:",
use the `move` keyword",
cmt_path_or_string),
suggestion)
.emit();
Expand Down
12 changes: 12 additions & 0 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ impl Diagnostic {

/// Prints out a message with a suggested edit of the code.
///
/// In case of short messages and a simple suggestion,
/// rustc displays it as a label like
///
/// "try adding parentheses: `(tup.0).1`"
///
/// The message
/// * should not end in any punctuation (a `:` is added automatically)
/// * should not be a question
/// * should not contain any parts like "the following", "as shown"
/// * may look like "to do xyz, use" or "to do xyz, use abc"
/// * may contain a name of a function, variable or type, but not whole expressions
///
/// See `diagnostic::CodeSuggestion` for more information.
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
self.suggestions.push(CodeSuggestion {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Emitter for EmitterWriter {
// don't display multiline suggestions as labels
sugg.substitution_parts[0].substitutions[0].find('\n').is_none() {
let substitution = &sugg.substitution_parts[0].substitutions[0];
let msg = format!("help: {} `{}`", sugg.msg, substitution);
let msg = format!("help: {}: `{}`", sugg.msg, substitution);
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
} else {
// if there are multiple suggestions, print them all in full
Expand Down
20 changes: 12 additions & 8 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,13 +2307,15 @@ impl<'a> Resolver<'a> {
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
enum_candidates.sort();
for (sp, variant_path, enum_path) in enum_candidates {
let msg = format!("there is an enum variant `{}`, did you mean to use `{}`?",
variant_path,
enum_path);
if sp == DUMMY_SP {
let msg = format!("there is an enum variant `{}`, \
try using `{}`?",
variant_path,
enum_path);
err.help(&msg);
} else {
err.span_help(sp, &msg);
err.span_suggestion(span, "you can try using the variant's enum",
enum_path);
}
}
}
Expand All @@ -2322,18 +2324,20 @@ impl<'a> Resolver<'a> {
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
match candidate {
AssocSuggestion::Field => {
err.span_label(span, format!("did you mean `self.{}`?", path_str));
err.span_suggestion(span, "try",
format!("self.{}", path_str));
if !self_is_available {
err.span_label(span, format!("`self` value is only available in \
methods with `self` parameter"));
}
}
AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_label(span, format!("did you mean `self.{}(...)`?",
path_str));
err.span_suggestion(span, "try",
format!("self.{}", path_str));
}
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
err.span_label(span, format!("did you mean `Self::{}`?", path_str));
err.span_suggestion(span, "try",
format!("Self::{}", path_str));
}
}
return err;
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,10 @@ impl<'a> Resolver<'a> {
if let Some(suggestion) = suggestion {
if suggestion != name {
if let MacroKind::Bang = kind {
err.help(&format!("did you mean `{}!`?", suggestion));
err.span_suggestion(span, "you could try the macro",
format!("{}!", suggestion));
} else {
err.help(&format!("did you mean `{}`?", suggestion));
err.span_suggestion(span, "try", suggestion.to_string());
}
} else {
err.help("have you added the `#[macro_use]` on the module/import?");
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(self.cast_span,
"try casting to a reference instead:",
"try casting to a reference instead",
format!("&{}{}", mtstr, s));
}
Err(_) => {
Expand All @@ -295,7 +295,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(self.cast_span,
"try casting to a `Box` instead:",
"try casting to a `Box` instead",
format!("Box<{}>", s));
}
Err(_) => span_help!(err, self.cast_span, "did you mean `Box<{}>`?", tstr),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
from a string reference. String concatenation \
appends the string on the right to the string \
on the left and may require reallocation. This \
requires ownership of the string on the left."), suggestion);
requires ownership of the string on the left"), suggestion);
is_string_addition = true;
}

Expand Down
Loading