Skip to content

Commit 40008dc

Browse files
committed
Auto merge of #71539 - Dylan-DPC:rollup-a2vbfh9, r=Dylan-DPC
Rollup of 8 pull requests Successful merges: - #69456 (fix misleading type annotation diagonstics) - #71330 (Only run dataflow for const qualification if type-based check would fail) - #71480 (Improve PanicInfo examples readability) - #71485 (Add BinaryHeap::retain as suggested in #42849) - #71512 (Remove useless "" args) - #71527 (Miscellaneous cleanup in `check_consts`) - #71534 (Avoid unused Option::map results) - #71535 (Fix typos in docs for keyword "in") Failed merges: r? @ghost
2 parents 3360cc3 + 32fb77d commit 40008dc

File tree

36 files changed

+451
-187
lines changed

36 files changed

+451
-187
lines changed

src/bootstrap/toolstate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ static NIGHTLY_TOOLS: &[(&str, &str)] = &[
9393
];
9494

9595
fn print_error(tool: &str, submodule: &str) {
96-
eprintln!("");
96+
eprintln!();
9797
eprintln!("We detected that this PR updated '{}', but its tests failed.", tool);
98-
eprintln!("");
98+
eprintln!();
9999
eprintln!("If you do intend to update '{}', please check the error messages above and", tool);
100100
eprintln!("commit another update.");
101-
eprintln!("");
101+
eprintln!();
102102
eprintln!("If you do NOT intend to update '{}', please ensure you did not accidentally", tool);
103103
eprintln!("change the submodule at '{}'. You may ask your reviewer for the", submodule);
104104
eprintln!("proper steps.");

src/liballoc/collections/binary_heap.rs

+28
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,34 @@ impl<T: Ord> BinaryHeap<T> {
665665
pub fn drain_sorted(&mut self) -> DrainSorted<'_, T> {
666666
DrainSorted { inner: self }
667667
}
668+
669+
/// Retains only the elements specified by the predicate.
670+
///
671+
/// In other words, remove all elements `e` such that `f(&e)` returns
672+
/// `false`. The elements are visited in unsorted (and unspecified) order.
673+
///
674+
/// # Examples
675+
///
676+
/// Basic usage:
677+
///
678+
/// ```
679+
/// #![feature(binary_heap_retain)]
680+
/// use std::collections::BinaryHeap;
681+
///
682+
/// let mut heap = BinaryHeap::from(vec![-10, -5, 1, 2, 4, 13]);
683+
///
684+
/// heap.retain(|x| x % 2 == 0); // only keep even numbers
685+
///
686+
/// assert_eq!(heap.into_sorted_vec(), [-10, 2, 4])
687+
/// ```
688+
#[unstable(feature = "binary_heap_retain", issue = "71503")]
689+
pub fn retain<F>(&mut self, f: F)
690+
where
691+
F: FnMut(&T) -> bool,
692+
{
693+
self.data.retain(f);
694+
self.rebuild();
695+
}
668696
}
669697

670698
impl<T> BinaryHeap<T> {

src/liballoc/tests/binary_heap.rs

+8
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ fn assert_covariance() {
372372
}
373373
}
374374

375+
#[test]
376+
fn test_retain() {
377+
let mut a = BinaryHeap::from(vec![-10, -5, 1, 2, 4, 13]);
378+
a.retain(|x| x % 2 == 0);
379+
380+
assert_eq!(a.into_sorted_vec(), [-10, 2, 4])
381+
}
382+
375383
// old binaryheap failed this test
376384
//
377385
// Integrity means that all elements are present after a comparison panics,

src/liballoc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(binary_heap_drain_sorted)]
1515
#![feature(vec_remove_item)]
1616
#![feature(split_inclusive)]
17+
#![feature(binary_heap_retain)]
1718

1819
use std::collections::hash_map::DefaultHasher;
1920
use std::hash::{Hash, Hasher};

src/libcore/panic.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ impl<'a> PanicInfo<'a> {
7777
/// use std::panic;
7878
///
7979
/// panic::set_hook(Box::new(|panic_info| {
80-
/// println!("panic occurred: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
80+
/// if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
81+
/// println!("panic occurred: {:?}", s);
82+
/// } else {
83+
/// println!("panic occurred");
84+
/// }
8185
/// }));
8286
///
8387
/// panic!("Normal panic");
@@ -112,8 +116,10 @@ impl<'a> PanicInfo<'a> {
112116
///
113117
/// panic::set_hook(Box::new(|panic_info| {
114118
/// if let Some(location) = panic_info.location() {
115-
/// println!("panic occurred in file '{}' at line {}", location.file(),
116-
/// location.line());
119+
/// println!("panic occurred in file '{}' at line {}",
120+
/// location.file(),
121+
/// location.line(),
122+
/// );
117123
/// } else {
118124
/// println!("panic occurred but can't get location information...");
119125
/// }

src/librustc_attr/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
9898
}
9999
}
100100

