Skip to content

Commit 41effad

Browse files
committed
Migrate ast_lowering::expr to SessionDiagnostic
1 parent 0d4f665 commit 41effad

File tree

3 files changed

+121
-76
lines changed

3 files changed

+121
-76
lines changed

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,73 @@ pub struct MisplacedImplTrait {
7878
pub span: Span,
7979
pub position: String,
8080
}
81+
82+
#[derive(SessionDiagnostic, Clone, Copy)]
83+
#[error(ast_lowering::rustc_box_attribute_error)]
84+
pub struct RustcBoxAttributeError {
85+
#[primary_span]
86+
pub span: Span,
87+
}
88+
89+
#[derive(SessionDiagnostic, Clone, Copy)]
90+
#[error(ast_lowering::underscore_expr_lhs_assign)]
91+
pub struct UnderscoreExprLhsAssign {
92+
#[primary_span]
93+
#[label]
94+
pub span: Span,
95+
}
96+
97+
#[derive(SessionDiagnostic, Clone, Copy)]
98+
#[error(ast_lowering::base_expression_double_dot)]
99+
pub struct BaseExpressionDoubleDot {
100+
#[primary_span]
101+
#[label]
102+
pub span: Span,
103+
}
104+
105+
#[derive(SessionDiagnostic, Clone, Copy)]
106+
#[error(ast_lowering::await_only_in_async_fn_and_blocks, code = "E0728")]
107+
pub struct AwaitOnlyInAsyncFnAndBlocks {
108+
#[primary_span]
109+
#[label]
110+
pub dot_await_span: Span,
111+
#[label(ast_lowering::this_not_async)]
112+
pub item_span: Option<Span>,
113+
}
114+
115+
#[derive(SessionDiagnostic, Clone, Copy)]
116+
#[error(ast_lowering::generator_too_many_parameters, code = "E0628")]
117+
pub struct GeneratorTooManyParameters {
118+
#[primary_span]
119+
pub fn_decl_span: Span,
120+
}
121+
122+
#[derive(SessionDiagnostic, Clone, Copy)]
123+
#[error(ast_lowering::closure_cannot_be_static, code = "E0697")]
124+
pub struct ClosureCannotBeStatic {
125+
#[primary_span]
126+
pub fn_decl_span: Span,
127+
}
128+
129+
#[derive(SessionDiagnostic, Clone, Copy)]
130+
#[help]
131+
#[error(ast_lowering::async_non_move_closure_not_supported, code = "E0708")]
132+
pub struct AsyncNonMoveClosureNotSupported {
133+
#[primary_span]
134+
pub fn_decl_span: Span,
135+
}
136+
137+
#[derive(SessionDiagnostic, Clone, Copy)]
138+
#[error(ast_lowering::functional_record_update_destructuring_assignment)]
139+
pub struct FunctionalRecordUpdateDestructuringAssignemnt {
140+
#[primary_span]
141+
#[suggestion(code = "", applicability = "machine-applicable")]
142+
pub span: Span,
143+
}
144+
145+
#[derive(SessionDiagnostic, Clone, Copy)]
146+
#[error(ast_lowering::async_generators_not_supported, code = "E0727")]
147+
pub struct AsyncGeneratorsNotSupported {
148+
#[primary_span]
149+
pub span: Span,
150+
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
use super::errors::{
2+
AsyncGeneratorsNotSupported, AsyncNonMoveClosureNotSupported, AwaitOnlyInAsyncFnAndBlocks,
3+
BaseExpressionDoubleDot, ClosureCannotBeStatic, FunctionalRecordUpdateDestructuringAssignemnt,
4+
GeneratorTooManyParameters, RustcBoxAttributeError, UnderscoreExprLhsAssign,
5+
};
16
use super::ResolverAstLoweringExt;
27
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
38
use crate::{FnDeclKind, ImplTraitPosition};
@@ -7,7 +12,6 @@ use rustc_ast::ptr::P as AstP;
712
use rustc_ast::*;
813
use rustc_data_structures::stack::ensure_sufficient_stack;
914
use rustc_data_structures::thin_vec::ThinVec;
10-
use rustc_errors::struct_span_err;
1115
use rustc_hir as hir;
1216
use rustc_hir::def::Res;
1317
use rustc_hir::definitions::DefPathData;
@@ -46,13 +50,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
4650
let hir_id = self.lower_node_id(e.id);
4751
return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
4852
} else {
49-
self.tcx.sess
50-
.struct_span_err(
51-
e.span,
52-
"#[rustc_box] requires precisely one argument \
53-
and no other attributes are allowed",
54-
)
55-
.emit();
53+
self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
5654
hir::ExprKind::Err
5755
}
5856
} else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
@@ -212,13 +210,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
212210
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
213211
}
214212
ExprKind::Underscore => {
215-
self.tcx
216-
.sess.struct_span_err(
217-
e.span,
218-
"in expressions, `_` can only be used on the left-hand side of an assignment",
219-
)
220-
.span_label(e.span, "`_` not allowed here")
221-
.emit();
213+
self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
222214
hir::ExprKind::Err
223215
}
224216
ExprKind::Path(ref qself, ref path) => {
@@ -250,11 +242,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
250242
let rest = match &se.rest {
251243
StructRest::Base(e) => Some(self.lower_expr(e)),
252244
StructRest::Rest(sp) => {
253-
self.tcx
254-
.sess
255-
.struct_span_err(*sp, "base expression required after `..`")
256-
.span_label(*sp, "add a base expression here")
257-
.emit();
245+
self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
258246
Some(&*self.arena.alloc(self.expr_err(*sp)))
259247
}
260248
StructRest::None => None,
@@ -663,17 +651,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
663651
match self.generator_kind {
664652
Some(hir::GeneratorKind::Async(_)) => {}
665653
Some(hir::GeneratorKind::Gen) | None => {
666-
let mut err = struct_span_err!(
667-
self.tcx.sess,
654+
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
668655
dot_await_span,
669-
E0728,
670-
"`await` is only allowed inside `async` functions and blocks"
671-
);
672-
err.span_label(dot_await_span, "only allowed inside `async` functions and blocks");
673-
if let Some(item_sp) = self.current_item {
674-
err.span_label(item_sp, "this is not `async`");
675-
}
676-
err.emit();
656+
item_span: self.current_item,
657+
});
677658
}
678659
}
679660
let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
@@ -893,13 +874,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
893874
match generator_kind {
894875
Some(hir::GeneratorKind::Gen) => {
895876
if decl.inputs.len() > 1 {
896-
struct_span_err!(
897-
self.tcx.sess,
898-
fn_decl_span,
899-
E0628,
900-
"too many parameters for a generator (expected 0 or 1 parameters)"
901-
)
902-
.emit();
877+
self.tcx.sess.emit_err(GeneratorTooManyParameters { fn_decl_span });
903878
}
904879
Some(movability)
905880
}
@@ -908,13 +883,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
908883
}
909884
None => {
910885
if movability == Movability::Static {
911-
struct_span_err!(
912-
self.tcx.sess,
913-
fn_decl_span,
914-
E0697,
915-
"closures cannot be static"
916-
)
917-
.emit();
886+
self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
918887
}
919888
None
920889
}
@@ -961,17 +930,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
961930
let body = self.with_new_scopes(|this| {
962931
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
963932
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
964-
struct_span_err!(
965-
this.tcx.sess,
966-
fn_decl_span,
967-
E0708,
968-
"`async` non-`move` closures with parameters are not currently supported",
969-
)
970-
.help(
971-
"consider using `let` statements to manually capture \
972-
variables by reference before entering an `async move` closure",
973-
)
974-
.emit();
933+
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
975934
}
976935

977936
// Transform `async |x: u8| -> X { ... }` into
@@ -1211,20 +1170,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
12111170
);
12121171
let fields_omitted = match &se.rest {
12131172
StructRest::Base(e) => {
1214-
self.tcx
1215-
.sess
1216-
.struct_span_err(
1217-
e.span,
1218-
"functional record updates are not allowed in destructuring \
1219-
assignments",
1220-
)
1221-
.span_suggestion(
1222-
e.span,
1223-
"consider removing the trailing pattern",
1224-
"",
1225-
rustc_errors::Applicability::MachineApplicable,
1226-
)
1227-
.emit();
1173+
self.tcx.sess.emit_err(FunctionalRecordUpdateDestructuringAssignemnt {
1174+
span: e.span,
1175+
});
12281176
true
12291177
}
12301178
StructRest::Rest(_) => true,
@@ -1421,13 +1369,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14211369
match self.generator_kind {
14221370
Some(hir::GeneratorKind::Gen) => {}
14231371
Some(hir::GeneratorKind::Async(_)) => {
1424-
struct_span_err!(
1425-
self.tcx.sess,
1426-
span,
1427-
E0727,
1428-
"`async` generators are not yet supported"
1429-
)
1430-
.emit();
1372+
self.tcx.sess.emit_err(AsyncGeneratorsNotSupported { span });
14311373
}
14321374
None => self.generator_kind = Some(hir::GeneratorKind::Gen),
14331375
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,36 @@ ast_lowering_remove_parentheses = remove these parentheses
1616
1717
ast_lowering_misplaced_impl_trait =
1818
`impl Trait` only allowed in function and inherent method return types, not in {$position}
19+
20+
ast_lowering_rustc_box_attribute_error =
21+
#[rustc_box] requires precisely one argument and no other attributes are allowed
22+
23+
ast_lowering_underscore_expr_lhs_assign =
24+
in expressions, `_` can only be used on the left-hand side of an assignment
25+
.label = `_` not allowed here
26+
27+
ast_lowering_base_expression_double_dot =
28+
base expression required after `..`
29+
.label = add a base expression here
30+
31+
ast_lowering_await_only_in_async_fn_and_blocks =
32+
`await` is only allowed inside `async` functions and blocks
33+
.label = only allowed inside `async` functions and blocks
34+
35+
ast_lowering_this_not_async = this is not `async`
36+
37+
ast_lowering_generator_too_many_parameters =
38+
too many parameters for a generator (expected 0 or 1 parameters)
39+
40+
ast_lowering_closure_cannot_be_static = closures cannot be static
41+
42+
ast_lowering_async_non_move_closure_not_supported =
43+
`async` non-`move` closures with parameters are not currently supported
44+
.help = consider using `let` statements to manually capture variables by reference before entering an `async move` closure
45+
46+
ast_lowering_functional_record_update_destructuring_assignment =
47+
functional record updates are not allowed in destructuring assignments
48+
.suggestion = consider removing the trailing pattern
49+
50+
ast_lowering_async_generators_not_supported =
51+
`async` generators are not yet supported

0 commit comments

Comments
 (0)