about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthew Jasper <mjjasper1@gmail.com>2018-09-15 15:45:12 +0100
committerMatthew Jasper <mjjasper1@gmail.com>2018-09-19 19:52:55 +0100
commitb210b3168a572c84710bc160915592307f7a3ac5 (patch)
tree821d6f99cccb0c4393fec7509b869599846ac998
parent4f3ff5a97bcd2d05ee0c768122752dc74f96ccc3 (diff)
downloadrust-b210b3168a572c84710bc160915592307f7a3ac5.tar.gz
rust-b210b3168a572c84710bc160915592307f7a3ac5.zip
Make the span of the MIR return place point to the return type
-rw-r--r--src/librustc_mir/build/mod.rs63
-rw-r--r--src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr32
-rw-r--r--src/test/ui/consts/min_const_fn/min_const_fn.stderr32
3 files changed, 81 insertions, 46 deletions
diff --git a/src/librustc_mir/build/mod.rs b/src/librustc_mir/build/mod.rs
index d2061d8eecf..f75ce7b0816 100644
--- a/src/librustc_mir/build/mod.rs
+++ b/src/librustc_mir/build/mod.rs
@@ -40,16 +40,44 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
     let id = tcx.hir.as_local_node_id(def_id).unwrap();
 
     // Figure out what primary body this item has.
-    let body_id = match tcx.hir.get(id) {
+    let (body_id, return_ty_span) = match tcx.hir.get(id) {
         Node::Variant(variant) =>
             return create_constructor_shim(tcx, id, &variant.node.data),
         Node::StructCtor(ctor) =>
             return create_constructor_shim(tcx, id, ctor),
 
-        _ => match tcx.hir.maybe_body_owned_by(id) {
-            Some(body) => body,
-            None => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
-        },
+        Node::Expr(hir::Expr { node: hir::ExprKind::Closure(_, decl, body_id, _, _), .. })
+        | Node::Item(hir::Item { node: hir::ItemKind::Fn(decl, _, _, body_id), .. })
+        | Node::ImplItem(
+            hir::ImplItem {
+                node: hir::ImplItemKind::Method(hir::MethodSig { decl, .. }, body_id),
+                ..
+            }
+        )
+        | Node::TraitItem(
+            hir::TraitItem {
+                node: hir::TraitItemKind::Method(
+                    hir::MethodSig { decl, .. },
+                    hir::TraitMethod::Provided(body_id),
+                ),
+                ..
+            }
+        ) => {
+            (*body_id, decl.output.span())
+        }
+        Node::Item(hir::Item { node: hir::ItemKind::Static(ty, _, body_id), .. })
+        | Node::Item(hir::Item { node: hir::ItemKind::Const(ty, body_id), .. })
+        | Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Const(ty, body_id), .. })
+        | Node::TraitItem(
+            hir::TraitItem { node: hir::TraitItemKind::Const(ty, Some(body_id)), .. }
+        ) => {
+            (*body_id, ty.span)
+        }
+        Node::AnonConst(hir::AnonConst { body, id, .. }) => {
+            (*body, tcx.hir.span(*id))
+        }
+
+        _ => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
     };
 
     tcx.infer_ctxt().enter(|infcx| {
@@ -124,9 +152,9 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
             };
 
             build::construct_fn(cx, id, arguments, safety, abi,
-                                return_ty, yield_ty, body)
+                                return_ty, yield_ty, return_ty_span, body)
         } else {
-            build::construct_const(cx, body_id)
+            build::construct_const(cx, body_id, return_ty_span)
         };
 
         // Convert the Mir to global types.
@@ -494,6 +522,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
                                    abi: Abi,
                                    return_ty: Ty<'gcx>,
                                    yield_ty: Option<Ty<'gcx>>,
+                                   return_ty_span: Span,
                                    body: &'gcx hir::Body)
                                    -> Mir<'tcx>
     where A: Iterator<Item=ArgInfo<'gcx>>
