Rollup merge of #100909 - nnethercote:minor-ast-LitKind-improvement, r=petrochenkov
Minor `ast::LitKind` improvements r? `@petrochenkov`
This commit is contained in:
commit
28ead17745
13 changed files with 21 additions and 21 deletions
|
@ -1751,7 +1751,8 @@ pub enum LitFloatType {
|
||||||
/// E.g., `"foo"`, `42`, `12.34`, or `bool`.
|
/// E.g., `"foo"`, `42`, `12.34`, or `bool`.
|
||||||
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
|
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
|
||||||
pub enum LitKind {
|
pub enum LitKind {
|
||||||
/// A string literal (`"foo"`).
|
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
|
||||||
|
/// from the original token's symbol.
|
||||||
Str(Symbol, StrStyle),
|
Str(Symbol, StrStyle),
|
||||||
/// A byte string (`b"foo"`).
|
/// A byte string (`b"foo"`).
|
||||||
ByteStr(Lrc<[u8]>),
|
ByteStr(Lrc<[u8]>),
|
||||||
|
@ -1761,12 +1762,13 @@ pub enum LitKind {
|
||||||
Char(char),
|
Char(char),
|
||||||
/// An integer literal (`1`).
|
/// An integer literal (`1`).
|
||||||
Int(u128, LitIntType),
|
Int(u128, LitIntType),
|
||||||
/// A float literal (`1f64` or `1E10f64`).
|
/// A float literal (`1f64` or `1E10f64`). Stored as a symbol rather than
|
||||||
|
/// `f64` so that `LitKind` can impl `Eq` and `Hash`.
|
||||||
Float(Symbol, LitFloatType),
|
Float(Symbol, LitFloatType),
|
||||||
/// A boolean literal.
|
/// A boolean literal.
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
/// Placeholder for a literal that wasn't well-formed in some way.
|
/// Placeholder for a literal that wasn't well-formed in some way.
|
||||||
Err(Symbol),
|
Err,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LitKind {
|
impl LitKind {
|
||||||
|
@ -1805,7 +1807,7 @@ impl LitKind {
|
||||||
| LitKind::Int(_, LitIntType::Unsuffixed)
|
| LitKind::Int(_, LitIntType::Unsuffixed)
|
||||||
| LitKind::Float(_, LitFloatType::Unsuffixed)
|
| LitKind::Float(_, LitFloatType::Unsuffixed)
|
||||||
| LitKind::Bool(..)
|
| LitKind::Bool(..)
|
||||||
| LitKind::Err(..) => false,
|
| LitKind::Err => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ impl LitKind {
|
||||||
|
|
||||||
LitKind::ByteStr(bytes.into())
|
LitKind::ByteStr(bytes.into())
|
||||||
}
|
}
|
||||||
token::Err => LitKind::Err(symbol),
|
token::Err => LitKind::Err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +199,7 @@ impl LitKind {
|
||||||
let symbol = if value { kw::True } else { kw::False };
|
let symbol = if value { kw::True } else { kw::False };
|
||||||
(token::Bool, symbol, None)
|
(token::Bool, symbol, None)
|
||||||
}
|
}
|
||||||
LitKind::Err(symbol) => (token::Err, symbol, None),
|
LitKind::Err => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
token::Lit::new(kind, symbol, suffix)
|
token::Lit::new(kind, symbol, suffix)
|
||||||
|
|
|
@ -928,7 +928,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
} else {
|
} else {
|
||||||
Lit {
|
Lit {
|
||||||
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
|
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
|
||||||
kind: LitKind::Err(kw::Empty),
|
kind: LitKind::Err,
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub fn expand_concat(
|
||||||
ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
|
ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..) => {
|
||||||
cx.span_err(e.span, "cannot concatenate a byte string literal");
|
cx.span_err(e.span, "cannot concatenate a byte string literal");
|
||||||
}
|
}
|
||||||
ast::LitKind::Err(_) => {
|
ast::LitKind::Err => {
|
||||||
has_errors = true;
|
has_errors = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -42,7 +42,7 @@ fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_ne
|
||||||
ast::LitKind::Bool(_) => {
|
ast::LitKind::Bool(_) => {
|
||||||
cx.span_err(expr.span, "cannot concatenate boolean literals");
|
cx.span_err(expr.span, "cannot concatenate boolean literals");
|
||||||
}
|
}
|
||||||
ast::LitKind::Err(_) => {}
|
ast::LitKind::Err => {}
|
||||||
ast::LitKind::Int(_, _) if !is_nested => {
|
ast::LitKind::Int(_, _) if !is_nested => {
|
||||||
let mut err = cx.struct_span_err(expr.span, "cannot concatenate numeric literals");
|
let mut err = cx.struct_span_err(expr.span, "cannot concatenate numeric literals");
|
||||||
if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) {
|
if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) {
|
||||||
|
|
|
@ -1227,7 +1227,7 @@ pub fn expr_to_spanned_string<'a>(
|
||||||
);
|
);
|
||||||
Some((err, true))
|
Some((err, true))
|
||||||
}
|
}
|
||||||
ast::LitKind::Err(_) => None,
|
ast::LitKind::Err => None,
|
||||||
_ => Some((cx.struct_span_err(l.span, err_msg), false)),
|
_ => Some((cx.struct_span_err(l.span, err_msg), false)),
|
||||||
},
|
},
|
||||||
ast::ExprKind::Err => None,
|
ast::ExprKind::Err => None,
|
||||||
|
|
|
@ -144,7 +144,7 @@ pub(crate) fn lit_to_mir_constant<'tcx>(
|
||||||
}
|
}
|
||||||
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
|
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
|
||||||
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
|
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
|
||||||
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
|
(ast::LitKind::Err, _) => return Err(LitToConstError::Reported),
|
||||||
_ => return Err(LitToConstError::TypeError),
|
_ => return Err(LitToConstError::TypeError),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub(crate) fn lit_to_const<'tcx>(
|
||||||
}
|
}
|
||||||
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
|
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),
|
||||||
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
|
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
|
||||||
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
|
(ast::LitKind::Err, _) => return Err(LitToConstError::Reported),
|
||||||
_ => return Err(LitToConstError::TypeError),
|
_ => return Err(LitToConstError::TypeError),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1383,7 +1383,7 @@ impl<'a> Parser<'a> {
|
||||||
match self.parse_str_lit() {
|
match self.parse_str_lit() {
|
||||||
Ok(str_lit) => Some(str_lit),
|
Ok(str_lit) => Some(str_lit),
|
||||||
Err(Some(lit)) => match lit.kind {
|
Err(Some(lit)) => match lit.kind {
|
||||||
ast::LitKind::Err(_) => None,
|
ast::LitKind::Err => None,
|
||||||
_ => {
|
_ => {
|
||||||
self.struct_span_err(lit.span, "non-string ABI literal")
|
self.struct_span_err(lit.span, "non-string ABI literal")
|
||||||
.span_suggestion(
|
.span_suggestion(
|
||||||
|
|
|
@ -1136,7 +1136,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
opt_ty.unwrap_or_else(|| self.next_float_var())
|
opt_ty.unwrap_or_else(|| self.next_float_var())
|
||||||
}
|
}
|
||||||
ast::LitKind::Bool(_) => tcx.types.bool,
|
ast::LitKind::Bool(_) => tcx.types.bool,
|
||||||
ast::LitKind::Err(_) => tcx.ty_error(),
|
ast::LitKind::Err => tcx.ty_error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -290,7 +290,7 @@ impl<'a> NormalizedPat<'a> {
|
||||||
LitKind::Char(val) => Self::LitInt(val.into()),
|
LitKind::Char(val) => Self::LitInt(val.into()),
|
||||||
LitKind::Int(val, _) => Self::LitInt(val),
|
LitKind::Int(val, _) => Self::LitInt(val),
|
||||||
LitKind::Bool(val) => Self::LitBool(val),
|
LitKind::Bool(val) => Self::LitBool(val),
|
||||||
LitKind::Float(..) | LitKind::Err(_) => Self::Wild,
|
LitKind::Float(..) | LitKind::Err => Self::Wild,
|
||||||
},
|
},
|
||||||
_ => Self::Wild,
|
_ => Self::Wild,
|
||||||
},
|
},
|
||||||
|
|
|
@ -276,7 +276,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
|
||||||
match lit.value.node {
|
match lit.value.node {
|
||||||
LitKind::Bool(val) => kind!("Bool({val:?})"),
|
LitKind::Bool(val) => kind!("Bool({val:?})"),
|
||||||
LitKind::Char(c) => kind!("Char({c:?})"),
|
LitKind::Char(c) => kind!("Char({c:?})"),
|
||||||
LitKind::Err(val) => kind!("Err({val})"),
|
LitKind::Err => kind!("Err"),
|
||||||
LitKind::Byte(b) => kind!("Byte({b})"),
|
LitKind::Byte(b) => kind!("Byte({b})"),
|
||||||
LitKind::Int(i, suffix) => {
|
LitKind::Int(i, suffix) => {
|
||||||
let int_ty = match suffix {
|
let int_ty = match suffix {
|
||||||
|
|
|
@ -45,7 +45,7 @@ pub enum Constant {
|
||||||
/// A reference
|
/// A reference
|
||||||
Ref(Box<Constant>),
|
Ref(Box<Constant>),
|
||||||
/// A literal with syntax error.
|
/// A literal with syntax error.
|
||||||
Err(Symbol),
|
Err,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Constant {
|
impl PartialEq for Constant {
|
||||||
|
@ -118,9 +118,7 @@ impl Hash for Constant {
|
||||||
Self::Ref(ref r) => {
|
Self::Ref(ref r) => {
|
||||||
r.hash(state);
|
r.hash(state);
|
||||||
},
|
},
|
||||||
Self::Err(ref s) => {
|
Self::Err => {},
|
||||||
s.hash(state);
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,7 +192,7 @@ pub fn lit_to_mir_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
|
||||||
_ => bug!(),
|
_ => bug!(),
|
||||||
},
|
},
|
||||||
LitKind::Bool(b) => Constant::Bool(b),
|
LitKind::Bool(b) => Constant::Bool(b),
|
||||||
LitKind::Err(s) => Constant::Err(s),
|
LitKind::Err => Constant::Err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue