Skip to content

Commit 6656413

Browse files
committed
Stop using DiagnosticBuilder::buffer in the parser.
One consequence is that errors returned by `maybe_new_parser_from_source_str` now must be consumed, so a bunch of places that previously ignored those errors now cancel them. (Most of them explicitly dropped the errors before. I guess that was to indicate "we are explicitly ignoring these", though I'm not 100% sure.)
1 parent d02150f commit 6656413

File tree

8 files changed

+39
-32
lines changed

8 files changed

+39
-32
lines changed

compiler/rustc_interface/src/interface.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg {
8282
Ok(..) => {}
8383
Err(err) => err.cancel(),
8484
},
85-
Err(errs) => drop(errs),
85+
Err(errs) => errs.into_iter().for_each(|err| err.cancel()),
8686
}
8787

8888
// If the user tried to use a key="value" flag, but is missing the quotes, provide
@@ -129,9 +129,12 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
129129
error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
130130
};
131131

132-
let Ok(mut parser) = maybe_new_parser_from_source_str(&sess, filename, s.to_string())
133-
else {
134-
expected_error();
132+
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, s.to_string()) {
133+
Ok(parser) => parser,
134+
Err(errs) => {
135+
errs.into_iter().for_each(|err| err.cancel());
136+
expected_error();
137+
}
135138
};
136139

