Rollup merge of #99935 - CAD97:unstable-syntax-lints, r=petrochenkov

Reenable disabled early syntax gates as future-incompatibility lints

- MCP: https://github.com/rust-lang/compiler-team/issues/535

The approach taken by this PR is

- Introduce a new lint, `unstable_syntax_pre_expansion`, and reenable the early syntax gates to emit it
- Use the diagnostic stashing mechanism to stash warnings the early warnings
- When the hard error occurs post expansion, steal and cancel the early warning
- Don't display any stashed warnings if errors are present to avoid the same noise problem that hiding type ascription errors is avoiding

Commits are working commits, but in a coherent steps-to-implement manner. Can be squashed if desired.

The preexisting `soft_unstable` lint seems like it would've been a good fit, but it is deny-by-default (appropriate for `#[bench]`) and these gates should be introduced as warn-by-default.

It may be desirable to change the stash mechanism's behavior to not flush lint errors in the presence of other errors either (like is done for warnings here), but upgrading a stash-using lint from warn to error perhaps is enough of a request to see the lint that they shouldn't be hidden; additionally, fixing the last error to get new errors thrown at you always feels bad, so if we know the lint errors are present, we should show them.

Using a new flag/mechanism for a "weak diagnostic" which is suppressed by other errors may also be desirable over assuming any stashed warnings are "weak," but this is the first user of stashing warnings and seems an appropriate use of stashing (it follows the "know more later to refine the diagnostic" pattern; here we learn that it's in a compiled position) so we get to define what it means to stash a non-hard-error diagnostic.

cc `````@petrochenkov````` (seconded MCP)
This commit is contained in:
Matthias Krüger 2022-08-20 19:45:10 +02:00 committed by GitHub
commit 8828af4d88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 417 additions and 46 deletions

View file

@ -459,6 +459,7 @@ struct HandlerInner {
pub enum StashKey {
ItemNoType,
UnderscoreForArrayLengths,
EarlySyntaxWarning,
}
fn default_track_diagnostic(_: &Diagnostic) {}
@ -626,19 +627,13 @@ impl Handler {
/// Stash a given diagnostic with the given `Span` and `StashKey` as the key for later stealing.
pub fn stash_diagnostic(&self, span: Span, key: StashKey, diag: Diagnostic) {
let mut inner = self.inner.borrow_mut();
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
// if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
// See the PR for a discussion.
inner.stashed_diagnostics.insert((span, key), diag);
inner.stash((span, key), diag);
}
/// Steal a previously stashed diagnostic with the given `Span` and `StashKey` as the key.
pub fn steal_diagnostic(&self, span: Span, key: StashKey) -> Option<DiagnosticBuilder<'_, ()>> {
self.inner
.borrow_mut()
.stashed_diagnostics
.remove(&(span, key))
.map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
let mut inner = self.inner.borrow_mut();
inner.steal((span, key)).map(|diag| DiagnosticBuilder::new_diagnostic(self, diag))
}
/// Emit all stashed diagnostics.
@ -1106,13 +1101,31 @@ impl HandlerInner {
/// Emit all stashed diagnostics.
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
let has_errors = self.has_errors();
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
let mut reported = None;
for mut diag in diags {
// Decrement the count tracking the stash; emitting will increment it.
if diag.is_error() {
reported = Some(ErrorGuaranteed(()));
if matches!(diag.level, Level::Error { lint: true }) {
self.lint_err_count -= 1;
} else {
self.err_count -= 1;
}
} else {
if diag.is_force_warn() {
self.warn_count -= 1;
} else {
// Unless they're forced, don't flush stashed warnings when
// there are errors, to avoid causing warning overload. The
// stash would've been stolen already if it were important.
if has_errors {
continue;
}
}
}
self.emit_diagnostic(&mut diag);
let reported_this = self.emit_diagnostic(&mut diag);
reported = reported.or(reported_this);
}
reported
}
@ -1302,9 +1315,47 @@ impl HandlerInner {
}
}
fn stash(&mut self, key: (Span, StashKey), diagnostic: Diagnostic) {
// Track the diagnostic for counts, but don't panic-if-treat-err-as-bug
// yet; that happens when we actually emit the diagnostic.
if diagnostic.is_error() {
if matches!(diagnostic.level, Level::Error { lint: true }) {
self.lint_err_count += 1;
} else {
self.err_count += 1;
}
} else {
// Warnings are only automatically flushed if they're forced.
if diagnostic.is_force_warn() {
self.warn_count += 1;
}
}
// FIXME(Centril, #69537): Consider reintroducing panic on overwriting a stashed diagnostic
// if/when we have a more robust macro-friendly replacement for `(span, key)` as a key.
// See the PR for a discussion.
self.stashed_diagnostics.insert(key, diagnostic);
}
fn steal(&mut self, key: (Span, StashKey)) -> Option<Diagnostic> {
let diagnostic = self.stashed_diagnostics.remove(&key)?;
if diagnostic.is_error() {
if matches!(diagnostic.level, Level::Error { lint: true }) {
self.lint_err_count -= 1;
} else {
self.err_count -= 1;
}
} else {
if diagnostic.is_force_warn() {
self.warn_count -= 1;
}
}
Some(diagnostic)
}
#[inline]
fn err_count(&self) -> usize {
self.err_count + self.stashed_diagnostics.len()
self.err_count
}
fn has_errors(&self) -> bool {