Mark incorrect recovered char
literals as TyErr
to avoid type errors
This commit is contained in:
parent
e9af312932
commit
a4ff1dcc53
9 changed files with 26 additions and 4 deletions
|
@ -164,6 +164,7 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
|
||||||
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
|
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
|
||||||
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
|
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
|
||||||
Str(value, style),
|
Str(value, style),
|
||||||
|
Err(value),
|
||||||
ByteStr(value),
|
ByteStr(value),
|
||||||
Byte(value),
|
Byte(value),
|
||||||
Char(value),
|
Char(value),
|
||||||
|
|
|
@ -37,6 +37,14 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>(
|
||||||
let id = tcx.allocate_bytes(s.as_bytes());
|
let id = tcx.allocate_bytes(s.as_bytes());
|
||||||
ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64, &tcx)
|
ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64, &tcx)
|
||||||
},
|
},
|
||||||
|
LitKind::Err(ref s) => {
|
||||||
|
let s = s.as_str();
|
||||||
|
let id = tcx.allocate_bytes(s.as_bytes());
|
||||||
|
return Ok(ty::Const {
|
||||||
|
val: ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64, &tcx),
|
||||||
|
ty: tcx.types.err,
|
||||||
|
});
|
||||||
|
},
|
||||||
LitKind::ByteStr(ref data) => {
|
LitKind::ByteStr(ref data) => {
|
||||||
let id = tcx.allocate_bytes(data);
|
let id = tcx.allocate_bytes(data);
|
||||||
ConstValue::Scalar(Scalar::Ptr(id.into()))
|
ConstValue::Scalar(Scalar::Ptr(id.into()))
|
||||||
|
|
|
@ -3121,7 +3121,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
opt_ty.unwrap_or_else(
|
opt_ty.unwrap_or_else(
|
||||||
|| tcx.mk_float_var(self.next_float_var_id()))
|
|| tcx.mk_float_var(self.next_float_var_id()))
|
||||||
}
|
}
|
||||||
ast::LitKind::Bool(_) => tcx.types.bool
|
ast::LitKind::Bool(_) => tcx.types.bool,
|
||||||
|
ast::LitKind::Err(_) => tcx.types.err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1285,6 +1285,8 @@ pub enum LitKind {
|
||||||
FloatUnsuffixed(Symbol),
|
FloatUnsuffixed(Symbol),
|
||||||
/// A boolean literal.
|
/// A boolean literal.
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
|
/// A recovered character literal that contains mutliple `char`s, most likely a typo.
|
||||||
|
Err(Symbol),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LitKind {
|
impl LitKind {
|
||||||
|
@ -1321,6 +1323,7 @@ impl LitKind {
|
||||||
| LitKind::ByteStr(..)
|
| LitKind::ByteStr(..)
|
||||||
| LitKind::Byte(..)
|
| LitKind::Byte(..)
|
||||||
| LitKind::Char(..)
|
| LitKind::Char(..)
|
||||||
|
| LitKind::Err(..)
|
||||||
| LitKind::Int(_, LitIntType::Unsuffixed)
|
| LitKind::Int(_, LitIntType::Unsuffixed)
|
||||||
| LitKind::FloatUnsuffixed(..)
|
| LitKind::FloatUnsuffixed(..)
|
||||||
| LitKind::Bool(..) => true,
|
| LitKind::Bool(..) => true,
|
||||||
|
|
|
@ -661,6 +661,7 @@ impl LitKind {
|
||||||
} else {
|
} else {
|
||||||
"false"
|
"false"
|
||||||
})), false),
|
})), false),
|
||||||
|
LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -466,7 +466,7 @@ crate fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Ha
|
||||||
match lit {
|
match lit {
|
||||||
token::Byte(i) => (true, Some(LitKind::Byte(byte_lit(&i.as_str()).0))),
|
token::Byte(i) => (true, Some(LitKind::Byte(byte_lit(&i.as_str()).0))),
|
||||||
token::Char(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
|
token::Char(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
|
||||||
token::Err(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
|
token::Err(i) => (true, Some(LitKind::Err(i))),
|
||||||
|
|
||||||
// There are some valid suffixes for integer and float literals,
|
// There are some valid suffixes for integer and float literals,
|
||||||
// so all the handling is done internally.
|
// so all the handling is done internally.
|
||||||
|
|
|
@ -473,8 +473,7 @@ impl Token {
|
||||||
|
|
||||||
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
|
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
|
||||||
DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
|
DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
|
||||||
Question | OpenDelim(..) | CloseDelim(..) => return None,
|
Question | OpenDelim(..) | CloseDelim(..) |
|
||||||
|
|
||||||
Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
|
Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
|
||||||
Whitespace | Comment | Shebang(..) | Eof => return None,
|
Whitespace | Comment | Shebang(..) | Eof => return None,
|
||||||
})
|
})
|
||||||
|
|
|
@ -604,6 +604,14 @@ pub trait PrintState<'a> {
|
||||||
}
|
}
|
||||||
match lit.node {
|
match lit.node {
|
||||||
ast::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
|
ast::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
|
||||||
|
ast::LitKind::Err(st) => {
|
||||||
|
let st = st.as_str().escape_debug();
|
||||||
|
let mut res = String::with_capacity(st.len() + 2);
|
||||||
|
res.push('\'');
|
||||||
|
res.push_str(&st);
|
||||||
|
res.push('\'');
|
||||||
|
self.writer().word(res)
|
||||||
|
}
|
||||||
ast::LitKind::Byte(byte) => {
|
ast::LitKind::Byte(byte) => {
|
||||||
let mut res = String::from("b'");
|
let mut res = String::from("b'");
|
||||||
res.extend(ascii::escape_default(byte).map(|c| c as char));
|
res.extend(ascii::escape_default(byte).map(|c| c as char));
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub fn expand_syntax_ext(
|
||||||
match e.node {
|
match e.node {
|
||||||
ast::ExprKind::Lit(ref lit) => match lit.node {
|
ast::ExprKind::Lit(ref lit) => match lit.node {
|
||||||
ast::LitKind::Str(ref s, _)
|
ast::LitKind::Str(ref s, _)
|
||||||
|
| ast::LitKind::Err(ref s)
|
||||||
| ast::LitKind::Float(ref s, _)
|
| ast::LitKind::Float(ref s, _)
|
||||||
| ast::LitKind::FloatUnsuffixed(ref s) => {
|
| ast::LitKind::FloatUnsuffixed(ref s) => {
|
||||||
accumulator.push_str(&s.as_str());
|
accumulator.push_str(&s.as_str());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue