Skip to content

Commit b3329f8

Browse files
authored
Rollup merge of #96405 - pvdrz:ambiguous-plus-diagnostic, r=davidtwco
Migrate ambiguous plus diagnostic to the new derive macro r? ````@davidtwco```` ````@jyn514````
2 parents d665a5e + e7ae9eb commit b3329f8

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,7 @@ dependencies = [
41584158
"rustc_errors",
41594159
"rustc_feature",
41604160
"rustc_lexer",
4161+
"rustc_macros",
41614162
"rustc_session",
41624163
"rustc_span",
41634164
"tracing",
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
parser-struct-literal-body-without-path =
22
struct literal body without path
33
.suggestion = you might have forgotten to add the struct literal inside the block
4+
5+
parser-maybe-report-ambiguous-plus =
6+
ambiguous `+` in a type
7+
.suggestion = use parentheses to disambiguate

compiler/rustc_macros/src/session_diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
308308
{
309309
fn into_diagnostic(
310310
self,
311-
#sess: &'__session_diagnostic_sess rustc_session::Session
311+
#sess: &'__session_diagnostic_sess rustc_session::parse::ParseSess
312312
) -> rustc_errors::DiagnosticBuilder<'__session_diagnostic_sess, #param_ty> {
313313
use rustc_errors::IntoDiagnosticArg;
314314
#implementation

compiler/rustc_parse/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1313
rustc_data_structures = { path = "../rustc_data_structures" }
1414
rustc_feature = { path = "../rustc_feature" }
1515
rustc_lexer = { path = "../rustc_lexer" }
16+
rustc_macros = { path = "../rustc_macros" }
1617
rustc_errors = { path = "../rustc_errors" }
1718
rustc_session = { path = "../rustc_session" }
1819
rustc_span = { path = "../rustc_span" }

compiler/rustc_parse/src/parser/diagnostics.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, Er
2121
use rustc_errors::{
2222
Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult,
2323
};
24+
use rustc_macros::SessionDiagnostic;
2425
use rustc_span::source_map::Spanned;
2526
use rustc_span::symbol::{kw, Ident};
2627
use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
@@ -241,6 +242,16 @@ impl MultiSugg {
241242
err.multipart_suggestions(msg, suggestions.map(|s| s.patches), applicability);
242243
}
243244
}
245+
246+
#[derive(SessionDiagnostic)]
247+
#[error(slug = "parser-maybe-report-ambiguous-plus")]
248+
struct AmbiguousPlus {
249+
pub sum_ty: String,
250+
#[primary_span]
251+
#[suggestion(code = "({sum_ty})")]
252+
pub span: Span,
253+
}
254+
244255
// SnapshotParser is used to create a snapshot of the parser
245256
// without causing duplicate errors being emitted when the `Parser`
246257
// is dropped.
@@ -1196,15 +1207,7 @@ impl<'a> Parser<'a> {
11961207
ty: &Ty,
11971208
) {
11981209
if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi {
1199-
let sum_with_parens = format!("({})", pprust::ty_to_string(&ty));
1200-
self.struct_span_err(ty.span, "ambiguous `+` in a type")
1201-
.span_suggestion(
1202-
ty.span,
1203-
"use parentheses to disambiguate",
1204-
sum_with_parens,
1205-
Applicability::MachineApplicable,
1206-
)
1207-
.emit();
1210+
self.sess.emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(&ty), span: ty.span });
12081211
}
12091212
}
12101213

compiler/rustc_session/src/parse.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
44
use crate::config::CheckCfg;
55
use crate::lint::{BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId};
6+
use crate::SessionDiagnostic;
67
use rustc_ast::node_id::NodeId;
78
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
89
use rustc_data_structures::sync::{Lock, Lrc};
910
use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler};
1011
use rustc_errors::{
1112
error_code, fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder,
12-
ErrorGuaranteed, MultiSpan,
13+
DiagnosticMessage, ErrorGuaranteed, MultiSpan,
1314
};
1415
use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
1516
use rustc_span::edition::Edition;
@@ -287,4 +288,23 @@ impl ParseSess {
287288
pub fn proc_macro_quoted_spans(&self) -> Vec<Span> {
288289
self.proc_macro_quoted_spans.lock().clone()
289290
}
291+
292+
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
293+
err.into_diagnostic(self).emit()
294+
}
295+
296+
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
297+
warning.into_diagnostic(self).emit()
298+
}
299+
300+
pub fn struct_err(
301+
&self,
302+
msg: impl Into<DiagnosticMessage>,
303+
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
304+
self.span_diagnostic.struct_err(msg)
305+
}
306+
307+
pub fn struct_warn(&self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'_, ()> {
308+
self.span_diagnostic.struct_warn(msg)
309+
}
290310
}

compiler/rustc_session/src/session.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub struct PerfStats {
212212
pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> {
213213
/// Write out as a diagnostic out of `sess`.
214214
#[must_use]
215-
fn into_diagnostic(self, sess: &'a Session) -> DiagnosticBuilder<'a, T>;
215+
fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, T>;
216216
}
217217

218218
impl Session {
@@ -334,7 +334,7 @@ impl Session {
334334
&self,
335335
msg: impl Into<DiagnosticMessage>,
336336
) -> DiagnosticBuilder<'_, ErrorGuaranteed> {
337-
self.diagnostic().struct_err(msg)
337+
self.parse_sess.struct_err(msg)
338338
}
339339
pub fn struct_err_with_code(
340340
&self,
@@ -414,10 +414,10 @@ impl Session {
414414
self.diagnostic().err(msg)
415415
}
416416
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
417-
err.into_diagnostic(self).emit()
417+
self.parse_sess.emit_err(err)
418418
}
419419
pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) {
420-
warning.into_diagnostic(self).emit()
420+
self.parse_sess.emit_warning(warning)
421421
}
422422
#[inline]
423423
pub fn err_count(&self) -> usize {

0 commit comments

Comments
 (0)