Skip to content

Commit b210b31

Browse files
committed
Make the span of the MIR return place point to the return type
1 parent 4f3ff5a commit b210b31

File tree

3 files changed

+81
-46
lines changed

3 files changed

+81
-46
lines changed

src/librustc_mir/build/mod.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,44 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
4040
let id = tcx.hir.as_local_node_id(def_id).unwrap();
4141

4242
// Figure out what primary body this item has.
43-
let body_id = match tcx.hir.get(id) {
43+
let (body_id, return_ty_span) = match tcx.hir.get(id) {
4444
Node::Variant(variant) =>
4545
return create_constructor_shim(tcx, id, &variant.node.data),
4646
Node::StructCtor(ctor) =>
4747
return create_constructor_shim(tcx, id, ctor),
4848

49-
_ => match tcx.hir.maybe_body_owned_by(id) {
50-
Some(body) => body,
51-
None => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
52-
},
49+
Node::Expr(hir::Expr { node: hir::ExprKind::Closure(_, decl, body_id, _, _), .. })
50+
| Node::Item(hir::Item { node: hir::ItemKind::Fn(decl, _, _, body_id), .. })
51+
| Node::ImplItem(
52+
hir::ImplItem {
53+
node: hir::ImplItemKind::Method(hir::MethodSig { decl, .. }, body_id),
54+
..
55+
}
56+
)
57+
| Node::TraitItem(
58+
hir::TraitItem {
59+
node: hir::TraitItemKind::Method(
60+
hir::MethodSig { decl, .. },
61+
hir::TraitMethod::Provided(body_id),
62+
),
63+
..
64+
}
65+
) => {
66+
(*body_id, decl.output.span())
67+
}
68+
Node::Item(hir::Item { node: hir::ItemKind::Static(ty, _, body_id), .. })
69+
| Node::Item(hir::Item { node: hir::ItemKind::Const(ty, body_id), .. })
70+
| Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Const(ty, body_id), .. })
71+
| Node::TraitItem(
72+
hir::TraitItem { node: hir::TraitItemKind::Const(ty, Some(body_id)), .. }
73+
) => {
74+
(*body_id, ty.span)
75+
}
76+
Node::AnonConst(hir::AnonConst { body, id, .. }) => {
77+
(*body, tcx.hir.span(*id))
78+
}
79+
80+
_ => span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id),
5381
};
5482

5583
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
124152
};
125153

126154
build::construct_fn(cx, id, arguments, safety, abi,
127-
return_ty, yield_ty, body)
155+
return_ty, yield_ty, return_ty_span, body)
128156
} else {
129-
build::construct_const(cx, body_id)
157+
build::construct_const(cx, body_id, return_ty_span)
130158
};
131159

132160
// Convert the Mir to global types.
@@ -494,6 +522,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
494522
abi: Abi,
495523
return_ty: Ty<'gcx>,
496524
yield_ty: Option<Ty<'gcx>>,
525+
return_ty_span: Span,
497526
body: &'gcx hir::Body)
498527
-> Mir<'tcx>
499528
where A: Iterator<Item=ArgInfo<'gcx>>
@@ -547,6 +576,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
547576
arguments.len(),
548577
safety,
549578
return_ty,
579+
return_ty_span,
550580
upvar_decls);
551581

552582
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>,
601631
mir
602632
}
603633

604-
fn construct_const<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
605-
body_id: hir::BodyId)
606-
-> Mir<'tcx> {
634+
fn construct_const<'a, 'gcx, 'tcx>(
635+
hir: Cx<'a, 'gcx, 'tcx>,
636+
body_id: hir::BodyId,
637+
ty_span: Span,
638+
) -> Mir<'tcx> {
607639
let tcx = hir.tcx();
608640
let ast_expr = &tcx.hir.body(body_id).value;
609641
let ty = hir.tables().expr_ty_adjusted(ast_expr);
610642
let owner_id = tcx.hir.body_owner(body_id);
611643
let span = tcx.hir.span(owner_id);
612-
let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, vec![]);
644+
let mut builder = Builder::new(hir.clone(), span, 0, Safety::Safe, ty, ty_span,vec![]);
613645

