Skip to content

Commit

Permalink
Auto merge of #98482 - cjgillot:short-struct-span-closure, r=estebank
Browse files Browse the repository at this point in the history
Shorten def_span of closures to just their header

Continuation of #93967.
  • Loading branch information
bors committed Jul 8, 2022
2 parents 9b21131 + d43fa93 commit eba361a
Show file tree
Hide file tree
Showing 175 changed files with 978 additions and 1,362 deletions.
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let arg_pos = args
.iter()
.enumerate()
.filter(|(_, arg)| arg.span == self.body.span)
.filter(|(_, arg)| arg.hir_id == closure_id)
.map(|(pos, _)| pos)
.next();
let def_id = hir.local_def_id(item_id);
Expand Down Expand Up @@ -903,9 +903,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let Some(span) = arg {
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
if self.infcx.tcx.sess.source_map().is_multiline(self.body.span) {
err.span_label(self.body.span, "in this closure");
}
err.span_label(self.body.span, "in this closure");
look_at_return = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ impl<'hir> Map<'hir> {
_ => named_span(item.span, item.ident, None),
},
Node::Ctor(_) => return self.opt_span(self.get_parent_node(hir_id)),
Node::Expr(Expr { kind: ExprKind::Closure { fn_decl_span, .. }, .. }) => *fn_decl_span,
_ => self.span_with_body(hir_id),
};
Some(span)
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,14 +747,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let (migration_string, migrated_variables_concat) =
migration_suggestion_for_2229(self.tcx, &need_migrations);

let local_def_id = closure_def_id.expect_local();
let closure_hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
let closure_span = self.tcx.hir().span(closure_hir_id);
let closure_head_span = self.tcx.sess.source_map().guess_head_span(closure_span);
let closure_hir_id =
self.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
let closure_head_span = self.tcx.def_span(closure_def_id);
self.tcx.struct_span_lint_hir(
lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
closure_hir_id,
closure_head_span,
closure_head_span,
|lint| {
let mut diagnostics_builder = lint.build(
&reasons.migration_message(),
Expand Down Expand Up @@ -827,12 +826,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
migrated_variables_concat
);

let closure_span = self.tcx.hir().span_with_body(closure_hir_id);
let mut closure_body_span = {
// If the body was entirely expanded from a macro
// invocation, i.e. the body is not contained inside the
// closure span, then we walk up the expansion until we
// find the span before the expansion.
let s = self.tcx.hir().span(body_id.hir_id);
let s = self.tcx.hir().span_with_body(body_id.hir_id);
s.find_ancestor_inside(closure_span).unwrap_or(s)
};

Expand Down
4 changes: 2 additions & 2 deletions src/test/codegen/generator-debug-msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ fn generator_test() -> impl Generator<Yield = i32, Return = ()> {
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant1", scope: [[GEN]],
// CHECK-SAME: file: [[FILE]], line: 18,
// CHECK-SAME: file: [[FILE]], line: 14,
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant2", scope: [[GEN]],
// CHECK-SAME: file: [[FILE]], line: 18,
// CHECK-SAME: file: [[FILE]], line: 14,
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "variant3", scope: [[GEN]],
Expand Down
4 changes: 2 additions & 2 deletions src/test/codegen/generator-debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ fn generator_test() -> impl Generator<Yield = i32, Return = ()> {
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "1", scope: [[VARIANT]],
// CHECK-SAME: file: [[FILE]], line: 18,
// CHECK-SAME: file: [[FILE]], line: 14,
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "2", scope: [[VARIANT]],
// CHECK-SAME: file: [[FILE]], line: 18,
// CHECK-SAME: file: [[FILE]], line: 14,
// CHECK-NOT: flags: DIFlagArtificial
// CHECK-SAME: )
// CHECK: {{!.*}} = !DIDerivedType(tag: DW_TAG_member, name: "3", scope: [[VARIANT]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
},
} */

fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 13:6]) -> () {
let mut _0: (); // return place in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 10:17]) -> () {
let mut _0: (); // return place in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
let mut _2: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
let _3: std::string::String; // in scope 0 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
let _4: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
let mut _5: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
let mut _6: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:18: 10:18
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
scope 1 {
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
}

bb0: {
_8 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
switchInt(move _8) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
_8 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
switchInt(move _8) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb1: {
Expand All @@ -44,11 +44,11 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
}

bb3: {
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb4 (cleanup): {
resume; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
resume; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb5 (cleanup): {
Expand All @@ -57,28 +57,28 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
}

bb6: {
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb7: {
goto -> bb9; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
goto -> bb9; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb8: {
goto -> bb3; // scope 0 at $DIR/generator-drop-cleanup.rs:13:5: 13:6
}

bb9: {
goto -> bb6; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
goto -> bb6; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb10: {
StorageLive(_4); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
StorageLive(_5); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
goto -> bb1; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
StorageLive(_4); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
StorageLive(_5); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
goto -> bb1; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}

bb11: {
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
return; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 10:17
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `main::{closure#0}` before StateTransform

fn main::{closure#0}(_1: [generator@$DIR/generator-storage-dead-unwind.rs:22:16: 28:6], _2: ()) -> ()
fn main::{closure#0}(_1: [generator@$DIR/generator-storage-dead-unwind.rs:22:16: 22:18], _2: ()) -> ()
yields ()
{
let mut _0: (); // return place in scope 0 at $DIR/generator-storage-dead-unwind.rs:22:19: 22:19
Expand Down Expand Up @@ -66,7 +66,7 @@ yields ()
}

bb4: {
return; // scope 0 at $DIR/generator-storage-dead-unwind.rs:28:6: 28:6
return; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:18: 22:18
}

bb5: {
Expand All @@ -82,7 +82,7 @@ yields ()
}

bb7: {
generator_drop; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 28:6
generator_drop; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 22:18
}

bb8 (cleanup): {
Expand All @@ -104,7 +104,7 @@ yields ()
}

bb11 (cleanup): {
resume; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 28:6
resume; // scope 0 at $DIR/generator-storage-dead-unwind.rs:22:16: 22:18
}

bb12 (cleanup): {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@
},
} */

fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]>, _2: u8) -> GeneratorState<(), ()> {
fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]>, _2: u8) -> GeneratorState<(), ()> {
debug _x => _10; // in scope 0 at $DIR/generator-tiny.rs:19:17: 19:19
let mut _0: std::ops::GeneratorState<(), ()>; // return place in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
let mut _0: std::ops::GeneratorState<(), ()>; // return place in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
let _3: HasDrop; // in scope 0 at $DIR/generator-tiny.rs:20:13: 20:15
let mut _4: !; // in scope 0 at $DIR/generator-tiny.rs:21:9: 24:10
let mut _5: (); // in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
let mut _5: (); // in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
let _6: u8; // in scope 0 at $DIR/generator-tiny.rs:22:13: 22:18
let mut _7: (); // in scope 0 at $DIR/generator-tiny.rs:22:13: 22:18
let _8: (); // in scope 0 at $DIR/generator-tiny.rs:23:13: 23:21
let mut _9: (); // in scope 0 at $DIR/generator-tiny.rs:19:25: 19:25
let _10: u8; // in scope 0 at $DIR/generator-tiny.rs:19:17: 19:19
let mut _11: u32; // in scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
let mut _11: u32; // in scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
scope 1 {
debug _d => (((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6])) as variant#3).0: HasDrop); // in scope 1 at $DIR/generator-tiny.rs:20:13: 20:15
debug _d => (((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24])) as variant#3).0: HasDrop); // in scope 1 at $DIR/generator-tiny.rs:20:13: 20:15
}

bb0: {
_11 = discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]))); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
switchInt(move _11) -> [0_u32: bb1, 3_u32: bb5, otherwise: bb6]; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
_11 = discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]))); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
switchInt(move _11) -> [0_u32: bb1, 3_u32: bb5, otherwise: bb6]; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
}

