Skip to content

Detect non-lifetime binder params shadowing item params #128357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2671,17 +2671,17 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
// Store all seen lifetimes names from outer scopes.
let mut seen_lifetimes = FxHashSet::default();

// We also can't shadow bindings from the parent item
if let RibKind::AssocItem = kind {
let mut add_bindings_for_ns = |ns| {
let parent_rib = self.ribs[ns]
.iter()
.rfind(|r| matches!(r.kind, RibKind::Item(..)))
.expect("associated item outside of an item");
// We also can't shadow bindings from associated parent items.
for ns in [ValueNS, TypeNS] {
for parent_rib in self.ribs[ns].iter().rev() {
seen_bindings.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
};
add_bindings_for_ns(ValueNS);
add_bindings_for_ns(TypeNS);

// Break at mod level, to account for nested items which are
// allowed to shadow generic param names.
if matches!(parent_rib.kind, RibKind::Module(..)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for lifetime shadowing uses LifetimeRibKind::Item below, but we use RibKind::Module here. The only case where I expect this to matter is nested items, in which case RibKind::Module operates fine enough, but I may be missing some subtle case.

break;
}
}
}

// Forbid shadowing lifetime bindings
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/119716-2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ known-bug: #119716
#![feature(non_lifetime_binders)]
trait Trait<T> {}
fn f<T>() -> impl for<T> Trait<impl Trait<T>> {}
fn f() -> impl for<T> Trait<impl Trait<T>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ fn bug<const N: Nat>(&self)
where
for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
//~^ ERROR only lifetime parameters can be used in this context
//~^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^^^ ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~^^^^ ERROR failed to resolve: use of undeclared type `COT`
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~| ERROR defaults for generic parameters are not allowed in `for<...>` binders
//~| ERROR failed to resolve: use of undeclared type `COT`
//~| ERROR the name `N` is already used for a generic parameter in this item's generic parameters
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ LL | fn bug<const N: Nat>(&self)
|
= note: associated functions are those in `impl` or `trait` definitions

error[E0403]: the name `N` is already used for a generic parameter in this item's generic parameters
--> $DIR/ice-predicates-of-no-entry-found-for-key-119275.rs:10:15
|
LL | fn bug<const N: Nat>(&self)
| - first use of `N`
...
LL | for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
| ^ already used

error[E0412]: cannot find type `Nat` in this scope
--> $DIR/ice-predicates-of-no-entry-found-for-key-119275.rs:6:17
|
Expand Down Expand Up @@ -40,7 +49,7 @@ error[E0433]: failed to resolve: use of undeclared type `COT`
LL | for<const N: usize = 3, T = u32> [(); COT::BYTES]:,
| ^^^ use of undeclared type `COT`

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0412, E0433, E0658.
For more information about an error, try `rustc --explain E0412`.
Some errors have detailed explanations: E0403, E0412, E0433, E0658.
For more information about an error, try `rustc --explain E0403`.
18 changes: 18 additions & 0 deletions tests/ui/traits/non_lifetime_binders/shadowed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(non_lifetime_binders)]
//~^ WARN the feature `non_lifetime_binders` is incomplete

fn function<T>() where for<T> (): Sized {}
//~^ ERROR the name `T` is already used for a generic parameter

struct Struct<T>(T) where for<T> (): Sized;
//~^ ERROR the name `T` is already used for a generic parameter

impl<T> Struct<T> {
fn method() where for<T> (): Sized {}
//~^ ERROR the name `T` is already used for a generic parameter
}

fn repeated() where for<T, T> (): Sized {}
//~^ ERROR the name `T` is already used for a generic parameter
Comment on lines +15 to +16
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one already existed, for the record. Just exercising it for good measure.


fn main() {}
44 changes: 44 additions & 0 deletions tests/ui/traits/non_lifetime_binders/shadowed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
--> $DIR/shadowed.rs:4:28
|
LL | fn function<T>() where for<T> (): Sized {}
| - ^ already used
| |
| first use of `T`

error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
--> $DIR/shadowed.rs:7:31
|
LL | struct Struct<T>(T) where for<T> (): Sized;
| - ^ already used
| |
| first use of `T`

error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
--> $DIR/shadowed.rs:11:27
|
LL | impl<T> Struct<T> {
| - first use of `T`
LL | fn method() where for<T> (): Sized {}
| ^ already used

error[E0403]: the name `T` is already used for a generic parameter in this item's generic parameters
--> $DIR/shadowed.rs:15:28
|
LL | fn repeated() where for<T, T> (): Sized {}
| - ^ already used
| |
| first use of `T`

warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/shadowed.rs:1:12
|
LL | #![feature(non_lifetime_binders)]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 4 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0403`.
Loading