614646
let mut block = START_BLOCK;
615647
let expr = builder.hir.mirror(ast_expr);
@@ -637,7 +669,7 @@ fn construct_error<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
637669
let owner_id = hir.tcx().hir.body_owner(body_id);
638670
let span = hir.tcx().hir.span(owner_id);
639671
let ty = hir.tcx().types.err;
640-
let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, vec![]);
672+
let mut builder = Builder::new(hir, span, 0, Safety::Safe, ty, span, vec![]);
641673
let source_info = builder.source_info(span);
642674
builder.cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
643675
builder.finish(None)
@@ -649,6 +681,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
649681
arg_count: usize,
650682
safety: Safety,
651683
return_ty: Ty<'tcx>,
684+
return_span: Span,
652685
upvar_decls: Vec<UpvarDecl>)
653686
-> Builder<'a, 'gcx, 'tcx> {
654687
let lint_level = LintLevel::Explicit(hir.root_lint_level);
@@ -665,8 +698,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
665698
push_unsafe_count: 0,
666699
unpushed_unsafe: safety,
667700
breakable_scopes: vec![],
668-
local_decls: IndexVec::from_elem_n(LocalDecl::new_return_place(return_ty,
669-
span), 1),
701+
local_decls: IndexVec::from_elem_n(
702+
LocalDecl::new_return_place(return_ty, return_span),
703+
1,
704+
),
670705
upvar_decls,
671706
var_indices: NodeMap(),
672707
unit_temp: None,

src/test/ui/consts/min_const_fn/min_const_fn.nll.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | const fn into_inner(self) -> T { self.0 } //~ destructors cannot be eva
55
| ^^^^ constant functions cannot evaluate destructors
66

77
error: mutable references in const fn are unstable
8-
--> $DIR/min_const_fn.rs:51:5
8+
--> $DIR/min_const_fn.rs:51:36
99
|
1010
LL | const fn get_mut(&mut self) -> &mut T { &mut self.0 }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^
1212

1313
error[E0493]: destructors cannot be evaluated at compile-time
1414
--> $DIR/min_const_fn.rs:56:28
@@ -17,10 +17,10 @@ LL | const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be
1717
| ^^^^ constant functions cannot evaluate destructors
1818

1919
error: mutable references in const fn are unstable
20-
--> $DIR/min_const_fn.rs:58:5
20+
--> $DIR/min_const_fn.rs:58:42
2121
|
2222
LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^
2424

2525
error[E0493]: destructors cannot be evaluated at compile-time
2626
--> $DIR/min_const_fn.rs:63:27
@@ -29,16 +29,16 @@ LL | const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
2929
| ^^^^ constant functions cannot evaluate destructors
3030

3131
error: mutable references in const fn are unstable
32-
--> $DIR/min_const_fn.rs:65:5
32+
--> $DIR/min_const_fn.rs:65:38
3333
|
3434
LL | const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
| ^^^^^^
3636

3737
error: mutable references in const fn are unstable
38-
--> $DIR/min_const_fn.rs:70:5
38+
--> $DIR/min_const_fn.rs:70:39
3939
|
4040
LL | const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| ^^^^^^
4242

