Skip to content

Commit

Permalink
Shorten def_span for more items.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jul 1, 2022
1 parent ca1e68b commit 42e4eee
Show file tree
Hide file tree
Showing 421 changed files with 2,171 additions and 3,495 deletions.
120 changes: 90 additions & 30 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,28 +934,105 @@ impl<'hir> Map<'hir> {
}

/// Gets the span of the definition of the specified HIR node.
/// This is used by `tcx.get_span`
/// This is used by `tcx.def_span`.
pub fn span(self, hir_id: HirId) -> Span {
self.opt_span(hir_id)
.unwrap_or_else(|| bug!("hir::map::Map::span: id not in map: {:?}", hir_id))
}

pub fn opt_span(self, hir_id: HirId) -> Option<Span> {
fn until_within(outer: Span, end: Span) -> Span {
if let Some(end) = end.find_ancestor_inside(outer) {
outer.with_hi(end.hi())
} else {
outer
}
}

fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
if ident.name != kw::Empty {
let mut span = until_within(item_span, ident.span);
if let Some(g) = generics
&& !g.span.is_dummy()
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
{
span = span.to(g_span);
}
span
} else {
item_span
}
}

let span = match self.find(hir_id)? {
Node::Param(param) => param.span,
// Function-like.
Node::Item(Item { kind: ItemKind::Fn(sig, ..), .. })
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, ..), .. })
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, ..), .. }) => sig.span,
// Constants and Statics.
Node::Item(Item {
kind:
ItemKind::Const(ty, ..)
| ItemKind::Static(ty, ..)
| ItemKind::Impl(Impl { self_ty: ty, .. }),
span: outer_span,
..
})
| Node::TraitItem(TraitItem {
kind: TraitItemKind::Const(ty, ..),
span: outer_span,
..
})
| Node::ImplItem(ImplItem {
kind: ImplItemKind::Const(ty, ..),
span: outer_span,
..
})
| Node::ForeignItem(ForeignItem {
kind: ForeignItemKind::Static(ty, ..),
span: outer_span,
..
}) => until_within(*outer_span, ty.span),
// With generics and bounds.
Node::Item(Item {
kind: ItemKind::Trait(_, _, generics, bounds, _),
span: outer_span,
..
})
| Node::TraitItem(TraitItem {
kind: TraitItemKind::Type(bounds, _),
generics,
span: outer_span,
..
}) => {
let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
until_within(*outer_span, end)
}
// Other cases.
Node::Item(item) => match &item.kind {
ItemKind::Fn(sig, _, _) => sig.span,
_ => item.span,
ItemKind::Use(path, _) => path.span,
_ => named_span(item.span, item.ident, item.kind.generics()),
},
Node::ForeignItem(foreign_item) => foreign_item.span,
Node::TraitItem(trait_item) => match &trait_item.kind {
TraitItemKind::Fn(sig, _) => sig.span,
_ => trait_item.span,
},
Node::ImplItem(impl_item) => match &impl_item.kind {
ImplItemKind::Fn(sig, _) => sig.span,
_ => impl_item.span,
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
Node::ForeignItem(item) => match item.kind {
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
_ => named_span(item.span, item.ident, None),
},
Node::Ctor(..) => return self.opt_span(self.get_parent_node(hir_id)),
_ => self.span_with_body(hir_id),
};
Some(span)
}

