Skip to content

Commit 250935d

Browse files
committed
Fix a hole in generic parameter import future-proofing
Add some tests for buggy derive helpers
1 parent 79134c0 commit 250935d

7 files changed

+68
-12
lines changed

src/librustc_resolve/lib.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6767
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
6868

6969
use std::cell::{Cell, RefCell};
70-
use std::{cmp, fmt, iter, ptr};
70+
use std::{cmp, fmt, iter, mem, ptr};
7171
use std::collections::BTreeSet;
7272
use std::mem::replace;
7373
use rustc_data_structures::ptr_key::PtrKey;
@@ -2375,11 +2375,27 @@ impl<'a> Resolver<'a> {
23752375
ast::UseTreeKind::Simple(..) if segments.len() == 1 => &[TypeNS, ValueNS][..],
23762376
_ => &[TypeNS],
23772377
};
2378+
let report_error = |this: &Self, ns| {
2379+
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
2380+
this.session.span_err(ident.span, &format!("imports cannot refer to {}", what));
2381+
};
2382+
23782383
for &ns in nss {
2379-
if let Some(LexicalScopeBinding::Def(..)) =
2380-
self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) {
2381-
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
2382-
self.session.span_err(ident.span, &format!("imports cannot refer to {}", what));
2384+
match self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) {
2385+
Some(LexicalScopeBinding::Def(..)) => {
2386+
report_error(self, ns);
2387+
}
2388+
Some(LexicalScopeBinding::Item(binding)) => {
2389+
let orig_blacklisted_binding =
2390+
mem::replace(&mut self.blacklisted_binding, Some(binding));
2391+
if let Some(LexicalScopeBinding::Def(..)) =
2392+
self.resolve_ident_in_lexical_scope(ident, ns, None,
2393+
use_tree.prefix.span) {
2394+
report_error(self, ns);
2395+
}
2396+
self.blacklisted_binding = orig_blacklisted_binding;
2397+
}
2398+
None => {}
23832399
}
23842400
}
23852401
} else if let ast::UseTreeKind::Nested(use_trees) = &use_tree.kind {

src/librustc_resolve/resolve_imports.rs

+5
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ impl<'a> Resolver<'a> {
223223
}
224224

225225
let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
226+
if let Some(blacklisted_binding) = this.blacklisted_binding {
227+
if ptr::eq(binding, blacklisted_binding) {
228+
return Err((Determined, Weak::No));
229+
}
230+
}
226231
// `extern crate` are always usable for backwards compatibility, see issue #37020,
227232
// remove this together with `PUB_USE_OF_PRIVATE_EXTERN_CRATE`.
228233
let usable = this.is_accessible(binding.vis) || binding.is_extern_crate();

src/test/ui/imports/issue-56125.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
4343
= note: `issue_56125` could refer to an extern crate passed with `--extern`
4444
= help: use `::issue_56125` to refer to this extern crate unambiguously
4545
note: `issue_56125` could also refer to the module imported here
46-
--> $DIR/issue-56125.rs:17:9
46+
--> $DIR/issue-56125.rs:18:9
4747
|
4848
LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
4949
| ^^^^^^^^^^^^^^

src/test/ui/proc-macro/derive-helper-shadowing.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ use derive_helper_shadowing::*;
55

66
#[my_attr] //~ ERROR `my_attr` is ambiguous
77
#[derive(MyTrait)]
8-
struct S;
8+
struct S {
9+
// FIXME No ambiguity, attributes in non-macro positions are not resolved properly
10+
#[my_attr]
11+
field: [u8; {
12+
// FIXME No ambiguity, derive helpers are not put into scope for non-attributes
13+
use my_attr;
914

10-
fn main() {}
15+
// FIXME No ambiguity, derive helpers are not put into scope for inner items
16+
#[my_attr]
17+
struct U;
18+
19+
mod inner {
20+
#[my_attr] //~ ERROR attribute `my_attr` is currently unknown
21+
struct V;
22+
}
23+
24+
0
25+
}]
26+
}
27+
28+
fn main() {
29+
let s = S { field: [] };
30+
}

src/test/ui/proc-macro/derive-helper-shadowing.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
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)
2+
--> $DIR/derive-helper-shadowing.rs:20:15
3+
|
4+
LL | #[my_attr] //~ ERROR attribute `my_attr` is currently unknown
5+
| ^^^^^^^
6+
|
7+
= help: add #![feature(custom_attribute)] to the crate attributes to enable
8+
19
error[E0659]: `my_attr` is ambiguous (derive helper attribute vs any other name)
210
--> $DIR/derive-helper-shadowing.rs:6:3
311
|
@@ -16,6 +24,7 @@ LL | use derive_helper_shadowing::*;
1624
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1725
= help: use `crate::my_attr` to refer to this attribute macro unambiguously
1826

19-
error: aborting due to previous error
27+
error: aborting due to 2 previous errors
2028

21-
For more information about this error, try `rustc --explain E0659`.
29+
Some errors occurred: E0658, E0659.
30+
For more information about an error, try `rustc --explain E0658`.

src/test/ui/rust-2018/future-proofing-locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn type_param<T>() {
1616
}
1717

1818
fn self_import<T>() {
19-
use T; // FIXME Should be an error, but future-proofing fails due to `T` being "self-shadowed"
19+
use T; //~ ERROR imports cannot refer to type parameters
2020
}
2121

2222
fn let_binding() {

src/test/ui/rust-2018/future-proofing-locals.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ error: imports cannot refer to type parameters
1616
LL | use T::*; //~ ERROR imports cannot refer to type parameters
1717
| ^
1818

19+
error: imports cannot refer to type parameters
20+
--> $DIR/future-proofing-locals.rs:19:9
21+
|
22+
LL | use T; //~ ERROR imports cannot refer to type parameters
23+
| ^
24+
1925
error: imports cannot refer to local variables
2026
--> $DIR/future-proofing-locals.rs:25:9
2127
|
@@ -46,5 +52,5 @@ error: imports cannot refer to local variables
4652
LL | use {T as _, x}; //~ ERROR imports cannot refer to type parameters
4753
| ^
4854

49-
error: aborting due to 8 previous errors
55+
error: aborting due to 9 previous errors
5056

0 commit comments

Comments
 (0)