Skip to content

Commit 170ccd6

Browse files
committed
Error if pub use references a private item.
[breaking-change] Closes #23266
1 parent 73afbef commit 170ccd6

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/librustc_resolve/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ register_diagnostics! {
2424
E0258, // import conflicts with existing submodule
2525
E0259, // an extern crate has already been imported into this module
2626
E0260, // name conflicts with an external crate that has been imported into this module
27-
E0317 // user-defined types or type parameters cannot shadow the primitive types
27+
E0317, // user-defined types or type parameters cannot shadow the primitive types
28+
E0364, // item is private
29+
E0365 // item is private
2830
}
2931

3032
__build_diagnostic_array! { DIAGNOSTICS }

src/librustc_resolve/lib.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,19 @@ impl NameBindings {
857857
None
858858
}
859859
}
860+
861+
fn is_public(&self, namespace: Namespace) -> bool {
862+
match namespace {
863+
TypeNS => {
864+
let type_def = self.type_def.borrow();
865+
type_def.as_ref().unwrap().modifiers.contains(PUBLIC)
866+
}
867+
ValueNS => {
868+
let value_def = self.value_def.borrow();
869+
value_def.as_ref().unwrap().modifiers.contains(PUBLIC)
870+
}
871+
}
872+
}
860873
}
861874

862875
/// Interns the names of the primitive types.
@@ -1334,22 +1347,33 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13341347
let mut type_result = UnknownResult;
13351348

13361349
// Search for direct children of the containing module.
1337-
build_reduced_graph::populate_module_if_necessary(self, &containing_module);
1350+
build_reduced_graph::populate_module_if_necessary(self, &target_module);
13381351

1339-
match containing_module.children.borrow().get(&source) {
1352+
match target_module.children.borrow().get(&source) {
13401353
None => {
13411354
// Continue.
13421355
}
13431356
Some(ref child_name_bindings) => {
1357+
// pub_err makes sure we don't give the same error twice.
1358+
let mut pub_err = false;
13441359
if child_name_bindings.defined_in_namespace(ValueNS) {
13451360
debug!("(resolving single import) found value binding");
1346-
value_result = BoundResult(containing_module.clone(),
1361+
value_result = BoundResult(target_module.clone(),
13471362
(*child_name_bindings).clone());
1363+
if directive.is_public && !child_name_bindings.is_public(ValueNS) {
1364+
let msg = format!("`{}` is private", token::get_name(source));
1365+
span_err!(self.session, directive.span, E0364, "{}", &msg);
1366+
pub_err = true;
1367+
}
13481368
}
13491369
if child_name_bindings.defined_in_namespace(TypeNS) {
13501370
debug!("(resolving single import) found type binding");
1351-
type_result = BoundResult(containing_module.clone(),
1371+
type_result = BoundResult(target_module.clone(),
13521372
(*child_name_bindings).clone());
1373+
if !pub_err && directive.is_public && !child_name_bindings.is_public(TypeNS) {
1374+
let msg = format!("`{}` is private", token::get_name(source));
1375+
span_err!(self.session, directive.span, E0365, "{}", &msg);
1376+
}
13531377
}
13541378
}
13551379
}

0 commit comments

Comments
 (0)