Auto merge of #124141 - nnethercote:rm-Nonterminal-and-TokenKind-Inte… · rust-lang/rust@f836ae4 (original) (raw)

1

1

`use std::borrow::Cow;

`

2

2

`use std::fmt;

`

3

``

`-

use std::sync::Arc;

`

4

3

``

5

4

`pub use LitKind::*;

`

6

``

`-

pub use Nonterminal::*;

`

7

5

`pub use NtExprKind::*;

`

8

6

`pub use NtPatKind::*;

`

9

7

`pub use TokenKind::*;

`

10

``

`-

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

`

11

8

`use rustc_macros::{Decodable, Encodable, HashStable_Generic};

`

12

9

`use rustc_span::edition::Edition;

`

13

10

`use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};

`

`@@ -16,7 +13,6 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};

`

16

13

`use rustc_span::{Ident, Symbol};

`

17

14

``

18

15

`use crate::ast;

`

19

``

`-

use crate::ptr::P;

`

20

16

`use crate::util::case::Case;

`

21

17

``

22

18

`#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

`

`@@ -34,10 +30,6 @@ pub enum InvisibleOrigin {

`

34

30

`` // Converted from proc_macro::Delimiter in

``

35

31

`` // proc_macro::Delimiter::to_internal, i.e. returned by a proc macro.

``

36

32

`ProcMacro,

`

37

``

-

38

``

`` -

// Converted from TokenKind::Interpolated in

``

39

``

`` -

// TokenStream::flatten_token. Treated similarly to ProcMacro.

``

40

``

`-

FlattenToken,

`

41

33

`}

`

42

34

``

43

35

`impl PartialEq for InvisibleOrigin {

`

`@@ -134,9 +126,7 @@ impl Delimiter {

`

134

126

`match self {

`

135

127

`Delimiter::Parenthesis | Delimiter::Bracket | Delimiter::Brace => false,

`

136

128

`Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) => false,

`

137

``

`-

Delimiter::Invisible(InvisibleOrigin::FlattenToken | InvisibleOrigin::ProcMacro) => {

`

138

``

`-

true

`

139

``

`-

}

`

``

129

`+

Delimiter::Invisible(InvisibleOrigin::ProcMacro) => true,

`

140

130

`}

`

141

131

`}

`

142

132

``

`@@ -337,9 +327,7 @@ impl From for bool {

`

337

327

`}

`

338

328

`}

`

339

329

``

340

``

`` -

// SAFETY: due to the Clone impl below, all fields of all variants other than

``

341

``

`` -

// Interpolated must impl Copy.

``

342

``

`-

#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

`

``

330

`+

#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

`

343

331

`pub enum TokenKind {

`

344

332

`/* Expression-operator symbols. */

`

345

333

`` /// =

``

`@@ -468,21 +456,6 @@ pub enum TokenKind {

`

468

456

`` /// the lifetime metavariable in the macro's RHS.

``

469

457

`NtLifetime(Ident, IdentIsRaw),

`

470

458

``

471

``

`-

/// An embedded AST node, as produced by a macro. This only exists for

`

472

``

`-

/// historical reasons. We'd like to get rid of it, for multiple reasons.

`

473

``

`-

/// - It's conceptually very strange. Saying a token can contain an AST

`

474

``

`-

/// node is like saying, in natural language, that a word can contain a

`

475

``

`-

/// sentence.

`

476

``

`-

/// - It requires special handling in a bunch of places in the parser.

`

477

``

`` -

/// - It prevents Token from implementing Copy.

``

478

``

`-

/// It adds complexity and likely slows things down. Please don't add new

`

479

``

`-

/// occurrences of this token kind!

`

480

``

`-

///

`

481

``

`` -

/// The span in the surrounding Token is that of the metavariable in the

``

482

``

`-

/// macro's RHS. The span within the Nonterminal is that of the fragment

`

483

``

`-

/// passed to the macro at the call site.

`

484

``

`-

Interpolated(Arc),

`

485

``

-

486

459

