Skip to content

Commit 3038f30

Browse files
committed
Auto merge of #38271 - jseyfried:rfc_1560_warning_cycle, r=nrc
resolve: change most backwards incompatible ambiguity errors to `legacy_imports` warnings Fixes #38176. r? @nrc or @nikomatsakis
2 parents 94ae2a2 + cfabce2 commit 3038f30

File tree

6 files changed

+111
-28
lines changed

6 files changed

+111
-28
lines changed

src/librustc/lint/builtin.rs

+7
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ declare_lint! {
211211
not named `mod.rs`"
212212
}
213213

214+
declare_lint! {
215+
pub LEGACY_IMPORTS,
216+
Warn,
217+
"detects names that resolve to ambiguous glob imports with RFC 1560"
218+
}
219+
214220
declare_lint! {
215221
pub DEPRECATED,
216222
Warn,
@@ -257,6 +263,7 @@ impl LintPass for HardwiredLints {
257263
PATTERNS_IN_FNS_WITHOUT_BODY,
258264
EXTRA_REQUIREMENT_IN_IMPL,
259265
LEGACY_DIRECTORY_OWNERSHIP,
266+
LEGACY_IMPORTS,
260267
DEPRECATED
261268
)
262269
}

src/librustc_lint/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
234234
id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
235235
reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
236236
},
237+
FutureIncompatibleInfo {
238+
id: LintId::of(LEGACY_IMPORTS),
239+
reference: "issue #38260 <https://github.com/rust-lang/rust/issues/38260>",
240+
},
237241
]);
238242

239243
// Register renamed and removed lints

src/librustc_resolve/lib.rs

+38-18
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
6464
use syntax::ast::{Local, Mutability, Pat, PatKind, Path};
6565
use syntax::ast::{PathSegment, PathParameters, QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6666

67-
use syntax_pos::{Span, DUMMY_SP};
67+
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6868
use errors::DiagnosticBuilder;
6969

7070
use std::cell::{Cell, RefCell};
@@ -896,6 +896,7 @@ enum NameBindingKind<'a> {
896896
Ambiguity {
897897
b1: &'a NameBinding<'a>,
898898
b2: &'a NameBinding<'a>,
899+
legacy: bool,
899900
}
900901
}
901902

@@ -907,13 +908,15 @@ struct AmbiguityError<'a> {
907908
lexical: bool,
908909
b1: &'a NameBinding<'a>,
909910
b2: &'a NameBinding<'a>,
911+
legacy: bool,
910912
}
911913

