Skip to content

Opportunistically split != to successfully parse never type #145536

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 11 additions & 6 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,24 +774,29 @@ impl<'a> Parser<'a> {
}
}

/// Eats `+` possibly breaking tokens like `+=` in process.
/// Eats `+` possibly breaking tokens like `+=` in the process.
fn eat_plus(&mut self) -> bool {
self.break_and_eat(exp!(Plus))
}

/// Eats `&` possibly breaking tokens like `&&` in process.
/// Eats `!` possibly breaking tokens like `!=` in the process.
fn eat_bang(&mut self) -> bool {
self.break_and_eat(exp!(Bang))
}

/// Eats `&` possibly breaking tokens like `&&` in the process.
/// Signals an error if `&` is not eaten.
fn expect_and(&mut self) -> PResult<'a, ()> {
if self.break_and_eat(exp!(And)) { Ok(()) } else { self.unexpected() }
}

/// Eats `|` possibly breaking tokens like `||` in process.
/// Eats `|` possibly breaking tokens like `||` in the process.
/// Signals an error if `|` was not eaten.
fn expect_or(&mut self) -> PResult<'a, ()> {
if self.break_and_eat(exp!(Or)) { Ok(()) } else { self.unexpected() }
}

/// Eats `<` possibly breaking tokens like `<<` in process.
/// Eats `<` possibly breaking tokens like `<<` in the process.
fn eat_lt(&mut self) -> bool {
let ate = self.break_and_eat(exp!(Lt));
if ate {
Expand All @@ -802,13 +807,13 @@ impl<'a> Parser<'a> {
ate
}

/// Eats `<` possibly breaking tokens like `<<` in process.
/// Eats `<` possibly breaking tokens like `<<` in the process.
/// Signals an error if `<` was not eaten.
fn expect_lt(&mut self) -> PResult<'a, ()> {
if self.eat_lt() { Ok(()) } else { self.unexpected() }
}

/// Eats `>` possibly breaking tokens like `>>` in process.
/// Eats `>` possibly breaking tokens like `>>` in the process.
/// Signals an error if `>` was not eaten.
fn expect_gt(&mut self) -> PResult<'a, ()> {
if self.break_and_eat(exp!(Gt)) {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,9 @@ impl<'a> Parser<'a> {
} else if let Some(form) = self.parse_range_end() {
self.parse_pat_range_to(form)? // `..=X`, `...X`, or `..X`.
} else if self.eat(exp!(Bang)) {
// Parse `!`
// Ideally we'd use `eat_bang` here to allow us to parse `!=>` as `! =>`. However,
// `break_and_eat` doesn't "reglue" the split-off `=` with any following `>` (since
// that would likely be super fragile and complex).
Copy link
Contributor

Choose a reason for hiding this comment

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

It will probably work if the support is added to break_two_token_op.

self.psess.gated_spans.gate(sym::never_patterns, self.prev_token.span);
PatKind::Never
} else if self.eat_keyword(exp!(Underscore)) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ impl<'a> Parser<'a> {
return Ok(ty);
}

let lo = self.token.span;
let mut impl_dyn_multi = false;
let mut lo = self.token.span;
let kind = if self.check(exp!(OpenParen)) {
self.parse_ty_tuple_or_parens(lo, allow_plus)?
} else if self.eat(exp!(Bang)) {
// Never type `!`
} else if self.eat_bang() {
lo = self.prev_token.span;
TyKind::Never
} else if self.eat(exp!(Star)) {
self.parse_ty_ptr()?
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/parser/token-splitting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Check some places in which we want to split multi-character punctuation.
//@ edition: 2015
//@ check-pass
#![feature(never_type)] // only used inside `bang_eq_never_ty`!
#![expect(bare_trait_objects)]

use std::fmt::Debug;

fn plus_eq_bound() {
// issue: <https://github.com/rust-lang/rust/issues/47856>
struct W<T: Clone + = ()> { t: T }
struct S<T: Clone += ()> { t: T }

// Bare & `dyn`-prefixed trait object types take different paths in the parser.
// Therefore, test both branches.

let _: Debug + = *(&() as &dyn Debug);
let _: Debug += *(&() as &dyn Debug);

let _: dyn Debug + = *(&() as &dyn Debug);
let _: dyn Debug += *(&() as &dyn Debug);

#[cfg(false)] fn w() where Trait + = () {}
#[cfg(false)] fn s() where Trait += () {}
}

fn bang_eq_never_ty(x: !) {
let _: ! = x;
let _: != x;

#[cfg(false)] struct W<const X: ! = { loop {} }>;
#[cfg(false)] struct S<const X: != { loop {} }>;
}
Copy link
Member Author

@fmease fmease Aug 17, 2025

Choose a reason for hiding this comment

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

Maybe add this, too:

#![feature(default_field_values)]

struct X<T> {
    w: fn(T) -> ! = |_| loop {},
    s: fn(T) -> != |_| loop {},
}

Doesn't really matter tho.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's add this one too.


fn main() {}
8 changes: 0 additions & 8 deletions tests/ui/parser/trait-plusequal-splitting.rs

This file was deleted.

Loading