Skip to content

Commit 1382d30

Browse files
committed
Migrate ast_lowering::expr to SessionDiagnostic
1 parent 0043d10 commit 1382d30

File tree

3 files changed

+125
-80
lines changed

3 files changed

+125
-80
lines changed

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_macros::SessionDiagnostic;
33
use rustc_span::{Span, Symbol};
44

55
#[derive(SessionDiagnostic, Clone, Copy)]
6-
#[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
6+
#[diag(ast_lowering::generic_type_with_parentheses, code = "E0214")]
77
pub struct GenericTypeWithParentheses {
88
#[primary_span]
99
#[label]
@@ -30,7 +30,7 @@ impl AddSubdiagnostic for UseAngleBrackets {
3030

3131
#[derive(SessionDiagnostic)]
3232
#[help]
33-
#[error(ast_lowering::invalid_abi, code = "E0703")]
33+
#[diag(ast_lowering::invalid_abi, code = "E0703")]
3434
pub struct InvalidAbi {
3535
#[primary_span]
3636
#[label]
@@ -40,7 +40,7 @@ pub struct InvalidAbi {
4040
}
4141

4242
#[derive(SessionDiagnostic, Clone, Copy)]
43-
#[error(ast_lowering::assoc_ty_parentheses)]
43+
#[diag(ast_lowering::assoc_ty_parentheses)]
4444
pub struct AssocTyParentheses {
4545
#[primary_span]
4646
pub span: Span,
@@ -72,9 +72,79 @@ impl AddSubdiagnostic for AssocTyParenthesesSub {
7272
}
7373

7474
#[derive(SessionDiagnostic)]
75-
#[error(ast_lowering::misplaced_impl_trait, code = "E0562")]
75+
#[diag(ast_lowering::misplaced_impl_trait, code = "E0562")]
7676
pub struct MisplacedImplTrait {
7777
#[primary_span]
7878
pub span: Span,
7979
pub position: String,
8080
}
81+
82+
#[derive(SessionDiagnostic, Clone, Copy)]
83+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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+
#[diag(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};
@@ -6,7 +11,6 @@ use rustc_ast::attr;
611
use rustc_ast::ptr::P as AstP;
712
use rustc_ast::*;
813
use rustc_data_structures::stack::ensure_sufficient_stack;
9-
use rustc_errors::struct_span_err;
1014
use rustc_hir as hir;
1115
use rustc_hir::def::Res;
1216
use rustc_hir::definitions::DefPathData;
@@ -45,13 +49,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
4549
let hir_id = self.lower_node_id(e.id);
4650
return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
4751
} else {
48-
self.tcx.sess
49-
.struct_span_err(
50-
e.span,
51-
"#[rustc_box] requires precisely one argument \
52-
and no other attributes are allowed",
53-
)
54-
.emit();
52+
self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
5553
hir::ExprKind::Err
5654
}
5755
} else if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
@@ -211,13 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
211209
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
212210
}
213211
ExprKind::Underscore => {
214-
self.tcx
215-
.sess.struct_span_err(
216-
e.span,
217-
"in expressions, `_` can only be used on the left-hand side of an assignment",
218-
)
219-
.span_label(e.span, "`_` not allowed here")
220-
.emit();
212+
self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
221213
hir::ExprKind::Err
222214
}
223215
ExprKind::Path(ref qself, ref path) => {
@@ -249,11 +241,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
249241
let rest = match &se.rest {
250242
StructRest::Base(e) => Some(self.lower_expr(e)),
251243
StructRest::Rest(sp) => {
252-
self.tcx
253-
.sess
254-
.struct_span_err(*sp, "base expression required after `..`")
255-
.span_label(*sp, "add a base expression here")
256-
.emit();
244+
self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
257245
Some(&*self.arena.alloc(self.expr_err(*sp)))
258246
}
259247
StructRest::None => None,
@@ -662,17 +650,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
662650
match self.generator_kind {
663651
Some(hir::GeneratorKind::Async(_)) => {}
664652
Some(hir::GeneratorKind::Gen) | None => {
665-
let mut err = struct_span_err!(
666-
self.tcx.sess,
653+
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
667654
dot_await_span,
668-
E0728,
669-
"`await` is only allowed inside `async` functions and blocks"
670-
);
671-
err.span_label(dot_await_span, "only allowed inside `async` functions and blocks");
672-
if let Some(item_sp) = self.current_item {
673-
err.span_label(item_sp, "this is not `async`");
674-
}
675-
err.emit();
655+
item_span: self.current_item,
656+
});
676657
}
677658
}
678659
let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
@@ -892,13 +873,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
892873
match generator_kind {
893874
Some(hir::GeneratorKind::Gen) => {
894875
if decl.inputs.len() > 1 {
895-
struct_span_err!(
896-
self.tcx.sess,
897-
fn_decl_span,
898-
E0628,
899-
"too many parameters for a generator (expected 0 or 1 parameters)"
900-
)
901-
.emit();
876+
self.tcx.sess.emit_err(GeneratorTooManyParameters { fn_decl_span });
902877
}
903878
Some(movability)
904879
}
@@ -907,13 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
907882
}
908883
None => {
909884
if movability == Movability::Static {
910-
struct_span_err!(
911-
self.tcx.sess,
912-
fn_decl_span,
913-
E0697,
914-
"closures cannot be static"
915-
)
916-
.emit();
885+
self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
917886
}
918887
None
919888
}
@@ -960,17 +929,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
960929
let body = self.with_new_scopes(|this| {
961930
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
962931
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
963-
struct_span_err!(
964-
this.tcx.sess,
965-
fn_decl_span,
966-
E0708,
967-
"`async` non-`move` closures with parameters are not currently supported",
968-
)
969-
.help(
970-
"consider using `let` statements to manually capture \
971-
variables by reference before entering an `async move` closure",
972-
)
973-
.emit();
932+
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
974933
}
975934

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

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)