Skip to content

Commit 6372a6d

Browse files
committed
Improve diagnostics for pattern bindings that illegally shadow items.
Improve unused import detection.
1 parent 07c706b commit 6372a6d

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

src/librustc/traits/object_safety.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//! - not reference the erased type `Self` except for in this receiver;
1818
//! - not have generic type parameters
1919
20-
use super::supertraits;
2120
use super::elaborate_predicates;
2221

2322
use hir::def_id::DefId;

src/librustc_resolve/lib.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ enum ResolutionError<'a> {
158158
/// error E0435: attempt to use a non-constant value in a constant
159159
AttemptToUseNonConstantValueInConstant,
160160
/// error E0530: X bindings cannot shadow Ys
161-
BindingShadowsSomethingUnacceptable(&'a str, &'a str, Name),
161+
BindingShadowsSomethingUnacceptable(&'a str, Name, &'a NameBinding<'a>),
162162
/// error E0531: unresolved pattern path kind `name`
163163
PatPathUnresolved(&'a str, &'a Path),
164164
/// error E0532: expected pattern path kind, found another pattern path kind
@@ -422,17 +422,16 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
422422
E0435,
423423
"attempt to use a non-constant value in a constant")
424424
}
425-
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, shadows_what, name) => {
425+
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
426+
let shadows_what = PathResolution::new(binding.def().unwrap()).kind_name();
426427
let mut err = struct_span_err!(resolver.session,
427428
span,
428429
E0530,
429430
"{}s cannot shadow {}s", what_binding, shadows_what);
430431
err.span_label(span, &format!("cannot be named the same as a {}", shadows_what));
431-
if let Success(binding) = resolver.current_module.resolve_name(name, ValueNS, true) {
432-
let participle = if binding.is_import() { "imported" } else { "defined" };
433-
err.span_label(binding.span, &format!("a {} `{}` is {} here",
434-
shadows_what, name, participle));
435-
}
432+
let participle = if binding.is_import() { "imported" } else { "defined" };
433+
let msg = &format!("a {} `{}` is {} here", shadows_what, name, participle);
434+
err.span_label(binding.span, msg);
436435
err
437436
}
438437
ResolutionError::PatPathUnresolved(expected_what, path) => {
@@ -712,12 +711,16 @@ impl<'a> LexicalScopeBinding<'a> {
712711
}
713712
}
714713

715-
fn module(self) -> Option<Module<'a>> {
714+
fn item(self) -> Option<&'a NameBinding<'a>> {
716715
match self {
717-
LexicalScopeBinding::Item(binding) => binding.module(),
716+
LexicalScopeBinding::Item(binding) => Some(binding),
718717
_ => None,
719718
}
720719
}
720+
721+
fn module(self) -> Option<Module<'a>> {
722+
self.item().and_then(NameBinding::module)
723+
}
721724
}
722725

723726
/// The link from a module up to its nearest parent node.
@@ -2316,16 +2319,17 @@ impl<'a> Resolver<'a> {
23162319
PatKind::Ident(bmode, ref ident, ref opt_pat) => {
23172320
// First try to resolve the identifier as some existing
23182321
// entity, then fall back to a fresh binding.
2319-
let resolution = self.resolve_identifier(ident.node, ValueNS, true)
2320-
.map(|local_def| PathResolution::new(local_def.def))
2321-
.and_then(|resolution| {
2322+
let binding = self.resolve_ident_in_lexical_scope(ident.node, ValueNS, false)
2323+
.and_then(LexicalScopeBinding::item);
2324+
let resolution = binding.and_then(NameBinding::def).and_then(|def| {
23222325
let always_binding = !pat_src.is_refutable() || opt_pat.is_some() ||
23232326
bmode != BindingMode::ByValue(Mutability::Immutable);
2324-
match resolution.base_def {
2327+
match def {
23252328
Def::Struct(..) | Def::Variant(..) |
23262329
Def::Const(..) | Def::AssociatedConst(..) if !always_binding => {
23272330
// A constant, unit variant, etc pattern.
2328-
Some(resolution)
2331+
self.record_use(ident.node.name, ValueNS, binding.unwrap());
2332+
Some(PathResolution::new(def))
23292333
}
23302334
Def::Struct(..) | Def::Variant(..) |
23312335
Def::Const(..) | Def::AssociatedConst(..) | Def::Static(..) => {
@@ -2334,7 +2338,7 @@ impl<'a> Resolver<'a> {
23342338
self,
23352339
ident.span,
23362340
ResolutionError::BindingShadowsSomethingUnacceptable(
2337-
pat_src.descr(), resolution.kind_name(), ident.node.name)
2341+
pat_src.descr(), ident.node.name, binding.unwrap())
23382342
);
23392343
None
23402344
}

src/librustc_trans/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use self::utils::{DIB, span_start, create_DIArray, is_node_local_to_unit};
1818
use self::namespace::mangled_name_of_item;
1919
use self::type_names::compute_debuginfo_type_name;
2020
use self::metadata::{type_metadata, diverging_type_metadata};
21-
use self::metadata::{file_metadata, scope_metadata, TypeMap};
21+
use self::metadata::{file_metadata, TypeMap};
2222
use self::source_loc::InternalDebugLocation::{self, UnknownLocation};
2323

2424
use llvm;

src/test/compile-fail/const-pattern-irrefutable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ fn main() {
2525
//~^ NOTE cannot be named the same as a constant
2626
let d = 4; //~ ERROR let bindings cannot shadow constants
2727
//~^ NOTE cannot be named the same as a constant
28+
fn f() {} // Check that the `NOTE`s still work with an item here (c.f. issue #35115).
2829
}

src/test/compile-fail/lint-unused-imports.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ fn g() {
7979
}
8080
}
8181

82+
// c.f. issue #35135
83+
#[allow(unused_variables)]
84+
fn h() {
85+
use test2::foo; //~ ERROR unused import
86+
let foo = 0;
87+
}
88+
8289
fn main() {
8390
cal(foo::Point{x:3, y:9});
8491
let mut a = 3;

0 commit comments

Comments
 (0)