Skip to content

Commit cc5af33

Browse files
committed
Autoderive ExternBlockSuggestion
1 parent 1f72129 commit cc5af33

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1100,16 +1100,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11001100
replace_span: self.ending_semi_or_hi(item.span),
11011101
extern_block_suggestion: match sig.header.ext {
11021102
Extern::None => None,
1103-
Extern::Implicit(start_span) => Some(ExternBlockSuggestion {
1103+
Extern::Implicit(start_span) => Some(ExternBlockSuggestion::Implicit {
11041104
start_span,
11051105
end_span: item.span.shrink_to_hi(),
1106-
abi: None,
1107-
}),
1108-
Extern::Explicit(abi, start_span) => Some(ExternBlockSuggestion {
1109-
start_span,
1110-
end_span: item.span.shrink_to_hi(),
1111-
abi: Some(abi.symbol_unescaped),
11121106
}),
1107+
Extern::Explicit(abi, start_span) => {
1108+
Some(ExternBlockSuggestion::Explicit {
1109+
start_span,
1110+
end_span: item.span.shrink_to_hi(),
1111+
abi: abi.symbol_unescaped,
1112+
})
1113+
}
11131114
},
11141115
});
11151116
}

compiler/rustc_ast_passes/src/errors.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Errors emitted by ast_passes.
22
3-
use rustc_errors::{fluent, AddToDiagnostic, Applicability, Diagnostic, SubdiagnosticMessage};
43
use rustc_macros::{Diagnostic, Subdiagnostic};
54
use rustc_span::{Span, Symbol};
65

@@ -207,28 +206,21 @@ pub struct FnWithoutBody {
207206
pub extern_block_suggestion: Option<ExternBlockSuggestion>,
208207
}
209208

210-
pub struct ExternBlockSuggestion {
211-
pub start_span: Span,
212-
pub end_span: Span,
213-
pub abi: Option<Symbol>,
214-
}
215-
216-
impl AddToDiagnostic for ExternBlockSuggestion {
217-
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
218-
where
219-
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
220-
{
221-
let start_suggestion = if let Some(abi) = self.abi {
222-
format!("extern \"{}\" {{", abi)
223-
} else {
224-
"extern {".to_owned()
225-
};
226-
let end_suggestion = " }".to_owned();
227-
228-
diag.multipart_suggestion(
229-
fluent::extern_block_suggestion,
230-
vec![(self.start_span, start_suggestion), (self.end_span, end_suggestion)],
231-
Applicability::MaybeIncorrect,
232-
);
233-
}
209+
#[derive(Subdiagnostic)]
210+
pub enum ExternBlockSuggestion {
211+
#[multipart_suggestion(ast_passes_extern_block_suggestion, applicability = "maybe-incorrect")]
212+
Implicit {
213+
#[suggestion_part(code = "extern {{")]
214+
start_span: Span,
215+
#[suggestion_part(code = " }}")]
216+
end_span: Span,
217+
},
218+
#[multipart_suggestion(ast_passes_extern_block_suggestion, applicability = "maybe-incorrect")]
219+
Explicit {
220+
#[suggestion_part(code = "extern \"{abi}\" {{")]
221+
start_span: Span,
222+
#[suggestion_part(code = " }}")]
223+
end_span: Span,
224+
abi: Symbol,
225+
},
234226
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ ast_passes_ty_alias_without_body =
8888
ast_passes_fn_without_body =
8989
free function without a body
9090
.suggestion = provide a definition for the function
91-
.extern_block_suggestion = if you meant to declare an externally defined function, use an `extern` block
91+
92+
ast_passes_extern_block_suggestion = if you meant to declare an externally defined function, use an `extern` block

0 commit comments

Comments
 (0)