Skip to content

Commit 2ab5d8a

Browse files
committed
Auto merge of rust-lang#57651 - JohnTitor:give-char-type, r=estebank
Implement new literal type `Err` Fixes rust-lang#57384 I removed `return Ok`, otherwise, two errors occur. Any solutions? r? @estebank
2 parents 588f94b + 4005d3a commit 2ab5d8a

25 files changed

+165
-58
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 2 additions & 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),
@@ -329,6 +330,7 @@ fn hash_token<'a, 'gcx, W: StableHasherResult>(
329330
match *lit {
330331
token::Lit::Byte(val) |
331332
token::Lit::Char(val) |
333+
token::Lit::Err(val) |
332334
token::Lit::Integer(val) |
333335
token::Lit::Float(val) |
334336
token::Lit::Str_(val) |

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/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'a> Classifier<'a> {
296296
token::Literal(lit, _suf) => {
297297
match lit {
298298
// Text literals.
299-
token::Byte(..) | token::Char(..) |
299+
token::Byte(..) | token::Char(..) | token::Err(..) |
300300
token::ByteStr(..) | token::ByteStrRaw(..) |
301301
token::Str_(..) | token::StrRaw(..) => Class::String,
302302

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
@@ -666,6 +666,7 @@ impl LitKind {
666666
} else {
667667
"false"
668668
})), false),
669+
LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
669670
}
670671
}
671672

src/libsyntax/ext/quote.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P<ast::Expr> {
646646

647647
token::Literal(token::Byte(i), suf) => return mk_lit!("Byte", suf, i),
648648
token::Literal(token::Char(i), suf) => return mk_lit!("Char", suf, i),
649+
token::Literal(token::Err(_i), _suf) => return cx.expr(sp, ast::ExprKind::Err),
649650
token::Literal(token::Integer(i), suf) => return mk_lit!("Integer", suf, i),
650651
token::Literal(token::Float(i), suf) => return mk_lit!("Float", suf, i),
651652
token::Literal(token::Str_(i), suf) => return mk_lit!("Str_", suf, i),

src/libsyntax/parse/lexer/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,10 @@ impl<'a> StringReader<'a> {
14081408
// lifetimes shouldn't end with a single quote
14091409
// if we find one, then this is an invalid character literal
14101410
if self.ch_is('\'') {
1411-
self.fatal_span_verbose(start_with_quote, self.next_pos,
1412-
String::from("character literal may only contain one codepoint"))
1413-
.raise();
1411+
self.err_span_(start_with_quote, self.next_pos,
1412+
"character literal may only contain one codepoint");
1413+
self.bump();
1414+
return Ok(token::Literal(token::Err(Symbol::intern("??")), None))
14141415

14151416
}
14161417

@@ -1445,7 +1446,7 @@ impl<'a> StringReader<'a> {
14451446
format!("\"{}\"", &self.src[start..end]),
14461447
Applicability::MachineApplicable
14471448
).emit();
1448-
return Ok(token::Literal(token::Str_(Symbol::intern("??")), None))
1449+
return Ok(token::Literal(token::Err(Symbol::intern("??")), None))
14491450
}
14501451
if self.ch_is('\n') || self.is_eof() || self.ch_is('/') {
14511452
// Only attempt to infer single line string literals. If we encounter

src/libsyntax/parse/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +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::Err(i))),
469470

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

src/libsyntax/parse/token.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl DelimToken {
6060
pub enum Lit {
6161
Byte(ast::Name),
6262
Char(ast::Name),
63+
Err(ast::Name),
6364
Integer(ast::Name),
6465
Float(ast::Name),
6566
Str_(ast::Name),
@@ -73,6 +74,7 @@ impl Lit {
7374
match *self {
7475
Byte(_) => "byte literal",
7576
Char(_) => "char literal",
77+
Err(_) => "invalid literal",
7678
Integer(_) => "integer literal",
7779
Float(_) => "float literal",
7880
Str_(_) | StrRaw(..) => "string literal",
@@ -471,8 +473,7 @@ impl Token {
471473

472474
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot |
473475
DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
474-
Question | OpenDelim(..) | CloseDelim(..) => return None,
475-
476+
Question | OpenDelim(..) | CloseDelim(..) |
476477
Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
477478
Whitespace | Comment | Shebang(..) | Eof => return None,
478479
})

0 commit comments

Comments
 (0)