Skip to content

Commit

Permalink
Auto merge of #108747 - matthiaskrgr:rollup-wfc7fx4, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 7 pull requests

Successful merges:

 - #108627 (Properly colorize multi-part suggestions in the same line)
 - #108632 (Omit unchanged options from config.toml in `configure.py`)
 - #108715 (Remove unclosed_delims from parser)
 - #108723 (rustdoc: function signature search with traits in `where` clause)
 - #108724 (field is not used outside the crate)
 - #108734 (rustdoc: Note in a type's layout/size if it is uninhabited)
 - #108736 (Remove `allow(potential_query_instability)` from `ast_passes`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Mar 4, 2023
2 parents f15f0ea + ff95645 commit bb18388
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 195 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
use rustc_ast::walk_list;
use rustc_ast::*;
use rustc_ast_pretty::pprust::{self, State};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxIndexMap;
use rustc_macros::Subdiagnostic;
use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{
Expand Down Expand Up @@ -643,7 +643,7 @@ fn validate_generic_param_order(
span: Span,
) {
let mut max_param: Option<ParamKindOrd> = None;
let mut out_of_order = FxHashMap::default();
let mut out_of_order = FxIndexMap::default();
let mut param_idents = Vec::with_capacity(generics.len());

for (idx, param) in generics.iter().enumerate() {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//!
//! The crate also contains other misc AST visitors, e.g. `node_count` and `show_span`.

#![allow(rustc::potential_query_instability)]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(iter_is_partitioned)]
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ impl EmitterWriter {
self.draw_code_line(
&mut buffer,
&mut row_num,
&Vec::new(),
&[],
p + line_start,
l,
show_code_change,
Expand All @@ -1919,7 +1919,7 @@ impl EmitterWriter {
self.draw_code_line(
&mut buffer,
&mut row_num,
&Vec::new(),
&[],
p + line_start,
l,
show_code_change,
Expand All @@ -1936,7 +1936,7 @@ impl EmitterWriter {
self.draw_code_line(
&mut buffer,
&mut row_num,
&Vec::new(),
&[],
p + line_start,
l,
show_code_change,
Expand All @@ -1951,7 +1951,7 @@ impl EmitterWriter {
self.draw_code_line(
&mut buffer,
&mut row_num,
highlight_parts,
&highlight_parts,
line_pos + line_start,
line,
show_code_change,
Expand Down Expand Up @@ -2176,7 +2176,7 @@ impl EmitterWriter {
&self,
buffer: &mut StyledBuffer,
row_num: &mut usize,
highlight_parts: &Vec<SubstitutionHighlight>,
highlight_parts: &[SubstitutionHighlight],
line_num: usize,
line_to_add: &str,
show_code_change: DisplaySuggestion,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl CodeSuggestion {
});
buf.push_str(&part.snippet);
let cur_hi = sm.lookup_char_pos(part.span.hi());
if prev_hi.line == cur_lo.line && cur_hi.line == cur_lo.line {
if cur_hi.line == cur_lo.line {
// Account for the difference between the width of the current code and the
// snippet being suggested, so that the *later* suggestions are correctly
// aligned on the screen.
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
/// call to `start_snapshot` and `rollback_to`.
#[derive(Clone)]
pub struct InferCtxtInner<'tcx> {
/// Cache for projections. This cache is snapshotted along with the infcx.
/// Cache for projections.
///
/// Public so that `traits::project` can use it.
pub projection_cache: traits::ProjectionCacheStorage<'tcx>,
/// This cache is snapshotted along with the infcx.
projection_cache: traits::ProjectionCacheStorage<'tcx>,

/// We instantiate `UnificationTable` with `bounds<Ty>` because the types
/// that might instantiate a general type variable have an order,
Expand Down
116 changes: 3 additions & 113 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::errors::{
};

use crate::fluent_generated as fluent;
use crate::lexer::UnmatchedDelim;
use crate::parser;
use rustc_ast as ast;
use rustc_ast::ptr::P;
Expand Down Expand Up @@ -220,7 +219,6 @@ impl MultiSugg {
/// is dropped.
pub struct SnapshotParser<'a> {
parser: Parser<'a>,
unclosed_delims: Vec<UnmatchedDelim>,
}

impl<'a> Deref for SnapshotParser<'a> {
Expand Down Expand Up @@ -255,27 +253,15 @@ impl<'a> Parser<'a> {
&self.sess.span_diagnostic
}

/// Replace `self` with `snapshot.parser` and extend `unclosed_delims` with `snapshot.unclosed_delims`.
/// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears.
/// Replace `self` with `snapshot.parser`.
pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {
*self = snapshot.parser;
self.unclosed_delims.extend(snapshot.unclosed_delims);
}

pub fn unclosed_delims(&self) -> &[UnmatchedDelim] {
&self.unclosed_delims
}

/// Create a snapshot of the `Parser`.
pub fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> {
let mut snapshot = self.clone();
let unclosed_delims = self.unclosed_delims.clone();
// Clear `unclosed_delims` in snapshot to avoid
// duplicate errors being emitted when the `Parser`
// is dropped (which may or may not happen, depending
// if the parsing the snapshot is created for is successful)
snapshot.unclosed_delims.clear();
SnapshotParser { parser: snapshot, unclosed_delims }
let snapshot = self.clone();
SnapshotParser { parser: snapshot }
}

pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
Expand Down Expand Up @@ -579,21 +565,6 @@ impl<'a> Parser<'a> {
} else {
label_sp
};
match self.recover_closing_delimiter(
&expected
.iter()
.filter_map(|tt| match tt {
TokenType::Token(t) => Some(t.clone()),
_ => None,
})
.collect::<Vec<_>>(),
err,
) {
Err(e) => err = e,
Ok(recovered) => {
return Ok(recovered);
}
}

if self.check_too_many_raw_str_terminators(&mut err) {
if expected.contains(&TokenType::Token(token::Semi)) && self.eat(&token::Semi) {
Expand Down Expand Up @@ -1573,12 +1544,6 @@ impl<'a> Parser<'a> {
);
let mut err = self.struct_span_err(sp, &msg);
let label_exp = format!("expected `{token_str}`");
match self.recover_closing_delimiter(&[t.clone()], err) {
Err(e) => err = e,
Ok(recovered) => {
return Ok(recovered);
}
}
let sm = self.sess.source_map();
if !sm.is_multiline(prev_sp.until(sp)) {
// When the spans are in the same line, it means that the only content
Expand Down Expand Up @@ -1795,81 +1760,6 @@ impl<'a> Parser<'a> {
}
}

pub(super) fn recover_closing_delimiter(
&mut self,
tokens: &[TokenKind],
mut err: DiagnosticBuilder<'a, ErrorGuaranteed>,
) -> PResult<'a, bool> {
let mut pos = None;
// We want to use the last closing delim that would apply.
for (i, unmatched) in self.unclosed_delims.iter().enumerate().rev() {
if tokens.contains(&token::CloseDelim(unmatched.expected_delim))
&& Some(self.token.span) > unmatched.unclosed_span
{
pos = Some(i);
}
}
match pos {
Some(pos) => {
// Recover and assume that the detected unclosed delimiter was meant for
// this location. Emit the diagnostic and act as if the delimiter was
// present for the parser's sake.

// Don't attempt to recover from this unclosed delimiter more than once.
let unmatched = self.unclosed_delims.remove(pos);
let delim = TokenType::Token(token::CloseDelim(unmatched.expected_delim));
if unmatched.found_delim.is_none() {
// We encountered `Eof`, set this fact here to avoid complaining about missing
// `fn main()` when we found place to suggest the closing brace.
*self.sess.reached_eof.borrow_mut() = true;
}

// We want to suggest the inclusion of the closing delimiter where it makes
// the most sense, which is immediately after the last token:
//
// {foo(bar {}}
// ^ ^
// | |
// | help: `)` may belong here
// |
// unclosed delimiter
if let Some(sp) = unmatched.unclosed_span {
let mut primary_span: Vec<Span> =
err.span.primary_spans().iter().cloned().collect();
primary_span.push(sp);
let mut primary_span: MultiSpan = primary_span.into();
for span_label in err.span.span_labels() {
if let Some(label) = span_label.label {
primary_span.push_span_label(span_label.span, label);
}
}
err.set_span(primary_span);
err.span_label(sp, "unclosed delimiter");
}
// Backticks should be removed to apply suggestions.
let mut delim = delim.to_string();
delim.retain(|c| c != '`');
err.span_suggestion_short(
self.prev_token.span.shrink_to_hi(),
&format!("`{delim}` may belong here"),
delim,
Applicability::MaybeIncorrect,
);
if unmatched.found_delim.is_none() {
// Encountered `Eof` when lexing blocks. Do not recover here to avoid knockdown
// errors which would be emitted elsewhere in the parser and let other error
// recovery consume the rest of the file.
Err(err)
} else {
err.emit();
self.expected_tokens.clear(); // Reduce the number of errors.
Ok(true)
}
}
_ => Err(err),
}
}

/// Eats tokens until we can be relatively sure we reached the end of the
/// statement. This is something of a best-effort heuristic.
///
Expand Down
13 changes: 0 additions & 13 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,19 +1394,6 @@ impl<'a> Parser<'a> {
self.parse_expr_let()
} else if self.eat_keyword(kw::Underscore) {
Ok(self.mk_expr(self.prev_token.span, ExprKind::Underscore))
} else if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
// Don't complain about bare semicolons after unclosed braces
// recovery in order to keep the error count down. Fixing the
// delimiters will possibly also fix the bare semicolon found in
// expression context. For example, silence the following error:
//
// error: expected expression, found `;`
// --> file.rs:2:13
// |
// 2 | foo(bar(;
// | ^ expected expression
self.bump();
Ok(self.mk_expr_err(self.token.span))
} else if self.token.uninterpolated_span().rust_2018() {
// `Span::rust_2018()` is somewhat expensive; don't get it repeatedly.
if self.check_keyword(kw::Async) {
Expand Down
24 changes: 6 additions & 18 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,13 @@ impl<'a> Parser<'a> {
return Ok(Some(item.into_inner()));
};

let mut unclosed_delims = vec![];
let item =
self.collect_tokens_trailing_token(attrs, force_collect, |this: &mut Self, attrs| {
let item =
this.parse_item_common_(attrs, mac_allowed, attrs_allowed, fn_parse_mode);
unclosed_delims.append(&mut this.unclosed_delims);
Ok((item?, TrailingToken::None))
})?;

self.unclosed_delims.append(&mut unclosed_delims);
Ok(item)
}

Expand Down Expand Up @@ -1960,21 +1957,12 @@ impl<'a> Parser<'a> {
// FIXME: This will make us not emit the help even for declarative
// macros within the same crate (that we can fix), which is sad.
if !span.from_expansion() {
if self.unclosed_delims.is_empty() {
let DelimSpan { open, close } = args.dspan;
err.multipart_suggestion(
"change the delimiters to curly braces",
vec![(open, "{".to_string()), (close, '}'.to_string())],
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
span,
"change the delimiters to curly braces",
" { /* items */ }",
Applicability::HasPlaceholders,
);
}
let DelimSpan { open, close } = args.dspan;
err.multipart_suggestion(
"change the delimiters to curly braces",
vec![(open, "{".to_string()), (close, '}'.to_string())],
Applicability::MaybeIncorrect,
);
err.span_suggestion(
span.shrink_to_hi(),
"add a semicolon",
Expand Down
17 changes: 3 additions & 14 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ pub struct Parser<'a> {
/// See the comments in the `parse_path_segment` function for more details.
unmatched_angle_bracket_count: u32,
max_angle_bracket_count: u32,
/// A list of all unclosed delimiters found by the lexer. If an entry is used for error recovery
/// it gets removed from here. Every entry left at the end gets emitted as an independent
/// error.
pub(super) unclosed_delims: Vec<UnmatchedDelim>,

last_unexpected_token_span: Option<Span>,
/// Span pointing at the `:` for the last type ascription the parser has seen, and whether it
/// looked like it could have been a mistyped path or literal `Option:Some(42)`).
Expand All @@ -168,7 +165,7 @@ pub struct Parser<'a> {
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
// it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Parser<'_>, 312);
rustc_data_structures::static_assert_size!(Parser<'_>, 288);

/// Stores span information about a closure.
#[derive(Clone)]
Expand Down Expand Up @@ -215,12 +212,6 @@ struct CaptureState {
inner_attr_ranges: FxHashMap<AttrId, ReplaceRange>,
}

impl<'a> Drop for Parser<'a> {
fn drop(&mut self) {
emit_unclosed_delims(&mut self.unclosed_delims, &self.sess);
}
}

/// Iterator over a `TokenStream` that produces `Token`s. It's a bit odd that
/// we (a) lex tokens into a nice tree structure (`TokenStream`), and then (b)
/// use this type to emit them as a linear sequence. But a linear sequence is
Expand Down Expand Up @@ -478,7 +469,6 @@ impl<'a> Parser<'a> {
desugar_doc_comments,
unmatched_angle_bracket_count: 0,
max_angle_bracket_count: 0,
unclosed_delims: Vec::new(),
last_unexpected_token_span: None,
last_type_ascription: None,
subparser_name,
Expand Down Expand Up @@ -859,7 +849,6 @@ impl<'a> Parser<'a> {
let mut recovered = false;
let mut trailing = false;
let mut v = ThinVec::new();
let unclosed_delims = !self.unclosed_delims.is_empty();

while !self.expect_any_with_type(kets, expect) {
if let token::CloseDelim(..) | token::Eof = self.token.kind {
Expand Down Expand Up @@ -901,7 +890,7 @@ impl<'a> Parser<'a> {
_ => {
// Attempt to keep parsing if it was a similar separator.
if let Some(tokens) = t.similar_tokens() {
if tokens.contains(&self.token.kind) && !unclosed_delims {
if tokens.contains(&self.token.kind) {
self.bump();
}
}
Expand Down
1 change: 1 addition & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ changelog-seen = 2
# General build configuration options
# =============================================================================
[build]

# The default stage to use for the `check` subcommand
#check-stage = 0

Expand Down
Loading

0 comments on commit bb18388

Please sign in to comment.