//! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
//! - [`EnumDef`] and [`Variant`]: Enum declaration.
-//! - [`Lit`] and [`LitKind`]: Literal expressions.
+//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions.
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation.
//! - [`Attribute`]: Metadata associated with item.
//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
}
impl Path {
- // Convert a span and an identifier to the corresponding
- // one-segment path.
+ /// Convert a span and an identifier to the corresponding
+ /// one-segment path.
pub fn from_ident(ident: Ident) -> Path {
Path { segments: thin_vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
}
/// A literal.
///
/// E.g., `"foo"`, `64`, `true`.
- Literal(Lit),
+ Lit(MetaItemLit),
}
/// A spanned compile-time attribute item.
/// Name value meta item.
///
/// E.g., `feature = "foo"` as in `#[feature = "foo"]`.
- NameValue(Lit),
+ NameValue(MetaItemLit),
}
/// A block (`{ .. }`).
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub enum Mutability {
- Mut,
+ // N.B. Order is deliberate, so that Not < Mut
Not,
+ Mut,
}
impl Mutability {
}
}
- pub fn prefix_str(&self) -> &'static str {
+ /// 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,
)
}
- // To a first-order approximation, is this a pattern
+ /// To a first-order approximation, is this a pattern?
pub fn is_approximately_pattern(&self) -> bool {
match &self.peel_parens().kind {
ExprKind::Box(_)
/// Conditionless loop (can be exited with `break`, `continue`, or `return`).
///
/// `'label: loop { block }`
- Loop(P<Block>, Option<Label>),
+ Loop(P<Block>, Option<Label>, Span),
/// A `match` block.
Match(P<Expr>, Vec<Arm>),
/// A closure (e.g., `move |a, b, c| a + b + c`).
}
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
-// expansion is completed, all cases end up either as a literal, which is the
-// form used after lowering to HIR, or as an error.
+// expansion is completed, all cases end up either as a meta item literal,
+// which is the form used after lowering to HIR, or as an error.
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum AttrArgsEq {
Ast(P<Expr>),
- Hir(Lit),
+ Hir(MetaItemLit),
}
impl AttrArgs {
Raw(u8),
}
-/// An AST literal.
+/// A literal in a meta item.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
-pub struct Lit {
+pub struct MetaItemLit {
/// The original literal token as written in source code.
pub token_lit: token::Lit,
/// The "semantic" representation of the literal lowered from the original tokens.
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
- /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
pub kind: LitKind,
pub span: Span,
}
-/// Same as `Lit`, but restricted to string literals.
+/// Similar to `MetaItemLit`, but restricted to string literals.
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
pub struct StrLit {
/// The original literal token as written in source code.
pub suffix: Option<Symbol>,
pub span: Span,
/// The unescaped "semantic" representation of the literal lowered from the original token.
- /// FIXME: Remove this and only create the semantic representation during lowering to HIR.
pub symbol_unescaped: Symbol,
}
Unsuffixed,
}
+/// This type is used within both `ast::MetaItemLit` and `hir::Lit`.
+///
/// Note that the entire literal (including the suffix) is considered when
/// deciding the `LitKind`. This means that float literals like `1f32` are
/// classified by this type as `Float`. This is different to `token::LitKind`
}
/// Return the `NodeId` of this variant's constructor, if it has one.
- pub fn ctor_id(&self) -> Option<NodeId> {
+ pub fn ctor_node_id(&self) -> Option<NodeId> {
match *self {
VariantData::Struct(..) => None,
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
static_assert_size!(Impl, 184);
static_assert_size!(Item, 184);
static_assert_size!(ItemKind, 112);
- static_assert_size!(Lit, 48);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 72);
+ static_assert_size!(MetaItemLit, 48);
static_assert_size!(Param, 40);
static_assert_size!(Pat, 88);
static_assert_size!(Path, 24);