Skip to content

Commit

Permalink
Auto merge of #32227 - jseyfried:fix_import_resolution_bug, r=alexcri…
Browse files Browse the repository at this point in the history
…chton

Fix import resolution bug

This fixes #32222, which was introduced in #31726.
  • Loading branch information
bors committed Mar 13, 2016
2 parents ce943eb + d102014 commit d5880ff
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ impl<'a> NameResolution<'a> {
}
}

fn increment_outstanding_references(&mut self, is_public: bool) {
self.outstanding_references += 1;
if is_public {
self.pub_outstanding_references += 1;
}
}

fn decrement_outstanding_references(&mut self, is_public: bool) {
let decrement_references = |count: &mut _| {
assert!(*count > 0);
*count -= 1;
};

decrement_references(&mut self.outstanding_references);
if is_public {
decrement_references(&mut self.pub_outstanding_references);
}
}

fn report_conflicts<F: FnMut(&NameBinding, &NameBinding)>(&self, mut report: F) {
let binding = match self.binding {
Some(binding) => binding,
Expand Down Expand Up @@ -253,26 +272,8 @@ impl<'a> ::ModuleS<'a> {
}

pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
let mut resolutions = self.resolutions.borrow_mut();
let resolution = resolutions.entry((name, ns)).or_insert_with(Default::default);
resolution.outstanding_references += 1;
if is_public {
resolution.pub_outstanding_references += 1;
}
}

fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
let decrement_references = |count: &mut _| {
assert!(*count > 0);
*count -= 1;
};

self.update_resolution(name, ns, |resolution| {
decrement_references(&mut resolution.outstanding_references);
if is_public {
decrement_references(&mut resolution.pub_outstanding_references);
}
})
self.resolutions.borrow_mut().entry((name, ns)).or_insert_with(Default::default)
.increment_outstanding_references(is_public);
}

// Use `update` to mutate the resolution for the name.
Expand Down Expand Up @@ -477,7 +478,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
// Temporarily count the directive as determined so that the resolution fails
// (as opposed to being indeterminate) when it can only be defined by the directive.
if !determined {
module_.decrement_outstanding_references_for(target, ns, directive.is_public)
module_.resolutions.borrow_mut().get_mut(&(target, ns)).unwrap()
.decrement_outstanding_references(directive.is_public);
}
let result =
self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
Expand Down Expand Up @@ -514,7 +516,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
self.report_conflict(target, ns, &directive.import(binding, None), old_binding);
}
}
module_.decrement_outstanding_references_for(target, ns, directive.is_public);

module_.update_resolution(target, ns, |resolution| {
resolution.decrement_outstanding_references(directive.is_public);
})
}

match (&value_result, &type_result) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/compile-fail/issue-32222.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs)]
#![allow(warnings)]

mod foo {
pub fn bar() {}
}

pub use foo::*;
use b::bar;

mod foobar {
use super::*;
}

mod a {
pub mod bar {}
}

mod b {
pub use a::bar;
}

#[rustc_error]
fn main() {} //~ ERROR compilation successful

0 comments on commit d5880ff

Please sign in to comment.