Mark incorrect recovered char literals as TyErr to avoid type errors

This commit is contained in:
Yuki Okushi 2019-01-20 14:51:54 +09:00
parent e9af312932
commit a4ff1dcc53
9 changed files with 26 additions and 4 deletions

View file

@ -164,6 +164,7 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
Str(value, style),
Err(value),
ByteStr(value),
Byte(value),
Char(value),

View file

@ -37,6 +37,14 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>(
let id = tcx.allocate_bytes(s.as_bytes());
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) => {
let id = tcx.allocate_bytes(data);
ConstValue::Scalar(Scalar::Ptr(id.into()))

View file

@ -3121,7 +3121,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
opt_ty.unwrap_or_else(
|| 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,
}
}

View file

@ -1285,6 +1285,8 @@ pub enum LitKind {
FloatUnsuffixed(Symbol),
/// A boolean literal.
Bool(bool),
/// A recovered character literal that contains mutliple `char`s, most likely a typo.
Err(Symbol),
}
impl LitKind {
@ -1321,6 +1323,7 @@ impl LitKind {
| LitKind::ByteStr(..)
| LitKind::Byte(..)
| LitKind::Char(..)
| LitKind::Err(..)
| LitKind::Int(_, LitIntType::Unsuffixed)
| LitKind::FloatUnsuffixed(..)
| LitKind::Bool(..) => true,

View file

@ -661,6 +661,7 @@ impl LitKind {
} else {
"false"
})), false),
LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
}
}

View file

@ -466,7 +466,7 @@ crate fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Ha
match lit {
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::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,
// so all the handling is done internally.

View file

@ -473,8 +473,7 @@ impl Token {
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
Question | OpenDelim(..) | CloseDelim(..) => return None,
Question | OpenDelim(..) | CloseDelim(..) |
Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
Whitespace | Comment | Shebang(..) | Eof => return None,
})

View file

@ -604,6 +604,14 @@ pub trait PrintState<'a> {
}
match lit.node {
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) => {
let mut res = String::from("b'");
res.extend(ascii::escape_default(byte).map(|c| c as char));

View file

@ -23,6 +23,7 @@ pub fn expand_syntax_ext(
match e.node {
ast::ExprKind::Lit(ref lit) => match lit.node {
ast::LitKind::Str(ref s, _)
| ast::LitKind::Err(ref s)
| ast::LitKind::Float(ref s, _)
| ast::LitKind::FloatUnsuffixed(ref s) => {
accumulator.push_str(&s.as_str());