Fix a hole in generic parameter import future-proofing

Add some tests for buggy derive helpers
This commit is contained in:
Vadim Petrochenkov 2018-12-30 20:07:43 +03:00
parent 79134c0517
commit 250935d0c7
7 changed files with 68 additions and 12 deletions

View file

@ -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 {

View file

@ -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();

View file

@ -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
| ^^^^^^^^^^^^^^

View file

@ -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: [] };
}

View file

@ -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`.

View file

@ -16,7 +16,7 @@ fn type_param<T>() {
}
fn self_import<T>() {
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() {

View file

@ -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