Skip to content

Commit 0043d10

Browse files
committed
Migrate ast_lowering::lib and ast_lowering::item to SessionDiagnostic
1 parent 73ae38b commit 0043d10

File tree

4 files changed

+83
-32
lines changed

4 files changed

+83
-32
lines changed

compiler/rustc_ast_lowering/src/errors.rs

+52-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;
3-
use rustc_span::Span;
3+
use rustc_span::{Span, Symbol};
44

55
#[derive(SessionDiagnostic, Clone, Copy)]
66
#[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
@@ -27,3 +27,54 @@ impl AddSubdiagnostic for UseAngleBrackets {
2727
);
2828
}
2929
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[help]
33+
#[error(ast_lowering::invalid_abi, code = "E0703")]
34+
pub struct InvalidAbi {
35+
#[primary_span]
36+
#[label]
37+
pub span: Span,
38+
pub abi: Symbol,
39+
pub valid_abis: String,
40+
}
41+
42+
#[derive(SessionDiagnostic, Clone, Copy)]
43+
#[error(ast_lowering::assoc_ty_parentheses)]
44+
pub struct AssocTyParentheses {
45+
#[primary_span]
46+
pub span: Span,
47+
#[subdiagnostic]
48+
pub sub: AssocTyParenthesesSub,
49+
}
50+
51+
#[derive(Clone, Copy)]
52+
pub enum AssocTyParenthesesSub {
53+
Empty { parentheses_span: Span },
54+
NotEmpty { open_param: Span, close_param: Span },
55+
}
56+
57+
impl AddSubdiagnostic for AssocTyParenthesesSub {
58+
fn add_to_diagnostic(self, diag: &mut Diagnostic) {
59+
match self {
60+
Self::Empty { parentheses_span } => diag.multipart_suggestion(
61+
fluent::ast_lowering::remove_parentheses,
62+
vec![(parentheses_span, String::new())],
63+
Applicability::MaybeIncorrect,
64+
),
65+
Self::NotEmpty { open_param, close_param } => diag.multipart_suggestion(
66+
fluent::ast_lowering::use_angle_brackets,
67+
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
68+
Applicability::MaybeIncorrect,
69+
),
70+
};
71+
}
72+
}
73+
74+
#[derive(SessionDiagnostic)]
75+
#[error(ast_lowering::misplaced_impl_trait, code = "E0562")]
76+
pub struct MisplacedImplTrait {
77+
#[primary_span]
78+
pub span: Span,
79+
pub position: String,
80+
}

compiler/rustc_ast_lowering/src/item.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::errors::InvalidAbi;
12
use super::ResolverAstLoweringExt;
23
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
34
use super::{FnDeclKind, LoweringContext, ParamMode};
@@ -7,7 +8,6 @@ use rustc_ast::visit::AssocCtxt;
78
use rustc_ast::*;
89
use rustc_data_structures::fx::FxHashMap;
910
use rustc_data_structures::sorted_map::SortedMap;
10-
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
1212
use rustc_hir::def::{DefKind, Res};
1313
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -1260,10 +1260,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
12601260
}
12611261

12621262
fn error_on_invalid_abi(&self, abi: StrLit) {
1263-
struct_span_err!(self.tcx.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol)
1264-
.span_label(abi.span, "invalid ABI")
1265-
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
1266-
.emit();
1263+
self.tcx.sess.emit_err(InvalidAbi {
1264+
span: abi.span,
1265+
abi: abi.symbol,
1266+
valid_abis: abi::all_names().join(", "),
1267+
});
12671268
}
12681269

12691270
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {

compiler/rustc_ast_lowering/src/lib.rs

+12-26
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#[macro_use]
4040
extern crate tracing;
4141

42+
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
43+
4244
use rustc_ast::ptr::P;
4345
use rustc_ast::visit;
4446
use rustc_ast::{self as ast, *};
@@ -49,7 +51,7 @@ use rustc_data_structures::fx::FxHashMap;
4951
use rustc_data_structures::sorted_map::SortedMap;
5052
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5153
use rustc_data_structures::sync::Lrc;
52-
use rustc_errors::{struct_span_err, Applicability, Handler, StashKey};
54+
use rustc_errors::{Handler, StashKey};
5355
use rustc_hir as hir;
5456
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5557
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
@@ -1071,19 +1073,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10711073
}
10721074

10731075
fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
1074-
let mut err = self.tcx.sess.struct_span_err(
1075-
data.span,
1076-
"parenthesized generic arguments cannot be used in associated type constraints",
1077-
);
10781076
// Suggest removing empty parentheses: "Trait()" -> "Trait"
1079-
if data.inputs.is_empty() {
1077+
let sub = if data.inputs.is_empty() {
10801078
let parentheses_span =
10811079
data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
1082-
err.multipart_suggestion(
1083-
"remove these parentheses",
1084-
vec![(parentheses_span, String::new())],
1085-
Applicability::MaybeIncorrect,
1086-
);
1080+
AssocTyParenthesesSub::Empty { parentheses_span }
10871081
}
10881082
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
10891083
else {
@@ -1097,13 +1091,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10971091
// End of last argument to end of parameters
10981092
let close_param =
10991093
data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
1100-
err.multipart_suggestion(
1101-
&format!("use angle brackets instead",),
1102-
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
1103-
Applicability::MaybeIncorrect,
1104-
);
1105-
}
1106-
err.emit();
1094+
AssocTyParenthesesSub::NotEmpty { open_param, close_param }
1095+
};
1096+
self.tcx.sess.emit_err(AssocTyParentheses { span: data.span, sub });
11071097
}
11081098

11091099
#[instrument(level = "debug", skip(self))]
@@ -1342,14 +1332,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13421332
path
13431333
}
13441334
ImplTraitContext::Disallowed(position) => {
1345-
let mut err = struct_span_err!(
1346-
self.tcx.sess,
1347-
t.span,
1348-
E0562,
1349-
"`impl Trait` only allowed in function and inherent method return types, not in {}",
1350-
position
1351-
);
1352-
err.emit();
1335+
self.tcx.sess.emit_err(MisplacedImplTrait {
1336+
span: t.span,
1337+
position: position.to_string(),
1338+
});
13531339
hir::TyKind::Err
13541340
}
13551341
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ ast_lowering_generic_type_with_parentheses =
33
.label = only `Fn` traits may use parentheses
44
55
ast_lowering_use_angle_brackets = use angle brackets instead
6+
7+
ast_lowering_invalid_abi =
8+
invalid ABI: found `{$abi}`
9+
.label = invalid ABI
10+
.help = valid ABIs: {$valid_abis}
11+
12+
ast_lowering_assoc_ty_parentheses =
13+
parenthesized generic arguments cannot be used in associated type constraints
14+
15+
ast_lowering_remove_parentheses = remove these parentheses
16+
17+
ast_lowering_misplaced_impl_trait =
18+
`impl Trait` only allowed in function and inherent method return types, not in {$position}

0 commit comments

Comments
 (0)