Skip to content

Commit 5172745

Browse files
committed
Warn instead of error when using an inaccessable extern crate
1 parent f8d6dcf commit 5172745

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/librustc/lint/builtin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ declare_lint! {
124124
"detect private items in public interfaces not caught by the old implementation"
125125
}
126126

127+
declare_lint! {
128+
pub INACCESSIBLE_EXTERN_CRATE,
129+
Warn,
130+
"use of inaccessible extern crate erroneously allowed"
131+
}
132+
127133
declare_lint! {
128134
pub INVALID_TYPE_PARAM_DEFAULT,
129135
Warn,
@@ -167,6 +173,7 @@ impl LintPass for HardwiredLints {
167173
TRIVIAL_CASTS,
168174
TRIVIAL_NUMERIC_CASTS,
169175
PRIVATE_IN_PUBLIC,
176+
INACCESSIBLE_EXTERN_CRATE,
170177
INVALID_TYPE_PARAM_DEFAULT,
171178
MATCH_OF_UNIT_VARIANT_VIA_PAREN_DOTDOT,
172179
CONST_ERR,

src/librustc_lint/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
158158
id: LintId::of(PRIVATE_IN_PUBLIC),
159159
reference: "the explanation for E0446 (`--explain E0446`)",
160160
},
161+
FutureIncompatibleInfo {
162+
id: LintId::of(INACCESSIBLE_EXTERN_CRATE),
163+
reference: "PR 31362 <https://github.com/rust-lang/rust/pull/31362>",
164+
},
161165
FutureIncompatibleInfo {
162166
id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
163167
reference: "PR 30742 <https://github.com/rust-lang/rust/pull/30724>",

src/librustc_privacy/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
743743
source_did: Option<DefId>,
744744
msg: &str)
745745
-> CheckResult {
746+
use rustc_front::hir::Item_::ItemExternCrate;
746747
debug!("ensure_public(span={:?}, to_check={:?}, source_did={:?}, msg={:?})",
747748
span, to_check, source_did, msg);
748749
let def_privacy = self.def_privacy(to_check);
@@ -763,6 +764,21 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
763764
// be local.)
764765
let def_id = source_did.unwrap_or(to_check);
765766
let node_id = self.tcx.map.as_local_node_id(def_id);
767+
768+
// Warn when using a inaccessible extern crate.
769+
if let Some(node_id) = self.tcx.map.as_local_node_id(to_check) {
770+
match self.tcx.map.get(node_id) {
771+
ast_map::Node::NodeItem(&hir::Item { node: ItemExternCrate(_), name, .. }) => {
772+
self.tcx.sess.add_lint(lint::builtin::INACCESSIBLE_EXTERN_CRATE,
773+
node_id,
774+
span,
775+
format!("extern crate `{}` is private", name));
776+
return None;
777+
}
778+
_ => {}
779+
}
780+
}
781+
766782
let (err_span, err_msg) = if Some(id) == node_id {
767783
return Some((span, format!("{} is private", msg), None));
768784
} else {

src/test/compile-fail/extern-crate-visibility.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(rustc_attrs)]
12+
#![allow(dead_code)]
13+
#![allow(unused_imports)]
14+
1115
mod foo {
1216
extern crate core;
13-
pub use self::core as reexported_core; // Check that private extern crates can be reexported
1417
}
1518

16-
// Check that private crates cannot be used from outside their modules
17-
use foo::core; //~ ERROR module `core` is inaccessible
18-
use foo::core::cell; //~ ERROR
19+
// Check that private crates can be used from outside their modules, albeit with warnings
20+
use foo::core; //~ WARN extern crate `core` is private
21+
//~^ WARN this was previously accepted by the compiler but is being phased out
22+
use foo::core::cell; //~ WARN extern crate `core` is private
23+
//~^ WARN this was previously accepted by the compiler but is being phased out
24+
25+
fn f() {
26+
foo::core::cell::Cell::new(0); //~ WARN extern crate `core` is private
27+
//~^ WARN this was previously accepted by the compiler but is being phased out
1928

20-
fn main() {
2129
use foo::*;
2230
mod core {} // Check that private crates are not glob imported
2331
}
32+
33+
#[rustc_error]
34+
fn main() {} //~ ERROR compilation successful

0 commit comments

Comments
 (0)