@@ -547,6 +576,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
         arguments.len(),
         safety,
         return_ty,
+        return_ty_span,
         upvar_decls);
 
     let fn_def_id = tcx.hir.local_def_id(fn_id);
@@ -601,15 +631,17 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
     mir
 }
 
-fn construct_const<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
-                                   body_id: hir::BodyId)
-                                   -> Mir<'tcx> {
+fn construct_const<'a, 'gcx, 'tcx>(
+    hir: Cx<'a, 'gcx, 'tcx>,
+    body_id: hir::BodyId,
+    ty_span: Span,
+) -> Mir<'tcx> {
     let tcx = hir.tcx();
     let ast_expr = &tcx.hir.body(body_id).value;
     let ty = hir.tables().expr_ty_adjusted(ast_expr);
     let owner_id = tcx.hir.body_owner(body_id);
     let span = tcx.hir.span(owner_id);
-    let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, vec![]);
+    let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, ty_span,vec![]);
 
     let mut block = START_BLOCK;
     let expr = builder.hir.mirror(ast_expr);
@@ -637,7 +669,7 @@ fn construct_error<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
     let owner_id = hir.tcx().hir.body_owner(body_id);
     let span = hir.tcx().hir.span(owner_id);
     let ty = hir.tcx().types.err;
-    let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, vec![]);
+    let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, span, vec![]);
     let source_info = builder.source_info(span);
     builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
     builder.finish(None)
@@ -649,6 +681,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
            arg_count: usize,
            safety: Safety,
            return_ty: Ty<'tcx>,
+           return_span: Span,
            upvar_decls: Vec<UpvarDecl>)
            -> Builder<'a, 'gcx, 'tcx> {
         let lint_level = LintLevel::Explicit(hir.root_lint_level);
@@ -665,8 +698,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
             push_unsafe_count: 0,
             unpushed_unsafe: safety,
             breakable_scopes: vec![],
-            local_decls: IndexVec::from_elem_n(LocalDecl::new_return_place(return_ty,
-                                                                             span), 1),
+            local_decls: IndexVec::from_elem_n(
+                LocalDecl::new_return_place(return_ty, return_span),
+                1,
+            ),
             upvar_decls,
             var_indices: NodeMap(),
             unit_temp: None,
diff --git a/src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr b/src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr
index b156e5a9731..5803b5e355a 100644
--- a/src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr
+++ b/src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr
@@ -5,10 +5,10 @@ LL |     const fn into_inner(self) -> T { self.0 } //~ destructors cannot be eva
    |                         ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:51:5