912914
impl<'a> NameBinding<'a> {
913915
fn module(&self) -> Option<Module<'a>> {
914916
match self.kind {
915917
NameBindingKind::Module(module) => Some(module),
916918
NameBindingKind::Import { binding, .. } => binding.module(),
919+
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.module(),
917920
_ => None,
918921
}
919922
}
@@ -923,6 +926,7 @@ impl<'a> NameBinding<'a> {
923926
NameBindingKind::Def(def) => def,
924927
NameBindingKind::Module(module) => module.def().unwrap(),
925928
NameBindingKind::Import { binding, .. } => binding.def(),
929+
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.def(),
926930
NameBindingKind::Ambiguity { .. } => Def::Err,
927931
}
928932
}
@@ -1349,11 +1353,14 @@ impl<'a> Resolver<'a> {
13491353
self.record_use(name, ns, binding, span)
13501354
}
13511355
NameBindingKind::Import { .. } => false,
1352-
NameBindingKind::Ambiguity { b1, b2 } => {
1356+
NameBindingKind::Ambiguity { b1, b2, legacy } => {
13531357
self.ambiguity_errors.push(AmbiguityError {
1354-
span: span, name: name, lexical: false, b1: b1, b2: b2,
1358+
span: span, name: name, lexical: false, b1: b1, b2: b2, legacy: legacy,
13551359
});
1356-
true
1360+
if legacy {
1361+
self.record_use(name, ns, b1, span);
1362+
}
1363+
!legacy
13571364
}
13581365
_ => false
13591366
}
@@ -3065,26 +3072,39 @@ impl<'a> Resolver<'a> {
30653072
self.report_shadowing_errors();
30663073
let mut reported_spans = FxHashSet();
30673074

3068-
for &AmbiguityError { span, name, b1, b2, lexical } in &self.ambiguity_errors {
3075+
for &AmbiguityError { span, name, b1, b2, lexical, legacy } in &self.ambiguity_errors {
30693076
if !reported_spans.insert(span) { continue }
30703077
let participle = |binding: &NameBinding| {
30713078
if binding.is_import() { "imported" } else { "defined" }
30723079
};
30733080
let msg1 = format!("`{}` could resolve to the name {} here", name, participle(b1));
30743081
let msg2 = format!("`{}` could also resolve to the name {} here", name, participle(b2));
3075-
self.session.struct_span_err(span, &format!("`{}` is ambiguous", name))
3076-
.span_note(b1.span, &msg1)
3077-
.span_note(b2.span, &msg2)
3078-
.note(&if !lexical && b1.is_glob_import() {
3079-
format!("consider adding an explicit import of `{}` to disambiguate", name)
3080-
} else if let Def::Macro(..) = b1.def() {
3081-
format!("macro-expanded {} do not shadow",
3082-
if b1.is_import() { "macro imports" } else { "macros" })
3083-
} else {
3084-
format!("macro-expanded {} do not shadow when used in a macro invocation path",
3085-
if b1.is_import() { "imports" } else { "items" })
3086-
})
3087-
.emit();
3082+
let note = if !lexical && b1.is_glob_import() {
3083+
format!("consider adding an explicit import of `{}` to disambiguate", name)
3084+
} else if let Def::Macro(..) = b1.def() {
3085+
format!("macro-expanded {} do not shadow",
3086+
if b1.is_import() { "macro imports" } else { "macros" })
3087+
} else {
3088+
format!("macro-expanded {} do not shadow when used in a macro invocation path",
3089+
if b1.is_import() { "imports" } else { "items" })
3090+
};
3091+
if legacy {
3092+
let id = match b2.kind {
3093+
NameBindingKind::Import { directive, .. } => directive.id,
3094+
_ => unreachable!(),
3095+
};
3096+
let mut span = MultiSpan::from_span(span);
3097+
span.push_span_label(b1.span, msg1);
3098+
span.push_span_label(b2.span, msg2);
3099+
let msg = format!("`{}` is ambiguous", name);
3100+
self.session.add_lint(lint::builtin::LEGACY_IMPORTS, id, span, msg);
3101+
} else {
3102+
self.session.struct_span_err(span, &format!("`{}` is ambiguous", name))
3103+
.span_note(b1.span, &msg1)
3104+
.span_note(b2.span, &msg2)
3105+
.note(&note)
3106+
.emit();
3107+
}
30883108
}
30893109

30903110
for &PrivacyError(span, name, binding) in &self.privacy_errors {

src/librustc_resolve/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'a> Resolver<'a> {
288288
Some(shadower) if shadower.def() != binding.def() => {
289289
self.ambiguity_errors.push(AmbiguityError {
290290
span: span, name: name, b1: shadower, b2: binding, lexical: true,
291+
legacy: false,
291292
});
292293
return Ok(shadower);
293294
}

src/librustc_resolve/resolve_imports.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl<'a> Resolver<'a> {
159159
binding.def() != shadowed_glob.def() {
160160
self.ambiguity_errors.push(AmbiguityError {
161161
span: span, name: name, lexical: false, b1: binding, b2: shadowed_glob,
162+
legacy: false,
162163
});
163164
}
164165
}
@@ -336,9 +337,9 @@ impl<'a> Resolver<'a> {
336337
}
337338

338339
pub fn ambiguity(&mut self, b1: &'a NameBinding<'a>, b2: &'a NameBinding<'a>)
339-
-> &'a NameBinding<'a> {
340+
-> &'a NameBinding<'a> {
340341
self.arenas.alloc_name_binding(NameBinding {
341-
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2 },
342+
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2, legacy: false },
342343
vis: if b1.vis.is_at_least(b2.vis, self) { b1.vis } else { b2.vis },
343344
span: b1.span,
344345
expansion: Mark::root(),
@@ -726,7 +727,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
726727
}
727728

728729
for (&(name, ns), resolution) in module.resolutions.borrow().iter() {
729-
let resolution = resolution.borrow();
730+
let resolution = &mut *resolution.borrow_mut();
730731
let binding = match resolution.binding {
731732
Some(binding) => binding,
732733
None => continue,
@@ -743,14 +744,34 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
743744
}
744745
}
745746

746-
if let NameBindingKind::Import { binding: orig_binding, directive, .. } = binding.kind {
747-
if ns == TypeNS && orig_binding.is_variant() &&
748-
!orig_binding.vis.is_at_least(binding.vis, self) {
749-
let msg = format!("variant `{}` is private, and cannot be reexported \
750-
(error E0364), consider declaring its enum as `pub`",
751-
name);
752-
self.session.add_lint(PRIVATE_IN_PUBLIC, directive.id, binding.span, msg);
747+
match binding.kind {
748+
NameBindingKind::Import { binding: orig_binding, directive, .. } => {
749+
if ns == TypeNS && orig_binding.is_variant() &&
750+
!orig_binding.vis.is_at_least(binding.vis, self) {
751+
let msg = format!("variant `{}` is private, and cannot be reexported \
752+
(error E0364), consider declaring its enum as `pub`",
753+
name);
754+
self.session.add_lint(PRIVATE_IN_PUBLIC, directive.id, binding.span, msg);
755+
}
756+
}
757+
NameBindingKind::Ambiguity { b1, b2, .. }
758+
if b1.is_glob_import() && b2.is_glob_import() => {
759+
let (orig_b1, orig_b2) = match (&b1.kind, &b2.kind) {
760+
(&NameBindingKind::Import { binding: b1, .. },
761+
&NameBindingKind::Import { binding: b2, .. }) => (b1, b2),
762+
_ => continue,
763+
};
764+
let (b1, b2) = match (orig_b1.vis, orig_b2.vis) {
765+
(ty::Visibility::Public, ty::Visibility::Public) => continue,
766+
(ty::Visibility::Public, _) => (b1, b2),
767+
(_, ty::Visibility::Public) => (b2, b1),
768+
_ => continue,
769+
};
770+
resolution.binding = Some(self.arenas.alloc_name_binding(NameBinding {
771+
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2, legacy: true }, ..*b1
772+
}));
753773
}
774+
_ => {}
754775
}
755776
}
756777

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
#![allow(unused)]
13+
14+
pub struct Foo;
15+
16+
mod bar {
17+
struct Foo;
18+
19+
mod baz {
20+
use *; //~ NOTE `Foo` could resolve to the name imported here
21+
use bar::*; //~ NOTE `Foo` could also resolve to the name imported here
22+
fn f(_: Foo) {}
23+
//~^ WARN `Foo` is ambiguous
24+
//~| WARN hard error in a future release
25+
//~| NOTE see issue #38260
26+
}
27+
}
28+
29+
#[rustc_error]
30+
fn main() {} //~ ERROR compilation successful

0 commit comments

Comments
 (0)