Skip to content

Commit

Permalink
Remove textual span from diagnostic string
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 13, 2021
1 parent a16f686 commit 15f9347
Show file tree
Hide file tree
Showing 125 changed files with 329 additions and 376 deletions.
67 changes: 10 additions & 57 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Expand Up @@ -135,7 +135,8 @@ fn msg_span_from_free_region(
) -> (String, Option<Span>) {
match *region {
ty::ReEarlyBound(_) | ty::ReFree(_) => {
msg_span_from_early_bound_and_free_regions(tcx, region)
let (msg, span) = msg_span_from_early_bound_and_free_regions(tcx, region);
(msg, Some(span))
}
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
ty::ReEmpty(ty::UniverseIndex::ROOT) => ("an empty lifetime".to_owned(), alt_span),
Expand All @@ -147,28 +148,20 @@ fn msg_span_from_free_region(
fn msg_span_from_early_bound_and_free_regions(
tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>,
) -> (String, Option<Span>) {
) -> (String, Span) {
let sm = tcx.sess.source_map();

let scope = region.free_region_binding_scope(tcx);
let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local());
let tag = match tcx.hir().find(node) {
Some(Node::Block(_) | Node::Expr(_)) => "body",
Some(Node::Item(it)) => item_scope_tag(&it),
Some(Node::TraitItem(it)) => trait_item_scope_tag(&it),
Some(Node::ImplItem(it)) => impl_item_scope_tag(&it),
Some(Node::ForeignItem(it)) => foreign_item_scope_tag(&it),
_ => unreachable!(),
};
let (prefix, span) = match *region {
match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = sm.guess_head_span(tcx.hir().span(node));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
sp = param.span;
}
(format!("the lifetime `{}` as defined on", br.name), sp)
(format!("the lifetime `{}` as defined here", br.name), sp)
}
ty::ReFree(ty::FreeRegion {
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
Expand All @@ -179,28 +172,26 @@ fn msg_span_from_early_bound_and_free_regions(
{
sp = param.span;
}
(format!("the lifetime `{}` as defined on", name), sp)
(format!("the lifetime `{}` as defined here", name), sp)
}
ty::ReFree(ref fr) => match fr.bound_region {
ty::BrAnon(idx) => {
if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) {
("the anonymous lifetime defined on".to_string(), ty.span)
("the anonymous lifetime defined here".to_string(), ty.span)
} else {
(
format!("the anonymous lifetime #{} defined on", idx + 1),
format!("the anonymous lifetime #{} defined here", idx + 1),
tcx.hir().span(node),
)
}
}
_ => (
format!("the lifetime `{}` as defined on", region),
format!("the lifetime `{}` as defined here", region),
sm.guess_head_span(tcx.hir().span(node)),
),
},
_ => bug!(),
};
let (msg, opt_span) = explain_span(tcx, tag, span);
(format!("{} {}", prefix, msg), opt_span)
}
}

fn emit_msg_span(
Expand All @@ -219,44 +210,6 @@ fn emit_msg_span(
}
}

