]> git.lizzy.rs Git - rust.git/commitdiff
review comments
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 24 Jan 2020 22:03:35 +0000 (14:03 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 24 Jan 2020 22:03:35 +0000 (14:03 -0800)
src/librustc/traits/error_reporting/suggestions.rs
src/librustc_errors/emitter.rs
src/test/ui/error-codes/E0746.fixed
src/test/ui/error-codes/E0746.rs
src/test/ui/error-codes/E0746.stderr

index cc2c97096e966d092b0bf3e5f067f4a9e9652cca..b2aec78c175adbd05d4cac88545ae523f230aa1d 100644 (file)
@@ -600,7 +600,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
 
         // Visit to make sure there's a single `return` type to suggest `impl Trait`,
         // otherwise suggest using `Box<dyn Trait>` or an enum.
-        let mut visitor = ReturnsVisitor::new();
+        let mut visitor = ReturnsVisitor::default();
         visitor.visit_body(&body);
 
         let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
@@ -742,7 +742,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
         {
             let body = hir.body(*body_id);
             // Point at all the `return`s in the function as they have failed trait bounds.
-            let mut visitor = ReturnsVisitor::new();
+            let mut visitor = ReturnsVisitor::default();
             visitor.visit_body(&body);
             let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
             for expr in &visitor.returns {
@@ -1696,17 +1696,12 @@ pub fn suggest_constraining_type_param(
 
 /// Collect all the returned expressions within the input expression.
 /// Used to point at the return spans when we want to suggest some change to them.
+#[derive(Default)]
 struct ReturnsVisitor<'v> {
     returns: Vec<&'v hir::Expr<'v>>,
     in_block_tail: bool,
 }
 
-impl ReturnsVisitor<'_> {
-    fn new() -> Self {
-        ReturnsVisitor { returns: vec![], in_block_tail: false }
-    }
-}
-
 impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
     type Map = rustc::hir::map::Map<'v>;
 
@@ -1715,6 +1710,10 @@ fn nested_visit_map(&mut self) -> hir::intravisit::NestedVisitorMap<'_, Self::Ma
     }
 
     fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
+        // Visit every expression to detect `return` paths, either through the function's tail
+        // expression or `return` statements. We walk all nodes to find `return` statements, but
+        // we only care about tail expressions when `in_block_tail` is `true`, which means that
+        // they're in the return path of the function body.
         match ex.kind {
             hir::ExprKind::Ret(Some(ex)) => {
                 self.returns.push(ex);
@@ -1741,7 +1740,7 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
     }
 
     fn visit_body(&mut self, body: &'v hir::Body<'v>) {
-        let prev = self.in_block_tail;
+        assert!(!self.in_block_tail);
         if body.generator_kind().is_none() {
             if let hir::ExprKind::Block(block, None) = body.value.kind {
                 if block.expr.is_some() {
@@ -1750,6 +1749,5 @@ fn visit_body(&mut self, body: &'v hir::Body<'v>) {
             }
         }
         hir::intravisit::walk_body(self, body);
-        self.in_block_tail = prev;
     }
 }
index 2149e46a1cfbc9327c6df084c06225c0e2f3cf67..b62e4223fea10e6780e019f40dab56827d7decba 100644 (file)
@@ -461,7 +461,7 @@ fn emit_diagnostic(&mut self, _: &Diagnostic) {}
 /// Maximum number of lines we will print for a multiline suggestion; arbitrary.
 ///
 /// This should be replaced with a more involved mechanism to output multiline suggestions that
-/// more closely mimmics the regular diagnostic output, where irrelevant code lines are ellided.
+/// more closely mimmics the regular diagnostic output, where irrelevant code lines are elided.
 pub const MAX_SUGGESTION_HIGHLIGHT_LINES: usize = 20;
 /// Maximum number of suggestions to be shown
 ///
index 70c7f791a2b884f3825fbd2db50819e8754e0824..ca8319aa020dceea3f35a462540de4f30b896ec1 100644 (file)
@@ -10,9 +10,9 @@ fn foo() -> impl Trait { Struct }
 
 fn bar() -> impl Trait { //~ ERROR E0746
     if true {
-        return 0u32;
+        return 0;
     }
-    42u32
+    42
 }
 
 fn main() {}
index fbf18246e1648b5a16e8ae3a248410e9ee052e96..bf5ba8fff562a81ef091917b380e301f5255cac2 100644 (file)
@@ -10,9 +10,9 @@ fn foo() -> dyn Trait { Struct }
 
 fn bar() -> dyn Trait { //~ ERROR E0746
     if true {
-        return 0u32;
+        return 0;
     }
-    42u32
+    42
 }
 
 fn main() {}
index 05c61f1149fb53e004322b39f93ac803e9de50cc..e7a8fd304cabee33798925753507c153e2a49965 100644 (file)
@@ -17,7 +17,7 @@ LL | fn bar() -> dyn Trait {
    |             ^^^^^^^^^ doesn't have a size known at compile-time
    |
    = note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
-help: return `impl Trait` instead, as all return paths are of type `u32`, which implements `Trait`
+help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
    |
 LL | fn bar() -> impl Trait {
    |             ^^^^^^^^^^