Skip to content

Commit ec2619c

Browse files
authoredMar 5, 2021
Rollup merge of rust-lang#80763 - petrochenkov:pubusecrate, r=estebank
resolve: Reduce scope of `pub_use_of_private_extern_crate` deprecation lint This lint was deny-by-default since July 2017, crater showed 7 uses on crates.io back then (rust-lang#42894 (comment)). Unfortunately, the construction `pub use foo as bar` where `foo` is `extern crate foo;` was used by an older version `bitflags`, so turning it into an error causes too many regressions. So, this PR reduces the scope of the lint instead of turning it into a hard error, and only turns some more rarely used components of it into errors.
2 parents e6a6df5 + 7b021aa commit ec2619c

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed
 

‎compiler/rustc_resolve/src/imports.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,21 @@ impl<'a> NameResolution<'a> {
156156
}
157157
}
158158

159+
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
160+
// are permitted for backward-compatibility under a deprecation lint.
161+
fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBinding<'_>) -> bool {
162+
match (&import.kind, &binding.kind) {
163+
(
164+
ImportKind::Single { .. },
165+
NameBindingKind::Import {
166+
import: Import { kind: ImportKind::ExternCrate { .. }, .. },
167+
..
168+
},
169+
) => import.vis.get() == ty::Visibility::Public,
170+
_ => false,
171+
}
172+
}
173+
159174
impl<'a> Resolver<'a> {
160175
crate fn resolve_ident_in_module_unadjusted(
161176
&mut self,
@@ -263,10 +278,7 @@ impl<'a> Resolver<'a> {
263278
return Err((Determined, Weak::No));
264279
}
265280
}
266-
// `extern crate` are always usable for backwards compatibility, see issue #37020,
267-
// remove this together with `PUB_USE_OF_PRIVATE_EXTERN_CRATE`.
268-
let usable = this.is_accessible_from(binding.vis, parent_scope.module)
269-
|| binding.is_extern_crate();
281+
let usable = this.is_accessible_from(binding.vis, parent_scope.module);
270282
if usable { Ok(binding) } else { Err((Determined, Weak::No)) }
271283
};
272284

@@ -309,10 +321,7 @@ impl<'a> Resolver<'a> {
309321
}
310322
}
311323

312-
if !(self.is_accessible_from(binding.vis, parent_scope.module) ||
313-
// Remove this together with `PUB_USE_OF_PRIVATE_EXTERN_CRATE`
314-
(self.last_import_segment && binding.is_extern_crate()))
315-
{
324+
if !self.is_accessible_from(binding.vis, parent_scope.module) {
316325
self.privacy_errors.push(PrivacyError {
317326
ident,
318327
binding,
@@ -455,9 +464,8 @@ impl<'a> Resolver<'a> {
455464
binding: &'a NameBinding<'a>,
456465
import: &'a Import<'a>,
457466
) -> &'a NameBinding<'a> {
458-
let vis = if binding.vis.is_at_least(import.vis.get(), self) ||
459-
// cf. `PUB_USE_OF_PRIVATE_EXTERN_CRATE`
460-
!import.is_glob() && binding.is_extern_crate()
467+
let vis = if binding.vis.is_at_least(import.vis.get(), self)
468+
|| pub_use_of_private_extern_crate_hack(import, binding)
461469
{
462470
import.vis.get()
463471
} else {
@@ -1188,7 +1196,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
11881196
// All namespaces must be re-exported with extra visibility for an error to occur.
11891197
if !any_successful_reexport {
11901198
let (ns, binding) = reexport_error.unwrap();
1191-
if ns == TypeNS && binding.is_extern_crate() {
1199+
if pub_use_of_private_extern_crate_hack(import, binding) {
11921200
let msg = format!(
11931201
"extern crate `{}` is private, and cannot be \
11941202
re-exported (error E0365), consider declaring with \

‎src/test/rustdoc/extern-links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#![crate_name = "foo"]
55

6-
extern crate extern_links;
6+
pub extern crate extern_links;
77

88
// @!has foo/index.html '//a' 'extern_links'
99
#[doc(no_inline)]

‎src/test/rustdoc/issue-28927.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// aux-build:issue-28927-1.rs
33
// ignore-cross-compile
44

5-
extern crate issue_28927_1 as inner1;
5+
pub extern crate issue_28927_1 as inner1;
66
pub use inner1 as foo;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(unused)]
2-
31
extern crate core;
42
pub use core as reexported_core; //~ ERROR `core` is private, and cannot be re-exported
53
//~^ WARN this was previously accepted
@@ -9,16 +7,14 @@ mod foo1 {
97
}
108

119
mod foo2 {
12-
use foo1::core; //~ ERROR `core` is private, and cannot be re-exported
13-
//~^ WARN this was previously accepted
10+
use foo1::core; //~ ERROR crate import `core` is private
1411
pub mod bar {
1512
extern crate core;
1613
}
1714
}
1815

1916
mod baz {
20-
pub use foo2::bar::core; //~ ERROR `core` is private, and cannot be re-exported
21-
//~^ WARN this was previously accepted
17+
pub use foo2::bar::core; //~ ERROR crate import `core` is private
2218
}
2319

2420
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
error: extern crate `core` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
2-
--> $DIR/pub-reexport-priv-extern-crate.rs:4:9
1+
error[E0603]: crate import `core` is private
2+
--> $DIR/pub-reexport-priv-extern-crate.rs:10:15
33
|
4-
LL | pub use core as reexported_core;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | use foo1::core;
5+
| ^^^^ private crate import
66
|
7-
= note: `#[deny(pub_use_of_private_extern_crate)]` on by default
8-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9-
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>
7+
note: the crate import `core` is defined here
8+
--> $DIR/pub-reexport-priv-extern-crate.rs:6:5
9+
|
10+
LL | extern crate core;
11+
| ^^^^^^^^^^^^^^^^^^
1012

11-
error: extern crate `core` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
12-
--> $DIR/pub-reexport-priv-extern-crate.rs:12:9
13+
error[E0603]: crate import `core` is private
14+
--> $DIR/pub-reexport-priv-extern-crate.rs:17:24
1315
|
14-
LL | use foo1::core;
15-
| ^^^^^^^^^^
16+
LL | pub use foo2::bar::core;
17+
| ^^^^ private crate import
1618
|
17-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
18-
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>
19+
note: the crate import `core` is defined here
20+
--> $DIR/pub-reexport-priv-extern-crate.rs:12:9
21+
|
22+
LL | extern crate core;
23+
| ^^^^^^^^^^^^^^^^^^
1924

2025
error: extern crate `core` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
21-
--> $DIR/pub-reexport-priv-extern-crate.rs:20:13
26+
--> $DIR/pub-reexport-priv-extern-crate.rs:2:9
2227
|
23-
LL | pub use foo2::bar::core;
24-
| ^^^^^^^^^^^^^^^
28+
LL | pub use core as reexported_core;
29+
| ^^^^^^^^^^^^^^^^^^^^^^^
2530
|
31+
= note: `#[deny(pub_use_of_private_extern_crate)]` on by default
2632
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2733
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>
2834

2935
error: aborting due to 3 previous errors
3036

37+
For more information about this error, try `rustc --explain E0603`.

0 commit comments

Comments
 (0)
Please sign in to comment.