fn item_scope_tag(item: &hir::Item<'_>) -> &'static str {
match item.kind {
hir::ItemKind::Impl { .. } => "impl",
hir::ItemKind::Struct(..) => "struct",
hir::ItemKind::Union(..) => "union",
hir::ItemKind::Enum(..) => "enum",
hir::ItemKind::Trait(..) => "trait",
hir::ItemKind::Fn(..) => "function body",
_ => "item",
}
}

fn trait_item_scope_tag(item: &hir::TraitItem<'_>) -> &'static str {
match item.kind {
hir::TraitItemKind::Fn(..) => "method body",
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => "associated item",
}
}

fn impl_item_scope_tag(item: &hir::ImplItem<'_>) -> &'static str {
match item.kind {
hir::ImplItemKind::Fn(..) => "method body",
hir::ImplItemKind::Const(..) | hir::ImplItemKind::TyAlias(..) => "associated item",
}
}

fn foreign_item_scope_tag(item: &hir::ForeignItem<'_>) -> &'static str {
match item.kind {
hir::ForeignItemKind::Fn(..) => "method body",
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => "associated item",
}
}

fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option<Span>) {
let lo = tcx.sess.source_map().lookup_char_pos(span.lo());
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1), Some(span))
}

pub fn unexpected_hidden_region_diagnostic(
tcx: TyCtxt<'tcx>,
span: Span,
Expand Down
Expand Up @@ -6,7 +6,7 @@ LL | const NAME: &'a str = "unit";
|
= note: expected reference `&'static str`
found reference `&'a str`
note: the lifetime `'a` as defined on the impl at 6:6...
note: the lifetime `'a` as defined here...
--> $DIR/associated-const-impl-wrong-lifetime.rs:6:6
|
LL | impl<'a> Foo for &'a () {
Expand Down
Expand Up @@ -24,12 +24,12 @@ LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d
|
= note: expected fn pointer `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'_>)`
found fn pointer `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'_>)`
note: the lifetime `'c` as defined on the method body at 27:24...
note: the lifetime `'c` as defined here...
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^
note: ...does not necessarily outlive the lifetime `'c` as defined on the method body at 27:24
note: ...does not necessarily outlive the lifetime `'c` as defined here
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
Expand All @@ -43,12 +43,12 @@ LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d
|
= note: expected fn pointer `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'_>)`
found fn pointer `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'_>)`
note: the lifetime `'c` as defined on the method body at 27:24...
note: the lifetime `'c` as defined here...
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^
note: ...does not necessarily outlive the lifetime `'c` as defined on the method body at 27:24
note: ...does not necessarily outlive the lifetime `'c` as defined here
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
|
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/c-variadic/issue-86053-1.stderr
Expand Up @@ -84,12 +84,12 @@ error[E0491]: in type `&'a &'b usize`, reference has a longer lifetime than the
LL | self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize ) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the function body at 10:16
note: the pointer is valid for the lifetime `'a` as defined here
--> $DIR/issue-86053-1.rs:10:16
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
| ^^
note: but the referenced data is only valid for the lifetime `'b` as defined on the function body at 10:21
note: but the referenced data is only valid for the lifetime `'b` as defined here
--> $DIR/issue-86053-1.rs:10:21
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/c-variadic/issue-86053-2.stderr
Expand Up @@ -5,7 +5,7 @@ LL | unsafe extern "C" fn ordering4<'a, F: H<&'static &'a ()>>(_: (), ...) {}
| ^^^^^^^^^^^^^^^^^^
|
= note: the pointer is valid for the static lifetime
note: but the referenced data is only valid for the lifetime `'a` as defined on the function body at 8:32
note: but the referenced data is only valid for the lifetime `'a` as defined here
--> $DIR/issue-86053-2.rs:8:32
|
LL | unsafe extern "C" fn ordering4<'a, F: H<&'static &'a ()>>(_: (), ...) {}
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr
Expand Up @@ -6,12 +6,12 @@ LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
|
= note: expected fn pointer `fn(&u32)`
found fn pointer `fn(&'x u32)`
note: the anonymous lifetime #1 defined on the body at 16:48...
note: the anonymous lifetime #1 defined here...
--> $DIR/expect-fn-supply-fn.rs:16:48
|
LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
| ^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'x` as defined on the function body at 13:36
note: ...does not necessarily outlive the lifetime `'x` as defined here
--> $DIR/expect-fn-supply-fn.rs:13:36
|
LL | fn expect_free_supply_free_from_fn<'x>(x: &'x u32) {
Expand All @@ -25,12 +25,12 @@ LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
|
= note: expected fn pointer `fn(&u32)`
found fn pointer `fn(&'x u32)`
note: the lifetime `'x` as defined on the function body at 13:36...
note: the lifetime `'x` as defined here...
--> $DIR/expect-fn-supply-fn.rs:13:36
|
LL | fn expect_free_supply_free_from_fn<'x>(x: &'x u32) {
| ^^
note: ...does not necessarily outlive the anonymous lifetime #1 defined on the body at 16:48
note: ...does not necessarily outlive the anonymous lifetime #1 defined here
--> $DIR/expect-fn-supply-fn.rs:16:48
|
LL | with_closure_expecting_fn_with_free_region(|x: fn(&'x u32), y| {});
Expand Down
Expand Up @@ -6,7 +6,7 @@ LL | closure_expecting_bound(|x: &'x u32| {
|
= note: expected reference `&u32`
found reference `&'x u32`
note: the anonymous lifetime #1 defined on the body at 14:29...
note: the anonymous lifetime #1 defined here...
--> $DIR/expect-region-supply-region-2.rs:14:29
|
LL | closure_expecting_bound(|x: &'x u32| {
Expand All @@ -18,7 +18,7 @@ LL | |
LL | | f = Some(x);
LL | | });
| |_____^
note: ...does not necessarily outlive the lifetime `'x` as defined on the function body at 9:30
note: ...does not necessarily outlive the lifetime `'x` as defined here
--> $DIR/expect-region-supply-region-2.rs:9:30
|
LL | fn expect_bound_supply_named<'x>() {
Expand All @@ -32,12 +32,12 @@ LL | closure_expecting_bound(|x: &'x u32| {
|
= note: expected reference `&u32`
found reference `&'x u32`
note: the lifetime `'x` as defined on the function body at 9:30...
note: the lifetime `'x` as defined here...
--> $DIR/expect-region-supply-region-2.rs:9:30
|
LL | fn expect_bound_supply_named<'x>() {
| ^^
note: ...does not necessarily outlive the anonymous lifetime #1 defined on the body at 14:29
note: ...does not necessarily outlive the anonymous lifetime #1 defined here
--> $DIR/expect-region-supply-region-2.rs:14:29
|
LL | closure_expecting_bound(|x: &'x u32| {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/dropck/reject-specialized-drops-8142.stderr
Expand Up @@ -30,7 +30,7 @@ LL | impl Drop for N<'static> { fn drop(&mut self) { } }
|
= note: expected struct `N<'n>`
found struct `N<'static>`
note: the lifetime `'n` as defined on the struct at 7:10...
note: the lifetime `'n` as defined here...
--> $DIR/reject-specialized-drops-8142.rs:7:10
|
LL | struct N<'n> { x: &'n i8 }
Expand Down Expand Up @@ -91,12 +91,12 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'lw`
LL | impl<'lw> Drop for W<'lw,'lw> { fn drop(&mut self) { } } // REJECT
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime `'l1` as defined on the struct at 16:10...
note: first, the lifetime cannot outlive the lifetime `'l1` as defined here...
--> $DIR/reject-specialized-drops-8142.rs:16:10
|
LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
| ^^^
note: ...but the lifetime must also be valid for the lifetime `'l2` as defined on the struct at 16:15...
note: ...but the lifetime must also be valid for the lifetime `'l2` as defined here...
--> $DIR/reject-specialized-drops-8142.rs:16:15
|
LL | struct W<'l1, 'l2> { x: &'l1 i8, y: &'l2 u8 }
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0308-2.stderr
Expand Up @@ -6,7 +6,7 @@ LL | impl Eq for &dyn DynEq {}
|
= note: expected trait `<&dyn DynEq as PartialEq>`
found trait `<&(dyn DynEq + 'static) as PartialEq>`
note: the lifetime `'_` as defined on the impl at 9:13...
note: the lifetime `'_` as defined here...
--> $DIR/E0308-2.rs:9:13
|
LL | impl Eq for &dyn DynEq {}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0478.stderr
Expand Up @@ -4,12 +4,12 @@ error[E0478]: lifetime bound not satisfied
LL | child: Box<dyn Wedding<'kiss> + 'SnowWhite>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'SnowWhite` as defined on the struct at 3:22
note: lifetime parameter instantiated with the lifetime `'SnowWhite` as defined here
--> $DIR/E0478.rs:3:22
|
LL | struct Prince<'kiss, 'SnowWhite> {
| ^^^^^^^^^^
note: but lifetime parameter must outlive the lifetime `'kiss` as defined on the struct at 3:15
note: but lifetime parameter must outlive the lifetime `'kiss` as defined here
--> $DIR/E0478.rs:3:15
|
LL | struct Prince<'kiss, 'SnowWhite> {
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/error-codes/E0490.stderr
Expand Up @@ -4,12 +4,12 @@ error[E0490]: a value of type `&'b ()` is borrowed for too long
LL | let x: &'a _ = &y;
| ^^
|
note: the type is valid for the lifetime `'a` as defined on the function body at 1:6
note: the type is valid for the lifetime `'a` as defined here
--> $DIR/E0490.rs:1:6
|
LL | fn f<'a, 'b>(y: &'b ()) {
| ^^
note: but the borrow lasts for the lifetime `'b` as defined on the function body at 1:10
note: but the borrow lasts for the lifetime `'b` as defined here
--> $DIR/E0490.rs:1:10
|
LL | fn f<'a, 'b>(y: &'b ()) {
Expand All @@ -21,7 +21,7 @@ error[E0495]: cannot infer an appropriate lifetime for borrow expression due to
LL | let x: &'a _ = &y;
| ^^
|
note: first, the lifetime cannot outlive the lifetime `'b` as defined on the function body at 1:10...
note: first, the lifetime cannot outlive the lifetime `'b` as defined here...
--> $DIR/E0490.rs:1:10
|
LL | fn f<'a, 'b>(y: &'b ()) {
Expand All @@ -31,7 +31,7 @@ note: ...so that the type `&'b ()` is not borrowed for too long
|
LL | let x: &'a _ = &y;
| ^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 1:6...
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
--> $DIR/E0490.rs:1:6
|
LL | fn f<'a, 'b>(y: &'b ()) {
Expand All @@ -48,7 +48,7 @@ error[E0495]: cannot infer an appropriate lifetime due to conflicting requiremen
LL | let x: &'a _ = &y;
| ^^
|
note: first, the lifetime cannot outlive the lifetime `'b` as defined on the function body at 1:10...
note: first, the lifetime cannot outlive the lifetime `'b` as defined here...
--> $DIR/E0490.rs:1:10
|
LL | fn f<'a, 'b>(y: &'b ()) {
Expand All @@ -60,7 +60,7 @@ LL | let x: &'a _ = &y;
| ^^
= note: expected `&'a &()`
found `&'a &'b ()`
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 1:6...
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
--> $DIR/E0490.rs:1:6
|
LL | fn f<'a, 'b>(y: &'b ()) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/explicit/explicit-self-lifetime-mismatch.stderr
Expand Up @@ -6,12 +6,12 @@ LL | Foo<'b,'a>
|
= note: expected struct `Foo<'a, 'b>`
found struct `Foo<'b, 'a>`
note: the lifetime `'b` as defined on the impl at 6:9...
note: the lifetime `'b` as defined here...
--> $DIR/explicit-self-lifetime-mismatch.rs:6:9
|
LL | impl<'a,'b> Foo<'a,'b> {
| ^^
note: ...does not necessarily outlive the lifetime `'a` as defined on the impl at 6:6
note: ...does not necessarily outlive the lifetime `'a` as defined here
--> $DIR/explicit-self-lifetime-mismatch.rs:6:6
|
LL | impl<'a,'b> Foo<'a,'b> {
Expand All @@ -25,12 +25,12 @@ LL | Foo<'b,'a>
|
= note: expected struct `Foo<'a, 'b>`
found struct `Foo<'b, 'a>`
note: the lifetime `'a` as defined on the impl at 6:6...
note: the lifetime `'a` as defined here...
--> $DIR/explicit-self-lifetime-mismatch.rs:6:6
|
LL | impl<'a,'b> Foo<'a,'b> {
| ^^
note: ...does not necessarily outlive the lifetime `'b` as defined on the impl at 6:9
note: ...does not necessarily outlive the lifetime `'b` as defined here
--> $DIR/explicit-self-lifetime-mismatch.rs:6:9
|
LL | impl<'a,'b> Foo<'a,'b> {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/generator/resume-arg-late-bound.stderr
Expand Up @@ -6,7 +6,7 @@ LL | test(gen);
|
= note: expected type `for<'a> Generator<&'a mut bool>`
found type `Generator<&mut bool>`
note: the required lifetime does not necessarily outlive the anonymous lifetime #1 defined on the body at 11:15
note: the required lifetime does not necessarily outlive the anonymous lifetime #1 defined here
--> $DIR/resume-arg-late-bound.rs:11:15
|
LL | let gen = |arg: &mut bool| {
Expand All @@ -29,7 +29,7 @@ LL | test(gen);
|
= note: expected type `for<'a> Generator<&'a mut bool>`
found type `Generator<&mut bool>`
note: the anonymous lifetime #1 defined on the body at 11:15 doesn't meet the lifetime requirements
note: the anonymous lifetime #1 defined here doesn't meet the lifetime requirements
--> $DIR/resume-arg-late-bound.rs:11:15
|
LL | let gen = |arg: &mut bool| {
Expand Down

0 comments on commit 15f9347

Please sign in to comment.