Skip to content

&mut references located in aliasable memory should be considered frozen #10445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nikomatsakis opened this issue Nov 12, 2013 · 4 comments
Closed
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system P-medium Medium priority

Comments

@nikomatsakis
Copy link
Contributor

We only permit &mut pointees to be mutated if the &mut is in a unique location (i.e., another &mut, a local variable, etc). Based on this, it is safe to conclude that the memory referenced by an &mut pointer found in an aliasable location is frozen. But the borrow checker considers such memory to be unsafe for use. This is overly conservative and prevents important compositional patterns.

In particular, I should be able to define a type BorrowedMap that looks like this:

struct BorrowedMap<'m,K,V> {
    map: &'m mut HashMap<K,V>
}

and now I should be able to implement the Map<K,V> trait for BorrowedMap:

impl<'m,K,V> Map<K,V> for BorrowedMap<'m,K,V> {
    fn insert(&mut self, key: K, value: V) { self.map.insert(key, value); }
    fn find<'a>(&'a self, key: &K) -> Option<'a V> {
        self.map.find(key) // ERROR
    }
}

However I can't because I get an error at the marked line. This is because self.map is an &mut found inside of an aliasable location (self) and hence it cannot be considered frozen. But why not? Nobody can mutate it, after all.

Note that the maximum lifetime of a freeze would be 'a (which is sufficient for this case).

Nominating.

@pnkfelix
Copy link
Member

possible duplicate of #10520

@pnkfelix
Copy link
Member

niko's already working on this so we'll give it P-high.

@nikomatsakis
Copy link
Contributor Author

Closing in favor of #10445

@nikomatsakis
Copy link
Contributor Author

Dup #9629

flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 10, 2023
…r=llogiq

Lintcheck maintenance

Make `cargo lintcheck -j 0` use all threads instead of panicking, and cleanup (and shorten) lintcheck's parsing code.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

2 participants