137140
let meta_item = match parser.parse_meta_item() {

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::{self, AttrStyle};
77
use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::util::unicode::contains_text_flow_control_chars;
10-
use rustc_errors::{error_code, Applicability, DiagCtxt, Diagnostic, StashKey};
10+
use rustc_errors::{error_code, Applicability, DiagCtxt, DiagnosticBuilder, StashKey};
1111
use rustc_lexer::unescape::{self, EscapeError, Mode};
1212
use rustc_lexer::{Base, DocStyle, RawStrError};
1313
use rustc_lexer::{Cursor, LiteralKind};
@@ -47,7 +47,7 @@ pub(crate) fn parse_token_trees<'sess, 'src>(
4747
mut src: &'src str,
4848
mut start_pos: BytePos,
4949
override_span: Option<Span>,
50-
) -> Result<TokenStream, Vec<Diagnostic>> {
50+
) -> Result<TokenStream, Vec<DiagnosticBuilder<'sess>>> {
5151
// Skip `#!`, if present.
5252
if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
5353
src = &src[shebang_len..];
@@ -76,13 +76,13 @@ pub(crate) fn parse_token_trees<'sess, 'src>(
7676
let mut buffer = Vec::with_capacity(1);
7777
for unmatched in unmatched_delims {
7878
if let Some(err) = make_unclosed_delims_error(unmatched, sess) {
79-
err.buffer(&mut buffer);
79+
buffer.push(err);
8080
}
8181
}
8282
if let Err(errs) = res {
8383
// Add unclosing delimiter or diff marker errors
8484
for err in errs {
85-
err.buffer(&mut buffer);
85+
buffer.push(err);
8686
}
8787
}
8888
Err(buffer)

compiler/rustc_parse/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_ast::tokenstream::TokenStream;
1919
use rustc_ast::{AttrItem, Attribute, MetaItem};
2020
use rustc_ast_pretty::pprust;
2121
use rustc_data_structures::sync::Lrc;
22-
use rustc_errors::{Diagnostic, PResult};
22+
use rustc_errors::{DiagnosticBuilder, FatalError, PResult};
2323
use rustc_session::parse::ParseSess;
2424
use rustc_span::{FileName, SourceFile, Span};
2525

@@ -45,14 +45,13 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
4545
/// A variant of 'panictry!' that works on a `Vec<Diagnostic>` instead of a single
4646
/// `DiagnosticBuilder`.
4747
macro_rules! panictry_buffer {
48-
($handler:expr, $e:expr) => {{
49-
use rustc_errors::FatalError;
48+
($e:expr) => {{
5049
use std::result::Result::{Err, Ok};
5150
match $e {
5251
Ok(e) => e,
5352
Err(errs) => {
5453
for e in errs {
55-
$handler.emit_diagnostic(e);
54+
e.emit();
5655
}
5756
FatalError.raise()
5857
}
@@ -100,16 +99,17 @@ pub fn parse_stream_from_source_str(
10099

101100
/// Creates a new parser from a source string.
102101
pub fn new_parser_from_source_str(sess: &ParseSess, name: FileName, source: String) -> Parser<'_> {
103-
panictry_buffer!(&sess.dcx, maybe_new_parser_from_source_str(sess, name, source))
102+
panictry_buffer!(maybe_new_parser_from_source_str(sess, name, source))
104103
}
105104

106105
/// Creates a new parser from a source string. Returns any buffered errors from lexing the initial
107-
/// token stream.
106+
/// token stream; these must be consumed via `emit`, `cancel`, etc., otherwise a panic will occur
107+
/// when they are dropped.
108108
pub fn maybe_new_parser_from_source_str(
109109
sess: &ParseSess,
110110
name: FileName,
111111
source: String,
112-
) -> Result<Parser<'_>, Vec<Diagnostic>> {
112+
) -> Result<Parser<'_>, Vec<DiagnosticBuilder<'_>>> {
113113
maybe_source_file_to_parser(sess, sess.source_map().new_source_file(name, source))
114114
}
115115

@@ -125,15 +125,15 @@ pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Option<Spa
125125
err.emit();
126126
});
127127

128-
panictry_buffer!(&sess.dcx, maybe_source_file_to_parser(sess, source_file))
128+
panictry_buffer!(maybe_source_file_to_parser(sess, source_file))
129129
}
130130

131131
/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
132132
/// the initial token stream.
133133
fn maybe_source_file_to_parser(
134134
sess: &ParseSess,
135135
source_file: Lrc<SourceFile>,
136-
) -> Result<Parser<'_>, Vec<Diagnostic>> {
136+
) -> Result<Parser<'_>, Vec<DiagnosticBuilder<'_>>> {
137137
let end_pos = source_file.end_position();
138138
let stream = maybe_file_to_stream(sess, source_file, None)?;
139139
let mut parser = stream_to_parser(sess, stream, None);
@@ -152,16 +152,16 @@ pub fn source_file_to_stream(
152152
source_file: Lrc<SourceFile>,
153153
override_span: Option<Span>,
154154
) -> TokenStream {
155-
panictry_buffer!(&sess.dcx, maybe_file_to_stream(sess, source_file, override_span))
155+
panictry_buffer!(maybe_file_to_stream(sess, source_file, override_span))
156156
}
157157

158158
/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
159159
/// parsing the token stream.
160-
fn maybe_file_to_stream(
161-
sess: &ParseSess,
160+
fn maybe_file_to_stream<'sess>(
161+
sess: &'sess ParseSess,
162162
source_file: Lrc<SourceFile>,
163163
override_span: Option<Span>,
164-
) -> Result<TokenStream, Vec<Diagnostic>> {
164+
) -> Result<TokenStream, Vec<DiagnosticBuilder<'sess>>> {
165165
let src = source_file.src.as_ref().unwrap_or_else(|| {
166166
sess.dcx.bug(format!(
167167
"cannot lex `source_file` without source: {}",

src/librustdoc/clean/render_macro_matchers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String
6969
let mut parser =
7070
match rustc_parse::maybe_new_parser_from_source_str(&sess, file_name, snippet.clone()) {
7171
Ok(parser) => parser,
72-
Err(diagnostics) => {
73-
drop(diagnostics);
72+
Err(errs) => {
73+
errs.into_iter().for_each(|err| err.cancel());
7474
return None;
7575
}
7676
};

src/librustdoc/doctest.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ pub(crate) fn make_test(
589589
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, source) {
590590
Ok(p) => p,
591591
Err(errs) => {
592-
drop(errs);
592+
errs.into_iter().for_each(|err| err.cancel());
593593
return (found_main, found_extern_crate, found_macro);
594594
}
595595
};
@@ -759,8 +759,10 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
759759
let mut parser =
760760
match maybe_new_parser_from_source_str(&sess, filename, source.to_owned()) {
761761
Ok(p) => p,
762-
Err(_) => {
763-
// If there is an unclosed delimiter, an error will be returned by the tokentrees.
762+
Err(errs) => {
763+
errs.into_iter().for_each(|err| err.cancel());
764+
// If there is an unclosed delimiter, an error will be returned by the
765+
// tokentrees.
764766
return false;
765767
}
766768
};

src/tools/clippy/clippy_lints/src/doc/needless_doctest_main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn check(
5353
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code) {
5454
Ok(p) => p,
5555
Err(errs) => {
56-
drop(errs);
56+
errs.into_iter().for_each(|err| err.cancel());
5757
return (false, test_attr_spans);
5858
},
5959
};

src/tools/rustfmt/src/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use rustc_ast::token::TokenKind;
55
use rustc_ast::{ast, attr, ptr};
6-
use rustc_errors::Diagnostic;
6+
use rustc_errors::DiagnosticBuilder;
77
use rustc_parse::{new_parser_from_file, parser::Parser as RawParser};
88
use rustc_span::{sym, Span};
99
use thin_vec::ThinVec;
@@ -65,7 +65,7 @@ impl<'a> ParserBuilder<'a> {
6565
fn parser(
6666
sess: &'a rustc_session::parse::ParseSess,
6767
input: Input,
68-
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> {
68+
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<DiagnosticBuilder<'a>>>> {
6969
match input {
7070
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
7171
new_parser_from_file(sess, file, None)

src/tools/rustfmt/src/parse/session.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::sync::atomic::{AtomicBool, Ordering};
44
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
55
use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter};
66
use rustc_errors::translation::Translate;
7-
use rustc_errors::{ColorConfig, DiagCtxt, Diagnostic, Level as DiagnosticLevel};
7+
use rustc_errors::{
8+
ColorConfig, DiagCtxt, Diagnostic, DiagnosticBuilder, Level as DiagnosticLevel,
9+
};
810
use rustc_session::parse::ParseSess as RawParseSess;
911
use rustc_span::{
1012
source_map::{FilePathMapping, SourceMap},
@@ -283,9 +285,9 @@ impl ParseSess {
283285

284286
// Methods that should be restricted within the parse module.
285287
impl ParseSess {
286-
pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diagnostic>) {
288+
pub(super) fn emit_diagnostics(&self, diagnostics: Vec<DiagnosticBuilder<'_>>) {
287289
for diagnostic in diagnostics {
288-
self.parse_sess.dcx.emit_diagnostic(diagnostic);
290+
diagnostic.emit();
289291
}
290292
}
291293

0 commit comments

Comments
 (0)