Skip to content

Commit e0952b2

Browse files
committed
Rename lint to redefining_runtime_symbols
1 parent 24de5ec commit e0952b2

File tree

8 files changed

+199
-198
lines changed

8 files changed

+199
-198
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ lint_cfg_attr_no_attributes =
195195
196196
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
197197
198-
lint_clashing_function_names_with_fundamental_functions = this function symbol name `{$symbol_name}` clashes with the fundamental functions expected with `core` and `std`
199-
.match_exactly = extra care must be taken when exposing a function with those symbol names, they must match exactly (ABI, function arguments, function return type, behavior, ...)
198+
lint_redefining_runtime_symbols = redefinition of the runtime `{$symbol_name}` symbol used by the standard library
199+
.match_exactly = extra care must be taken when redefining those symbols, they must match exactly (ABI, function arguments, function return type, behavior, ...)
200200
.learn_more = see <https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library> for the more details
201-
.help = either allow this lint or remove any `#[unsafe(no_mangle)]` or `#[unsafe(export_name = "{$symbol_name}")]` if present
201+
.help = either allow this lint or remove any `#[unsafe(no_mangle)]` or `#[unsafe(export_name = "{$symbol_name}")]`
202202
203203
lint_closure_returning_async_block = closure returning async block can be made into an async closure
204204
.label = this async block can be removed, and the closure can be turned into an async closure

compiler/rustc_lint/src/fundamental_functions.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ mod errors;
4848
mod expect;
4949
mod for_loops_over_fallibles;
5050
mod foreign_modules;
51-
mod fundamental_functions;
5251
mod if_let_rescope;
5352
mod impl_trait_overcaptures;
5453
mod internal;
@@ -71,6 +70,7 @@ mod pass_by_value;
7170
mod passes;
7271
mod precedence;
7372
mod ptr_nulls;
73+
mod redefining_runtime_symbols;
7474
mod redundant_semicolon;
7575
mod reference_casting;
7676
mod shadowed_into_iter;
@@ -93,7 +93,6 @@ use deref_into_dyn_supertrait::*;
9393
use drop_forget_useless::*;
9494
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
9595
use for_loops_over_fallibles::*;
96-
use fundamental_functions::*;
9796
use if_let_rescope::IfLetRescope;
9897
use impl_trait_overcaptures::ImplTraitOvercaptures;
9998
use internal::*;
@@ -112,6 +111,7 @@ use opaque_hidden_inferred_bound::*;
112111
use pass_by_value::*;
113112
use precedence::*;
114113
use ptr_nulls::*;
114+
use redefining_runtime_symbols::*;
115115
use redundant_semicolon::*;
116116
use reference_casting::*;
117117
use rustc_hir::def_id::LocalModDefId;
@@ -242,7 +242,7 @@ late_lint_methods!(
242242
AsyncClosureUsage: AsyncClosureUsage,
243243
AsyncFnInTrait: AsyncFnInTrait,
244244
NonLocalDefinitions: NonLocalDefinitions::default(),
245-
FundamentalFunctions: FundamentalFunctions,
245+
RedefiningRuntimeSymbols: RedefiningRuntimeSymbols,
246246
ImplTraitOvercaptures: ImplTraitOvercaptures,
247247
IfLetRescope: IfLetRescope::default(),
248248
StaticMutRefs: StaticMutRefs,

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,13 +705,13 @@ pub(crate) enum UseLetUnderscoreIgnoreSuggestion {
705705
},
706706
}
707707

