Skip to content

Commit 079f44f

Browse files
committed
Fix a hole in generic parameter import future-proofing
Add some tests for buggy derive helpers
1 parent 19836b2 commit 079f44f

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

src/librustc_resolve/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan};
7575
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
7676

7777
use std::cell::{Cell, RefCell};
78-
use std::{cmp, fmt, iter, ptr};
78+
use std::{cmp, fmt, iter, mem, ptr};
7979
use std::collections::BTreeSet;
8080
use std::mem::replace;
8181
use rustc_data_structures::ptr_key::PtrKey;
@@ -2394,11 +2394,27 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
23942394
ast::UseTreeKind::Simple(..) if segments.len() == 1 => &[TypeNS, ValueNS][..],
23952395
_ => &[TypeNS],
23962396
};
2397+
let report_error = |this: &Self, ns| {
2398+
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
2399+
this.session.span_err(ident.span, &format!("imports cannot refer to {}", what));
2400+
};
2401+
23972402
for &ns in nss {
2398-
if let Some(LexicalScopeBinding::Def(..)) =
2399-
self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) {
2400-
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
2401-
self.session.span_err(ident.span, &format!("imports cannot refer to {}", what));
2403+
match self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) {
2404+
Some(LexicalScopeBinding::Def(..)) => {
2405+
report_error(self, ns);
2406+
}
2407+
Some(LexicalScopeBinding::Item(binding)) => {
2408+
let orig_blacklisted_binding =
2409+
mem::replace(&mut self.blacklisted_binding, Some(binding));
2410+
if let Some(LexicalScopeBinding::Def(..)) =
2411+
self.resolve_ident_in_lexical_scope(ident, ns, None,
2412+
use_tree.prefix.span) {
2413+
report_error(self, ns);
2414+
}
2415+
self.blacklisted_binding = orig_blacklisted_binding;
2416+
}
2417+
None => {}
24022418
}
24032419
}
24042420
} else if let ast::UseTreeKind::Nested(use_trees) = &use_tree.kind {

src/librustc_resolve/resolve_imports.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
228228
}
229229

230230
let check_usable = |this: &mut Self, binding: &'a NameBinding<'a>| {
231+
if let Some(blacklisted_binding) = this.blacklisted_binding {
232+
if ptr::eq(binding, blacklisted_binding) {
233+
return Err((Determined, Weak::No));
234+
}
235+
}
231236
// `extern crate` are always usable for backwards compatibility, see issue #37020,
232237
// remove this together with `PUB_USE_OF_PRIVATE_EXTERN_CRATE`.
233238
let usable = this.is_accessible(binding.vis) || binding.is_extern_crate();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
5555
= note: `issue_56125` could refer to an extern crate passed with `--extern`
5656
= help: use `::issue_56125` to refer to this extern crate unambiguously
5757
note: `issue_56125` could also refer to the module imported here
58-
--> $DIR/issue-56125.rs:17:9
58+
--> $DIR/issue-56125.rs:18:9
5959
|
6060
LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
6161
| ^^^^^^^^^^^^^^

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

Lines changed: 22 additions & 2 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn type_param<T>() {
1717
}
1818

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

2323
fn let_binding() {

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

Lines changed: 7 additions & 1 deletion
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)