Skip to content

Commit

Permalink
Updated metadata::creader::resolve_crate_deps to use the correct span…
Browse files Browse the repository at this point in the history
…. Clarified error message when an external crate's dependency is missing. Closes #2404.
  • Loading branch information
cmacknz committed Feb 17, 2014
1 parent 57d273f commit 37bf97a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
29 changes: 21 additions & 8 deletions src/librustc/metadata/creader.rs
Expand Up @@ -24,7 +24,7 @@ use syntax::ast;
use syntax::abi;
use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::codemap::{Span, DUMMY_SP};
use syntax::codemap::{Span};
use syntax::diagnostic::SpanHandler;
use syntax::ext::base::{CrateLoader, MacroCrate};
use syntax::parse::token::{IdentInterner, InternedString};
Expand Down Expand Up @@ -147,6 +147,7 @@ fn visit_view_item(e: &mut Env, i: &ast::ViewItem) {
match extract_crate_info(i) {
Some(info) => {
let cnum = resolve_crate(e,
None,
info.ident.clone(),
info.name.clone(),
info.version.clone(),
Expand Down Expand Up @@ -299,6 +300,7 @@ fn existing_match(e: &Env, name: &str, version: &str, hash: &str) -> Option<ast:
}

fn resolve_crate(e: &mut Env,
root_ident: Option<~str>,
ident: ~str,
name: ~str,
version: ~str,
Expand All @@ -319,7 +321,7 @@ fn resolve_crate(e: &mut Env,
};
let loader::Library {
dylib, rlib, metadata
} = load_ctxt.load_library_crate();
} = load_ctxt.load_library_crate(root_ident.clone());

let attrs = decoder::get_crate_attributes(metadata.as_slice());
let crateid = attr::find_crateid(attrs).unwrap();
Expand All @@ -338,8 +340,17 @@ fn resolve_crate(e: &mut Env,
}
e.next_crate_num += 1;

// Maintain a reference to the top most crate.
let root_crate = match root_ident {
Some(c) => c,
None => load_ctxt.ident.clone()
};

// Now resolve the crates referenced by this crate
let cnum_map = resolve_crate_deps(e, metadata.as_slice());
let cnum_map = resolve_crate_deps(e,
Some(root_crate),
metadata.as_slice(),
span);

let cmeta = @cstore::crate_metadata {
name: load_ctxt.name,
Expand All @@ -364,7 +375,10 @@ fn resolve_crate(e: &mut Env,
}

// Go through the crate metadata and load any crates that it references
fn resolve_crate_deps(e: &mut Env, cdata: &[u8]) -> cstore::cnum_map {
fn resolve_crate_deps(e: &mut Env,
root_ident: Option<~str>,
cdata: &[u8], span : Span)
-> cstore::cnum_map {
debug!("resolving deps of external crate");
// The map from crate numbers in the crate we're resolving to local crate
// numbers
Expand All @@ -387,15 +401,13 @@ fn resolve_crate_deps(e: &mut Env, cdata: &[u8]) -> cstore::cnum_map {
None => {
debug!("need to load it");
// This is a new one so we've got to load it
// FIXME (#2404): Need better error reporting than just a bogus
// span.
let fake_span = DUMMY_SP;
let local_cnum = resolve_crate(e,
root_ident.clone(),
cname_str.get().to_str(),
cname_str.get().to_str(),
dep.vers.clone(),
dep.hash.clone(),
fake_span);
span);
cnum_map.insert(extrn_cnum, local_cnum);
}
}
Expand Down Expand Up @@ -427,6 +439,7 @@ impl CrateLoader for Loader {
fn load_crate(&mut self, krate: &ast::ViewItem) -> MacroCrate {
let info = extract_crate_info(krate).unwrap();
let cnum = resolve_crate(&mut self.env,
None,
info.ident.clone(),
info.name.clone(),
info.version.clone(),
Expand Down
12 changes: 8 additions & 4 deletions src/librustc/metadata/loader.rs
Expand Up @@ -65,13 +65,17 @@ pub struct ArchiveMetadata {
}

impl Context {
pub fn load_library_crate(&self) -> Library {
pub fn load_library_crate(&self, root_ident: Option<~str>) -> Library {
match self.find_library_crate() {
Some(t) => t,
None => {
self.sess.span_fatal(self.span,
format!("can't find crate for `{}`",
self.ident));
let message = match root_ident {
None => format!("can't find crate for `{}`", self.ident),
Some(c) => format!("can't find crate for `{}` which `{}` depends on",
self.ident,
c)
};
self.sess.span_fatal(self.span, message);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/run-make/missing-crate-dependency/Makefile
@@ -0,0 +1,9 @@
-include ../tools.mk

all:
$(RUSTC) --crate-type=rlib crateA.rs
$(RUSTC) --crate-type=rlib crateB.rs
rm $(TMPDIR)/$(call RLIB_GLOB,crateA)
# Ensure crateC fails to compile since dependency crateA is missing
$(RUSTC) crateC.rs 2>&1 | \
grep "error: can't find crate for \`crateA\` which \`crateB\` depends on"
12 changes: 12 additions & 0 deletions src/test/run-make/missing-crate-dependency/crateA.rs
@@ -0,0 +1,12 @@
// Copyright 2014 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.

// Base crate
pub fn func() {}
11 changes: 11 additions & 0 deletions src/test/run-make/missing-crate-dependency/crateB.rs
@@ -0,0 +1,11 @@
// Copyright 2014 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.

extern crate crateA;
13 changes: 13 additions & 0 deletions src/test/run-make/missing-crate-dependency/crateC.rs
@@ -0,0 +1,13 @@
// Copyright 2014 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.

extern crate crateB;

fn main() {}

5 comments on commit 37bf97a

@bors
Copy link
Contributor

@bors bors commented on 37bf97a Feb 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at cmacknz@37bf97a

@bors
Copy link
Contributor

@bors bors commented on 37bf97a Feb 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gentlefolk/rust/issue-2404 = 37bf97a into auto

@bors
Copy link
Contributor

@bors bors commented on 37bf97a Feb 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gentlefolk/rust/issue-2404 = 37bf97a merged ok, testing candidate = 25147b2

@bors
Copy link
Contributor

@bors bors commented on 37bf97a Feb 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 37bf97a Feb 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 25147b2

Please sign in to comment.