diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:42 +0000 |
commit | cec1877e180393eba0f6ddb0cf97bf3a791631c7 (patch) | |
tree | 47b4dac2a9dd9a40c30c251b4d4a72d7ccf77e9f /compiler/rustc_ast | |
parent | Adding debian version 1.74.1+dfsg1-1. (diff) | |
download | rustc-cec1877e180393eba0f6ddb0cf97bf3a791631c7.tar.xz rustc-cec1877e180393eba0f6ddb0cf97bf3a791631c7.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast')
-rw-r--r-- | compiler/rustc_ast/Cargo.toml | 6 | ||||
-rw-r--r-- | compiler/rustc_ast/src/ast.rs | 113 | ||||
-rw-r--r-- | compiler/rustc_ast/src/attr/mod.rs | 12 | ||||
-rw-r--r-- | compiler/rustc_ast/src/entry.rs | 29 | ||||
-rw-r--r-- | compiler/rustc_ast/src/lib.rs | 8 | ||||
-rw-r--r-- | compiler/rustc_ast/src/mut_visit.rs | 18 | ||||
-rw-r--r-- | compiler/rustc_ast/src/token.rs | 46 | ||||
-rw-r--r-- | compiler/rustc_ast/src/tokenstream.rs | 4 | ||||
-rw-r--r-- | compiler/rustc_ast/src/util/classify.rs | 2 | ||||
-rw-r--r-- | compiler/rustc_ast/src/util/parser.rs | 4 | ||||
-rw-r--r-- | compiler/rustc_ast/src/visit.rs | 8 |
11 files changed, 154 insertions, 96 deletions
diff --git a/compiler/rustc_ast/Cargo.toml b/compiler/rustc_ast/Cargo.toml index f0632ac92..59e3d8558 100644 --- a/compiler/rustc_ast/Cargo.toml +++ b/compiler/rustc_ast/Cargo.toml @@ -3,9 +3,8 @@ name = "rustc_ast" version = "0.0.0" edition = "2021" -[lib] - [dependencies] +# tidy-alphabetical-start bitflags = "1.2.1" memchr = "2.5.0" rustc_data_structures = { path = "../rustc_data_structures" } @@ -14,6 +13,9 @@ rustc_lexer = { path = "../rustc_lexer" } rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } +# For Mutability and Movability, which could be uplifted into a common crate. +rustc_type_ir = { path = "../rustc_type_ir" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } thin-vec = "0.2.12" tracing = "0.1" +# tidy-alphabetical-end diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index e8cbd7b69..c85ff6f5c 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -34,6 +34,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; +pub use rustc_type_ir::{Movability, Mutability}; use std::fmt; use std::mem; use thin_vec::{thin_vec, ThinVec}; @@ -733,6 +734,8 @@ pub enum RangeSyntax { } /// All the different flavors of pattern that Rust recognizes. +// +// Adding a new variant? Please update `test_pat` in `tests/ui/macros/stringify.rs`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum PatKind { /// Represents a wildcard pattern (`_`). @@ -800,57 +803,6 @@ pub enum PatKind { MacCall(P<MacCall>), } -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] -#[derive(HashStable_Generic, Encodable, Decodable)] -pub enum Mutability { - // N.B. Order is deliberate, so that Not < Mut - Not, - Mut, -} - -impl Mutability { - pub fn invert(self) -> Self { - match self { - Mutability::Mut => Mutability::Not, - Mutability::Not => Mutability::Mut, - } - } - - /// Returns `""` (empty string) or `"mut "` depending on the mutability. - pub fn prefix_str(self) -> &'static str { - match self { - Mutability::Mut => "mut ", - Mutability::Not => "", - } - } - - /// Returns `"&"` or `"&mut "` depending on the mutability. - pub fn ref_prefix_str(self) -> &'static str { - match self { - Mutability::Not => "&", - Mutability::Mut => "&mut ", - } - } - - /// Returns `""` (empty string) or `"mutably "` depending on the mutability. - pub fn mutably_str(self) -> &'static str { - match self { - Mutability::Not => "", - Mutability::Mut => "mutably ", - } - } - - /// Return `true` if self is mutable - pub fn is_mut(self) -> bool { - matches!(self, Self::Mut) - } - - /// Return `true` if self is **not** mutable - pub fn is_not(self) -> bool { - matches!(self, Self::Not) - } -} - /// The kind of borrow in an `AddrOf` expression, /// e.g., `&place` or `&raw const place`. #[derive(Clone, Copy, PartialEq, Eq, Debug)] @@ -1017,6 +969,7 @@ impl Stmt { } } +// Adding a new variant? Please update `test_stmt` in `tests/ui/macros/stringify.rs`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum StmtKind { /// A local (let) binding. @@ -1282,7 +1235,7 @@ impl Expr { ExprKind::Closure(..) => ExprPrecedence::Closure, ExprKind::Block(..) => ExprPrecedence::Block, ExprKind::TryBlock(..) => ExprPrecedence::TryBlock, - ExprKind::Async(..) => ExprPrecedence::Async, + ExprKind::Gen(..) => ExprPrecedence::Gen, ExprKind::Await(..) => ExprPrecedence::Await, ExprKind::Assign(..) => ExprPrecedence::Assign, ExprKind::AssignOp(..) => ExprPrecedence::AssignOp, @@ -1395,6 +1348,7 @@ pub struct StructExpr { pub rest: StructRest, } +// Adding a new variant? Please update `test_expr` in `tests/ui/macros/stringify.rs`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum ExprKind { /// An array (`[a, b, c, d]`) @@ -1451,11 +1405,9 @@ pub enum ExprKind { Closure(Box<Closure>), /// A block (`'label: { ... }`). Block(P<Block>, Option<Label>), - /// An async block (`async move { ... }`). - /// - /// The async block used to have a `NodeId`, which was removed in favor of - /// using the parent `NodeId` of the parent `Expr`. - Async(CaptureBy, P<Block>), + /// An `async` block (`async move { ... }`), + /// or a `gen` block (`gen move { ... }`) + Gen(CaptureBy, P<Block>, GenBlockKind), /// An await expression (`my_future.await`). Span is of await keyword. Await(P<Expr>, Span), @@ -1545,6 +1497,28 @@ pub enum ExprKind { Err, } +/// Used to differentiate between `async {}` blocks and `gen {}` blocks. +#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)] +pub enum GenBlockKind { + Async, + Gen, +} + +impl fmt::Display for GenBlockKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.modifier().fmt(f) + } +} + +impl GenBlockKind { + pub fn modifier(&self) -> &'static str { + match self { + GenBlockKind::Async => "async", + GenBlockKind::Gen => "gen", + } + } +} + /// The explicit `Self` type in a "qualified path". The actual /// path, including the trait and the associated item, is stored /// separately. `position` represents the index of the associated @@ -1574,22 +1548,14 @@ pub struct QSelf { #[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] pub enum CaptureBy { /// `move |x| y + x`. - Value, + Value { + /// The span of the `move` keyword. + move_kw: Span, + }, /// `move` keyword was not specified. Ref, } -/// The movability of a generator / closure literal: -/// whether a generator contains self-references, causing it to be `!Unpin`. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable, Debug, Copy)] -#[derive(HashStable_Generic)] -pub enum Movability { - /// May contain self-references, `!Unpin`. - Static, - /// Must not contain self-references, `Unpin`. - Movable, -} - /// Closure lifetime binder, `for<'a, 'b>` in `for<'a, 'b> |_: &'a (), _: &'b ()|`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum ClosureBinder { @@ -2076,6 +2042,8 @@ pub struct BareFnTy { } /// The various kinds of type recognized by the compiler. +// +// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum TyKind { /// A variable-length slice (`[T]`). @@ -2418,6 +2386,12 @@ pub enum Async { No, } +#[derive(Copy, Clone, Encodable, Decodable, Debug)] +pub enum Gen { + Yes { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId }, + No, +} + impl Async { pub fn is_async(self) -> bool { matches!(self, Async::Yes { .. }) @@ -2941,6 +2915,7 @@ pub struct ConstItem { pub expr: Option<P<Expr>>, } +// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`. #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index db008ea13..be7d1b207 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -197,10 +197,10 @@ impl Attribute { .unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}")) .to_attr_token_stream() .to_tokenstream(), - &AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token( - Token::new(token::DocComment(comment_kind, self.style, data), self.span), - Spacing::Alone, - )]), + &AttrKind::DocComment(comment_kind, data) => TokenStream::token_alone( + token::DocComment(comment_kind, self.style, data), + self.span, + ), } } } @@ -520,9 +520,7 @@ impl NestedMetaItem { I: Iterator<Item = &'a TokenTree>, { match tokens.peek() { - Some(TokenTree::Token(token, _)) - if let Some(lit) = MetaItemLit::from_token(token) => - { + Some(TokenTree::Token(token, _)) if let Some(lit) = MetaItemLit::from_token(token) => { tokens.next(); return Some(NestedMetaItem::Lit(lit)); } diff --git a/compiler/rustc_ast/src/entry.rs b/compiler/rustc_ast/src/entry.rs index 337014619..cd8a5a669 100644 --- a/compiler/rustc_ast/src/entry.rs +++ b/compiler/rustc_ast/src/entry.rs @@ -1,3 +1,7 @@ +use crate::{attr, Attribute}; +use rustc_span::symbol::sym; +use rustc_span::Symbol; + #[derive(Debug)] pub enum EntryPointType { None, @@ -6,3 +10,28 @@ pub enum EntryPointType { Start, OtherMain, // Not an entry point, but some other function named main } + +pub fn entry_point_type( + attrs: &[Attribute], + at_root: bool, + name: Option<Symbol>, +) -> EntryPointType { + if attr::contains_name(attrs, sym::start) { + EntryPointType::Start + } else if attr::contains_name(attrs, sym::rustc_main) { + EntryPointType::RustcMainAttr + } else { + if let Some(name) = name + && name == sym::main + { + if at_root { + // This is a top-level function so it can be `main`. + EntryPointType::MainNamed + } else { + EntryPointType::OtherMain + } + } else { + EntryPointType::None + } + } +} diff --git a/compiler/rustc_ast/src/lib.rs b/compiler/rustc_ast/src/lib.rs index b07ed1d1c..c1f6ad6a2 100644 --- a/compiler/rustc_ast/src/lib.rs +++ b/compiler/rustc_ast/src/lib.rs @@ -8,6 +8,9 @@ html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/", test(attr(deny(warnings))) )] +#![cfg_attr(not(bootstrap), doc(rust_logo))] +#![cfg_attr(not(bootstrap), allow(internal_features))] +#![cfg_attr(not(bootstrap), feature(rustdoc_internals))] #![feature(associated_type_bounds)] #![feature(box_patterns)] #![feature(const_trait_impl)] @@ -50,14 +53,15 @@ pub mod visit; pub use self::ast::*; pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasSpan, HasTokens}; -pub use self::format::*; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in `rustc_middle`. -pub trait HashStableContext: rustc_span::HashStableContext { +pub trait HashStableContext: + rustc_type_ir::HashStableContext + rustc_span::HashStableContext +{ fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher); } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index ba2887146..7c0a78253 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -302,6 +302,10 @@ pub trait MutVisitor: Sized { fn visit_format_args(&mut self, fmt: &mut FormatArgs) { noop_visit_format_args(fmt, self) } + + fn visit_capture_by(&mut self, capture_by: &mut CaptureBy) { + noop_visit_capture_by(capture_by, self) + } } /// Use a map-style function (`FnOnce(T) -> T`) to overwrite a `&mut T`. Useful @@ -1397,7 +1401,7 @@ pub fn noop_visit_expr<T: MutVisitor>( } ExprKind::Closure(box Closure { binder, - capture_clause: _, + capture_clause, constness, asyncness, movability: _, @@ -1409,6 +1413,7 @@ pub fn noop_visit_expr<T: MutVisitor>( vis.visit_closure_binder(binder); visit_constness(constness, vis); vis.visit_asyncness(asyncness); + vis.visit_capture_by(capture_clause); vis.visit_fn_decl(fn_decl); vis.visit_expr(body); vis.visit_span(fn_decl_span); @@ -1418,7 +1423,7 @@ pub fn noop_visit_expr<T: MutVisitor>( vis.visit_block(blk); visit_opt(label, |label| vis.visit_label(label)); } - ExprKind::Async(_capture_by, body) => { + ExprKind::Gen(_capture_by, body, _) => { vis.visit_block(body); } ExprKind::Await(expr, await_kw_span) => { @@ -1562,6 +1567,15 @@ pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) { vis.visit_span(&mut visibility.span); } +pub fn noop_visit_capture_by<T: MutVisitor>(capture_by: &mut CaptureBy, vis: &mut T) { + match capture_by { + CaptureBy::Ref => {} + CaptureBy::Value { move_kw } => { + vis.visit_span(move_kw); + } + } +} + /// Some value for the AST node that is valid but possibly meaningless. pub trait DummyAstNode { fn dummy() -> Self; diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index 300b1486f..914c97a14 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -107,13 +107,11 @@ impl Lit { /// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation. pub fn from_token(token: &Token) -> Option<Lit> { match token.uninterpolate().kind { - Ident(name, false) if name.is_bool_lit() => { - Some(Lit::new(Bool, name, None)) - } + Ident(name, false) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)), Literal(token_lit) => Some(token_lit), Interpolated(ref nt) if let NtExpr(expr) | NtLiteral(expr) = &**nt - && let ast::ExprKind::Lit(token_lit) = expr.kind => + && let ast::ExprKind::Lit(token_lit) = expr.kind => { Some(token_lit) } @@ -199,6 +197,7 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: bool) -> bool { kw::Continue, kw::False, kw::For, + kw::Gen, kw::If, kw::Let, kw::Loop, @@ -229,35 +228,61 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool { #[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)] pub enum TokenKind { /* Expression-operator symbols. */ + /// `=` Eq, + /// `<` Lt, + /// `<=` Le, + /// `==` EqEq, + /// `!=` Ne, + /// `>` Ge, + /// `>=` Gt, + /// `&&` AndAnd, + /// `||` OrOr, + /// `!` Not, + /// `~` Tilde, BinOp(BinOpToken), BinOpEq(BinOpToken), /* Structural symbols */ + /// `@` At, + /// `.` Dot, + /// `..` DotDot, + /// `...` DotDotDot, + /// `..=` DotDotEq, + /// `,` Comma, + /// `;` Semi, + /// `:` Colon, + /// `::` ModSep, + /// `->` RArrow, + /// `<-` LArrow, + /// `=>` FatArrow, + /// `#` Pound, + /// `$` Dollar, + /// `?` Question, /// Used by proc macros for representing lifetimes, not generated by lexer right now. SingleQuote, @@ -296,6 +321,7 @@ pub enum TokenKind { /// similarly to symbols in string literal tokens. DocComment(CommentKind, ast::AttrStyle, Symbol), + /// End Of File Eof, } @@ -404,7 +430,7 @@ impl Token { [DotDot, DotDotDot, DotDotEq].contains(&self.kind) } - pub fn is_op(&self) -> bool { + pub fn is_punct(&self) -> bool { match self.kind { Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp(_) | BinOpEq(_) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon @@ -446,7 +472,7 @@ impl Token { } } - /// Returns `true` if the token can appear at the start of an pattern. + /// Returns `true` if the token can appear at the start of a pattern. /// /// Shamelessly borrowed from `can_begin_expr`, only used for diagnostics right now. pub fn can_begin_pattern(&self) -> bool { @@ -628,7 +654,9 @@ impl Token { /// Returns `true` if the token is an interpolated path. fn is_path(&self) -> bool { - if let Interpolated(nt) = &self.kind && let NtPath(..) = **nt { + if let Interpolated(nt) = &self.kind + && let NtPath(..) = **nt + { return true; } @@ -650,7 +678,9 @@ impl Token { /// Is the token an interpolated block (`$b:block`)? pub fn is_whole_block(&self) -> bool { - if let Interpolated(nt) = &self.kind && let NtBlock(..) = **nt { + if let Interpolated(nt) = &self.kind + && let NtBlock(..) = **nt + { return true; } diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 1e18b1232..23b8f9c12 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -550,7 +550,9 @@ impl TokenStream { let stream_iter = stream.0.iter().cloned(); - if let Some(first) = stream.0.first() && Self::try_glue_to_last(vec_mut, first) { + if let Some(first) = stream.0.first() + && Self::try_glue_to_last(vec_mut, first) + { // Now skip the first token tree from `stream`. vec_mut.extend(stream_iter.skip(1)); } else { diff --git a/compiler/rustc_ast/src/util/classify.rs b/compiler/rustc_ast/src/util/classify.rs index f9f1c0cf9..821fca665 100644 --- a/compiler/rustc_ast/src/util/classify.rs +++ b/compiler/rustc_ast/src/util/classify.rs @@ -46,7 +46,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> { Closure(closure) => { expr = &closure.body; } - Async(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..) + Gen(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..) | TryBlock(..) | While(..) => break Some(expr), _ => break None, } diff --git a/compiler/rustc_ast/src/util/parser.rs b/compiler/rustc_ast/src/util/parser.rs index d3e43e202..13768c120 100644 --- a/compiler/rustc_ast/src/util/parser.rs +++ b/compiler/rustc_ast/src/util/parser.rs @@ -285,7 +285,7 @@ pub enum ExprPrecedence { Block, TryBlock, Struct, - Async, + Gen, Await, Err, } @@ -351,7 +351,7 @@ impl ExprPrecedence { | ExprPrecedence::ConstBlock | ExprPrecedence::Block | ExprPrecedence::TryBlock - | ExprPrecedence::Async + | ExprPrecedence::Gen | ExprPrecedence::Struct | ExprPrecedence::Err => PREC_PAREN, } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index e66c4a9ee..1caa39e2d 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -251,6 +251,9 @@ pub trait Visitor<'ast>: Sized { fn visit_inline_asm_sym(&mut self, sym: &'ast InlineAsmSym) { walk_inline_asm_sym(self, sym) } + fn visit_capture_by(&mut self, _capture_by: &'ast CaptureBy) { + // Nothing to do + } } #[macro_export] @@ -857,7 +860,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { } ExprKind::Closure(box Closure { binder, - capture_clause: _, + capture_clause, asyncness: _, constness: _, movability: _, @@ -866,13 +869,14 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { fn_decl_span: _, fn_arg_span: _, }) => { + visitor.visit_capture_by(capture_clause); visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id) } ExprKind::Block(block, opt_label) => { walk_list!(visitor, visit_label, opt_label); visitor.visit_block(block); } - ExprKind::Async(_, body) => { + ExprKind::Gen(_, body, _) => { visitor.visit_block(body); } ExprKind::Await(expr, _) => visitor.visit_expr(expr), |