/// Like `hir.span()`, but includes the body of items
/// (instead of just the item header)
pub fn span_with_body(self, hir_id: HirId) -> Span {
match self.get(hir_id) {
Node::Param(param) => param.span,
Node::Item(item) => item.span,
Node::ForeignItem(foreign_item) => foreign_item.span,
Node::TraitItem(trait_item) => trait_item.span,
Node::ImplItem(impl_item) => impl_item.span,
Node::Variant(variant) => variant.span,
Node::Field(field) => field.span,
Node::AnonConst(constant) => self.body(constant.body).value.span,
Expand All @@ -973,29 +1050,12 @@ impl<'hir> Map<'hir> {
Node::Pat(pat) => pat.span,
Node::Arm(arm) => arm.span,
Node::Block(block) => block.span,
Node::Ctor(..) => match self.find(self.get_parent_node(hir_id))? {
Node::Item(item) => item.span,
Node::Variant(variant) => variant.span,
_ => unreachable!(),
},
Node::Ctor(..) => self.span_with_body(self.get_parent_node(hir_id)),
Node::Lifetime(lifetime) => lifetime.span,
Node::GenericParam(param) => param.span,
Node::Infer(i) => i.span,
Node::Local(local) => local.span,
Node::Crate(item) => item.spans.inner_span,
};
Some(span)
}

/// Like `hir.span()`, but includes the body of function items
/// (instead of just the function header)
pub fn span_with_body(self, hir_id: HirId) -> Span {
match self.find(hir_id) {
Some(Node::TraitItem(item)) => item.span,
Some(Node::ImplItem(impl_item)) => impl_item.span,
Some(Node::Item(item)) => item.span,
Some(_) => self.span(hir_id),
_ => bug!("hir::map::Map::span_with_body: id not in map: {:?}", hir_id),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/incremental/issue-61323.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ struct C(Box<A>);

#[cfg(cfail)]
struct C(A);
//[cfail]~^ ERROR 12:1: 12:13: recursive type `C` has infinite size [E0072]
//[cfail]~^ ERROR 12:1: 12:9: recursive type `C` has infinite size [E0072]

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45
return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:28
}

bb2 (cleanup): {
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:28
}
- }
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ static BOP: &i32 = {
_1 = &_2; // scope 0 at $DIR/const-promotion-extern-static.rs:16:20: 16:23
_0 = &(*_1); // scope 0 at $DIR/const-promotion-extern-static.rs:16:20: 16:23
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:16:22: 16:23
return; // scope 0 at $DIR/const-promotion-extern-static.rs:16:1: 16:24
return; // scope 0 at $DIR/const-promotion-extern-static.rs:16:1: 16:17
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:28
}

bb2 (cleanup): {
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:28
}
}
-
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `<impl at $DIR/issue-41697.rs:18:1: 22:2>::{constant#0}` after SimplifyCfg-promote-consts
// MIR for `<impl at $DIR/issue-41697.rs:18:1: 18:23>::{constant#0}` after SimplifyCfg-promote-consts

