Skip to content

Commit a4ff1dc

Browse files
committed
Mark incorrect recovered char literals as TyErr to avoid type errors
1 parent e9af312 commit a4ff1dc

File tree

9 files changed

+26
-4
lines changed

9 files changed

+26
-4
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
164164
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
165165
impl_stable_hash_for!(enum ::syntax::ast::LitKind {
166166
Str(value, style),
167+
Err(value),
167168
ByteStr(value),
168169
Byte(value),
169170
Char(value),

src/librustc_mir/hair/constant.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ crate fn lit_to_const<'a, 'gcx, 'tcx>(
3737
let id = tcx.allocate_bytes(s.as_bytes());
3838
ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64, &tcx)
3939
},
40+
LitKind::Err(ref s) => {
41+
let s = s.as_str();
42+
let id = tcx.allocate_bytes(s.as_bytes());
43+
return Ok(ty::Const {
44+
val: ConstValue::new_slice(Scalar::Ptr(id.into()), s.len() as u64, &tcx),
45+
ty: tcx.types.err,
46+
});
47+
},
4048
LitKind::ByteStr(ref data) => {
4149
let id = tcx.allocate_bytes(data);
4250
ConstValue::Scalar(Scalar::Ptr(id.into()))

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3121,7 +3121,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
31213121
opt_ty.unwrap_or_else(
31223122
|| tcx.mk_float_var(self.next_float_var_id()))
31233123
}
3124-
ast::LitKind::Bool(_) => tcx.types.bool
3124+
ast::LitKind::Bool(_) => tcx.types.bool,
3125+
ast::LitKind::Err(_) => tcx.types.err,
31253126
}
31263127
}
31273128

src/libsyntax/ast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,8 @@ pub enum LitKind {
12851285
FloatUnsuffixed(Symbol),
12861286
/// A boolean literal.
12871287
Bool(bool),
1288+
/// A recovered character literal that contains mutliple `char`s, most likely a typo.
1289+
Err(Symbol),
12881290
}
12891291

12901292
impl LitKind {
@@ -1321,6 +1323,7 @@ impl LitKind {
13211323
| LitKind::ByteStr(..)
13221324
| LitKind::Byte(..)
13231325
| LitKind::Char(..)
1326+
| LitKind::Err(..)
13241327
| LitKind::Int(_, LitIntType::Unsuffixed)
13251328
| LitKind::FloatUnsuffixed(..)
13261329
| LitKind::Bool(..) => true,

src/libsyntax/attr/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ impl LitKind {
661661
} else {
662662
"false"
663663
})), false),
664+
LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
664665
}
665666
}
666667

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ crate fn lit_token(lit: token::Lit, suf: Option<Symbol>, diag: Option<(Span, &Ha
466466
match lit {
467467
token::Byte(i) => (true, Some(LitKind::Byte(byte_lit(&i.as_str()).0))),
468468
token::Char(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
469-
token::Err(i) => (true, Some(LitKind::Char(char_lit(&i.as_str(), diag).0))),
469+
token::Err(i) => (true, Some(LitKind::Err(i))),
470470

471471
// There are some valid suffixes for integer and float literals,
472472
// so all the handling is done internally.

src/libsyntax/parse/token.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ impl Token {
473473

474474
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
475475
DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
476-
Question | OpenDelim(..) | CloseDelim(..) => return None,
477-
476+
Question | OpenDelim(..) | CloseDelim(..) |
478477
Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
479478
Whitespace | Comment | Shebang(..) | Eof => return None,
480479
})

src/libsyntax/print/pprust.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@ pub trait PrintState<'a> {
604604
}
605605
match lit.node {
606606
ast::LitKind::Str(st, style) => self.print_string(&st.as_str(), style),
607+
ast::LitKind::Err(st) => {
608+
let st = st.as_str().escape_debug();
609+
let mut res = String::with_capacity(st.len() + 2);
610+
res.push('\'');
611+
res.push_str(&st);
612+
res.push('\'');
613+
self.writer().word(res)
614+
}
607615
ast::LitKind::Byte(byte) => {
608616
let mut res = String::from("b'");
609617
res.extend(ascii::escape_default(byte).map(|c| c as char));

src/libsyntax_ext/concat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn expand_syntax_ext(
2323
match e.node {
2424
ast::ExprKind::Lit(ref lit) => match lit.node {
2525
ast::LitKind::Str(ref s, _)
26+
| ast::LitKind::Err(ref s)
2627
| ast::LitKind::Float(ref s, _)
2728
| ast::LitKind::FloatUnsuffixed(ref s) => {
2829
accumulator.push_str(&s.as_str());

0 commit comments

Comments
 (0)