Skip to content

Commit d86e487

Browse files
committed
Start warning cycle.
1 parent 235a7a7 commit d86e487

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/librustc_resolve/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ enum NameBindingKind<'a> {
894894
binding: &'a NameBinding<'a>,
895895
directive: &'a ImportDirective<'a>,
896896
used: Cell<bool>,
897+
legacy_self_import: bool,
897898
},
898899
Ambiguity {
899900
b1: &'a NameBinding<'a>,
@@ -1346,8 +1347,13 @@ impl<'a> Resolver<'a> {
13461347
}
13471348

13481349
match binding.kind {
1349-
NameBindingKind::Import { directive, binding, ref used } if !used.get() => {
1350+
NameBindingKind::Import { directive, binding, ref used, legacy_self_import }
1351+
if !used.get() => {
13501352
used.set(true);
1353+
if legacy_self_import {
1354+
self.warn_legacy_self_import(directive);
1355+
return false;
1356+
}
13511357
self.used_imports.insert((directive.id, ns));
13521358
self.add_to_glob_map(directive.id, ident);
13531359
self.record_use(ident, ns, binding, span)
@@ -3110,6 +3116,12 @@ impl<'a> Resolver<'a> {
31103116
err.emit();
31113117
self.name_already_seen.insert(name, span);
31123118
}
3119+
3120+
fn warn_legacy_self_import(&self, directive: &'a ImportDirective<'a>) {
3121+
let (id, span) = (directive.id, directive.span);
3122+
let msg = "`self` no longer imports values".to_string();
3123+
self.session.add_lint(lint::builtin::LEGACY_IMPORTS, id, span, msg);
3124+
}
31133125
}
31143126

31153127
fn is_struct_like(def: Def) -> bool {

src/librustc_resolve/resolve_imports.rs

+18
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<'a> Resolver<'a> {
297297
binding: binding,
298298
directive: directive,
299299
used: Cell::new(false),
300+
legacy_self_import: false,
300301
},
301302
span: directive.span,
302303
vis: vis,
@@ -594,6 +595,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
594595
};
595596

596597
let mut all_ns_err = true;
598+
let mut legacy_self_import = None;
597599
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
598600
if let Ok(binding) = result[ns].get() {
599601
all_ns_err = false;
@@ -602,9 +604,25 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
602604
Some(this.dummy_binding);
603605
}
604606
}
607+
} else if let Ok(binding) = this.resolve_ident_in_module(module, ident, ns, false, None) {
608+
legacy_self_import = Some(directive);
609+
let binding = this.arenas.alloc_name_binding(NameBinding {
610+
kind: NameBindingKind::Import {
611+
binding: binding,
612+
directive: directive,
613+
used: Cell::new(false),
614+
legacy_self_import: true,
615+
},
616+
..*binding
617+
});
618+
let _ = this.try_define(directive.parent, ident, ns, binding);
605619
});
606620

607621
if all_ns_err {
622+
if let Some(directive) = legacy_self_import {
623+
self.warn_legacy_self_import(directive);
624+
return None;
625+
}
608626
let mut all_ns_failed = true;
609627
self.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
610628
match this.resolve_ident_in_module(module, ident, ns, false, Some(span)) {

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010

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

13+
#![allow(unused)]
14+
#![deny(legacy_imports)]
15+
1316
mod foo {
1417
pub fn f() { }
1518
}
1619
use foo::f::{self};
17-
//~^ ERROR unresolved import
18-
//~| NOTE no `f` in `foo`
20+
//~^ ERROR `self` no longer imports values
21+
//~| WARN hard error
1922

2023
mod bar {
2124
pub fn baz() {}
2225
pub mod baz {}
2326
}
2427
use bar::baz::{self};
28+
//~^ ERROR `self` no longer imports values
29+
//~| WARN hard error
2530

2631
fn main() {
2732
baz();
28-
//~^ ERROR unresolved name `baz`
29-
//~| NOTE unresolved name
30-
//~| HELP module `baz` cannot be used as an expression
3133
}

0 commit comments

Comments
 (0)