Skip to content

RefCell::borrow does not pass borrow-check without a seemingly no-op let binding #23338

Closed
@SimonSapin

Description

@SimonSapin

When upgrading Rust in Servo, many usages of RefCell::borrow that were previously fine now cause borrow-check errors like this:

components/script/dom/xmlhttprequest.rs:678:9: 678:13 error: `self` does not live long enough
components/script/dom/xmlhttprequest.rs:678         self.status_text.borrow().clone()
                                                                               ^~~~
components/script/dom/xmlhttprequest.rs:677:39: 679:6 note: reference must be valid for the destruction scope surrounding block at 677:38...
components/script/dom/xmlhttprequest.rs:677     fn StatusText(self) -> ByteString {
components/script/dom/xmlhttprequest.rs:678         self.status_text.borrow().clone()
components/script/dom/xmlhttprequest.rs:679     }
components/script/dom/xmlhttprequest.rs:677:39: 679:6 note: ...but borrowed value is only valid for the block at 677:38
components/script/dom/xmlhttprequest.rs:677     fn StatusText(self) -> ByteString {
components/script/dom/xmlhttprequest.rs:678         self.status_text.borrow().clone()
components/script/dom/xmlhttprequest.rs:679     }

This error message makes no sense to me, the two blocks it talks about are the same.

This can be worked around by binding the result of .borrow() with let before using it, enough though such a change looks like a no-op:

 fn StatusText(self) -> ByteString {
-     self.status_text.borrow().clone()
+     let status_text = self.status_text.borrow();
+     status_text.clone()
}

Metadata

Metadata

Assignees

Labels

A-destructorsArea: Destructors (`Drop`, …)A-lifetimesArea: Lifetimes / regions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions