Skip to content

Commit ef8ee64

Browse files
committed
Auto merge of rust-lang#50760 - petrochenkov:legimp, r=nikomatsakis
Turn deprecation lint `legacy_imports` into a hard error Closes rust-lang#38260 The lint was introduced in Dec 2016, then made deny-by-default in Jun 2017 when crater run found 0 regressions caused by it. This lint requires some not entirely trivial amount of import resolution logic that (surprisingly or not) interacts with `feature(use_extern_macros)` (rust-lang#35896), so it would be desirable to remove it before stabilizing `use_extern_macros`. In particular, this PR fixes the failing example in rust-lang#50725 (but not the whole issue, `use std::panic::{self}` still can cause other undesirable errors when `use_extern_macros` is enabled).
2 parents c95e1cc + d1b0274 commit ef8ee64

File tree

9 files changed

+35
-160
lines changed

9 files changed

+35
-160
lines changed

src/doc/rustc/src/lints/listing/deny-by-default.md

-37
Original file line numberDiff line numberDiff line change
@@ -91,43 +91,6 @@ The legacy_directory_ownership warning is issued when
9191
The warning can be fixed by renaming the parent module to "mod.rs" and moving
9292
it into its own directory if appropriate.
9393

94-
## legacy-imports
95-
96-
This lint detects names that resolve to ambiguous glob imports. Some example
97-
code that triggers this lint:
98-
99-
```rust,ignore
100-
pub struct Foo;
101-
102-
mod bar {
103-
struct Foo;
104-
105-
mod baz {
106-
use *;
107-
use bar::*;
108-
fn f(_: Foo) {}
109-
}
110-
}
111-
```
112-
113-
This will produce:
114-
115-
```text
116-
error: `Foo` is ambiguous
117-
--> src/main.rs:9:17
118-
|
119-
7 | use *;
120-
| - `Foo` could refer to the name imported here
121-
8 | use bar::*;
122-
| ------ `Foo` could also refer to the name imported here
123-
9 | fn f(_: Foo) {}
124-
| ^^^
125-
|
126-
= note: #[deny(legacy_imports)] on by default
127-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
128-
= note: for more information, see issue #38260 <https://github.com/rust-lang/rust/issues/38260>
129-
```
130-
13194

13295
## missing-fragment-specifier
13396

src/librustc/lint/builtin.rs

-7
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,6 @@ declare_lint! {
176176
not named `mod.rs`"
177177
}
178178

