Skip to content

Commit 73ae38b

Browse files
committed
Migrate ast_lowering::path to SessionDiagnostic
1 parent a8a33cf commit 73ae38b

File tree

7 files changed

+46
-15
lines changed

7 files changed

+46
-15
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3575,6 +3575,7 @@ dependencies = [
35753575
"rustc_errors",
35763576
"rustc_hir",
35773577
"rustc_index",
3578+
"rustc_macros",
35783579
"rustc_middle",
35793580
"rustc_query_system",
35803581
"rustc_session",

Diff for: compiler/rustc_ast_lowering/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rustc_target = { path = "../rustc_target" }
1515
rustc_data_structures = { path = "../rustc_data_structures" }
1616
rustc_index = { path = "../rustc_index" }
1717
rustc_middle = { path = "../rustc_middle" }
18+
rustc_macros = { path = "../rustc_macros" }
1819
rustc_query_system = { path = "../rustc_query_system" }
1920
rustc_span = { path = "../rustc_span" }
2021
rustc_errors = { path = "../rustc_errors" }

Diff for: compiler/rustc_ast_lowering/src/errors.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic};
2+
use rustc_macros::SessionDiagnostic;
3+
use rustc_span::Span;
4+
5+
#[derive(SessionDiagnostic, Clone, Copy)]
6+
#[error(ast_lowering::generic_type_with_parentheses, code = "E0214")]
7+
pub struct GenericTypeWithParentheses {
8+
#[primary_span]
9+
#[label]
10+
pub span: Span,
11+
#[subdiagnostic]
12+
pub sub: Option<UseAngleBrackets>,
13+
}
14+
15+
#[derive(Clone, Copy)]
16+
pub struct UseAngleBrackets {
17+
pub open_param: Span,
18+
pub close_param: Span,
19+
}
20+
21+
impl AddSubdiagnostic for UseAngleBrackets {
22+
fn add_to_diagnostic(self, diag: &mut Diagnostic) {
23+
diag.multipart_suggestion(
24+
fluent::ast_lowering::use_angle_brackets,
25+
vec![(self.open_param, String::from("<")), (self.close_param, String::from(">"))],
26+
Applicability::MaybeIncorrect,
27+
);
28+
}
29+
}

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ macro_rules! arena_vec {
7575

7676
mod asm;
7777
mod block;
78+
mod errors;
7879
mod expr;
7980
mod index;
8081
mod item;

Diff for: compiler/rustc_ast_lowering/src/path.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::ImplTraitPosition;
22

3+
use super::errors::{GenericTypeWithParentheses, UseAngleBrackets};
34
use super::ResolverAstLoweringExt;
45
use super::{GenericArgsCtor, LifetimeRes, ParenthesizedGenericArgs};
56
use super::{ImplTraitContext, LoweringContext, ParamMode};
67

78
use rustc_ast::{self as ast, *};
8-
use rustc_errors::{struct_span_err, Applicability};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, PartialRes, Res};
1111
use rustc_hir::GenericArg;
@@ -185,18 +185,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
185185
) -> hir::PathSegment<'hir> {
186186
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment,);
187187
let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args {
188-
let msg = "parenthesized type parameters may only be used with a `Fn` trait";
189188
match **generic_args {
190189
GenericArgs::AngleBracketed(ref data) => {
191190
self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
192191
}
193192
GenericArgs::Parenthesized(ref data) => match parenthesized_generic_args {
194193
ParenthesizedGenericArgs::Ok => self.lower_parenthesized_parameter_data(data),
195194
ParenthesizedGenericArgs::Err => {
196-
let mut err = struct_span_err!(self.tcx.sess, data.span, E0214, "{}", msg);
197-
err.span_label(data.span, "only `Fn` traits may use parentheses");
198195
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
199-
if !data.inputs.is_empty() {
196+
let sub = if !data.inputs.is_empty() {
200197
// Start of the span to the 1st character of 1st argument
201198
let open_param = data.inputs_span.shrink_to_lo().to(data
202199
.inputs
@@ -212,16 +209,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
212209
.span
213210
.shrink_to_hi()
214211
.to(data.inputs_span.shrink_to_hi());
215-
err.multipart_suggestion(
216-
&format!("use angle brackets instead",),
217-
vec![
218-
(open_param, String::from("<")),
219-
(close_param, String::from(">")),
220-
],
221-
Applicability::MaybeIncorrect,
222-
);
223-
}
224-
err.emit();
212+
213+
Some(UseAngleBrackets { open_param, close_param })
214+
} else {
215+
None
216+
};
217+
self.tcx.sess.emit_err(GenericTypeWithParentheses { span: data.span, sub });
225218
(
226219
self.lower_angle_bracketed_parameter_data(
227220
&data.as_angle_bracketed_args(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ast_lowering_generic_type_with_parentheses =
2+
parenthesized type parameters may only be used with a `Fn` trait
3+
.label = only `Fn` traits may use parentheses
4+
5+
ast_lowering_use_angle_brackets = use angle brackets instead

Diff for: compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use unic_langid::{langid, LanguageIdentifier};
3232

3333
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
3434
fluent_messages! {
35+
ast_lowering => "../locales/en-US/ast_lowering.ftl",
3536
ast_passes => "../locales/en-US/ast_passes.ftl",
3637
borrowck => "../locales/en-US/borrowck.ftl",
3738
builtin_macros => "../locales/en-US/builtin_macros.ftl",

0 commit comments

Comments
 (0)