Skip to content

Commit 996f06f

Browse files
authored
Rollup merge of rust-lang#41061 - arielb1:parent-lock, r=eddyb
cstore: return an immutable borrow from `visible_parent_map` This prevents an ICE when `visible_parent_map` is called multiple times, for example when an item referenced in an impl signature is imported from an `extern crate` statement occurs within an impl. Fixes rust-lang#41053. r? @eddyb
2 parents 88e97f0 + 60381cd commit 996f06f

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 2 deletions
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>;
@@ -303,7 +303,7 @@ impl CrateStore for DummyCrateStore {
303303
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
304304
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
305305
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
306-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
306+
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
307307
bug!("visible_parent_map")
308308
}
309309
fn item_generics_cloned(&self, def: DefId) -> ty::Generics

src/librustc_metadata/cstore_impl.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,19 @@ impl CrateStore for cstore::CStore {
511511
/// Returns a map from a sufficiently visible external item (i.e. an external item that is
512512
/// visible from at least one local module) to a sufficiently visible parent (considering
513513
/// modules that re-export the external item to be parents).
514-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
515-
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
516-
if !visible_parent_map.is_empty() { return visible_parent_map; }
514+
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
515+
{
516+
let visible_parent_map = self.visible_parent_map.borrow();
517+
if !visible_parent_map.is_empty() {
518+
return visible_parent_map;
519+
}
520+
}
517521

518522
use std::collections::vec_deque::VecDeque;
519523
use std::collections::hash_map::Entry;
524+
525+
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
526+
520527
for cnum in (1 .. self.next_crate_num().as_usize()).map(CrateNum::new) {
521528
let cdata = self.get_crate_data(cnum);
522529

@@ -560,6 +567,7 @@ impl CrateStore for cstore::CStore {
560567
}
561568
}
562569

563-
visible_parent_map
570+
drop(visible_parent_map);
571+
self.visible_parent_map.borrow()
564572
}
565573
}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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)