4343
error: trait bounds other than `Sized` on const fn parameters are unstable
4444
--> $DIR/min_const_fn.rs:88:16
@@ -155,10 +155,10 @@ LL | impl<T: Sync + Sized> Foo<T> {
155155
| ^
156156

157157
error: `impl Trait` in const fn is unstable
158-
--> $DIR/min_const_fn.rs:137:1
158+
--> $DIR/min_const_fn.rs:137:24
159159
|
160160
LL | const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
161-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162162

163163
error: trait bounds other than `Sized` on const fn parameters are unstable
164164
--> $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
173173
| ^^^^^^^^^^^^^^^^^^^^
174174

175175
error: `impl Trait` in const fn is unstable
176-
--> $DIR/min_const_fn.rs:142:1
176+
--> $DIR/min_const_fn.rs:142:23
177177
|
178178
LL | const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
179-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179+
| ^^^^^^^^^^^^^^^^^^^^
180180

181181
error: trait bounds other than `Sized` on const fn parameters are unstable
182182
--> $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
185185
| ^^
186186

187187
error: trait bounds other than `Sized` on const fn parameters are unstable
188-
--> $DIR/min_const_fn.rs:144:1
188+
--> $DIR/min_const_fn.rs:144:32
189189
|
190190
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
191-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192192

193193
error[E0597]: borrowed value does not live long enough
194194
--> $DIR/min_const_fn.rs:144:64
@@ -213,10 +213,10 @@ LL | const fn no_fn_ptrs(_x: fn()) {}
213213
| ^^
214214

215215
error: function pointers in const fn are unstable
216-
--> $DIR/min_const_fn.rs:154:1
216+
--> $DIR/min_const_fn.rs:154:27
217217
|
218218
LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
219-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
219+
| ^^^^
220220

221221
error: aborting due to 36 previous errors
222222

src/test/ui/consts/min_const_fn/min_const_fn.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | const fn into_inner(self) -> T { self.0 } //~ destructors cannot be eva
55
| ^^^^ constant functions cannot evaluate destructors
66

77
error: mutable references in const fn are unstable
8-
--> $DIR/min_const_fn.rs:51:5
8+
--> $DIR/min_const_fn.rs:51:36
99
|
1010
LL | const fn get_mut(&mut self) -> &mut T { &mut self.0 }
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^
1212

1313
error[E0493]: destructors cannot be evaluated at compile-time
1414
--> $DIR/min_const_fn.rs:56:28
@@ -17,10 +17,10 @@ LL | const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be
1717
| ^^^^ constant functions cannot evaluate destructors
1818

1919
error: mutable references in const fn are unstable
20-
--> $DIR/min_const_fn.rs:58:5
20+
--> $DIR/min_const_fn.rs:58:42
2121
|
2222
LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^
2424

2525
error[E0493]: destructors cannot be evaluated at compile-time
2626
--> $DIR/min_const_fn.rs:63:27
@@ -29,16 +29,16 @@ LL | const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
2929
| ^^^^ constant functions cannot evaluate destructors
3030

3131
error: mutable references in const fn are unstable
32-
--> $DIR/min_const_fn.rs:65:5
32+
--> $DIR/min_const_fn.rs:65:38
3333
|
3434
LL | const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
| ^^^^^^
3636

3737
error: mutable references in const fn are unstable
38-
--> $DIR/min_const_fn.rs:70:5
38+
--> $DIR/min_const_fn.rs:70:39
3939
|
4040
LL | const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| ^^^^^^
4242

4343
error: trait bounds other than `Sized` on const fn parameters are unstable
4444
--> $DIR/min_const_fn.rs:88:16
@@ -155,10 +155,10 @@ LL | impl<T: Sync + Sized> Foo<T> {
155155
| ^
156156

157157
error: `impl Trait` in const fn is unstable
158-
--> $DIR/min_const_fn.rs:137:1
158+
--> $DIR/min_const_fn.rs:137:24
159159
|
160160
LL | const fn no_rpit2() -> AlanTuring<impl std::fmt::Debug> { AlanTuring(0) }
161-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162162

163163
error: trait bounds other than `Sized` on const fn parameters are unstable
164164
--> $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
173173
| ^^^^^^^^^^^^^^^^^^^^
174174

175175
error: `impl Trait` in const fn is unstable
176-
--> $DIR/min_const_fn.rs:142:1
176+
--> $DIR/min_const_fn.rs:142:23
177177
|
178178
LL | const fn no_rpit() -> impl std::fmt::Debug {} //~ ERROR `impl Trait` in const fn is unstable
179-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179+
| ^^^^^^^^^^^^^^^^^^^^
180180

181181
error: trait bounds other than `Sized` on const fn parameters are unstable
182182
--> $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
185185
| ^^
186186

187187
error: trait bounds other than `Sized` on const fn parameters are unstable
188-
--> $DIR/min_const_fn.rs:144:1
188+
--> $DIR/min_const_fn.rs:144:32
189189
|
190190
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
191-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192192

193193
error: trait bounds other than `Sized` on const fn parameters are unstable
194194
--> $DIR/min_const_fn.rs:149:41
@@ -203,10 +203,10 @@ LL | const fn no_fn_ptrs(_x: fn()) {}
203203
| ^^
204204

205205
error: function pointers in const fn are unstable
206-
--> $DIR/min_const_fn.rs:154:1
206+
--> $DIR/min_const_fn.rs:154:27
207207
|
208208
LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
209-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
209+
| ^^^^
210210

211211
error: aborting due to 35 previous errors
212212

0 commit comments

Comments
 (0)