+  --> $DIR/min_const_fn.rs:51:36
    |
 LL |     const fn get_mut(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                    ^^^^^^
 
 error[E0493]: destructors cannot be evaluated at compile-time
   --> $DIR/min_const_fn.rs:56:28
@@ -17,10 +17,10 @@ LL |     const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be
    |                            ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:58:5
+  --> $DIR/min_const_fn.rs:58:42
    |
 LL |     const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                          ^^^^^^
 
 error[E0493]: destructors cannot be evaluated at compile-time
   --> $DIR/min_const_fn.rs:63:27
@@ -29,16 +29,16 @@ LL |     const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
    |                           ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:65:5
+  --> $DIR/min_const_fn.rs:65:38
    |
 LL |     const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                      ^^^^^^
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:70:5
+  --> $DIR/min_const_fn.rs:70:39
    |
 LL |     const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                       ^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:88:16
@@ -155,10 +155,10 @@ LL | impl<T: Sync + Sized> Foo<T> {
    |      ^
 
 error: `impl Trait` in const fn is unstable
-  --> $DIR/min_const_fn.rs:137:1
+  --> $DIR/min_const_fn.rs:137:24
    |
 LL | const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:139:34
@@ -173,10 +173,10 @@ LL | const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other
    |                      ^^^^^^^^^^^^^^^^^^^^
 
 error: `impl Trait` in const fn is unstable
-  --> $DIR/min_const_fn.rs:142:1
+  --> $DIR/min_const_fn.rs:142:23
    |
 LL | const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                       ^^^^^^^^^^^^^^^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:143:23
@@ -185,10 +185,10 @@ LL | const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds o
    |                       ^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
-  --> $DIR/min_const_fn.rs:144:1
+  --> $DIR/min_const_fn.rs:144:32
    |
 LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error[E0597]: borrowed value does not live long enough
   --> $DIR/min_const_fn.rs:144:64
@@ -213,10 +213,10 @@ LL | const fn no_fn_ptrs(_x: fn()) {}
    |                     ^^
 
 error: function pointers in const fn are unstable
-  --> $DIR/min_const_fn.rs:154:1
+  --> $DIR/min_const_fn.rs:154:27
    |
 LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                           ^^^^
 
 error: aborting due to 36 previous errors
 
diff --git a/src/test/ui/consts/min_const_fn/min_const_fn.stderr b/src/test/ui/consts/min_const_fn/min_const_fn.stderr
index 019948c31b1..6779b8a7614 100644
--- a/src/test/ui/consts/min_const_fn/min_const_fn.stderr
+++ b/src/test/ui/consts/min_const_fn/min_const_fn.stderr
@@ -5,10 +5,10 @@ LL |     const fn into_inner(self) -> T { self.0 } //~ destructors cannot be eva
    |                         ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:51:5
+  --> $DIR/min_const_fn.rs:51:36
    |
 LL |     const fn get_mut(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                    ^^^^^^
 
 error[E0493]: destructors cannot be evaluated at compile-time
   --> $DIR/min_const_fn.rs:56:28
@@ -17,10 +17,10 @@ LL |     const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be
    |                            ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:58:5
+  --> $DIR/min_const_fn.rs:58:42
    |
 LL |     const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                          ^^^^^^
 
 error[E0493]: destructors cannot be evaluated at compile-time
   --> $DIR/min_const_fn.rs:63:27
@@ -29,16 +29,16 @@ LL |     const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
    |                           ^^^^ constant functions cannot evaluate destructors
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:65:5
+  --> $DIR/min_const_fn.rs:65:38
    |
 LL |     const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                      ^^^^^^
 
 error: mutable references in const fn are unstable
-  --> $DIR/min_const_fn.rs:70:5
+  --> $DIR/min_const_fn.rs:70:39
    |
 LL |     const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                       ^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:88:16
@@ -155,10 +155,10 @@ LL | impl<T: Sync + Sized> Foo<T> {
    |      ^
 
 error: `impl Trait` in const fn is unstable
-  --> $DIR/min_const_fn.rs:137:1
+  --> $DIR/min_const_fn.rs:137:24
    |
 LL | const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:139:34
@@ -173,10 +173,10 @@ LL | const fn no_apit(_x: impl std::fmt::Debug) {} //~ ERROR trait bounds other
    |                      ^^^^^^^^^^^^^^^^^^^^
 
 error: `impl Trait` in const fn is unstable
-  --> $DIR/min_const_fn.rs:142:1
+  --> $DIR/min_const_fn.rs:142:23
    |
 LL | const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                       ^^^^^^^^^^^^^^^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:143:23
@@ -185,10 +185,10 @@ LL | const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {} //~ ERROR trait bounds o
    |                       ^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
-  --> $DIR/min_const_fn.rs:144:1
+  --> $DIR/min_const_fn.rs:144:32
    |
 LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: trait bounds other than `Sized` on const fn parameters are unstable
   --> $DIR/min_const_fn.rs:149:41
@@ -203,10 +203,10 @@ LL | const fn no_fn_ptrs(_x: fn()) {}
    |                     ^^
 
 error: function pointers in const fn are unstable
-  --> $DIR/min_const_fn.rs:154:1
+  --> $DIR/min_const_fn.rs:154:27
    |
 LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                           ^^^^
 
 error: aborting due to 35 previous errors