`/// A doc comment token.

`

487

460

`` /// Symbol is the doc comment's data excluding its "quotes" (///, /**, etc)

``

488

461

`/// similarly to symbols in string literal tokens.

`

`@@ -492,20 +465,7 @@ pub enum TokenKind {

`

492

465

`Eof,

`

493

466

`}

`

494

467

``

495

``

`-

impl Clone for TokenKind {

`

496

``

`-

fn clone(&self) -> Self {

`

497

``

`` -

// TokenKind would impl Copy if it weren't for Interpolated. So

``

498

``

`` -

// for all other variants, this implementation of clone is just like

``

499

``

`` -

// a copy. This is faster than the derive(Clone) version which has a

``

500

``

`-

// separate path for every variant.

`

501

``

`-

match self {

`

502

``

`-

Interpolated(nt) => Interpolated(Arc::clone(nt)),

`

503

``

`-

_ => unsafe { std::ptr::read(self) },

`

504

``

`-

}

`

505

``

`-

}

`

506

``

`-

}

`

507

``

-

508

``

`-

#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

`

``

468

`+

#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

`

509

469

`pub struct Token {

`

510

470

`pub kind: TokenKind,

`

511

471

`pub span: Span,

`

`@@ -600,7 +560,7 @@ impl Token {

`

600

560

` | FatArrow | Pound | Dollar | Question | SingleQuote => true,

`

601

561

``

602

562

`OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)

`

603

``

`-

| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,

`

``

563

`+

| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,

`

604

564

`}

`

605

565

`}

`

606

566

``

`@@ -631,7 +591,6 @@ impl Token {

`

631

591

`PathSep | // global path

`

632

592

`Lifetime(..) | // labeled loop

`

633

593

`Pound => true, // expression attributes

`

634

``

`-

Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),

`

635

594

`OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(

`

636

595

`MetaVarKind::Block |

`

637

596

`MetaVarKind::Expr { .. } |

`

`@@ -703,7 +662,6 @@ impl Token {

`

703

662

`match self.kind {

`

704

663

`OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,

`

705

664

`Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,

`

706

``

`-

Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),

`

707

665

`OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(

`

708

666

`MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,

`

709

667

`))) => true,

`

`@@ -831,31 +789,20 @@ impl Token {

`

831

789

`/// Is this a pre-parsed expression dropped into the token stream

`

832

790

`/// (which happens while parsing the result of macro expansion)?

`

833

791

`pub fn is_metavar_expr(&self) -> bool {

`

834

``

`-

#[allow(irrefutable_let_patterns)] // FIXME: temporary

`

835

``

`-

if let Interpolated(nt) = &self.kind

`

836

``

`-

&& let NtBlock(_) = &**nt

`

837

``

`-

{

`

838

``

`-

true

`

839

``

`-

} else if matches!(

`

``

792

`+

matches!(

`

840

793

`self.is_metavar_seq(),

`

841

``

`-

Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)

`

842

``

`-

) {

`

843

``

`-

true

`

844

``

`-

} else {

`

845

``

`-

matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))

`

846

``

`-

}

`

``

794

`+

Some(

`

``

795

`+

MetaVarKind::Expr { .. }

`

``

796

`+

| MetaVarKind::Literal

`

``

797

`+

| MetaVarKind::Path

`

``

798

`+

| MetaVarKind::Block

`

``

799

`+

)

`

``

800

`+

)

`

847

801

`}

`

848

802

``

849

``

`` -

/// Is the token an interpolated block ($b:block)?

``

850

``

`-

pub fn is_whole_block(&self) -> bool {

`

851

``

`-

#[allow(irrefutable_let_patterns)] // FIXME: temporary

`

852

``

`-

if let Interpolated(nt) = &self.kind

`

853

``

`-

&& let NtBlock(..) = &**nt

`

854

``

`-

{

`

855

``

`-

return true;

`

856

``

`-

}

`

857

``

-

858

``

`-

false

`

``

803

`` +

/// Are we at a block from a metavar ($b:block)?

