Skip to content
Open
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
3 changes: 2 additions & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ impl<'a> PostExpansionVisitor<'a> {
}

for param in params {
if !param.bounds.is_empty() {
if !matches!(param.kind, ast::GenericParamKind::Type { .. }) && !param.bounds.is_empty()
{
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
self.sess.dcx().emit_err(errors::ForbiddenBound { spans });
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4423,6 +4423,13 @@ pub(crate) struct MisspelledKw {
pub is_incorrect_case: bool,
}

#[derive(Diagnostic)]
#[diag("bounds cannot be used in this context")]
pub(crate) struct ForbiddenBound {
#[primary_span]
pub spans: Vec<Span>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum TokenDescription {
ReservedIdentifier,
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,24 @@ impl<'a> Parser<'a> {
self.expect_lt()?;
let params = self.parse_generic_params()?;
self.expect_gt()?;

// Issue 149695
// Deny and remove bounds for type parameters in higher-ranked binders,
// otherwise nested items may have parents not in hir.
let params = params
.into_iter()
.map(|mut param| {
if matches!(param.kind, ast::GenericParamKind::Type { .. })
&& !param.bounds.is_empty()
{
let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
self.dcx().emit_err(errors::ForbiddenBound { spans });
param.bounds.clear();
}
param
})
.collect::<ThinVec<_>>();

// We rely on AST validation to rule out invalid cases: There must not be
// type or const parameters, and parameters must not have bounds.
Ok((params, Some(lo.to(self.prev_token.span))))
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/tests/source/non-lifetime-binders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() where for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized {
fn main() where for<'a, T, const C: usize> [&'a T; C]: Sized {
let x = for<T>
|| {};

Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/tests/target/non-lifetime-binders.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main()
where
for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized,
for<'a, T, const C: usize> [&'a T; C]: Sized,
{
let x = for<T> || {};

Expand Down
14 changes: 14 additions & 0 deletions tests/ui/traits/non_lifetime_binders/bad-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ edition: 2024

#![feature(non_lifetime_binders)]
#![expect(incomplete_features)]

fn produce() -> for<A: A<{ //~ ERROR expected a type, found a trait
//~^ ERROR bounds cannot be used in this context
//~^^ ERROR late-bound type parameter not allowed on trait object types
#[derive(Hash)]
enum A {}
struct A<A>;
}>> Trait {} //~ ERROR cannot find trait `Trait` in this scope

fn main() {}
42 changes: 42 additions & 0 deletions tests/ui/traits/non_lifetime_binders/bad-bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
error: bounds cannot be used in this context
--> $DIR/bad-bounds.rs:6:24
|
LL | fn produce() -> for<A: A<{
| ________________________^
LL | |
LL | |
LL | | #[derive(Hash)]
LL | | enum A {}
LL | | struct A<A>;
LL | | }>> Trait {}
| |__^

error[E0405]: cannot find trait `Trait` in this scope
--> $DIR/bad-bounds.rs:12:5
|
LL | }>> Trait {}
| ^^^^^ not found in this scope

error: late-bound type parameter not allowed on trait object types
--> $DIR/bad-bounds.rs:6:21
|
LL | fn produce() -> for<A: A<{
| ^

error[E0782]: expected a type, found a trait
--> $DIR/bad-bounds.rs:6:17
|
LL | fn produce() -> for<A: A<{
| _________________^
LL | |
LL | |
LL | | #[derive(Hash)]
LL | | enum A {}
LL | | struct A<A>;
LL | | }>> Trait {}
| |_________^

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0405, E0782.
For more information about an error, try `rustc --explain E0405`.
Loading