# Make borrow safe earlier

**URL:** https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717
**Category:** language design
**Created:** [June 12, 2018, 12:10am UTC](https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717 "2018-06-12T00:10:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![naim](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/naim/32/3522_2.png) [@naim](https://internals.rust-lang.org/u/naim)
#### Post date: [June 12, 2018, 12:10am UTC](https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717/1 "2018-06-12T00:10:18Z")

</div>

Consider the following code:

```rust
struct A<'a> { dal: &'a mut B, }
struct B { }

impl B {
	pub fn get_object(&mut self) -> Option<&str> { Some("Got Object") }
}

impl<'a> A<'a> {
	pub fn new(a: &mut B) -> A { A{ dal: a } }
	pub fn do_something(&mut self) {
		match self.dal.get_object() {
			None => {
				// this causes E0502 since *self.dal was a mutable borrowed and we are attempting to borrow *self.
				// In this case, *self is unusable, although it seems it would be perfectly safe to use... 
				// Shouldn't the borrow have ended when no valid reference was returned (like within Some(...))?
				self.report_error();
			},
			Some(v) => {
				println!("retreived {}", v);
			}
		}
	}

	fn report_error(&self) { println!("report some error..."); }
}

fn main() {
	let mut t = B{};
	let mut x = A::new(&mut t);

	x.do_something();
}

```

Shouldn’t it be made safe to borrow `*self` again after it’s previous use is no longer accessible?

Any thought?

---

<div class="post-metadata">

### Author: ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)
#### Post date: [June 12, 2018, 12:16am UTC](https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717/2 "2018-06-12T00:16:59Z")

</div>

I believe this is exactly the sort of code that will become legal with the “non-lexical lifetimes” feature, which is documented at [https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md](https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md) and is already implemented on nightly.

---

<div class="post-metadata">

### Author: ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)
#### Post date: [June 12, 2018, 12:18am UTC](https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717/3 "2018-06-12T00:18:00Z")

</div>

Yes, you can try it on nightly with `#![feature(nll)]` at the top of your crate, and this code is accepted.

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)
#### Post date: [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/make-borrow-safe-earlier/7717/4 "2019-03-25T08:30:22Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
