Skip to content

Commit 7dffd14

Browse files
committed
lint: port unsafe diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
1 parent 4c63a21 commit 7dffd14

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,33 @@ lint-builtin-box-pointers = type uses owned (Box type) pointers: {$ty}
294294
295295
lint-builtin-non-shorthand-field-patterns = the `{$ident}:` in this pattern is redundant
296296
.suggestion = use shorthand field pattern
297+
298+
lint-builtin-overridden-symbol-name =
299+
the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
300+
301+
lint-builtin-overridden-symbol-section =
302+
the program's behavior with overridden link sections on items is unpredictable and Rust cannot provide guarantees when you manually override them
303+
304+
lint-builtin-allow-internal-unsafe =
305+
`allow_internal_unsafe` allows defining macros using unsafe without triggering the `unsafe_code` lint at their call site
306+
307+
lint-builtin-unsafe-block = usage of an `unsafe` block
308+
309+
lint-builtin-unsafe-trait = declaration of an `unsafe` trait
310+
311+
lint-builtin-unsafe-impl = implementation of an `unsafe` trait
312+
313+
lint-builtin-no-mangle-fn = declaration of a `no_mangle` function
314+
lint-builtin-export-name-fn = declaration of a function with `export_name`
315+
lint-builtin-link-section-fn = declaration of a function with `link_section`
316+
317+
lint-builtin-no-mangle-static = declaration of a `no_mangle` static
318+
lint-builtin-export-name-static = declaration of a static with `export_name`
319+
lint-builtin-link-section-static = declaration of a static with `link_section`
320+
321+
lint-builtin-no-mangle-method = declaration of a `no_mangle` method
322+
lint-builtin-export-name-method = declaration of a method with `export_name`
323+
324+
lint-builtin-decl-unsafe-fn = declaration of an `unsafe` function
325+
lint-builtin-decl-unsafe-method = declaration of an `unsafe` method
326+
lint-builtin-impl-unsafe-method = implementation of an `unsafe` method

compiler/rustc_lint/src/builtin.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use rustc_ast::{self as ast, *};
3131
use rustc_ast_pretty::pprust::{self, expr_to_string};
3232
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3333
use rustc_data_structures::stack::ensure_sufficient_stack;
34-
use rustc_errors::{fluent, Applicability, Diagnostic, DiagnosticStyledString, MultiSpan};
34+
use rustc_errors::{
35+
fluent, Applicability, Diagnostic, DiagnosticMessage, DiagnosticStyledString, MultiSpan,
36+
};
3537
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
3638
use rustc_hir as hir;
3739
use rustc_hir::def::{DefKind, Res};
@@ -326,26 +328,25 @@ impl UnsafeCode {
326328
cx.struct_span_lint(UNSAFE_CODE, span, decorate);
327329
}
328330

