Skip to content

Commit

Permalink
Fix a hole in generic parameter import future-proofing
Browse files Browse the repository at this point in the history
Add some tests for buggy derive helpers
  • Loading branch information
petrochenkov committed Jan 12, 2019
1 parent 79134c0 commit 250935d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/librustc_resolve/lib.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/issue-56125.stderr
Expand Up @@ -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
| ^^^^^^^^^^^^^^
Expand Down
24 changes: 22 additions & 2 deletions src/test/ui/proc-macro/derive-helper-shadowing.rs
Expand Up @@ -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: [] };
}
13 changes: 11 additions & 2 deletions 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
|
Expand All @@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/rust-2018/future-proofing-locals.rs
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/rust-2018/future-proofing-locals.stderr
Expand Up @@ -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
|
Expand Down Expand Up @@ -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

0 comments on commit 250935d

Please sign in to comment.