<impl at $DIR/issue-41697.rs:18:1: 22:2>::{constant#0}: usize = {
<impl at $DIR/issue-41697.rs:18:1: 18:23>::{constant#0}: usize = {
let mut _0: usize; // return place in scope 0 at $DIR/issue-41697.rs:18:19: 18:22
let mut _1: (usize, bool); // in scope 0 at $DIR/issue-41697.rs:18:19: 18:22

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `<impl at $DIR/issue-41697.rs:18:1: 22:2>::{constant#0}` after SimplifyCfg-promote-consts
// MIR for `<impl at $DIR/issue-41697.rs:18:1: 18:23>::{constant#0}` after SimplifyCfg-promote-consts

<impl at $DIR/issue-41697.rs:18:1: 22:2>::{constant#0}: usize = {
<impl at $DIR/issue-41697.rs:18:1: 18:23>::{constant#0}: usize = {
let mut _0: usize; // return place in scope 0 at $DIR/issue-41697.rs:18:19: 18:22
let mut _1: (usize, bool); // in scope 0 at $DIR/issue-41697.rs:18:19: 18:22

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `<impl at $DIR/retag.rs:11:1: 19:2>::foo` after SimplifyCfg-elaborate-drops
// MIR for `<impl at $DIR/retag.rs:11:1: 11:10>::foo` after SimplifyCfg-elaborate-drops

fn <impl at $DIR/retag.rs:11:1: 19:2>::foo(_1: &Test, _2: &mut i32) -> &mut i32 {
fn <impl at $DIR/retag.rs:11:1: 11:10>::foo(_1: &Test, _2: &mut i32) -> &mut i32 {
debug self => _1; // in scope 0 at $DIR/retag.rs:13:16: 13:21
debug x => _2; // in scope 0 at $DIR/retag.rs:13:23: 13:24
let mut _0: &mut i32; // return place in scope 0 at $DIR/retag.rs:13:42: 13:53
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `<impl at $DIR/retag.rs:11:1: 19:2>::foo_shr` after SimplifyCfg-elaborate-drops
// MIR for `<impl at $DIR/retag.rs:11:1: 11:10>::foo_shr` after SimplifyCfg-elaborate-drops

fn <impl at $DIR/retag.rs:11:1: 19:2>::foo_shr(_1: &Test, _2: &i32) -> &i32 {
fn <impl at $DIR/retag.rs:11:1: 11:10>::foo_shr(_1: &Test, _2: &i32) -> &i32 {
debug self => _1; // in scope 0 at $DIR/retag.rs:16:20: 16:25
debug x => _2; // in scope 0 at $DIR/retag.rs:16:27: 16:28
let mut _0: &i32; // return place in scope 0 at $DIR/retag.rs:16:42: 16:49
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,6 @@ static XXX: &Foo = {
_0 = &(*_1); // scope 0 at $DIR/storage_live_dead_in_statics.rs:5:28: 23:2
StorageDead(_5); // scope 0 at $DIR/storage_live_dead_in_statics.rs:23:1: 23:2
StorageDead(_1); // scope 0 at $DIR/storage_live_dead_in_statics.rs:23:1: 23:2
return; // scope 0 at $DIR/storage_live_dead_in_statics.rs:5:1: 23:3
return; // scope 0 at $DIR/storage_live_dead_in_statics.rs:5:1: 5:25
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// MIR for `<impl at $DIR/unusual-item-types.rs:9:1: 11:2>::ASSOCIATED_CONSTANT` 0 mir_map
// MIR for `<impl at $DIR/unusual-item-types.rs:9:1: 9:7>::ASSOCIATED_CONSTANT` 0 mir_map

const <impl at $DIR/unusual-item-types.rs:9:1: 11:2>::ASSOCIATED_CONSTANT: i32 = {
const <impl at $DIR/unusual-item-types.rs:9:1: 9:7>::ASSOCIATED_CONSTANT: i32 = {
let mut _0: i32; // return place in scope 0 at $DIR/unusual-item-types.rs:10:32: 10:35

bb0: {
_0 = const 2_i32; // scope 0 at $DIR/unusual-item-types.rs:10:38: 10:39
return; // scope 0 at $DIR/unusual-item-types.rs:10:5: 10:40
return; // scope 0 at $DIR/unusual-item-types.rs:10:5: 10:35
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// MIR for `<impl at $DIR/unusual-item-types.rs:9:1: 11:2>::ASSOCIATED_CONSTANT` 0 mir_map
// MIR for `<impl at $DIR/unusual-item-types.rs:9:1: 9:7>::ASSOCIATED_CONSTANT` 0 mir_map

const <impl at $DIR/unusual-item-types.rs:9:1: 11:2>::ASSOCIATED_CONSTANT: i32 = {
const <impl at $DIR/unusual-item-types.rs:9:1: 9:7>::ASSOCIATED_CONSTANT: i32 = {
let mut _0: i32; // return place in scope 0 at $DIR/unusual-item-types.rs:10:32: 10:35

bb0: {
_0 = const 2_i32; // scope 0 at $DIR/unusual-item-types.rs:10:38: 10:39
return; // scope 0 at $DIR/unusual-item-types.rs:10:5: 10:40
return; // scope 0 at $DIR/unusual-item-types.rs:10:5: 10:35
}
}
5 changes: 2 additions & 3 deletions src/test/rustdoc-ui/lint-missing-doc-code-example.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error: missing code example in this documentation
--> $DIR/lint-missing-doc-code-example.rs:19:1
|
LL | / pub mod module1 {
LL | | }
| |_^
LL | pub mod module1 {
| ^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-missing-doc-code-example.rs:2:9
Expand Down
10 changes: 5 additions & 5 deletions src/test/rustdoc/check-source-code-urls-to-def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ extern crate source_code;
#[path = "auxiliary/source-code-bar.rs"]
pub mod bar;

// @count - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#5-7"]' 4
// @count - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#5"]' 4
use bar::Bar;
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#13-17"]' 'self'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#13"]' 'self'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14"]' 'Trait'
use bar::sub::{self, Trait};

pub struct Foo;
Expand All @@ -42,8 +42,8 @@ pub fn foo(a: u32, b: &str, c: String, d: Foo, e: bar::Bar, f: source_code::Sour
y.hello();
}

// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'bar::sub::Trait'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14-16"]' 'Trait'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14"]' 'bar::sub::Trait'
// @has - '//a[@href="../../src/foo/auxiliary/source-code-bar.rs.html#14"]' 'Trait'
pub fn foo2<T: bar::sub::Trait, V: Trait>(t: &T, v: &V, b: bool) {}

pub trait AnotherTrait {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/inline_cross/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ extern crate macros;
// @has - '//*[@class="docblock"]' 'docs for my_macro'
// @has - '//*[@class="stab deprecated"]' 'Deprecated since 1.2.3: text'
// @has - '//*[@class="stab unstable"]' 'macro_test'
// @has - '//a/@href' '../src/macros/macros.rs.html#8-10'
// @has - '//a/@href' '../src/macros/macros.rs.html#8'
pub use macros::my_macro;
8 changes: 2 additions & 6 deletions src/test/ui/array-slice-vec/array_const_index-0.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: any use of this value will cause an error
--> $DIR/array_const_index-0.rs:2:16
|
LL | const B: i32 = (&A)[1];
| ---------------^^^^^^^-
| |
| index out of bounds: the length is 0 but the index is 1
| ------------ ^^^^^^^ index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
Expand All @@ -17,9 +15,7 @@ error: any use of this value will cause an error
--> $DIR/array_const_index-0.rs:2:16
|
LL | const B: i32 = (&A)[1];
| ---------------^^^^^^^-
| |
| index out of bounds: the length is 0 but the index is 1
| ------------ ^^^^^^^ index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
Expand Down
8 changes: 2 additions & 6 deletions src/test/ui/array-slice-vec/array_const_index-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: any use of this value will cause an error
--> $DIR/array_const_index-1.rs:2:16
|
LL | const B: i32 = A[1];
| ---------------^^^^-
| |
| index out of bounds: the length is 0 but the index is 1
| ------------ ^^^^ index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
Expand All @@ -17,9 +15,7 @@ error: any use of this value will cause an error
--> $DIR/array_const_index-1.rs:2:16
|
LL | const B: i32 = A[1];
| ---------------^^^^-
| |
| index out of bounds: the length is 0 but the index is 1
| ------------ ^^^^ index out of bounds: the length is 0 but the index is 1
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/associated-consts/assoc-const-ty-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ note: associated constant defined here does not match type
--> $DIR/assoc-const-ty-mismatch.rs:5:3
|
LL | const N: usize;
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^

error: mismatch in bind of associated type, got const
--> $DIR/assoc-const-ty-mismatch.rs:25:18
Expand All @@ -20,7 +20,7 @@ note: associated type defined here does not match const
--> $DIR/assoc-const-ty-mismatch.rs:9:3
|
LL | type T;
| ^^^^^^^
| ^^^^^^

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ note: candidate #1 is defined in an impl of the trait `Foo` for the type `i32`
--> $DIR/associated-const-ambiguity-report.rs:10:5
|
LL | const ID: i32 = 1;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `Bar` for the type `i32`
--> $DIR/associated-const-ambiguity-report.rs:14:5
|
LL | const ID: i32 = 3;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^
help: disambiguate the associated constant for candidate #1
|
LL | const X: i32 = <i32 as Foo>::ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0624]: associated constant `ID` is private
--> $DIR/associated-const-private-impl.rs:13:30
|
LL | const ID: i32 = 1;
| ------------------ private associated constant defined here
| ------------- private associated constant defined here
...
LL | assert_eq!(1, bar1::Foo::ID);
| ^^ private associated constant
Expand Down
Loading

0 comments on commit 42e4eee

Please sign in to comment.