``

``

804

`+

pub fn is_metavar_block(&self) -> bool {

`

``

805

`+

matches!(self.is_metavar_seq(), Some(MetaVarKind::Block))

`

859

806

`}

`

860

807

``

861

808

`` /// Returns true if the token is either the mut or const keyword.

``

`@@ -1024,7 +971,7 @@ impl Token {

`

1024

971

` | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq

`

1025

972

` | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question

`

1026

973

` | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)

`

1027

``

`-

| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof,

`

``

974

`+

| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof,

`

1028

975

` _,

`

1029

976

`) => {

`

1030

977

`return None;

`

`@@ -1063,12 +1010,6 @@ pub enum NtExprKind {

`

1063

1010

`Expr2021 { inferred: bool },

`

1064

1011

`}

`

1065

1012

``

1066

``

`-

#[derive(Clone, Encodable, Decodable)]

`

1067

``

`-

/// For interpolation during macro expansion.

`

1068

``

`-

pub enum Nonterminal {

`

1069

``

`-

NtBlock(Past::Block),

`

1070

``

`-

}

`

1071

``

-

1072

1013

`#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]

`

1073

1014

`pub enum NonterminalKind {

`

1074

1015

`Item,

`

`@@ -1152,47 +1093,6 @@ impl fmt::Display for NonterminalKind {

`

1152

1093

`}

`

1153

1094

`}

`

1154

1095

``

1155

``

`-

impl Nonterminal {

`

1156

``

`-

pub fn use_span(&self) -> Span {

`

1157

``

`-

match self {

`

1158

``

`-

NtBlock(block) => block.span,

`

1159

``

`-

}

`

1160

``

`-

}

`

1161

``

-

1162

``

`-

pub fn descr(&self) -> &'static str {

`

1163

``

`-

match self {

`

1164

``

`-

NtBlock(..) => "block",

`

1165

``

`-

}

`

1166

``

`-

}

`

1167

``

`-

}

`

1168

``

-

1169

``

`-

impl PartialEq for Nonterminal {

`

1170

``

`-

fn eq(&self, _rhs: &Self) -> bool {

`

1171

``

`-

// FIXME: Assume that all nonterminals are not equal, we can't compare them

`

1172

``

`-

// correctly based on data from AST. This will prevent them from matching each other

`

1173

``

`-

// in macros. The comparison will become possible only when each nonterminal has an

`

1174

``

`-

// attached token stream from which it was parsed.

`

1175

``

`-

false

`

1176

``

`-

}

`

1177

``

`-

}

`

1178

``

-

1179

``

`-

impl fmt::Debug for Nonterminal {

`

1180

``

`-

fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

`

1181

``

`-

match *self {

`

1182

``

`-

NtBlock(..) => f.pad("NtBlock(..)"),

`

1183

``

`-

}

`

1184

``

`-

}

`

1185

``

`-

}

`

1186

``

-

1187

``

`-

impl HashStable for Nonterminal

`

1188

``

`-

where

`

1189

``

`-

CTX: crate::HashStableContext,

`

1190

``

`-

{

`

1191

``

`-

fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {

`

1192

``

`-

panic!("interpolated tokens should not be present in the HIR")

`

1193

``

`-

}

`

1194

``

`-

}

`

1195

``

-

1196

1096

`// Some types are used a lot. Make sure they don't unintentionally get bigger.

`

1197

1097

`#[cfg(target_pointer_width = "64")]

`

1198

1098

`mod size_asserts {

`

`@@ -1202,7 +1102,6 @@ mod size_asserts {

`

1202

1102

`// tidy-alphabetical-start

`

1203

1103

`static_assert_size!(Lit, 12);

`

1204

1104

`static_assert_size!(LitKind, 2);

`

1205

``

`-

static_assert_size!(Nonterminal, 8);

`

1206

1105

`static_assert_size!(Token, 24);

`

1207

1106

`static_assert_size!(TokenKind, 16);

`

1208

1107

`// tidy-alphabetical-end

`