Auto merge of #97086 - 5225225:link-section-is-unsafe, r=davidtwco

Report unsafe for overriding link sections

I'm not too sure about the lint wording here, but I couldn't think of anything better.
This commit is contained in:
bors 2022-06-06 10:43:27 +00:00
commit 79b6bad406
3 changed files with 64 additions and 16 deletions

View file

@ -338,6 +338,17 @@ impl UnsafeCode {
.emit();
})
}
fn report_overridden_symbol_section(&self, cx: &EarlyContext<'_>, span: Span, msg: &str) {
self.report_unsafe(cx, span, |lint| {
lint.build(msg)
.note(
"the program's behavior with overridden link sections on items is unpredictable \
and Rust cannot provide guarantees when you manually override them",
)
.emit();
})
}
}
impl EarlyLintPass for UnsafeCode {
@ -385,6 +396,7 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` function",
);
}
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name(
cx,
@ -392,6 +404,14 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a function with `export_name`",
);
}
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section(
cx,
attr.span,
"declaration of a function with `link_section`",
);
}
}
ast::ItemKind::Static(..) => {
@ -402,6 +422,7 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a `no_mangle` static",
);
}
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name(
cx,
@ -409,6 +430,14 @@ impl EarlyLintPass for UnsafeCode {
"declaration of a static with `export_name`",
);
}
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section(
cx,
attr.span,
"declaration of a static with `link_section`",
);
}
}
_ => {}