Skip to content

Fix import resolution bug #32227

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

Merged
merged 3 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -478,7 +479,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 @@ -515,7 +517,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