179-
declare_lint! {
180-
pub LEGACY_IMPORTS,
181-
Deny,
182-
"detects names that resolve to ambiguous glob imports with RFC 1560"
183-
}
184-
185179
declare_lint! {
186180
pub LEGACY_CONSTRUCTOR_VISIBILITY,
187181
Deny,
@@ -314,7 +308,6 @@ impl LintPass for HardwiredLints {
314308
SAFE_PACKED_BORROWS,
315309
PATTERNS_IN_FNS_WITHOUT_BODY,
316310
LEGACY_DIRECTORY_OWNERSHIP,
317-
LEGACY_IMPORTS,
318311
LEGACY_CONSTRUCTOR_VISIBILITY,
319312
MISSING_FRAGMENT_SPECIFIER,
320313
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,

src/librustc_lint/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
225225
reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
226226
edition: None,
227227
},
228-
FutureIncompatibleInfo {
229-
id: LintId::of(LEGACY_IMPORTS),
230-
reference: "issue #38260 <https://github.com/rust-lang/rust/issues/38260>",
231-
edition: None,
232-
},
233228
FutureIncompatibleInfo {
234229
id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
235230
reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
@@ -318,6 +313,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
318313
"converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
319314
store.register_removed("extra_requirement_in_impl",
320315
"converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
316+
store.register_removed("legacy_imports",
317+
"converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
321318
store.register_removed("coerce_never",
322319
"converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
323320
store.register_removed("resolve_trait_on_defaulted_unit",

src/librustc_resolve/lib.rs

+14-44
Original file line numberDiff line numberDiff line change
@@ -1224,12 +1224,10 @@ enum NameBindingKind<'a> {
12241224
binding: &'a NameBinding<'a>,
12251225
directive: &'a ImportDirective<'a>,
12261226
used: Cell<bool>,
1227-
legacy_self_import: bool,
12281227
},
12291228
Ambiguity {
12301229
b1: &'a NameBinding<'a>,
12311230
b2: &'a NameBinding<'a>,
1232-
legacy: bool,
12331231
}
12341232
}
12351233

@@ -1251,15 +1249,13 @@ struct AmbiguityError<'a> {
12511249
lexical: bool,
12521250
b1: &'a NameBinding<'a>,
12531251
b2: &'a NameBinding<'a>,
1254-
legacy: bool,
12551252
}
12561253

12571254
impl<'a> NameBinding<'a> {
12581255
fn module(&self) -> Option<Module<'a>> {
12591256
match self.kind {
12601257
NameBindingKind::Module(module) => Some(module),
12611258
NameBindingKind::Import { binding, .. } => binding.module(),
1262-
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.module(),
12631259
_ => None,
12641260
}
12651261
}
@@ -1269,7 +1265,6 @@ impl<'a> NameBinding<'a> {
12691265
NameBindingKind::Def(def) => def,
12701266
NameBindingKind::Module(module) => module.def().unwrap(),
12711267
NameBindingKind::Import { binding, .. } => binding.def(),
1272-
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.def(),
12731268
NameBindingKind::Ambiguity { .. } => Def::Err,
12741269
}
12751270
}
@@ -1852,27 +1847,20 @@ impl<'a> Resolver<'a> {
18521847
fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>, span: Span)
18531848
-> bool /* true if an error was reported */ {
18541849
match binding.kind {
1855-
NameBindingKind::Import { directive, binding, ref used, legacy_self_import }
1850+
NameBindingKind::Import { directive, binding, ref used }
18561851
if !used.get() => {
18571852
used.set(true);
18581853
directive.used.set(true);
1859-
if legacy_self_import {
1860-
self.warn_legacy_self_import(directive);
1861-
return false;
1862-
}
18631854
self.used_imports.insert((directive.id, ns));
18641855
self.add_to_glob_map(directive.id, ident);
18651856
self.record_use(ident, ns, binding, span)
18661857
}
18671858
NameBindingKind::Import { .. } => false,
1868-
NameBindingKind::Ambiguity { b1, b2, legacy } => {
1859+
NameBindingKind::Ambiguity { b1, b2 } => {
18691860
self.ambiguity_errors.push(AmbiguityError {
1870-
span: span, name: ident.name, lexical: false, b1: b1, b2: b2, legacy,
1861+
span, name: ident.name, lexical: false, b1, b2,
18711862
});
1872-
if legacy {
1873-
self.record_use(ident, ns, b1, span);
1874-
}
1875-
!legacy
1863+
true
18761864
}
18771865
_ => false
18781866
}
@@ -4128,7 +4116,7 @@ impl<'a> Resolver<'a> {
41284116
self.report_proc_macro_import(krate);
41294117
let mut reported_spans = FxHashSet();
41304118

4131-
for &AmbiguityError { span, name, b1, b2, lexical, legacy } in &self.ambiguity_errors {
4119+
for &AmbiguityError { span, name, b1, b2, lexical } in &self.ambiguity_errors {
41324120
if !reported_spans.insert(span) { continue }
41334121
let participle = |binding: &NameBinding| {
41344122
if binding.is_import() { "imported" } else { "defined" }
@@ -4144,27 +4132,15 @@ impl<'a> Resolver<'a> {
41444132
format!("macro-expanded {} do not shadow when used in a macro invocation path",
41454133
if b1.is_import() { "imports" } else { "items" })
41464134
};
4147-
if legacy {
4148-
let id = match b2.kind {
4149-
NameBindingKind::Import { directive, .. } => directive.id,
4150-
_ => unreachable!(),
4151-
};
4152-
let mut span = MultiSpan::from_span(span);
4153-
span.push_span_label(b1.span, msg1);
4154-
span.push_span_label(b2.span, msg2);
4155-
let msg = format!("`{}` is ambiguous", name);
4156-
self.session.buffer_lint(lint::builtin::LEGACY_IMPORTS, id, span, &msg);
4157-
} else {
4158-
let mut err =
4159-
struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
4160-
err.span_note(b1.span, &msg1);
4161-
match b2.def() {
4162-
Def::Macro(..) if b2.span == DUMMY_SP =>
4163-
err.note(&format!("`{}` is also a builtin macro", name)),
4164-
_ => err.span_note(b2.span, &msg2),
4165-
};
4166-
err.note(&note).emit();
4167-
}
4135+
4136+
let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
4137+
err.span_note(b1.span, &msg1);
4138+
match b2.def() {
4139+
Def::Macro(..) if b2.span == DUMMY_SP =>
4140+
err.note(&format!("`{}` is also a builtin macro", name)),
4141+
_ => err.span_note(b2.span, &msg2),
4142+
};
4143+
err.note(&note).emit();
41684144
}
41694145

41704146
for &PrivacyError(span, name, binding) in &self.privacy_errors {
@@ -4315,12 +4291,6 @@ impl<'a> Resolver<'a> {
43154291
self.name_already_seen.insert(name, span);
43164292
}
43174293

4318-
fn warn_legacy_self_import(&self, directive: &'a ImportDirective<'a>) {
4319-
let (id, span) = (directive.id, directive.span);
4320-
let msg = "`self` no longer imports values";
4321-
self.session.buffer_lint(lint::builtin::LEGACY_IMPORTS, id, span, msg);
4322-
}
4323-
43244294
fn check_proc_macro_attrs(&mut self, attrs: &[ast::Attribute]) {
43254295
if self.proc_macro_enabled { return; }
43264296

src/librustc_resolve/macros.rs

-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ impl<'a> Resolver<'a> {
528528
b1: shadower,
529529
b2: binding,
530530
lexical: true,
531-
legacy: false,
532531
});
533532
return potential_illegal_shadower;
534533
}

src/librustc_resolve/resolve_imports.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ impl<'a> Resolver<'a> {
178178
lexical: false,
179179
b1: binding,
180180
b2: shadowed_glob,
181-
legacy: false,
182181
});
183182
}
184183
}
@@ -350,7 +349,6 @@ impl<'a> Resolver<'a> {
350349
binding,
351350
directive,
352351
used: Cell::new(false),
353-
legacy_self_import: false,
354352
},
355353
span: directive.span,
356354
vis,
@@ -399,7 +397,7 @@ impl<'a> Resolver<'a> {
399397
pub fn ambiguity(&self, b1: &'a NameBinding<'a>, b2: &'a NameBinding<'a>)
400398
-> &'a NameBinding<'a> {
401399
self.arenas.alloc_name_binding(NameBinding {
402-
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2, legacy: false },
400+
kind: NameBindingKind::Ambiguity { b1, b2 },
403401
vis: if b1.vis.is_at_least(b2.vis, self) { b1.vis } else { b2.vis },
404402
span: b1.span,
405403
expansion: Mark::root(),
@@ -691,7 +689,6 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
691689
binding,
692690
directive,
693691
used: Cell::new(false),
694-
legacy_self_import: false,
695692
},
696693
vis: directive.vis.get(),
697694
span: directive.span,
@@ -751,7 +748,6 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
751748
};
752749

753750
let mut all_ns_err = true;
754-
let mut legacy_self_import = None;
755751
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
756752
if let Ok(binding) = result[ns].get() {
757753
all_ns_err = false;
@@ -760,30 +756,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
760756
Some(this.dummy_binding);
761757
}
762758
}
763-
} else if let Ok(binding) = this.resolve_ident_in_module(module,
764-
ident,
765-
ns,
766-
false,
767-
false,
768-
directive.span) {
769-
legacy_self_import = Some(directive);
770-
let binding = this.arenas.alloc_name_binding(NameBinding {
771-
kind: NameBindingKind::Import {
772-
binding,
773-
directive,
774-
used: Cell::new(false),
775-
legacy_self_import: true,
776-
},
777-
..*binding
778-
});
779-
let _ = this.try_define(directive.parent, ident, ns, binding);
780759
});
781760

782761
if all_ns_err {
783-
if let Some(directive) = legacy_self_import {
784-
self.warn_legacy_self_import(directive);
785-
return None;
786-
}
787762
let mut all_ns_failed = true;
788763
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
789764
match this.resolve_ident_in_module(module, ident, ns, false, true, span) {
@@ -1050,23 +1025,6 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
10501025
err.emit();
10511026
}
10521027
}
1053-
NameBindingKind::Ambiguity { b1, b2, .. }
1054-
if b1.is_glob_import() && b2.is_glob_import() => {
1055-
let (orig_b1, orig_b2) = match (&b1.kind, &b2.kind) {
1056-
(&NameBindingKind::Import { binding: b1, .. },
1057-
&NameBindingKind::Import { binding: b2, .. }) => (b1, b2),
1058-
_ => continue,
1059-
};
1060-
let (b1, b2) = match (orig_b1.vis, orig_b2.vis) {
1061-
(ty::Visibility::Public, ty::Visibility::Public) => continue,
1062-
(ty::Visibility::Public, _) => (b1, b2),
1063-
(_, ty::Visibility::Public) => (b2, b1),
1064-
_ => continue,
1065-
};
1066-
resolution.binding = Some(self.arenas.alloc_name_binding(NameBinding {
1067-
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2, legacy: true }, ..*b1
1068-
}));
1069-
}
10701028
_ => {}
10711029
}
10721030
}

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@
1010

