Skip to content

Commit cfabce2

Browse files
committed
Demote most backwards incompatible ambiguity errors from RFC 1560 to warnings.
1 parent 7b06438 commit cfabce2

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
@@ -65,7 +65,7 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
6565
use syntax::ast::{Local, Mutability, Pat, PatKind, Path};
6666
use syntax::ast::{PathSegment, PathParameters, QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6767

68-
use syntax_pos::{Span, DUMMY_SP};
68+
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6969
use errors::DiagnosticBuilder;
7070

7171
use std::cell::{Cell, RefCell};
@@ -897,6 +897,7 @@ enum NameBindingKind<'a> {
897897
Ambiguity {
898898
b1: &'a NameBinding<'a>,
899899
b2: &'a NameBinding<'a>,
900+
legacy: bool,
900901
}
901902
}
902903

@@ -908,13 +909,15 @@ struct AmbiguityError<'a> {
908909
lexical: bool,
909910
b1: &'a NameBinding<'a>,
910911
b2: &'a NameBinding<'a>,
912+
legacy: bool,
911913
}
912914

913915
impl<'a> NameBinding<'a> {
914916
fn module(&self) -> Option<Module<'a>> {
915917
match self.kind {
916918
NameBindingKind::Module(module) => Some(module),
917919
NameBindingKind::Import { binding, .. } => binding.module(),
920+
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.module(),
918921
_ => None,
919922
}
920923
}
@@ -924,6 +927,7 @@ impl<'a> NameBinding<'a> {
924927
NameBindingKind::Def(def) => def,
925928
NameBindingKind::Module(module) => module.def().unwrap(),
926929
NameBindingKind::Import { binding, .. } => binding.def(),
930+
NameBindingKind::Ambiguity { legacy: true, b1, .. } => b1.def(),
927931
NameBindingKind::Ambiguity { .. } => Def::Err,
928932
}
929933
}
@@ -1350,11 +1354,14 @@ impl<'a> Resolver<'a> {
13501354
self.record_use(name, ns, binding, span)
13511355
}
13521356
NameBindingKind::Import { .. } => false,
1353-
NameBindingKind::Ambiguity { b1, b2 } => {
1357+
NameBindingKind::Ambiguity { b1, b2, legacy } => {
13541358
self.ambiguity_errors.push(AmbiguityError {
1355-
span: span, name: name, lexical: false, b1: b1, b2: b2,
1359+
span: span, name: name, lexical: false, b1: b1, b2: b2, legacy: legacy,
13561360
});
1357-
true
1361+
if legacy {
1362+
self.record_use(name, ns, b1, span);
1363+
}
1364+
!legacy
13581365
}
13591366
_ => false
13601367
}
@@ -3064,26 +3071,39 @@ impl<'a> Resolver<'a> {
30643071
self.report_shadowing_errors();
30653072
let mut reported_spans = FxHashSet();
30663073

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

30893109
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
@@ -161,6 +161,7 @@ impl<'a> Resolver<'a> {
161161
binding.def() != shadowed_glob.def() {
162162
self.ambiguity_errors.push(AmbiguityError {
163163
span: span, name: name, lexical: false, b1: binding, b2: shadowed_glob,
164+
legacy: false,
164165
});
165166
}
166167
}
@@ -338,9 +339,9 @@ impl<'a> Resolver<'a> {
338339
}
339340

340341
pub fn ambiguity(&mut self, b1: &'a NameBinding<'a>, b2: &'a NameBinding<'a>)
341-
-> &'a NameBinding<'a> {
342+
-> &'a NameBinding<'a> {
342343
self.arenas.alloc_name_binding(NameBinding {
343-
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2 },
344+
kind: NameBindingKind::Ambiguity { b1: b1, b2: b2, legacy: false },
344345
vis: if b1.vis.is_at_least(b2.vis, self) { b1.vis } else { b2.vis },
345346
span: b1.span,
346347
expansion: Mark::root(),
@@ -728,7 +729,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
728729
}
729730

730731
for (&(name, ns), resolution) in module.resolutions.borrow().iter() {
731-
let resolution = resolution.borrow();
732+
let resolution = &mut *resolution.borrow_mut();
732733
let binding = match resolution.binding {
733734
Some(binding) => binding,
734735
None => continue,
@@ -745,14 +746,34 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
745746
}
746747
}
747748

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

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)