Skip to content

Commit 939c1cb

Browse files
committed
review comments
1 parent fb2511c commit 939c1cb

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

src/librustc/hir/map/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ impl<'hir> Map<'hir> {
649649
}
650650
}
651651

652-
pub fn is_const_scope(&self, hir_id: HirId) -> bool {
652+
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
653+
/// Used exclusively for diagnostics, to avoid suggestion function calls.
654+
pub fn is_const_context(&self, hir_id: HirId) -> bool {
653655
let parent_id = self.get_parent_item(hir_id);
654656
match self.get(parent_id) {
655657
Node::Item(&Item {

src/librustc/infer/error_reporting/need_type_info.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
153153
let (ty_msg, suffix) = match &local_visitor.found_ty {
154154
Some(ty) if &ty.to_string() != "_" &&
155155
name == "_" &&
156-
// FIXME: Remove this check after `impl_trait_in_bindings` is stabilized.
156+
// FIXME: Remove this check after `impl_trait_in_bindings` is stabilized. #63527
157157
(!ty.is_impl_trait() || self.tcx.features().impl_trait_in_bindings) &&
158158
!ty.is_closure() => // The suggestion doesn't make sense for closures.
159159
{
@@ -163,7 +163,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
163163
}
164164
Some(ty) if &ty.to_string() != "_" &&
165165
ty.to_string() != name &&
166-
// FIXME: Remove this check after `impl_trait_in_bindings` is stabilized.
166+
// FIXME: Remove this check after `impl_trait_in_bindings` is stabilized. #63527
167167
(!ty.is_impl_trait() || self.tcx.features().impl_trait_in_bindings) &&
168168
!ty.is_closure() => // The suggestion doesn't make sense for closures.
169169
{
@@ -185,12 +185,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
185185
.map(|args| args.tuple_fields()
186186
.map(|arg| arg.to_string())
187187
.collect::<Vec<_>>().join(", "))
188-
.unwrap_or_else(String::new);
188+
.unwrap_or_default();
189189
// This suggestion is incomplete, as the user will get further type inference
190190
// errors due to the `_` placeholders and the introduction of `Box`, but it does
191191
// nudge them in the right direction.
192192
(msg, format!(
193-
"a boxed closure type like `Box<Fn({}) -> {}>`",
193+
"a boxed closure type like `Box<dyn Fn({}) -> {}>`",
194194
args,
195195
fn_sig.output().skip_binder().to_string(),
196196
))

src/librustc_typeck/check/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
549549
checked_ty: Ty<'tcx>,
550550
expected_ty: Ty<'tcx>,
551551
) -> bool {
552-
if self.tcx.hir().is_const_scope(expr.hir_id) {
552+
if self.tcx.hir().is_const_context(expr.hir_id) {
553553
// Shouldn't suggest `.into()` on `const`s.
554554
// FIXME(estebank): modify once we decide to suggest `as` casts
555555
return false;

src/librustc_typeck/check/mod.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -3990,27 +3990,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39903990
expected: Ty<'tcx>,
39913991
found: Ty<'tcx>,
39923992
) {
3993-
if self.tcx.hir().is_const_scope(expr.hir_id) {
3993+
if self.tcx.hir().is_const_context(expr.hir_id) {
39943994
// Do not suggest `Box::new` in const context.
39953995
return;
39963996
}
3997-
if expected.is_box() && !found.is_box() {
3998-
let boxed_found = self.tcx.mk_box(found);
3999-
if let (true, Ok(snippet)) = (
4000-
self.can_coerce(boxed_found, expected),
4001-
self.sess().source_map().span_to_snippet(expr.span),
4002-
) {
4003-
err.span_suggestion(
4004-
expr.span,
4005-
"you can store this in the heap calling `Box::new`",
4006-
format!("Box::new({})", snippet),
4007-
Applicability::MachineApplicable,
4008-
);
4009-
err.note("for more information about the distinction between the stack and the \
4010-
heap, read https://doc.rust-lang.org/book/ch15-01-box.html, \
4011-
https://doc.rust-lang.org/rust-by-example/std/box.html and \
4012-
https://doc.rust-lang.org/std/boxed/index.html");
4013-
}
3997+
if !expected.is_box() || found.is_box() {
3998+
return;
3999+
}
4000+
let boxed_found = self.tcx.mk_box(found);
4001+
if let (true, Ok(snippet)) = (
4002+
self.can_coerce(boxed_found, expected),
4003+
self.sess().source_map().span_to_snippet(expr.span),
4004+
) {
4005+
err.span_suggestion(
4006+
expr.span,
4007+
"store this in the heap by calling `Box::new`",
4008+
format!("Box::new({})", snippet),
4009+
Applicability::MachineApplicable,
4010+
);
4011+
err.note("for more on the distinction between the stack and the \
4012+
heap, read https://doc.rust-lang.org/book/ch15-01-box.html, \
4013+
https://doc.rust-lang.org/rust-by-example/std/box.html, and \
4014+
https://doc.rust-lang.org/std/boxed/index.html");
40144015
}
40154016
}
40164017

src/test/ui/inference/cannot-infer-closure.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed for the closure
22
--> $DIR/cannot-infer-closure.rs:3:9
33
|
44
LL | let x = |a: (), b: ()| {
5-
| - consider giving `x` a boxed closure type like `Box<Fn((), ()) -> std::result::Result<(), _>>`
5+
| - consider giving `x` a boxed closure type like `Box<dyn Fn((), ()) -> std::result::Result<(), _>>`
66
LL | Err(a)?;
77
| ^^^^^^^ cannot infer type
88

src/test/ui/suggestions/suggest-box.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LL | | };
1010
|
1111
= note: expected type `std::boxed::Box<dyn std::ops::Fn() -> std::result::Result<(), ()>>`
1212
found type `[closure@$DIR/suggest-box.rs:4:47: 7:6]`
13-
= note: for more information about the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html and https://doc.rust-lang.org/std/boxed/index.html
14-
help: you can store this in the heap calling `Box::new`
13+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
14+
help: store this in the heap by calling `Box::new`
1515
|
1616
LL | let _x: Box<dyn Fn() -> Result<(), ()>> = Box::new(|| {
1717
LL | Err(())?;

0 commit comments

Comments
 (0)