Skip to content

Commit e827b17

Browse files
committed
Move make_unclosed_delims_error to lexer/diagonostics.rs
Signed-off-by: xizheyin <[email protected]>
1 parent fae7785 commit e827b17

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

compiler/rustc_parse/src/lexer/diagnostics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use rustc_ast::token::Delimiter;
22
use rustc_errors::Diag;
3+
use rustc_session::parse::ParseSess;
34
use rustc_span::Span;
45
use rustc_span::source_map::SourceMap;
56

67
use super::UnmatchedDelim;
8+
use crate::errors::MismatchedClosingDelimiter;
9+
use crate::pprust;
710

811
#[derive(Default)]
912
pub(super) struct TokenTreeDiagInfo {
@@ -116,3 +119,24 @@ pub(super) fn report_suspicious_mismatch_block(
116119
}
117120
}
118121
}
122+
123+
pub(crate) fn make_unclosed_delims_error(
124+
unmatched: UnmatchedDelim,
125+
psess: &ParseSess,
126+
) -> Option<Diag<'_>> {
127+
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
128+
// `unmatched_delims` only for error recovery in the `Parser`.
129+
let found_delim = unmatched.found_delim?;
130+
let mut spans = vec![unmatched.found_span];
131+
if let Some(sp) = unmatched.unclosed_span {
132+
spans.push(sp);
133+
};
134+
let err = psess.dcx().create_err(MismatchedClosingDelimiter {
135+
spans,
136+
delimiter: pprust::token_kind_to_string(&found_delim.as_close_token_kind()).to_string(),
137+
unmatched: unmatched.found_span,
138+
opening_candidate: unmatched.candidate_span,
139+
unclosed: unmatched.unclosed_span,
140+
});
141+
Some(err)
142+
}

compiler/rustc_parse/src/lexer/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ops::Range;
22

3+
use diagnostics::make_unclosed_delims_error;
34
use rustc_ast::ast::{self, AttrStyle};
45
use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind};
56
use rustc_ast::tokenstream::TokenStream;
@@ -17,9 +18,9 @@ use rustc_session::parse::ParseSess;
1718
use rustc_span::{BytePos, Pos, Span, Symbol};
1819
use tracing::debug;
1920

21+
use crate::errors;
2022
use crate::lexer::diagnostics::TokenTreeDiagInfo;
2123
use crate::lexer::unicode_chars::UNICODE_ARRAY;
22-
use crate::{errors, make_unclosed_delims_error};
2324

2425
mod diagnostics;
2526
mod tokentrees;

compiler/rustc_parse/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
3232

3333
#[macro_use]
3434
pub mod parser;
35-
use parser::{Parser, make_unclosed_delims_error};
35+
use parser::Parser;
3636
pub mod lexer;
3737
pub mod validate_attr;
3838

compiler/rustc_parse/src/parser/mod.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ use token_type::TokenTypeSet;
4343
pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
4444
use tracing::debug;
4545

46-
use crate::errors::{
47-
self, IncorrectVisibilityRestriction, MismatchedClosingDelimiter, NonStringAbiLiteral,
48-
};
46+
use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral};
4947
use crate::exp;
50-
use crate::lexer::UnmatchedDelim;
5148

5249
#[cfg(test)]
5350
mod tests;
@@ -1745,27 +1742,6 @@ impl<'a> Parser<'a> {
17451742
}
17461743
}
17471744

1748-
pub(crate) fn make_unclosed_delims_error(
1749-
unmatched: UnmatchedDelim,
1750-
psess: &ParseSess,
1751-
) -> Option<Diag<'_>> {
1752-
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
1753-
// `unmatched_delims` only for error recovery in the `Parser`.
1754-
let found_delim = unmatched.found_delim?;
1755-
let mut spans = vec![unmatched.found_span];
1756-
if let Some(sp) = unmatched.unclosed_span {
1757-
spans.push(sp);
1758-
};
1759-
let err = psess.dcx().create_err(MismatchedClosingDelimiter {
1760-
spans,
1761-
delimiter: pprust::token_kind_to_string(&found_delim.as_close_token_kind()).to_string(),
1762-
unmatched: unmatched.found_span,
1763-
opening_candidate: unmatched.candidate_span,
1764-
unclosed: unmatched.unclosed_span,
1765-
});
1766-
Some(err)
1767-
}
1768-
17691745
/// A helper struct used when building an `AttrTokenStream` from
17701746
/// a `LazyAttrTokenStream`. Both delimiter and non-delimited tokens
17711747
/// are stored as `FlatToken::Token`. A vector of `FlatToken`s

0 commit comments

Comments
 (0)