1111
// Test that `fn foo::bar::{self}` only imports `bar` in the type namespace.
1212

13-
#![allow(unused)]
14-
1513
mod foo {
1614
pub fn f() { }
1715
}
18-
use foo::f::{self};
19-
//~^ ERROR `self` no longer imports values
20-
//~| WARN hard error
16+
use foo::f::{self}; //~ ERROR unresolved import `foo::f`
2117

2218
mod bar {
2319
pub fn baz() {}
2420
pub mod baz {}
2521
}
2622
use bar::baz::{self};
27-
//~^ ERROR `self` no longer imports values
28-
//~| WARN hard error
2923

3024
fn main() {
31-
baz();
25+
baz(); //~ ERROR expected function, found module `baz`
3226
}

src/test/ui/imports/rfc-1560-warning-cycle.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![allow(unused)]
12-
1311
pub struct Foo;
1412

1513
mod bar {
@@ -18,9 +16,7 @@ mod bar {
1816
mod baz {
1917
use *;
2018
use bar::*;
21-
fn f(_: Foo) {}
22-
//~^ ERROR `Foo` is ambiguous
23-
//~| WARN hard error in a future release
19+
fn f(_: Foo) {} //~ ERROR `Foo` is ambiguous
2420
}
2521
}
2622

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
error: `Foo` is ambiguous
2-
--> $DIR/rfc-1560-warning-cycle.rs:21:17
1+
error[E0659]: `Foo` is ambiguous
2+
--> $DIR/rfc-1560-warning-cycle.rs:19:17
33
|
4-
LL | use *;
5-
| - `Foo` could refer to the name imported here
6-
LL | use bar::*;
7-
| ------ `Foo` could also refer to the name imported here
8-
LL | fn f(_: Foo) {}
4+
LL | fn f(_: Foo) {} //~ ERROR `Foo` is ambiguous
95
| ^^^
106
|
11-
= note: #[deny(legacy_imports)] on by default
12-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13-
= note: for more information, see issue #38260 <https://github.com/rust-lang/rust/issues/38260>
7+
note: `Foo` could refer to the name imported here
8+
--> $DIR/rfc-1560-warning-cycle.rs:17:13
9+
|
10+
LL | use *;
11+
| ^
12+
note: `Foo` could also refer to the name imported here
13+
--> $DIR/rfc-1560-warning-cycle.rs:18:13
14+
|
15+
LL | use bar::*;
16+
| ^^^^^^
17+
= note: consider adding an explicit import of `Foo` to disambiguate
1418

1519
error: aborting due to previous error
1620

21+
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)