Skip to content

Commit 25ed6e4

Browse files
committed
Add ErrorGuaranteed to ast::LitKind::Err, token::LitKind::Err.
This mostly works well, and eliminates a couple of delayed bugs. One annoying thing is that we should really also add an `ErrorGuaranteed` to `proc_macro::bridge::LitKind::Err`. But that's difficult because `proc_macro` doesn't have access to `ErrorGuaranteed`, so we have to fake it.
1 parent 332c577 commit 25ed6e4

File tree

26 files changed

+85
-64
lines changed

26 files changed

+85
-64
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ pub enum LitKind {
18461846
/// A boolean literal (`true`, `false`).
18471847
Bool(bool),
18481848
/// Placeholder for a literal that wasn't well-formed in some way.
1849-
Err,
1849+
Err(ErrorGuaranteed),
18501850
}
18511851

18521852
impl LitKind {
@@ -1893,7 +1893,7 @@ impl LitKind {
18931893
| LitKind::Int(_, LitIntType::Unsuffixed)
18941894
| LitKind::Float(_, LitFloatType::Unsuffixed)
18951895
| LitKind::Bool(..)
1896-
| LitKind::Err => false,
1896+
| LitKind::Err(_) => false,
18971897
}
18981898
}
18991899
}

compiler/rustc_ast/src/token.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_macros::HashStable_Generic;
1313
use rustc_span::symbol::{kw, sym};
1414
#[allow(hidden_glob_reexports)]
1515
use rustc_span::symbol::{Ident, Symbol};
16-
use rustc_span::{edition::Edition, Span, DUMMY_SP};
16+
use rustc_span::{edition::Edition, ErrorGuaranteed, Span, DUMMY_SP};
1717
use std::borrow::Cow;
1818
use std::fmt;
1919

@@ -75,7 +75,7 @@ pub enum LitKind {
7575
ByteStrRaw(u8), // raw byte string delimited by `n` hash symbols
7676
CStr,
7777
CStrRaw(u8),
78-
Err,
78+
Err(ErrorGuaranteed),
7979
}
8080

8181
/// A literal token.
@@ -144,7 +144,7 @@ impl fmt::Display for Lit {
144144
CStrRaw(n) => {
145145
write!(f, "cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize))?
146146
}
147-
Integer | Float | Bool | Err => write!(f, "{symbol}")?,
147+
Integer | Float | Bool | Err(_) => write!(f, "{symbol}")?,
148148
}
149149

150150
if let Some(suffix) = suffix {
@@ -159,7 +159,7 @@ impl LitKind {
159159
/// An English article for the literal token kind.
160160
pub fn article(self) -> &'static str {
161161
match self {
162-
Integer | Err => "an",
162+
Integer | Err(_) => "an",
163163
_ => "a",
164164
}
165165
}
@@ -174,12 +174,12 @@ impl LitKind {
174174
Str | StrRaw(..) => "string",
175175
ByteStr | ByteStrRaw(..) => "byte string",
176176
CStr | CStrRaw(..) => "C string",
177-
Err => "error",
177+
Err(_) => "error",
178178
}
179179
}
180180

181181
pub(crate) fn may_have_suffix(self) -> bool {
182-
matches!(self, Integer | Float | Err)
182+
matches!(self, Integer | Float | Err(_))
183183
}
184184
}
185185

