Skip to content

Dubious region behaviour affected by &_ annotations #36082

Closed
@arielb1

Description

@arielb1

Meta

$ rustc -V
rustc 1.13.0-dev (528c6f3ed 2016-08-25)

STR

This code compiles and runs successfully

use std::cell::RefCell;

fn main() {
    let r = 0;
    let x = RefCell::new((&r,));

    let val = x.borrow().0;
    println!("{}", val);

    x.borrow_mut();
}

But adding a &_ type annotation causes a double borrow panic:

use std::cell::RefCell;

fn main() {
    let r = 0;
    let x = RefCell::new((&r,));

    let val: &_ = x.borrow().0;
    println!("{}", val);

    x.borrow_mut();
}

Notes

"Specification-wise", the type annotation changing the code behaviour is rather dubious. The root cause is this gem of a function.

The behaviour is actually documented in the code:

    // Rule B. `let x: &[...] = [foo().x]`. The rvalue `[foo().x]`
    // would have an extended lifetime, but not `foo()`.

That "common case" does not apply in today's Rust, of course.

Of course, changing this now would be a [breaking-change], but we are landing a similar significant change by @KiChjang (#36029). I will post my take of the rules on a rust-memory-model issue, because @ubsan nicely asked me to (maybe that should be on the RFCs repo instead?).

cc @nrc @pnkfelix @ubsan

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions