Extend and use hir::Node::body_id

This commit is contained in:
Maybe Waffle 2023-04-12 18:45:43 +00:00
parent 102c0af5a7
commit cac143f0e3
20 changed files with 102 additions and 89 deletions

View File

@ -1306,26 +1306,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let local_ty = self.body.local_decls[local].ty;
// Get the body the error happens in
let &body_id =
if let hir::Node::Item(hir::Item { kind, .. }) = hir.get(self.mir_hir_id())
&& let hir::ItemKind::Static(_, _, body_id)
| hir::ItemKind::Const(_, body_id)
| hir::ItemKind::Fn(_, _, body_id) = kind
{
body_id
} else if let hir::Node::TraitItem(hir::TraitItem { kind, .. }) = hir.get(self.mir_hir_id())
&& let hir::TraitItemKind::Const(_, Some(body_id))
| hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) = kind
{
body_id
}else if let hir::Node::ImplItem(hir::ImplItem { kind, .. }) = hir.get(self.mir_hir_id())
&& let hir::ImplItemKind::Const(_, body_id)
| hir::ImplItemKind::Fn(_, body_id) = kind
{
body_id
} else {
return
};
let Some(body_id) = hir.get(self.mir_hir_id()).body_id() else { return };
let body_expr = hir.body(body_id).value;

View File

@ -3529,12 +3529,20 @@ impl<'hir> OwnerNode<'hir> {
pub fn body_id(&self) -> Option<BodyId> {
match self {
OwnerNode::TraitItem(TraitItem {
kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)),
OwnerNode::Item(Item {
kind:
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
..
})
| OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. })
| OwnerNode::Item(Item { kind: ItemKind::Fn(.., body_id), .. }) => Some(*body_id),
| OwnerNode::TraitItem(TraitItem {
kind:
TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
..
})
| OwnerNode::ImplItem(ImplItem {
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
..
}) => Some(*body),
_ => None,
}
}
@ -3729,12 +3737,27 @@ impl<'hir> Node<'hir> {
pub fn body_id(&self) -> Option<BodyId> {
match self {
Node::TraitItem(TraitItem {
kind: TraitItemKind::Fn(_, TraitFn::Provided(body_id)),
Node::Item(Item {
kind:
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
..
})
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(_, body_id), .. })
| Node::Item(Item { kind: ItemKind::Fn(.., body_id), .. }) => Some(*body_id),
| Node::TraitItem(TraitItem {
kind:
TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
..
})
| Node::ImplItem(ImplItem {
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
..
})
| Node::Expr(Expr {
kind:
ExprKind::ConstBlock(AnonConst { body, .. })
| ExprKind::Closure(Closure { body, .. })
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
..
}) => Some(*body),
_ => None,
}
}

View File

@ -4,6 +4,7 @@ error[E0597]: `bar` does not live long enough
LL | let x = {
| - borrow later stored here
LL | let bar = 22;
| --- binding `bar` declared here
LL | Foo::new(&bar).await
| ^^^^ borrowed value does not live long enough
LL |

View File

@ -13,6 +13,7 @@ error[E0597]: `bar` does not live long enough
LL | let x = {
| - borrow later stored here
LL | let bar = 22;
| --- binding `bar` declared here
LL | Foo::new(&bar).await
| ^^^^ borrowed value does not live long enough
LL |

View File

@ -4,6 +4,8 @@ error[E0597]: `x` does not live long enough
LL | impl<'a, T: 'static> Generic<'a, T> {
| -- lifetime `'a` defined here
...
LL | let x: &'static [T] = &[];
| - binding `x` declared here
LL | &x
| ^^
| |
@ -16,6 +18,8 @@ LL | };
error[E0597]: `x` does not live long enough
--> $DIR/generic-slice.rs:27:5
|
LL | let x: &[_] = &[];
| - binding `x` declared here
LL | &x
| ^^
| |

View File

@ -9,7 +9,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut binding, &mut true, No);
|
error[E0716]: temporary value dropped while borrowed
--> $DIR/auto-trait-regions.rs:48:35
@ -22,7 +26,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut true, &mut binding, No);
|
error: implementation of `Foo` is not general enough
--> $DIR/auto-trait-regions.rs:34:5

View File

