Skip to content

Commit 60381cd

Browse files
committed
cstore: return an immutable borrow from visible_parent_map
Fixes rust-lang#41053.
1 parent 5309a3e commit 60381cd

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub trait CrateStore {
172172
fn stability(&self, def: DefId) -> Option<attr::Stability>;
173173
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
174174
fn visibility(&self, def: DefId) -> ty::Visibility;
175-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
175+
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
176176
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
177177
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
178178
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
@@ -302,7 +302,7 @@ impl CrateStore for DummyCrateStore {
302302
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
303303
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
304304
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
305-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
305+
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
306306
bug!("visible_parent_map")
307307
}
308308
fn item_generics_cloned(&self, def: DefId) -> ty::Generics

src/librustc_metadata/cstore_impl.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,19 @@ impl CrateStore for cstore::CStore {
507507
/// Returns a map from a sufficiently visible external item (i.e. an external item that is
508508
/// visible from at least one local module) to a sufficiently visible parent (considering
509509
/// modules that re-export the external item to be parents).
510-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
511-
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
512-
if !visible_parent_map.is_empty() { return visible_parent_map; }
510+
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
511+
{
512+
let visible_parent_map = self.visible_parent_map.borrow();
513+
if !visible_parent_map.is_empty() {
514+
return visible_parent_map;
515+
}
516+
}
513517

514518
use std::collections::vec_deque::VecDeque;
515519
use std::collections::hash_map::Entry;
520+
521+
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
522+
516523
for cnum in (1 .. self.next_crate_num().as_usize()).map(CrateNum::new) {
517524
let cdata = self.get_crate_data(cnum);
518525

@@ -556,6 +563,7 @@ impl CrateStore for cstore::CStore {
556563
}
557564
}
558565

559-
visible_parent_map
566+
drop(visible_parent_map);
567+
self.visible_parent_map.borrow()
560568
}
561569
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct Test;

src/test/run-pass/issue-41053.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue_41053.rs
12+
13+
pub trait Trait { fn foo(&self) {} }
14+
15+
pub struct Foo;
16+
17+
impl Iterator for Foo {
18+
type Item = Box<Trait>;
19+
fn next(&mut self) -> Option<Box<Trait>> {
20+
extern crate issue_41053;
21+
impl ::Trait for issue_41053::Test {
22+
fn foo(&self) {}
23+
}
24+
Some(Box::new(issue_41053::Test))
25+
}
26+
}
27+
28+
fn main() {
29+
Foo.next().unwrap().foo();
30+
}

0 commit comments

Comments
 (0)