diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index e656e5329b5..7c05913467c 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -67,7 +67,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan}; use errors::{Applicability, DiagnosticBuilder, DiagnosticId}; use std::cell::{Cell, RefCell}; -use std::{cmp, fmt, iter, ptr}; +use std::{cmp, fmt, iter, mem, ptr}; use std::collections::BTreeSet; use std::mem::replace; use rustc_data_structures::ptr_key::PtrKey; @@ -2375,11 +2375,27 @@ impl<'a> Resolver<'a> { ast::UseTreeKind::Simple(..) if segments.len() == 1 => &[TypeNS, ValueNS][..], _ => &[TypeNS], }; + let report_error = |this: &Self, ns| { + let what = if ns == TypeNS { "type parameters" } else { "local variables" }; + this.session.span_err(ident.span, &format!("imports cannot refer to {}", what)); + }; + for &ns in nss { - if let Some(LexicalScopeBinding::Def(..)) = - self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) { - let what = if ns == TypeNS { "type parameters" } else { "local variables" }; - self.session.span_err(ident.span, &format!("imports cannot refer to {}", what)); + match self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) { + Some(LexicalScopeBinding::Def(..)) => { + report_error(self, ns); + } + Some(LexicalScopeBinding::Item(binding)) => { + let orig_blacklisted_binding = + mem::replace(&mut self.blacklisted_binding, Some(binding)); + if let Some(LexicalScopeBinding::Def(..)) = + self.resolve_ident_in_lexical_scope(ident, ns, None, + use_tree.prefix.span) { + report_error(self, ns); + } + self.blacklisted_binding = orig_blacklisted_binding; + } + None => {} } } } else if let ast::UseTreeKind::Nested(use_trees) = &use_tree.kind { diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index c84dbd29746..fd55897522b 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -223,6 +223,11 @@ impl<'a> Resolver<'a> { } let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| { + if let Some(blacklisted_binding) = this.blacklisted_binding { + if ptr::eq(binding, blacklisted_binding) { + return Err((Determined, Weak::No)); + } + } // `extern crate` are always usable for backwards compatibility, see issue #37020, // remove this together with `PUB_USE_OF_PRIVATE_EXTERN_CRATE`. let usable = this.is_accessible(binding.vis) || binding.is_extern_crate(); diff --git a/src/test/ui/imports/issue-56125.stderr b/src/test/ui/imports/issue-56125.stderr index 13d4068b562..844962b910a 100644 --- a/src/test/ui/imports/issue-56125.stderr +++ b/src/test/ui/imports/issue-56125.stderr @@ -43,7 +43,7 @@ LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous = note: `issue_56125` could refer to an extern crate passed with `--extern` = help: use `::issue_56125` to refer to this extern crate unambiguously note: `issue_56125` could also refer to the module imported here - --> $DIR/issue-56125.rs:17:9 + --> $DIR/issue-56125.rs:18:9 | LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/proc-macro/derive-helper-shadowing.rs b/src/test/ui/proc-macro/derive-helper-shadowing.rs index aa9eae0ba31..f6fe9f9fd8b 100644 --- a/src/test/ui/proc-macro/derive-helper-shadowing.rs +++ b/src/test/ui/proc-macro/derive-helper-shadowing.rs @@ -5,6 +5,26 @@ use derive_helper_shadowing::*; #[my_attr] //~ ERROR `my_attr` is ambiguous #[derive(MyTrait)] -struct S; +struct S { + // FIXME No ambiguity, attributes in non-macro positions are not resolved properly + #[my_attr] + field: [u8; { + // FIXME No ambiguity, derive helpers are not put into scope for non-attributes + use my_attr; -fn main() {} + // FIXME No ambiguity, derive helpers are not put into scope for inner items + #[my_attr] + struct U; + + mod inner { + #[my_attr] //~ ERROR attribute `my_attr` is currently unknown + struct V; + } + + 0 + }] +} + +fn main() { + let s = S { field: [] }; +} diff --git a/src/test/ui/proc-macro/derive-helper-shadowing.stderr b/src/test/ui/proc-macro/derive-helper-shadowing.stderr index cc50fefc464..8180c84d3f6 100644 --- a/src/test/ui/proc-macro/derive-helper-shadowing.stderr +++ b/src/test/ui/proc-macro/derive-helper-shadowing.stderr @@ -1,3 +1,11 @@ +error[E0658]: The attribute `my_attr` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642) + --> $DIR/derive-helper-shadowing.rs:20:15 + | +LL | #[my_attr] //~ ERROR attribute `my_attr` is currently unknown + | ^^^^^^^ + | + = help: add #![feature(custom_attribute)] to the crate attributes to enable + error[E0659]: `my_attr` is ambiguous (derive helper attribute vs any other name) --> $DIR/derive-helper-shadowing.rs:6:3 | @@ -16,6 +24,7 @@ LL | use derive_helper_shadowing::*; | ^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: use `crate::my_attr` to refer to this attribute macro unambiguously -error: aborting due to previous error +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0659`. +Some errors occurred: E0658, E0659. +For more information about an error, try `rustc --explain E0658`. diff --git a/src/test/ui/rust-2018/future-proofing-locals.rs b/src/test/ui/rust-2018/future-proofing-locals.rs index 5c377dda7c6..1e53c2d1dac 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.rs +++ b/src/test/ui/rust-2018/future-proofing-locals.rs @@ -16,7 +16,7 @@ fn type_param() { } fn self_import() { - use T; // FIXME Should be an error, but future-proofing fails due to `T` being "self-shadowed" + use T; //~ ERROR imports cannot refer to type parameters } fn let_binding() { diff --git a/src/test/ui/rust-2018/future-proofing-locals.stderr b/src/test/ui/rust-2018/future-proofing-locals.stderr index 68354b332a9..413e199cd64 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.stderr +++ b/src/test/ui/rust-2018/future-proofing-locals.stderr @@ -16,6 +16,12 @@ error: imports cannot refer to type parameters LL | use T::*; //~ ERROR imports cannot refer to type parameters | ^ +error: imports cannot refer to type parameters + --> $DIR/future-proofing-locals.rs:19:9 + | +LL | use T; //~ ERROR imports cannot refer to type parameters + | ^ + error: imports cannot refer to local variables --> $DIR/future-proofing-locals.rs:25:9 | @@ -46,5 +52,5 @@ error: imports cannot refer to local variables LL | use {T as _, x}; //~ ERROR imports cannot refer to type parameters | ^ -error: aborting due to 8 previous errors +error: aborting due to 9 previous errors