compiler/rustc_ast/src/util/literal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl LitKind {
144144
buf.push(0);
145145
LitKind::CStr(buf.into(), StrStyle::Raw(n))
146146
}
147-
token::Err => LitKind::Err,
147+
token::Err(guar) => LitKind::Err(guar),
148148
})
149149
}
150150
}
@@ -201,7 +201,7 @@ impl fmt::Display for LitKind {
201201
}
202202
}
203203
LitKind::Bool(b) => write!(f, "{}", if b { "true" } else { "false" })?,
204-
LitKind::Err => {
204+
LitKind::Err(_) => {
205205
// This only shows up in places like `-Zunpretty=hir` output, so we
206206
// don't bother to produce something useful.
207207
write!(f, "<bad-literal>")?;
@@ -237,7 +237,7 @@ impl MetaItemLit {
237237
LitKind::Char(_) => token::Char,
238238
LitKind::Int(..) => token::Integer,
239239
LitKind::Float(..) => token::Float,
240-
LitKind::Err => token::Err,
240+
LitKind::Err(guar) => token::Err(guar),
241241
};
242242

243243
token::Lit::new(kind, self.symbol, self.suffix)

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
124124
let lit_kind = match LitKind::from_token_lit(*token_lit) {
125125
Ok(lit_kind) => lit_kind,
126126
Err(err) => {
127-
report_lit_error(&self.tcx.sess.parse_sess, err, *token_lit, e.span);
128-
LitKind::Err
127+
let guar = report_lit_error(
128+
&self.tcx.sess.parse_sess,
129+
err,
130+
*token_lit,
131+
e.span,
132+
);
133+
LitKind::Err(guar)
129134
}
130135
};
131136
let lit = self.arena.alloc(respan(self.lower_span(e.span), lit_kind));

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,10 +966,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
966966
{
967967
lit
968968
} else {
969+
let guar = self.dcx().has_errors().unwrap();
969970
MetaItemLit {
970971
symbol: kw::Empty,
971972
suffix: None,
972-
kind: LitKind::Err,
973+
kind: LitKind::Err(guar),
973974
span: DUMMY_SP,
974975
}
975976
};

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn literal_to_string(lit: token::Lit) -> String {
254254
token::CStrRaw(n) => {
255255
format!("cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize))
256256
}
257-
token::Integer | token::Float | token::Bool | token::Err => symbol.to_string(),
257+
token::Integer | token::Float | token::Bool | token::Err(_) => symbol.to_string(),
258258
};
259259

260260
if let Some(suffix) = suffix {

compiler/rustc_builtin_macros/src/concat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn expand_concat(
4040
cx.dcx().emit_err(errors::ConcatBytestr { span: e.span });
4141
has_errors = true;
4242
}
43-
Ok(ast::LitKind::Err) => {
43+
Ok(ast::LitKind::Err(_)) => {
4444
has_errors = true;
4545
}
4646
Err(err) => {

compiler/rustc_builtin_macros/src/concat_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn invalid_type_err(
4444
Ok(ast::LitKind::Bool(_)) => {
4545
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "boolean", sugg: None });
4646
}
47-
Ok(ast::LitKind::Err) => {}
47+
Ok(ast::LitKind::Err(_)) => {}
4848
Ok(ast::LitKind::Int(_, _)) if !is_nested => {
4949
let sugg =
5050
snippet.map(|snippet| ConcatBytesInvalidSuggestion::IntLit { span: span, snippet });

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ pub fn expr_to_spanned_string<'a>(
12661266
);
12671267
Some((err, true))
12681268
}
1269-
Ok(ast::LitKind::Err) => None,
1269+
Ok(ast::LitKind::Err(_)) => None,
12701270
Err(err) => {
12711271
report_lit_error(&cx.sess.parse_sess, err, token_lit, expr.span);
12721272
None

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::util::literal::escape_byte_str_symbol;
1010
use rustc_ast_pretty::pprust;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::sync::Lrc;
13-
use rustc_errors::{MultiSpan, PResult};
13+
use rustc_errors::{ErrorGuaranteed, MultiSpan, PResult};
1414
use rustc_parse::lexer::nfc_normalize;
1515
use rustc_parse::parse_stream_from_source_str;
1616
use rustc_session::parse::ParseSess;
@@ -63,7 +63,12 @@ impl FromInternal<token::LitKind> for LitKind {
6363
token::ByteStrRaw(n) => LitKind::ByteStrRaw(n),
6464
token::CStr => LitKind::CStr,
6565
token::CStrRaw(n) => LitKind::CStrRaw(n),
66-
token::Err => LitKind::Err,
66+
token::Err(_guar) => {
67+
// This is the only place a `pm::bridge::LitKind::ErrWithGuar`
68+
// is constructed. Note that an `ErrorGuaranteed` is available,
69+
// as required. See the comment in `to_internal`.
70+
LitKind::ErrWithGuar
71+
}
6772
token::Bool => unreachable!(),
6873
}
6974
}
@@ -82,7 +87,16 @@ impl ToInternal<token::LitKind> for LitKind {
8287
LitKind::ByteStrRaw(n) => token::ByteStrRaw(n),
8388
LitKind::CStr => token::CStr,
8489
LitKind::CStrRaw(n) => token::CStrRaw(n),
85-
LitKind::Err => token::Err,
90+
LitKind::ErrWithGuar => {
91+
// This is annoying but valid. `LitKind::ErrWithGuar` would
92+
// have an `ErrorGuaranteed` except that type isn't available
93+
// in that crate. So we have to fake one. And we don't want to
94+
// use a delayed bug because there might be lots of these,
95+
// which would be expensive.
96+
#[allow(deprecated)]
97+
let guar = ErrorGuaranteed::unchecked_error_guaranteed();
98+
token::Err(guar)
99+
}
86100
}
87101
}
88102
}
@@ -477,7 +491,7 @@ impl server::FreeFunctions for Rustc<'_, '_> {
477491
| token::LitKind::ByteStrRaw(_)
478492
| token::LitKind::CStr
479493
| token::LitKind::CStrRaw(_)
480-
| token::LitKind::Err => return Err(()),
494+
| token::LitKind::Err(_) => return Err(()),
481495
token::LitKind::Integer | token::LitKind::Float => {}
482496
}
483497

0 commit comments

Comments
 (0)