329-
fn report_overridden_symbol_name(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
331+
fn report_overridden_symbol_name(
332+
&self,
333+
cx: &EarlyContext<'_>,
334+
span: Span,
335+
msg: DiagnosticMessage,
336+
) {
330337
self.report_unsafe(cx, span, |lint| {
331-
lint.build(msg)
332-
.note(
333-
"the linker's behavior with multiple libraries exporting duplicate symbol \
334-
names is undefined and Rust cannot provide guarantees when you manually \
335-
override them",
336-
)
337-
.emit();
338+
lint.build(msg).note(fluent::lint::builtin_overridden_symbol_name).emit();
338339
})
339340
}
340341

341-
fn report_overridden_symbol_section(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
342+
fn report_overridden_symbol_section(
343+
&self,
344+
cx: &EarlyContext<'_>,
345+
span: Span,
346+
msg: DiagnosticMessage,
347+
) {
342348
self.report_unsafe(cx, span, |lint| {
343-
lint.build(msg)
344-
.note(
345-
"the program's behavior with overridden link sections on items is unpredictable \
346-
and Rust cannot provide guarantees when you manually override them",
347-
)
348-
.emit();
349+
lint.build(msg).note(fluent::lint::builtin_overridden_symbol_section).emit();
349350
})
350351
}
351352
}
@@ -354,12 +355,7 @@ impl EarlyLintPass for UnsafeCode {
354355
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
355356
if attr.has_name(sym::allow_internal_unsafe) {
356357
self.report_unsafe(cx, attr.span, |lint| {
357-
lint.build(
358-
"`allow_internal_unsafe` allows defining \
359-
macros using unsafe without triggering \
360-
the `unsafe_code` lint at their call site",
361-
)
362-
.emit();
358+
lint.build(fluent::lint::builtin_allow_internal_unsafe).emit();
363359
});
364360
}
365361
}
@@ -369,7 +365,7 @@ impl EarlyLintPass for UnsafeCode {
369365
// Don't warn about generated blocks; that'll just pollute the output.
370366
if blk.rules == ast::BlockCheckMode::Unsafe(ast::UserProvided) {
371367
self.report_unsafe(cx, blk.span, |lint| {
372-
lint.build("usage of an `unsafe` block").emit();
368+
lint.build(fluent::lint::builtin_unsafe_block).emit();
373369
});
374370
}
375371
}
@@ -379,36 +375,36 @@ impl EarlyLintPass for UnsafeCode {
379375
match it.kind {
380376
ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => self
381377
.report_unsafe(cx, it.span, |lint| {
382-
lint.build("declaration of an `unsafe` trait").emit();
378+
lint.build(fluent::lint::builtin_unsafe_trait).emit();
383379
}),
384380

385381
ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => self
386382
.report_unsafe(cx, it.span, |lint| {
387-
lint.build("implementation of an `unsafe` trait").emit();
383+
lint.build(fluent::lint::builtin_unsafe_impl).emit();
388384
}),
389385

390386
ast::ItemKind::Fn(..) => {
391387
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
392388
self.report_overridden_symbol_name(
393389
cx,
394390
attr.span,
395-
"declaration of a `no_mangle` function",
391+
fluent::lint::builtin_no_mangle_fn,
396392
);
397393
}
398394

399395
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
400396
self.report_overridden_symbol_name(
401397
cx,
402398
attr.span,
403-
"declaration of a function with `export_name`",
399+
fluent::lint::builtin_export_name_fn,
404400
);
405401
}
406402

407403
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
408404
self.report_overridden_symbol_section(
409405
cx,
410406
attr.span,
411-
"declaration of a function with `link_section`",
407+
fluent::lint::builtin_link_section_fn,
412408
);
413409
}
414410
}
@@ -418,23 +414,23 @@ impl EarlyLintPass for UnsafeCode {
418414
self.report_overridden_symbol_name(
419415
cx,
420416
attr.span,
421-
"declaration of a `no_mangle` static",
417+
fluent::lint::builtin_no_mangle_static,
422418
);
423419
}
424420

425421
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
426422
self.report_overridden_symbol_name(
427423
cx,
428424
attr.span,
429-
"declaration of a static with `export_name`",
425+
fluent::lint::builtin_export_name_static,
430426
);
431427
}
432428

433429
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
434430
self.report_overridden_symbol_section(
435431
cx,
436432
attr.span,
437-
"declaration of a static with `link_section`",
433+
fluent::lint::builtin_link_section_static,
438434
);
439435
}
440436
}
@@ -449,14 +445,14 @@ impl EarlyLintPass for UnsafeCode {
449445
self.report_overridden_symbol_name(
450446
cx,
451447
attr.span,
452-
"declaration of a `no_mangle` method",
448+
fluent::lint::builtin_no_mangle_method,
453449
);
454450
}
455451
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
456452
self.report_overridden_symbol_name(
457453
cx,
458454
attr.span,
459-
"declaration of a method with `export_name`",
455+
fluent::lint::builtin_export_name_method,
460456
);
461457
}
462458
}
@@ -474,9 +470,9 @@ impl EarlyLintPass for UnsafeCode {
474470
{
475471
let msg = match ctxt {
476472
FnCtxt::Foreign => return,
477-
FnCtxt::Free => "declaration of an `unsafe` function",
478-
FnCtxt::Assoc(_) if body.is_none() => "declaration of an `unsafe` method",
479-
FnCtxt::Assoc(_) => "implementation of an `unsafe` method",
473+
FnCtxt::Free => fluent::lint::builtin_decl_unsafe_fn,
474+
FnCtxt::Assoc(_) if body.is_none() => fluent::lint::builtin_decl_unsafe_method,
475+
FnCtxt::Assoc(_) => fluent::lint::builtin_impl_unsafe_method,
480476
};
481477
self.report_unsafe(cx, span, |lint| {
482478
lint.build(msg).emit();

0 commit comments

Comments
 (0)