101-
diagnostic.map(|d| {
101+
if let Some(d) = diagnostic {
102102
struct_span_err!(d, attr.span, E0633, "malformed `unwind` attribute input")
103103
.span_label(attr.span, "invalid argument")
104104
.span_suggestions(
@@ -110,7 +110,7 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
110110
Applicability::MachineApplicable,
111111
)
112112
.emit();
113-
});
113+
};
114114
}
115115
}
116116
}

src/librustc_expand/expand.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11721172
// ignore derives so they remain unused
11731173
let (attr, after_derive) = self.classify_nonitem(&mut expr);
11741174

1175-
if attr.is_some() {
1175+
if let Some(ref attr_value) = attr {
11761176
// Collect the invoc regardless of whether or not attributes are permitted here
11771177
// expansion will eat the attribute so it won't error later.
1178-
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1178+
self.cfg.maybe_emit_expr_attr_err(attr_value);
11791179

11801180
// AstFragmentKind::Expr requires the macro to emit an expression.
11811181
return self
@@ -1322,8 +1322,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13221322
// Ignore derives so they remain unused.
13231323
let (attr, after_derive) = self.classify_nonitem(&mut expr);
13241324

1325-
if attr.is_some() {
1326-
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
1325+
if let Some(ref attr_value) = attr {
1326+
self.cfg.maybe_emit_expr_attr_err(attr_value);
13271327

13281328
return self
13291329
.collect_attr(

src/librustc_hir/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl DefPath {
246246

247247
let mut opt_delimiter = None;
248248
for component in &self.data {
249-
opt_delimiter.map(|d| s.push(d));
249+
s.extend(opt_delimiter);
250250
opt_delimiter = Some('-');
251251
if component.disambiguator == 0 {
252252
write!(s, "{}", component.data.as_symbol()).unwrap();

src/librustc_infer/infer/error_reporting/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
755755
},
756756
ObligationCauseCode::IfExpression(box IfExpressionCause { then, outer, semicolon }) => {
757757
err.span_label(then, "expected because of this");
758-
outer.map(|sp| err.span_label(sp, "`if` and `else` have incompatible types"));
758+
if let Some(sp) = outer {
759+
err.span_label(sp, "`if` and `else` have incompatible types");
760+
}
759761
if let Some(sp) = semicolon {
760762
err.span_suggestion_short(
761763
sp,

src/librustc_infer/infer/error_reporting/need_type_info.rs

+52-6
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,27 @@ use std::borrow::Cow;
1717
struct FindHirNodeVisitor<'a, 'tcx> {
1818
infcx: &'a InferCtxt<'a, 'tcx>,
1919
target: GenericArg<'tcx>,
20+
target_span: Span,
2021
found_node_ty: Option<Ty<'tcx>>,
2122
found_local_pattern: Option<&'tcx Pat<'tcx>>,
2223
found_arg_pattern: Option<&'tcx Pat<'tcx>>,
2324
found_closure: Option<&'tcx Expr<'tcx>>,
2425
found_method_call: Option<&'tcx Expr<'tcx>>,
26+
found_exact_method_call: Option<&'tcx Expr<'tcx>>,
2527
}
2628

2729
impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
28-
fn new(infcx: &'a InferCtxt<'a, 'tcx>, target: GenericArg<'tcx>) -> Self {
30+
fn new(infcx: &'a InferCtxt<'a, 'tcx>, target: GenericArg<'tcx>, target_span: Span) -> Self {
2931
Self {
3032
infcx,
3133
target,
34+
target_span,
3235
found_node_ty: None,
3336
found_local_pattern: None,
3437
found_arg_pattern: None,
3538
found_closure: None,
3639
found_method_call: None,
40+
found_exact_method_call: None,
3741
}
3842
}
3943

@@ -103,6 +107,17 @@ impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
103107
}
104108

105109
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
110+
if let ExprKind::MethodCall(_, call_span, exprs) = expr.kind {
111+
if call_span == self.target_span
112+
&& Some(self.target)
113+
== self.infcx.in_progress_tables.and_then(|tables| {
114+
tables.borrow().node_type_opt(exprs.first().unwrap().hir_id).map(Into::into)
115+
})
116+
{
117+
self.found_exact_method_call = Some(&expr);
118+
return;
119+
}
120+
}
106121
if self.node_ty_contains_target(expr.hir_id).is_some() {
107122
match expr.kind {
108123
ExprKind::Closure(..) => self.found_closure = Some(&expr),
@@ -234,7 +249,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
234249
let ty = self.resolve_vars_if_possible(&ty);
235250
let (name, name_sp, descr, parent_name, parent_descr) = self.extract_type_name(&ty, None);
236251

237-
let mut local_visitor = FindHirNodeVisitor::new(&self, ty.into());
252+
let mut local_visitor = FindHirNodeVisitor::new(&self, ty.into(), span);
238253
let ty_to_string = |ty: Ty<'tcx>| -> String {
239254
let mut s = String::new();
240255
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
@@ -287,14 +302,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
287302
(!ty.is_impl_trait() || self.tcx.features().impl_trait_in_bindings)
288303
};
289304

290-
let ty_msg = match local_visitor.found_node_ty {
291-
Some(ty::TyS { kind: ty::Closure(_, substs), .. }) => {
305+
let ty_msg = match (local_visitor.found_node_ty, local_visitor.found_exact_method_call) {
306+
(_, Some(_)) => String::new(),
307+
(Some(ty::TyS { kind: ty::Closure(_, substs), .. }), _) => {
292308
let fn_sig = substs.as_closure().sig();
293309
let args = closure_args(&fn_sig);
294310
let ret = fn_sig.output().skip_binder().to_string();
295311
format!(" for the closure `fn({}) -> {}`", args, ret)
296312
}
297-
Some(ty) if is_named_and_not_impl_trait(ty) => {
313+
(Some(ty), _) if is_named_and_not_impl_trait(ty) => {
298314
let ty = ty_to_string(ty);
299315
format!(" for `{}`", ty)
300316
}
@@ -370,7 +386,37 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
370386
_ => "a type".to_string(),
371387
};
372388

373-
if let Some(pattern) = local_visitor.found_arg_pattern {
389+
if let Some(e) = local_visitor.found_exact_method_call {
390+
if let ExprKind::MethodCall(segment, ..) = &e.kind {
391+
// Suggest specifying type params or point out the return type of the call:
392+
//
393+
// error[E0282]: type annotations needed
394+
// --> $DIR/type-annotations-needed-expr.rs:2:39
395+
// |
396+
// LL | let _ = x.into_iter().sum() as f64;
397+
// | ^^^
398+
// | |
399+
// | cannot infer type for `S`
400+
// | help: consider specifying the type argument in
401+
// | the method call: `sum::<S>`
402+
// |
403+
// = note: type must be known at this point
404+
//
405+
// or
406+
//
407+
// error[E0282]: type annotations needed
408+
// --> $DIR/issue-65611.rs:59:20
409+
// |
410+
// LL | let x = buffer.last().unwrap().0.clone();
411+
// | -------^^^^--
412+
// | | |
413+
// | | cannot infer type for `T`
414+
// | this method call resolves to `std::option::Option<&T>`
415+
// |
416+
// = note: type must be known at this point
417+
self.annotate_method_call(segment, e, &mut err);
418+
}
419+
} else if let Some(pattern) = local_visitor.found_arg_pattern {
374420
// We don't want to show the default label for closures.
375421
//
376422
// So, before clearing, the output would look something like this:

src/librustc_metadata/creader.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ fn dump_crates(cstore: &CStore) {
101101
info!(" hash: {}", data.hash());
102102
info!(" reqd: {:?}", data.dep_kind());
103103
let CrateSource { dylib, rlib, rmeta } = data.source();
104-
dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display()));
105-
rlib.as_ref().map(|rl| info!(" rlib: {}", rl.0.display()));
106-
rmeta.as_ref().map(|rl| info!(" rmeta: {}", rl.0.display()));
104+
if let Some(dylib) = dylib {
105+
info!(" dylib: {}", dylib.0.display());
106+
}
107+
if let Some(rlib) = rlib {
108+
info!(" rlib: {}", rlib.0.display());
109+
}
110+
if let Some(rmeta) = rmeta {
111+
info!(" rmeta: {}", rmeta.0.display());
112+
}
107113
});
108114
}
109115

0 commit comments

Comments
 (0)