Skip to content

Commit 29fcfa5

Browse files
committed
Migrate ast_lowering::pat to SessionDiagnostic
1 parent 11d9f34 commit 29fcfa5

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

compiler/rustc_ast_lowering/src/errors.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
22
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3-
use rustc_span::{Span, Symbol};
3+
use rustc_span::{symbol::Ident, Span, Symbol};
44

55
#[derive(SessionDiagnostic, Clone, Copy)]
66
#[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
@@ -270,3 +270,39 @@ pub struct RegisterConflict<'a> {
270270
#[help]
271271
pub in_out: Option<Span>,
272272
}
273+
274+
#[derive(SessionDiagnostic, Clone, Copy)]
275+
#[help]
276+
#[error(ast_lowering::sub_tuple_binding)]
277+
pub struct SubTupleBinding<'a> {
278+
#[primary_span]
279+
#[label]
280+
#[suggestion_verbose(
281+
ast_lowering::sub_tuple_binding_suggestion,
282+
code = "..",
283+
applicability = "maybe-incorrect"
284+
)]
285+
pub span: Span,
286+
pub ident: Ident,
287+
pub ident_name: Symbol,
288+
pub ctx: &'a str,
289+
}
290+
291+
#[derive(SessionDiagnostic, Clone, Copy)]
292+
#[error(ast_lowering::extra_double_dot)]
293+
pub struct ExtraDoubleDot<'a> {
294+
#[primary_span]
295+
#[label]
296+
pub span: Span,
297+
#[label(ast_lowering::previously_used_here)]
298+
pub prev_span: Span,
299+
pub ctx: &'a str,
300+
}
301+
302+
#[derive(SessionDiagnostic, Clone, Copy)]
303+
#[note]
304+
#[error(ast_lowering::misplaced_double_dot)]
305+
pub struct MisplacedDoubleDot {
306+
#[primary_span]
307+
pub span: Span,
308+
}

compiler/rustc_ast_lowering/src/pat.rs

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use super::errors::{ExtraDoubleDot, MisplacedDoubleDot, SubTupleBinding};
12
use super::ResolverAstLoweringExt;
23
use super::{ImplTraitContext, LoweringContext, ParamMode};
34
use crate::ImplTraitPosition;
45

56
use rustc_ast::ptr::P;
67
use rustc_ast::*;
78
use rustc_data_structures::stack::ensure_sufficient_stack;
8-
use rustc_errors::Applicability;
99
use rustc_hir as hir;
1010
use rustc_hir::def::Res;
1111
use rustc_span::symbol::Ident;
@@ -134,20 +134,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
134134
// This is not allowed as a sub-tuple pattern
135135
PatKind::Ident(ref _bm, ident, Some(ref sub)) if sub.is_rest() => {
136136
let sp = pat.span;
137-
self.diagnostic()
138-
.struct_span_err(
139-
sp,
140-
&format!("`{} @` is not allowed in a {}", ident.name, ctx),
141-
)
142-
.span_label(sp, "this is only allowed in slice patterns")
143-
.help("remove this and bind each tuple field independently")
144-
.span_suggestion_verbose(
145-
sp,
146-
&format!("if you don't need to use the contents of {}, discard the tuple's remaining fields", ident),
147-
"..",
148-
Applicability::MaybeIncorrect,
149-
)
150-
.emit();
137+
self.tcx.sess.emit_err(SubTupleBinding {
138+
span: sp,
139+
ident_name: ident.name,
140+
ident,
141+
ctx,
142+
});
151143
}
152144
_ => {}
153145
}
@@ -296,19 +288,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
296288

297289
/// Emit a friendly error for extra `..` patterns in a tuple/tuple struct/slice pattern.
298290
pub(crate) fn ban_extra_rest_pat(&self, sp: Span, prev_sp: Span, ctx: &str) {
299-
self.diagnostic()
300-
.struct_span_err(sp, &format!("`..` can only be used once per {} pattern", ctx))
301-
.span_label(sp, &format!("can only be used once per {} pattern", ctx))
302-
.span_label(prev_sp, "previously used here")
303-
.emit();
291+
self.tcx.sess.emit_err(ExtraDoubleDot { span: sp, prev_span: prev_sp, ctx });
304292
}
305293

306294
/// Used to ban the `..` pattern in places it shouldn't be semantically.
307295
fn ban_illegal_rest_pat(&self, sp: Span) -> hir::PatKind<'hir> {
308-
self.diagnostic()
309-
.struct_span_err(sp, "`..` patterns are not allowed here")
310-
.note("only allowed in tuple, tuple struct, and slice patterns")
311-
.emit();
296+
self.tcx.sess.emit_err(MisplacedDoubleDot { span: sp });
312297

313298
// We're not in a list context so `..` can be reasonably treated
314299
// as `_` because it should always be valid and roughly matches the

compiler/rustc_error_messages/locales/en-US/ast_lowering.ftl

+17
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,20 @@ ast_lowering_register_conflict =
103103
ast_lowering_register1 = register `{$reg1_name}`
104104
105105
ast_lowering_register2 = register `{$reg2_name}`
106+
107+
ast_lowering_sub_tuple_binding =
108+
`{$ident_name} @` is not allowed in a {$ctx}
109+
.label = this is only allowed in slice patterns
110+
.help = remove this and bind each tuple field independently
111+
112+
ast_lowering_sub_tuple_binding_suggestion = if you don't need to use the contents of {$ident}, discard the tuple's remaining fields
113+
114+
ast_lowering_extra_double_dot =
115+
`..` can only be used once per {$ctx} pattern
116+
.label = can only be used once per {$ctx} pattern
117+
118+
ast_lowering_previously_used_here = previously used here
119+
120+
ast_lowering_misplaced_double_dot =
121+
`..` patterns are not allowed here
122+
.note = only allowed in tuple, tuple struct, and slice patterns

0 commit comments

Comments
 (0)