Skip to content

Commit 454c600

Browse files
Detect non-lifetime binder params shadowing item params
1 parent 4db3d12 commit 454c600

File tree

6 files changed

+89
-17
lines changed

6 files changed

+89
-17
lines changed

compiler/rustc_resolve/src/late.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2671,17 +2671,17 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26712671
// Store all seen lifetimes names from outer scopes.
26722672
let mut seen_lifetimes = FxHashSet::default();
26732673

2674-
// We also can't shadow bindings from the parent item
2675-
if let RibKind::AssocItem = kind {
2676-
let mut add_bindings_for_ns = |ns| {
2677-
let parent_rib = self.ribs[ns]
2678-
.iter()
2679-
.rfind(|r| matches!(r.kind, RibKind::Item(..)))
2680-
.expect("associated item outside of an item");
2674+
// We also can't shadow bindings from associated parent items.
2675+
for ns in [ValueNS, TypeNS] {
2676+
for parent_rib in self.ribs[ns].iter().rev() {
26812677
seen_bindings.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
2682-
};
2683-
add_bindings_for_ns(ValueNS);
2684-
add_bindings_for_ns(TypeNS);
2678+
2679+
// Break at mod level, to account for nested items which are
2680+
// allowed to shadow generic param names.
2681+
if matches!(parent_rib.kind, RibKind::Module(..)) {
2682+
break;
2683+
}
2684+
}
26852685
}
26862686

26872687
// Forbid shadowing lifetime bindings

tests/crashes/119716-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//@ known-bug: #119716
22
#![feature(non_lifetime_binders)]
33
trait Trait<T> {}
4-
fn f<T>() -> impl for<T> Trait<impl Trait<T>> {}
4+
fn f() -> impl for<T> Trait<impl Trait<T>> {}

tests/ui/const-generics/generic_const_exprs/ice-predicates-of-no-entry-found-for-key-119275.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ fn bug<const N: Nat>(&self)
99
where
1010
for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
1111
//~^ ERROR only lifetime parameters can be used in this context
12-
//~^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
13-
//~^^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
14-
//~^^^^ ERROR failed to resolve: use of undeclared type `COT`
12+
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
13+
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
14+
//~| ERROR failed to resolve: use of undeclared type `COT`
15+
//~| ERROR the name `N` is already used for a generic parameter in this item's generic parameters
1516
{
1617
}
1718

tests/ui/const-generics/generic_const_exprs/ice-predicates-of-no-entry-found-for-key-119275.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ LL | fn bug<const N: Nat>(&self)
66
|
77
= note: associated functions are those in `impl` or `trait` definitions
88

9+
error[E0403]: the name `N` is already used for a generic parameter in this item's generic parameters
10+
--> $DIR/ice-predicates-of-no-entry-found-for-key-119275.rs:10:15
11+
|
12+
LL | fn bug<const N: Nat>(&self)
13+
| - first use of `N`
14+
...
15+
LL | for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
16+
| ^ already used
17+
918
error[E0412]: cannot find type `Nat` in this scope
1019
--> $DIR/ice-predicates-of-no-entry-found-for-key-119275.rs:6:17
1120
|
@@ -40,7 +49,7 @@ error[E0433]: failed to resolve: use of undeclared type `COT`
4049
LL | for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
4150
| ^^^ use of undeclared type `COT`
4251

43-
error: aborting due to 6 previous errors
52+
error: aborting due to 7 previous errors
4453

45-
Some errors have detailed explanations: E0412, E0433, E0658.
46-
For more information about an error, try `rustc --explain E0412`.
54+
Some errors have detailed explanations: E0403, E0412, E0433, E0658.
55+
For more information about an error, try `rustc --explain E0403`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(non_lifetime_binders)]
2+
//~^ WARN the feature `non_lifetime_binders` is incomplete
3+
4+
fn function<T>() where for<T> (): Sized {}
5+
//~^ ERROR the name `T` is already used for a generic parameter
6+
7+
struct Struct<T>(T) where for<T> (): Sized;
8+
//~^ ERROR the name `T` is already used for a generic parameter
9+
10+
impl<T> Struct<T> {
11+
fn method() where for<T> (): Sized {}
12+
//~^ ERROR the name `T` is already used for a generic parameter
13+
}
14+
15+
fn repeated() where for<T, T> (): Sized {}
16+
//~^ ERROR the name `T` is already used for a generic parameter
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
2+
--> $DIR/shadowed.rs:4:28
3+
|
4+
LL | fn function<T>() where for<T> (): Sized {}
5+
| - ^ already used
6+
| |
7+
| first use of `T`
8+
9+
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
10+
--> $DIR/shadowed.rs:7:31
11+
|
12+
LL | struct Struct<T>(T) where for<T> (): Sized;
13+
| - ^ already used
14+
| |
15+
| first use of `T`
16+
17+
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
18+
--> $DIR/shadowed.rs:11:27
19+
|
20+
LL | impl<T> Struct<T> {
21+
| - first use of `T`
22+
LL | fn method() where for<T> (): Sized {}
23+
| ^ already used
24+
25+
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
26+
--> $DIR/shadowed.rs:15:28
27+
|
28+
LL | fn repeated() where for<T, T> (): Sized {}
29+
| - ^ already used
30+
| |
31+
| first use of `T`
32+
33+
warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
34+
--> $DIR/shadowed.rs:1:12
35+
|
36+
LL | #![feature(non_lifetime_binders)]
37+
| ^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
40+
= note: `#[warn(incomplete_features)]` on by default
41+
42+
error: aborting due to 4 previous errors; 1 warning emitted
43+
44+
For more information about this error, try `rustc --explain E0403`.

0 commit comments

Comments
 (0)