708-
// fundamental_functions.rs
708+
// redefining_runtime_symbols.rs
709709
#[derive(LintDiagnostic)]
710-
#[diag(lint_clashing_function_names_with_fundamental_functions)]
710+
#[diag(lint_redefining_runtime_symbols)]
711711
#[note(lint_match_exactly)]
712712
#[note(lint_learn_more)]
713713
#[help]
714-
pub(crate) struct ClashingFunctionNamesWithFundamentalFunctions {
714+
pub(crate) struct RedefiningRuntimeSymbolsDiag {
715715
pub symbol_name: String,
716716
}
717717

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use rustc_hir as hir;
2+
use rustc_session::{declare_lint, declare_lint_pass};
3+
4+
use crate::lints::RedefiningRuntimeSymbolsDiag;
5+
use crate::{LateContext, LateLintPass, LintContext};
6+
7+
declare_lint! {
8+
/// The `redefining_runtime_symbols` lint checks for items whose symbol name redefines
9+
/// a runtime symbols expected by `core` and/or `std`.
10+
///
11+
/// ### Example
12+
///
13+
/// ```rust,compile_fail
14+
/// #![deny(redefining_runtime_symbols)]
15+
///
16+
/// #[unsafe(no_mangle)]
17+
/// pub fn strlen() {} // redefines the libc `strlen` function
18+
/// ```
19+
///
20+
/// {{produces}}
21+
///
22+
/// ### Explanation
23+
///
24+
/// Up-most care is required when redefining runtime symbols assumed and
25+
/// used by the standard library. They must follow the C specification, not use any
26+
/// standard-library facility or undefined behavior may occur.
27+
///
28+
/// The symbols currently checked are respectively:
29+
/// - from `core`[^1]: `memcpy`, `memmove`, `memset`, `memcmp`, `bcmp`, `strlen`
30+
/// - from `std`: `read`, `write`, `open`, `close`
31+
///
32+
/// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library
33+
pub REDEFINING_RUNTIME_SYMBOLS,
34+
Warn,
35+
"redefining a symbol used by the standard library"
36+
}
37+
38+
declare_lint_pass!(RedefiningRuntimeSymbols => [REDEFINING_RUNTIME_SYMBOLS]);
39+
40+
static CORE_RUNTIME_SYMBOLS: &[&str] = &["memcpy", "memmove", "memset", "memcmp", "bcmp", "strlen"];
41+
42+
static STD_RUNTIME_SYMBOLS: &[&str] = &["open", "read", "write", "close"];
43+
44+
impl<'tcx> LateLintPass<'tcx> for RedefiningRuntimeSymbols {
45+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
46+
// Bail-out if the item is not a function/method or static.
47+
match item.kind {
48+
hir::ItemKind::Fn { sig: _, ident: _, generics: _, body: _, has_body: true }
49+
| hir::ItemKind::Static(..) => {}
50+
_ => return,
51+
}
52+
53+
// Compute the symbol name of our item (without mangling, as our mangling cannot ever
54+
// conflict with runtime symbols).
55+
let Some(symbol_name) = rustc_symbol_mangling::symbol_name_without_mangling(
56+
cx.tcx,
57+
rustc_middle::ty::InstanceKind::Item(item.owner_id.to_def_id()),
58+
) else {
59+
return;
60+
};
61+
62+
if CORE_RUNTIME_SYMBOLS.contains(&&*symbol_name)
63+
|| STD_RUNTIME_SYMBOLS.contains(&&*symbol_name)
64+
{
65+
cx.emit_span_lint(
66+
REDEFINING_RUNTIME_SYMBOLS,
67+
item.span,
68+
RedefiningRuntimeSymbolsDiag { symbol_name },
69+
);
70+
}
71+
}
72+
}

tests/ui/lint/clashing-fn-names-with-fundamental-functions.stderr

Lines changed: 0 additions & 107 deletions
This file was deleted.

tests/ui/lint/clashing-fn-names-with-fundamental-functions.rs renamed to tests/ui/lint/redefining-runtime-symbols.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,45 @@ pub extern "C" fn memcpy(
1111
src: *const c_void,
1212
n: i64,
1313
) -> *mut c_void { std::ptr::null_mut() }
14-
//~^^^^^ WARN `memcpy` clashes
14+
//~^^^^^ WARN redefinition of the runtime `memcpy` symbol
1515

1616
#[no_mangle]
1717
pub fn memmove() {}
18-
//~^ WARN `memmove` clashes
18+
//~^ WARN redefinition of the runtime `memmove` symbol
1919

2020
#[no_mangle]
2121
pub fn memset() {}
22-
//~^ WARN `memset` clashes
22+
//~^ WARN redefinition of the runtime `memset` symbol
2323

2424
#[no_mangle]
2525
pub fn memcmp() {}
26-
//~^ WARN `memcmp` clashes
26+
//~^ WARN redefinition of the runtime `memcmp` symbol
2727

2828
#[export_name = "bcmp"]
2929
pub fn bcmp_() {}
30-
//~^ WARN `bcmp` clashes
30+
//~^ WARN redefinition of the runtime `bcmp` symbol
3131

3232
#[no_mangle]
33-
pub fn strlen() {}
34-
//~^ WARN `strlen` clashes
33+
pub static strlen: () = ();
34+
//~^ WARN redefinition of the runtime `strlen` symbol
3535

3636
// From std
3737

3838
#[no_mangle]
3939
pub fn open() {}
40-
//~^ WARN `open` clashes
40+
//~^ WARN redefinition of the runtime `open` symbol
4141

4242
#[export_name = "read"]
4343
pub async fn read1() {}
44-
//~^ WARN `read` clashes
44+
//~^ WARN redefinition of the runtime `read` symbol
4545

4646
#[export_name = "write"]
4747
pub fn write1() {}
48-
//~^ WARN `write` clashes
48+
//~^ WARN redefinition of the runtime `write` symbol
4949

5050
#[export_name = "close"]
5151
pub fn close_() {}
52-
//~^ WARN `close` clashes
52+
//~^ WARN redefinition of the runtime `close` symbol
5353

5454
extern "C" {
5555
// No warning, not a body.

0 commit comments

Comments
 (0)