Skip to content

Commit 600e385

Browse files
committed
review comments
1 parent d493dcc commit 600e385

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

src/librustc/traits/error_reporting/suggestions.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
600600

601601
// Visit to make sure there's a single `return` type to suggest `impl Trait`,
602602
// otherwise suggest using `Box<dyn Trait>` or an enum.
603-
let mut visitor = ReturnsVisitor::new();
603+
let mut visitor = ReturnsVisitor::default();
604604
visitor.visit_body(&body);
605605

606606
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
@@ -742,7 +742,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
742742
{
743743
let body = hir.body(*body_id);
744744
// Point at all the `return`s in the function as they have failed trait bounds.
745-
let mut visitor = ReturnsVisitor::new();
745+
let mut visitor = ReturnsVisitor::default();
746746
visitor.visit_body(&body);
747747
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
748748
for expr in &visitor.returns {
@@ -1696,17 +1696,12 @@ pub fn suggest_constraining_type_param(
16961696

16971697
/// Collect all the returned expressions within the input expression.
16981698
/// Used to point at the return spans when we want to suggest some change to them.
1699+
#[derive(Default)]
16991700
struct ReturnsVisitor<'v> {
17001701
returns: Vec<&'v hir::Expr<'v>>,
17011702
in_block_tail: bool,
17021703
}
17031704

1704-
impl ReturnsVisitor<'_> {
1705-
fn new() -> Self {
1706-
ReturnsVisitor { returns: vec![], in_block_tail: false }
1707-
}
1708-
}
1709-
17101705
impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17111706
type Map = rustc::hir::map::Map<'v>;
17121707

@@ -1715,6 +1710,10 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17151710
}
17161711

17171712
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
1713+
// Visit every expression to detect `return` paths, either through the function's tail
1714+
// expression or `return` statements. We walk all nodes to find `return` statements, but
1715+
// we only care about tail expressions when `in_block_tail` is `true`, which means that
1716+
// they're in the return path of the function body.
17181717
match ex.kind {
17191718
hir::ExprKind::Ret(Some(ex)) => {
17201719
self.returns.push(ex);
@@ -1741,7 +1740,7 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17411740
}
17421741

17431742
fn visit_body(&mut self, body: &'v hir::Body<'v>) {
1744-
let prev = self.in_block_tail;
1743+
assert!(!self.in_block_tail);
17451744
if body.generator_kind().is_none() {
17461745
if let hir::ExprKind::Block(block, None) = body.value.kind {
17471746
if block.expr.is_some() {
@@ -1750,6 +1749,5 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
17501749
}
17511750
}
17521751
hir::intravisit::walk_body(self, body);
1753-
self.in_block_tail = prev;
17541752
}
17551753
}

src/librustc_errors/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ pub const MAX_HIGHLIGHT_LINES: usize = 6;
461461
/// Maximum number of lines we will print for a multiline suggestion; arbitrary.
462462
///
463463
/// This should be replaced with a more involved mechanism to output multiline suggestions that
464-
/// more closely mimmics the regular diagnostic output, where irrelevant code lines are ellided.
464+
/// more closely mimmics the regular diagnostic output, where irrelevant code lines are elided.
465465
pub const MAX_SUGGESTION_HIGHLIGHT_LINES: usize = 20;
466466
/// Maximum number of suggestions to be shown
467467
///

src/test/ui/error-codes/E0746.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ fn foo() -> impl Trait { Struct }
1010

1111
fn bar() -> impl Trait { //~ ERROR E0746
1212
if true {
13-
return 0u32;
13+
return 0;
1414
}
15-
42u32
15+
42
1616
}
1717

1818
fn main() {}

src/test/ui/error-codes/E0746.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ fn foo() -> dyn Trait { Struct }
1010

1111
fn bar() -> dyn Trait { //~ ERROR E0746
1212
if true {
13-
return 0u32;
13+
return 0;
1414
}
15-
42u32
15+
42
1616
}
1717

1818
fn main() {}

src/test/ui/error-codes/E0746.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | fn bar() -> dyn Trait {
1717
| ^^^^^^^^^ doesn't have a size known at compile-time
1818
|
1919
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
20-
help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
20+
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
2121
|
2222
LL | fn bar() -> impl Trait {
2323
| ^^^^^^^^^^

0 commit comments

Comments
 (0)