@ -9,7 +9,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut binding, &mut true, No);
|
error[E0716]: temporary value dropped while borrowed
--> $DIR/auto-trait-regions.rs:48:35
@ -22,7 +26,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut true, &mut binding, No);
|
error: implementation of `Foo` is not general enough
--> $DIR/auto-trait-regions.rs:34:5

View File

@ -9,7 +9,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut binding, &mut true, No);
|
error[E0716]: temporary value dropped while borrowed
--> $DIR/auto-trait-regions.rs:48:35
@ -22,7 +26,11 @@ LL | let a = A(&mut true, &mut true, No);
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = true;
LL ~ let a = A(&mut true, &mut binding, No);
|
error: implementation of `Foo` is not general enough
--> $DIR/auto-trait-regions.rs:34:5

View File

@ -1,47 +0,0 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/auto-trait-regions.rs:48:24
|
LL | let a = A(&mut true, &mut true, No);
| ^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
...
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error[E0716]: temporary value dropped while borrowed
--> $DIR/auto-trait-regions.rs:48:35
|
LL | let a = A(&mut true, &mut true, No);
| ^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
...
LL | assert_foo(a);
| - borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
error: implementation of `Foo` is not general enough
--> $DIR/auto-trait-regions.rs:34:5
|
LL | assert_foo(gen);
| ^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
|
= note: `&'0 OnlyFooIfStaticRef` must implement `Foo`, for any lifetime `'0`...
= note: ...but `Foo` is actually implemented for the type `&'static OnlyFooIfStaticRef`
error: implementation of `Foo` is not general enough
--> $DIR/auto-trait-regions.rs:54:5
|
LL | assert_foo(gen);
| ^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
|
= note: `Foo` would have to be implemented for the type `A<'0, '1>`, for any two lifetimes `'0` and `'1`...
= note: ...but `Foo` is actually implemented for the type `A<'_, '2>`, for some specific lifetime `'2`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0716`.

View File

@ -12,6 +12,8 @@ LL | async fn f2(m: Msg<'_>) {}
error[E0597]: `buf` does not live long enough
--> $DIR/issue-69314.rs:14:19
|
LL | let mut buf = [0; 512];
| ------- binding `buf` declared here
LL | let m2 = &buf[..];
| ^^^ borrowed value does not live long enough
LL | let m = Self::g(m2).await;

View File

@ -31,7 +31,9 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | SomeEnum::SomeVariant::<&'a u32> { t: &c };
| ^^
| |

View File

@ -31,7 +31,9 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | SomeStruct::<&'a u32> { t: &c };
| ^^
| |

View File

@ -34,7 +34,10 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | combine(
LL | SomeEnum::SomeVariant(Cell::new(&c)),
| ----------^^-
| | |

View File

@ -31,7 +31,9 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | SomeEnum::SomeVariant::<&'a u32>(&c);
| ^^
| |

View File

@ -33,7 +33,10 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | let f = SomeStruct::<&'a u32>;
LL | f(&c);
| --^^-
| | |
@ -47,7 +50,9 @@ error[E0597]: `c` does not live long enough
|
LL | let f = SomeStruct::<&'a u32>;
| - lifetime `'1` appears in the type of `f`
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | f(&c);
| --^^-
| | |

View File

@ -31,7 +31,9 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | SomeStruct::<&'a u32>(&c);
| ^^
| |

View File

@ -31,7 +31,9 @@ error[E0597]: `c` does not live long enough
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let _closure = || {
LL | let c = 66;
| - binding `c` declared here
LL | some_fn::<&'a u32>(&c);
| -------------------^^-
| | |

View File

@ -33,6 +33,8 @@ error[E0597]: `c` does not live long enough
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let c = 66;
| - binding `c` declared here
LL | a.method::<&'a u32>(b, &c);
| ------------------------^^-
| | |

View File

@ -33,6 +33,8 @@ error[E0597]: `c` does not live long enough
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| -- lifetime `'a` defined here
...
LL | let c = 66;
| - binding `c` declared here
LL | <_ as Bazoom<_>>::method::<&'a u32>(&a, b, &c);
| -------------------------------------------^^-
| | |

View File

@ -1,6 +1,8 @@
error[E0597]: `p` does not live long enough
--> $DIR/issue-18118.rs:4:9
|
LL | let p = 3;
| - binding `p` declared here
LL | &p
| ^^
| |