Skip to content

Commit f04eb37

Browse files
committed
auto merge of #6347 : cmr/rust/unknown_module_resolve_error, r=catamorphism
This improves error reporting for the following class of imports: ```rust use foo::bar; ``` Where foo, the topmost module, is unresolved. It now results in: ```text /tmp/foo.rs:1:4: 1:7 error: unresolved import. perhapsyou forgot an 'extern mod foo'? /tmp/foo.rs:1 use foo::bar; ^~~ /tmp/foo.rs:1:4: 1:12 error: failed to resolve import: foo::bar /tmp/foo.rs:1 use foo::bar; ^~~~~~~~ error: failed to resolve imports error: aborting due to 3 previous errors ``` This is the first of a series of changes I plan on making to unresolved name error messages.
2 parents 3e106cf + 9cbab89 commit f04eb37

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/librustc/middle/resolve.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use syntax::attr::{attr_metas, contains_name, attrs_contains_name};
6464
use syntax::parse::token::ident_interner;
6565
use syntax::parse::token::special_idents;
6666
use syntax::print::pprust::path_to_str;
67-
use syntax::codemap::{span, dummy_sp};
67+
use syntax::codemap::{span, dummy_sp, BytePos};
6868
use syntax::visit::{default_visitor, mk_vt, Visitor, visit_block};
6969
use syntax::visit::{visit_crate, visit_expr, visit_expr_opt};
7070
use syntax::visit::{visit_foreign_item, visit_item};
@@ -2482,6 +2482,16 @@ pub impl Resolver {
24822482
TypeNS,
24832483
name_search_type) {
24842484
Failed => {
2485+
let segment_name = self.session.str_of(name);
2486+
let module_name = self.module_to_str(search_module);
2487+
if module_name == ~"???" {
2488+
self.session.span_err(span {lo: span.lo, hi: span.lo +
2489+
BytePos(str::len(*segment_name)), expn_info:
2490+
span.expn_info}, fmt!("unresolved import. maybe \
2491+
a missing 'extern mod %s'?",
2492+
*segment_name));
2493+
return Failed;
2494+
}
24852495
self.session.span_err(span, ~"unresolved name");
24862496
return Failed;
24872497
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// Testing that we don't fail abnormally after hitting the errors
1212

13-
use unresolved::*; //~ ERROR unresolved name
13+
use unresolved::*; //~ ERROR unresolved import. maybe a missing
1414
//~^ ERROR failed to resolve import
1515

1616
fn main() {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2013 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+
use foo::bar; //~ ERROR unresolved import. maybe a missing
12+
//~^ ERROR failed to resolve import

0 commit comments

Comments
 (0)