bb1: {
_10 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
_10 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
nop; // scope 0 at $DIR/generator-tiny.rs:20:13: 20:15
Deinit((((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6])) as variant#3).0: HasDrop)); // scope 0 at $DIR/generator-tiny.rs:20:18: 20:25
Deinit((((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24])) as variant#3).0: HasDrop)); // scope 0 at $DIR/generator-tiny.rs:20:18: 20:25
StorageLive(_4); // scope 1 at $DIR/generator-tiny.rs:21:9: 24:10
goto -> bb2; // scope 1 at $DIR/generator-tiny.rs:21:9: 24:10
}
Expand All @@ -50,7 +50,7 @@ fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]
Deinit(_0); // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
((_0 as Yielded).0: ()) = move _7; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
discriminant(_0) = 0; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]))) = 3; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
discriminant((*(_1.0: &mut [generator@$DIR/generator-tiny.rs:19:16: 19:24]))) = 3; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
return; // scope 1 at $DIR/generator-tiny.rs:22:13: 22:18
}

Expand All @@ -71,14 +71,14 @@ fn main::{closure#0}(_1: Pin<&mut [generator@$DIR/generator-tiny.rs:19:16: 25:6]
}

bb5: {
StorageLive(_4); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
StorageLive(_6); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
StorageLive(_7); // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
_6 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
goto -> bb3; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
StorageLive(_4); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
StorageLive(_6); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
StorageLive(_7); // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
_6 = move _2; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
goto -> bb3; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
}

bb6: {
unreachable; // scope 0 at $DIR/generator-tiny.rs:19:16: 25:6
unreachable; // scope 0 at $DIR/generator-tiny.rs:19:16: 19:24
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
let mut _10: i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
let mut _11: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
let mut _12: &i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:24
let mut _13: &T; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:24
let mut _12: &i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:17
let mut _13: &T; // in scope 2 at $DIR/inline-closure-captures.rs:11:13: 11:17
}
}

Expand All @@ -33,8 +33,8 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
Deinit(_3); // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
(_3.0: &i32) = move _4; // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
(_3.1: &T) = move _5; // scope 0 at $DIR/inline-closure-captures.rs:11:13: 11:24
StorageDead(_5); // scope 0 at $DIR/inline-closure-captures.rs:11:23: 11:24
StorageDead(_4); // scope 0 at $DIR/inline-closure-captures.rs:11:23: 11:24
StorageDead(_5); // scope 0 at $DIR/inline-closure-captures.rs:11:16: 11:17
StorageDead(_4); // scope 0 at $DIR/inline-closure-captures.rs:11:16: 11:17
StorageLive(_6); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:6
_6 = &_3; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:6
StorageLive(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
Expand Down
Loading

0 comments on commit eba361a

Please sign in to comment.