Skip to content

Commit d5880ff

Browse files
committed
Auto merge of #32227 - jseyfried:fix_import_resolution_bug, r=alexcrichton
Fix import resolution bug This fixes #32222, which was introduced in #31726.
2 parents ce943eb + d102014 commit d5880ff

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

src/librustc_resolve/resolve_imports.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ impl<'a> NameResolution<'a> {
176176
}
177177
}
178178

179+
fn increment_outstanding_references(&mut self, is_public: bool) {
180+
self.outstanding_references += 1;
181+
if is_public {
182+
self.pub_outstanding_references += 1;
183+
}
184+
}
185+
186+
fn decrement_outstanding_references(&mut self, is_public: bool) {
187+
let decrement_references = |count: &mut _| {
188+
assert!(*count > 0);
189+
*count -= 1;
190+
};
191+
192+
decrement_references(&mut self.outstanding_references);
193+
if is_public {
194+
decrement_references(&mut self.pub_outstanding_references);
195+
}
196+
}
197+
179198
fn report_conflicts<F: FnMut(&NameBinding, &NameBinding)>(&self, mut report: F) {
180199
let binding = match self.binding {
181200
Some(binding) => binding,
@@ -253,26 +272,8 @@ impl<'a> ::ModuleS<'a> {
253272
}
254273

255274
pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
256-
let mut resolutions = self.resolutions.borrow_mut();
257-
let resolution = resolutions.entry((name, ns)).or_insert_with(Default::default);
258-
resolution.outstanding_references += 1;
259-
if is_public {
260-
resolution.pub_outstanding_references += 1;
261-
}
262-
}
263-
264-
fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
265-
let decrement_references = |count: &mut _| {
266-
assert!(*count > 0);
267-
*count -= 1;
268-
};
269-
270-
self.update_resolution(name, ns, |resolution| {
271-
decrement_references(&mut resolution.outstanding_references);
272-
if is_public {
273-
decrement_references(&mut resolution.pub_outstanding_references);
274-
}
275-
})
275+
self.resolutions.borrow_mut().entry((name, ns)).or_insert_with(Default::default)
276+
.increment_outstanding_references(is_public);
276277
}
277278

278279
// Use `update` to mutate the resolution for the name.
@@ -477,7 +478,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
477478
// Temporarily count the directive as determined so that the resolution fails
478479
// (as opposed to being indeterminate) when it can only be defined by the directive.
479480
if !determined {
480-
module_.decrement_outstanding_references_for(target, ns, directive.is_public)
481+
module_.resolutions.borrow_mut().get_mut(&(target, ns)).unwrap()
482+
.decrement_outstanding_references(directive.is_public);
481483
}
482484
let result =
483485
self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
@@ -514,7 +516,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
514516
self.report_conflict(target, ns, &directive.import(binding, None), old_binding);
515517
}
516518
}
517-
module_.decrement_outstanding_references_for(target, ns, directive.is_public);
519+
520+
module_.update_resolution(target, ns, |resolution| {
521+
resolution.decrement_outstanding_references(directive.is_public);
522+
})
518523
}
519524

520525
match (&value_result, &type_result) {

src/test/compile-fail/issue-32222.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 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+
#![feature(rustc_attrs)]
12+
#![allow(warnings)]
13+
14+
mod foo {
15+
pub fn bar() {}
16+
}
17+
18+
pub use foo::*;
19+
use b::bar;
20+
21+
mod foobar {
22+
use super::*;
23+
}
24+
25+
mod a {
26+
pub mod bar {}
27+
}
28+
29+
mod b {
30+
pub use a::bar;
31+
}
32+
33+
#[rustc_error]
34+
fn main() {} //~ ERROR compilation successful

0 commit comments

Comments
 (0)