-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::cell::*; | ||
|
||
#[derive(Default)] | ||
struct Test { | ||
pub foo: u32, | ||
} | ||
|
||
trait FooSetter { | ||
fn set_foo(&mut self, value: u32); | ||
} | ||
|
||
impl FooSetter for Test { | ||
fn set_foo(&mut self, value: u32) { | ||
self.foo = value; | ||
} | ||
} | ||
|
||
trait BaseSetter{ | ||
fn set(&mut self, value: u32); | ||
} | ||
impl BaseSetter for dyn FooSetter { | ||
fn set(&mut self, value: u32){ | ||
self.set_foo(value); | ||
} | ||
} | ||
|
||
struct TestHolder<'a> { | ||
pub holder: Option<RefCell<&'a mut dyn FooSetter>>, | ||
} | ||
|
||
impl <'a>TestHolder<'a>{ | ||
pub fn test_foo(&self){ | ||
self.holder.as_ref().unwrap().borrow_mut().set(20); | ||
//~^ ERROR borrowed data escapes outside of method | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut test = Test::default(); | ||
test.foo = 10; | ||
{ | ||
let holder = TestHolder { holder: Some(RefCell::from(&mut test))}; | ||
|
||
holder.test_foo(); | ||
} | ||
test.foo = 30; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0521]: borrowed data escapes outside of method | ||
--> $DIR/dyn-trait-static-obligation.rs:33:8 | ||
| | ||
LL | impl <'a>TestHolder<'a>{ | ||
| -- lifetime `'a` defined here | ||
LL | pub fn test_foo(&self){ | ||
| ----- `self` is a reference that is only valid in the method body | ||
LL | self.holder.as_ref().unwrap().borrow_mut().set(20); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| `self` escapes the method body here | ||
| argument requires that `'a` must outlive `'static` | ||
| | ||
= note: requirement occurs because of a mutable reference to `dyn FooSetter` | ||
= note: mutable references are invariant over their type parameter | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
help: consider relaxing the implicit `'static` requirement on the impl | ||
| | ||
LL | impl BaseSetter for dyn FooSetter + '_ { | ||
| ++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0521`. |